Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLD][BPF] Enable BPF shared object creation #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -12,6 +12,7 @@ add_lld_library(lldELF
Arch/AMDGPU.cpp
Arch/ARM.cpp
Arch/AVR.cpp
Arch/BPF.cpp
Arch/Hexagon.cpp
Arch/Mips.cpp
Arch/MipsArchTree.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 *getTarget() {
return getARMTargetInfo();
case EM_AVR:
return getAVRTargetInfo();
case EM_BPF:
return getBPFTargetInfo();
case EM_HEXAGON:
return getHexagonTargetInfo();
case EM_MIPS:
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ TargetInfo *getAArch64TargetInfo();
TargetInfo *getAMDGPUTargetInfo();
TargetInfo *getARMTargetInfo();
TargetInfo *getAVRTargetInfo();
TargetInfo *getBPFTargetInfo();
TargetInfo *getHexagonTargetInfo();
TargetInfo *getMSP430TargetInfo();
TargetInfo *getPPC64TargetInfo();
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 @@ -5,4 +5,5 @@
// No relocation
ELF_RELOC(R_BPF_NONE, 0)
ELF_RELOC(R_BPF_64_64, 1)
ELF_RELOC(R_BPF_64_RELATIVE, 8) // B + A
ELF_RELOC(R_BPF_64_32, 10)
2 changes: 1 addition & 1 deletion llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BPFELFObjectWriter : public MCELFObjectTargetWriter {

} // end anonymous namespace

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