Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 938fb7e

Browse files
committed
[SOL] Enable BPF shared object creation
1 parent 8a08e05 commit 938fb7e

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

lld/ELF/Arch/BPF.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===- BPF.cpp ------------------------------------------------------------===//
2+
//
3+
// The LLVM Linker
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "InputFiles.h"
11+
#include "Symbols.h"
12+
#include "Target.h"
13+
#include "lld/Common/ErrorHandler.h"
14+
#include "llvm/Object/ELF.h"
15+
#include "llvm/Support/Endian.h"
16+
17+
using namespace llvm;
18+
using namespace llvm::object;
19+
using namespace llvm::support::endian;
20+
using namespace llvm::ELF;
21+
22+
namespace lld {
23+
namespace elf {
24+
25+
namespace {
26+
class BPF final : public TargetInfo {
27+
public:
28+
BPF();
29+
RelExpr getRelExpr(RelType type, const Symbol &s,
30+
const uint8_t *loc) const override;
31+
RelType getDynRel(RelType type) const override;
32+
void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
33+
};
34+
} // namespace
35+
36+
BPF::BPF() {
37+
noneRel = R_BPF_NONE;
38+
relativeRel = R_BPF_64_RELATIVE;
39+
symbolicRel = R_BPF_64_64;
40+
}
41+
42+
RelExpr BPF::getRelExpr(RelType type, const Symbol &s,
43+
const uint8_t *loc) const {
44+
switch (type) {
45+
case R_BPF_64_32:
46+
return R_PC;
47+
case R_BPF_64_64:
48+
return R_ABS;
49+
default:
50+
error(getErrorLocation(loc) + "unrecognized reloc " + toString(type));
51+
}
52+
return R_NONE;
53+
}
54+
55+
RelType BPF::getDynRel(RelType type) const {
56+
return type;
57+
}
58+
59+
void BPF::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
60+
switch (type) {
61+
case R_BPF_64_32: {
62+
// Relocation of a symbol
63+
write32le(loc + 4, ((val - 8) / 8) & 0xFFFFFFFF);
64+
break;
65+
}
66+
case R_BPF_64_64: {
67+
// Relocation of a lddw instruction
68+
// 64 bit address is divided into the imm of this and the following
69+
// instructions, lower 32 first.
70+
write32le(loc + 4, val & 0xFFFFFFFF);
71+
write32le(loc + 8 + 4, val >> 32);
72+
break;
73+
}
74+
default:
75+
error(getErrorLocation(loc) + "unrecognized reloc " + toString(type));
76+
}
77+
}
78+
79+
TargetInfo *getBPFTargetInfo() {
80+
static BPF target;
81+
return ⌖
82+
}
83+
84+
} // namespace elf
85+
} // namespace lld

lld/ELF/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_lld_library(lldELF
88
Arch/AMDGPU.cpp
99
Arch/ARM.cpp
1010
Arch/AVR.cpp
11+
Arch/BPF.cpp
1112
Arch/Hexagon.cpp
1213
Arch/Mips.cpp
1314
Arch/MipsArchTree.cpp

lld/ELF/Target.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ TargetInfo *elf::getTarget() {
6060
return getARMTargetInfo();
6161
case EM_AVR:
6262
return getAVRTargetInfo();
63+
case EM_BPF:
64+
return getBPFTargetInfo();
6365
case EM_HEXAGON:
6466
return getHexagonTargetInfo();
6567
case EM_MIPS:

lld/ELF/Target.h

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ TargetInfo *getAArch64TargetInfo();
175175
TargetInfo *getAMDGPUTargetInfo();
176176
TargetInfo *getARMTargetInfo();
177177
TargetInfo *getAVRTargetInfo();
178+
TargetInfo *getBPFTargetInfo();
178179
TargetInfo *getHexagonTargetInfo();
179180
TargetInfo *getMSP430TargetInfo();
180181
TargetInfo *getPPC64TargetInfo();

llvm/include/llvm/BinaryFormat/ELFRelocs/BPF.def

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ ELF_RELOC(R_BPF_64_64, 1)
88
ELF_RELOC(R_BPF_64_ABS64, 2)
99
ELF_RELOC(R_BPF_64_ABS32, 3)
1010
ELF_RELOC(R_BPF_64_NODYLD32, 4)
11+
ELF_RELOC(R_BPF_64_RELATIVE, 8) // B + A
1112
ELF_RELOC(R_BPF_64_32, 10)

0 commit comments

Comments
 (0)