From 08106ceeb43e26baad0be0d64cdf989b26bb9cdc Mon Sep 17 00:00:00 2001 From: wj Date: Fri, 15 Apr 2022 04:23:34 +0200 Subject: [PATCH] [SOL] re-enable debug info and add R_BPF_64_{ABS64, ABS32, NODYLD32} relocations to lld Co-Authored-By: Richard Patel [SOL] emit R_BPF_64_{ABS64, ABS32} for .debug_* sections --- lld/ELF/Arch/BPF.cpp | 17 +++++++++++++++++ llvm/lib/MC/ELFObjectWriter.cpp | 6 ++++++ llvm/lib/Target/BPF/BPFTargetMachine.cpp | 3 +-- .../BPF/MCTargetDesc/BPFELFObjectWriter.cpp | 3 +++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Arch/BPF.cpp b/lld/ELF/Arch/BPF.cpp index fbe5a8c1d7339..d948e4e62666c 100644 --- a/lld/ELF/Arch/BPF.cpp +++ b/lld/ELF/Arch/BPF.cpp @@ -45,6 +45,9 @@ RelExpr BPF::getRelExpr(RelType type, const Symbol &s, switch (type) { case R_BPF_64_32: return R_PC; + case R_BPF_64_ABS32: + case R_BPF_64_NODYLD32: + case R_BPF_64_ABS64: case R_BPF_64_64: return R_ABS; default: @@ -68,6 +71,12 @@ void BPF::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { write32le(loc + 4, ((val - 8) / 8) & 0xFFFFFFFF); break; } + case R_BPF_64_ABS32: + case R_BPF_64_NODYLD32: { + // Relocation used by .BTF.ext and DWARF + write32le(loc, val & 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 @@ -76,6 +85,14 @@ void BPF::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { write32le(loc + 8 + 4, val >> 32); break; } + case R_BPF_64_ABS64: { + // The relocation type is used for normal 64-bit data. The + // actual to-be-relocated data is stored at r_offset and the + // read/write data bitsize is 64 (8 bytes). The relocation can + // be resolved with the symbol value plus implicit addend. + write64le(loc, val); + break; + } default: error(getErrorLocation(loc) + "unrecognized reloc " + toString(rel.type)); } diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index e0ea44626b7f3..bc9605ee8b749 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -1455,6 +1455,12 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm, return; unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel); + + unsigned Flags = FixupSection.getFlags(); + // Change R_BPF_64_64 relocations in .debug_* sections to R_BPF_64_ABS64 + if (Ctx.getTargetTriple().isBPF() && !(Flags & ELF::SHF_ALLOC) && (Type==ELF::R_BPF_64_64)) + Type = ELF::R_BPF_64_ABS64; + const auto *Parent = cast(Fragment->getParent()); // Emiting relocation with sybmol for CG Profile to help with --cg-profile. bool RelocateWithSymbol = diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp index 08d6b89ffa036..024ef6aa2321c 100644 --- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp +++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp @@ -83,8 +83,7 @@ BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT, BPFMCAsmInfo *MAI = static_cast(const_cast(AsmInfo.get())); MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS()); - bool IsSolana = TT.getArch() == Triple::sbf || FS.contains("solana"); - MAI->setSupportsDebugInformation(!IsSolana); + MAI->setSupportsDebugInformation(true); } namespace { diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp index 50899f2b9e2fa..dd4ba3c512740 100644 --- a/llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp +++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp @@ -92,6 +92,9 @@ unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_WRITE)) return ELF::R_BPF_64_NODYLD32; } + // .debug_* sections + if (!(Flags & ELF::SHF_ALLOC)) + return ELF::R_BPF_64_ABS32; } } return isSolana ? ELF::R_BPF_64_32 : ELF::R_BPF_64_ABS32;