From dc6c0cf94dfb6fb1ea05794ae0a09bf575c667fd Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 22 Mar 2019 02:43:17 +0000 Subject: [PATCH] [DWARF] Refactor RelocVisitor and fix computation of SHT_RELA-typed relocation entries Summary: This is the lld-side change of D57939 Reviewers: echristo, dblaikie, ruiu, espindola Reviewed By: echristo, ruiu Subscribers: jdoerfert, emaste, arichardson, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D57940 llvm-svn: 356730 --- lld/ELF/DWARF.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lld/ELF/DWARF.cpp b/lld/ELF/DWARF.cpp index ad1363b..727ad53 100644 --- a/lld/ELF/DWARF.cpp +++ b/lld/ELF/DWARF.cpp @@ -54,6 +54,25 @@ template LLDDwarfObj::LLDDwarfObj(ObjFile *Obj) { } } +namespace { +template struct LLDRelocationResolver { + // In the ELF ABIs, S sepresents the value of the symbol in the relocation + // entry. For Rela, the addend is stored as part of the relocation entry. + static uint64_t Resolve(object::RelocationRef Ref, uint64_t S, + uint64_t /* A */) { + return S + Ref.getRawDataRefImpl().p; + } +}; + +template struct LLDRelocationResolver> { + // For Rel, the addend A is supplied by the caller. + static uint64_t Resolve(object::RelocationRef /*Ref*/, uint64_t S, + uint64_t A) { + return S + A; + } +}; +} // namespace + // Find if there is a relocation at Pos in Sec. The code is a bit // more complicated than usual because we need to pass a section index // to llvm since it has no idea about InputSection. @@ -83,14 +102,17 @@ LLDDwarfObj::findAux(const InputSectionBase &Sec, uint64_t Pos, llvm::utohexstr(Rel.r_offset) + " has unsupported target"); return None; } - uint64_t Val = DR->Value + getAddend(Rel); + uint64_t Val = DR->Value; // FIXME: We should be consistent about always adding the file // offset or not. if (DR->Section->Flags & ELF::SHF_ALLOC) Val += cast(DR->Section)->getOffsetInFile(); - return RelocAddrEntry{SecIndex, Val}; + DataRefImpl D; + D.p = getAddend(Rel); + return RelocAddrEntry{SecIndex, RelocationRef(D, nullptr), + LLDRelocationResolver::Resolve, Val}; } template -- 2.7.4