|
| 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 |
0 commit comments