Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -1802,6 +1802,104 @@ } } +namespace { +template class ObjectInfo final : public LoadedObjectInfo { +public: + uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override { + return 0; + } + std::unique_ptr clone() const override { return {}; } + + ObjectInfo(const object::ObjectFile &Obj) + : ElfObj(cast>(Obj)) {} + + bool relocate(const object::SectionRef &RelSec, + RelocAddrMap *Map) const override { + StringRef S; + if (std::error_code EC = RelSec.getName(S)) + return false; + if (S == ".rela.debug_ranges" || S == ".rel.debug_ranges") + return relocateDebugRanges(RelSec, Map); + return false; + } + +private: + typedef typename ELFT::Shdr Elf_Shdr; + typedef typename ELFT::Sym Elf_Sym; + + const ELFObjectFile &ElfObj; + + bool relocateDebugRanges(const object::SectionRef &RelSec, + RelocAddrMap *Map) const { + const ELFFile *ElfFile = ElfObj.getELFFile(); + const Elf_Shdr *RelSecShdr = ElfObj.getSection(RelSec.getRawDataRefImpl()); + const Elf_Shdr *SymTabShdr = + check(ElfFile->getSection(RelSecShdr->sh_link)); + + if (RelSecShdr->sh_type == SHT_RELA) { + ArrayRef Entries = check( + ElfFile->template getSectionContentsAsArray( + RelSecShdr)); + scanDebugRangesRelocations(SymTabShdr, Entries, Map); + } else { + ArrayRef Entries = + check(ElfFile->template getSectionContentsAsArray( + RelSecShdr)); + scanDebugRangesRelocations(SymTabShdr, Entries, Map); + } + + return true; + } + + uint64_t getAddend(const Elf_Shdr *Sec, const typename ELFT::Rel &Rel) const { + const ELFFile *ElfFile = ElfObj.getELFFile(); + ArrayRef Data = + check(ElfFile->template getSectionContentsAsArray(Sec)); + return Target->getImplicitAddend(Data.begin() + Rel.r_offset, + Rel.getType(Config->IsMips64EL)); + } + + uint64_t getAddend(const Elf_Shdr *Sec, + const typename ELFT::Rela &Rel) const { + return Rel.r_addend; + } + + template + void scanDebugRangesRelocations(const Elf_Shdr *SymTab, ArrayRef Rels, + RelocAddrMap *Map) const { + const ELFFile *ElfFile = ElfObj.getELFFile(); + + ArrayRef Symbols = check(ElfFile->symbols(SymTab)); + ArrayRef Sections = check(ElfFile->sections()); + + for (const RelTy &Rel : Rels) { + uint32_t SymId = Rel.getSymbol(Config->IsMips64EL); + uint64_t SecIndex = Symbols[SymId].st_shndx; + uint64_t A = getAddend(&Sections[SecIndex], Rel); + RelocAddrEntry RelEntry = {SecIndex, A}; + Map->insert({Rel.r_offset, RelEntry}); + } + } +}; + +std::unique_ptr +createObjectInfo(const object::ObjectFile &Obj) { + switch (Config->EKind) { + case ELF32LEKind: + return llvm::make_unique>(Obj); + case ELF32BEKind: + return llvm::make_unique>(Obj); + case ELF64LEKind: + return llvm::make_unique>(Obj); + case ELF64BEKind: + return llvm::make_unique>(Obj); + default: + llvm_unreachable("unknown Config->EKind"); + } +} + +} // namespace + GdbIndexChunk GdbIndexSection::readDwarf(InputSection *Sec) { Expected> Obj = object::ObjectFile::createObjectFile(Sec->File->MB); @@ -1810,7 +1908,8 @@ return {}; } - DWARFContextInMemory Dwarf(*Obj.get()); + std::unique_ptr ObjInfo = createObjectInfo(*Obj.get()); + DWARFContextInMemory Dwarf(*Obj.get(), ObjInfo.get()); GdbIndexChunk Ret; Ret.CompilationUnits = readCuList(Dwarf, Sec);