Skip to content

Commit

Permalink
[SOL] Enable BPF shared object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
dmakarov authored and LucasSte committed Feb 16, 2024
1 parent 92e0db8 commit 1e5c03b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
85 changes: 85 additions & 0 deletions lld/ELF/Arch/BPF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===- BPF.cpp ------------------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "InputFiles.h"
#include "Symbols.h"
#include "Target.h"
#include "lld/Common/ErrorHandler.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/Endian.h"

using namespace llvm;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::ELF;

namespace lld {
namespace elf {

namespace {
class BPF final : public TargetInfo {
public:
BPF();
RelExpr getRelExpr(RelType type, const Symbol &s,
const uint8_t *loc) const override;
RelType getDynRel(RelType type) const override;
void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
};
} // namespace

BPF::BPF() {
noneRel = R_BPF_NONE;
relativeRel = R_BPF_64_RELATIVE;
symbolicRel = R_BPF_64_64;
}

RelExpr BPF::getRelExpr(RelType type, const Symbol &s,
const uint8_t *loc) const {
switch (type) {
case R_BPF_64_32:
return R_PC;
case R_BPF_64_64:
return R_ABS;
default:
error(getErrorLocation(loc) + "unrecognized reloc " + toString(type));
}
return R_NONE;
}

RelType BPF::getDynRel(RelType type) const {
return type;
}

void BPF::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
switch (type) {
case R_BPF_64_32: {
// Relocation of a symbol
write32le(loc + 4, ((val - 8) / 8) & 0xFFFFFFFF);
break;
}
case R_BPF_64_64: {
// Relocation of a lddw instruction
// 64 bit address is divided into the imm of this and the following
// instructions, lower 32 first.
write32le(loc + 4, val & 0xFFFFFFFF);
write32le(loc + 8 + 4, val >> 32);
break;
}
default:
error(getErrorLocation(loc) + "unrecognized reloc " + toString(type));
}
}

TargetInfo *getBPFTargetInfo() {
static BPF target;
return ⌖
}

} // namespace elf
} // namespace lld
1 change: 1 addition & 0 deletions lld/ELF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_lld_library(lldELF
Arch/AMDGPU.cpp
Arch/ARM.cpp
Arch/AVR.cpp
Arch/BPF.cpp
Arch/Hexagon.cpp
Arch/LoongArch.cpp
Arch/Mips.cpp
Expand Down
2 changes: 2 additions & 0 deletions lld/ELF/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ TargetInfo *elf::getTarget() {
return getARMTargetInfo();
case EM_AVR:
return getAVRTargetInfo();
case EM_BPF:
return getBPFTargetInfo();
case EM_HEXAGON:
return getHexagonTargetInfo();
case EM_LOONGARCH:
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ TargetInfo *getAArch64TargetInfo();
TargetInfo *getAMDGPUTargetInfo();
TargetInfo *getARMTargetInfo();
TargetInfo *getAVRTargetInfo();
TargetInfo *getBPFTargetInfo();
TargetInfo *getHexagonTargetInfo();
TargetInfo *getLoongArchTargetInfo();
TargetInfo *getMSP430TargetInfo();
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/BinaryFormat/ELFRelocs/BPF.def
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ ELF_RELOC(R_BPF_64_64, 1)
ELF_RELOC(R_BPF_64_ABS64, 2)
ELF_RELOC(R_BPF_64_ABS32, 3)
ELF_RELOC(R_BPF_64_NODYLD32, 4)
ELF_RELOC(R_BPF_64_RELATIVE, 8) // B + A
ELF_RELOC(R_BPF_64_32, 10)

0 comments on commit 1e5c03b

Please sign in to comment.