Skip to content

Commit 25d2d68

Browse files
authored
[LLD][BPF] Enable BPF shared object creation (#1)
1 parent c16b771 commit 25d2d68

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
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
@@ -12,6 +12,7 @@ add_lld_library(lldELF
1212
Arch/AMDGPU.cpp
1313
Arch/ARM.cpp
1414
Arch/AVR.cpp
15+
Arch/BPF.cpp
1516
Arch/Hexagon.cpp
1617
Arch/Mips.cpp
1718
Arch/MipsArchTree.cpp

lld/ELF/Target.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ TargetInfo *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
@@ -147,6 +147,7 @@ TargetInfo *getAArch64TargetInfo();
147147
TargetInfo *getAMDGPUTargetInfo();
148148
TargetInfo *getARMTargetInfo();
149149
TargetInfo *getAVRTargetInfo();
150+
TargetInfo *getBPFTargetInfo();
150151
TargetInfo *getHexagonTargetInfo();
151152
TargetInfo *getMSP430TargetInfo();
152153
TargetInfo *getPPC64TargetInfo();

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
// No relocation
66
ELF_RELOC(R_BPF_NONE, 0)
77
ELF_RELOC(R_BPF_64_64, 1)
8+
ELF_RELOC(R_BPF_64_RELATIVE, 8) // B + A
89
ELF_RELOC(R_BPF_64_32, 10)

llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BPFELFObjectWriter : public MCELFObjectTargetWriter {
3434

3535
} // end anonymous namespace
3636

37-
// Avoid section relocations because the BPF backend can only handle
37+
// Avoid section relocations because the BPF backend can only handle
3838
// section relocations with values (offset into the section containing
3939
// the symbol being relocated). Forcing a relocation with a symbol
4040
// will result in the symbol's index being used in the .o file instead.

0 commit comments

Comments
 (0)