1 //===- DWARF.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 // The -gdb-index option instructs the linker to emit a .gdb_index section. 11 // The section contains information to make gdb startup faster. 12 // The format of the section is described at 13 // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "DWARF.h" 18 #include "Symbols.h" 19 #include "Target.h" 20 #include "lld/Common/Memory.h" 21 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 22 #include "llvm/Object/ELFObjectFile.h" 23 24 using namespace llvm; 25 using namespace llvm::object; 26 using namespace lld; 27 using namespace lld::elf; 28 29 template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *Obj) { 30 for (InputSectionBase *Sec : Obj->getSections()) { 31 if (!Sec) 32 continue; 33 34 if (LLDDWARFSection *M = StringSwitch<LLDDWARFSection *>(Sec->Name) 35 .Case(".debug_info", &InfoSection) 36 .Case(".debug_ranges", &RangeSection) 37 .Case(".debug_line", &LineSection) 38 .Default(nullptr)) { 39 M->Data = toStringRef(Sec->data()); 40 M->Sec = Sec; 41 continue; 42 } 43 44 if (Sec->Name == ".debug_abbrev") 45 AbbrevSection = toStringRef(Sec->data()); 46 else if (Sec->Name == ".debug_gnu_pubnames") 47 GnuPubNamesSection = toStringRef(Sec->data()); 48 else if (Sec->Name == ".debug_gnu_pubtypes") 49 GnuPubTypesSection = toStringRef(Sec->data()); 50 else if (Sec->Name == ".debug_str") 51 StrSection = toStringRef(Sec->data()); 52 else if (Sec->Name == ".debug_line_str") 53 LineStringSection = toStringRef(Sec->data()); 54 } 55 } 56 57 // Find if there is a relocation at Pos in Sec. The code is a bit 58 // more complicated than usual because we need to pass a section index 59 // to llvm since it has no idea about InputSection. 60 template <class ELFT> 61 template <class RelTy> 62 Optional<RelocAddrEntry> 63 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &Sec, uint64_t Pos, 64 ArrayRef<RelTy> Rels) const { 65 auto It = std::lower_bound( 66 Rels.begin(), Rels.end(), Pos, 67 [](const RelTy &A, uint64_t B) { return A.r_offset < B; }); 68 if (It == Rels.end() || It->r_offset != Pos) 69 return None; 70 const RelTy &Rel = *It; 71 72 const ObjFile<ELFT> *File = Sec.getFile<ELFT>(); 73 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL); 74 const typename ELFT::Sym &Sym = File->getELFSyms()[SymIndex]; 75 uint32_t SecIndex = File->getSectionIndex(Sym); 76 77 // Broken debug info can point to a non-Defined symbol. 78 auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel)); 79 if (!DR) { 80 RelType Type = Rel.getType(Config->IsMips64EL); 81 if (Type != Target->NoneRel) 82 error(toString(File) + ": relocation " + lld::toString(Type) + " at 0x" + 83 llvm::utohexstr(Rel.r_offset) + " has unsupported target"); 84 return None; 85 } 86 uint64_t Val = DR->Value + getAddend<ELFT>(Rel); 87 88 // FIXME: We should be consistent about always adding the file 89 // offset or not. 90 if (DR->Section->Flags & ELF::SHF_ALLOC) 91 Val += cast<InputSection>(DR->Section)->getOffsetInFile(); 92 93 return RelocAddrEntry{SecIndex, Val}; 94 } 95 96 template <class ELFT> 97 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &S, 98 uint64_t Pos) const { 99 auto &Sec = static_cast<const LLDDWARFSection &>(S); 100 if (Sec.Sec->AreRelocsRela) 101 return findAux(*Sec.Sec, Pos, Sec.Sec->template relas<ELFT>()); 102 return findAux(*Sec.Sec, Pos, Sec.Sec->template rels<ELFT>()); 103 } 104 105 template class elf::LLDDwarfObj<ELF32LE>; 106 template class elf::LLDDwarfObj<ELF32BE>; 107 template class elf::LLDDwarfObj<ELF64LE>; 108 template class elf::LLDDwarfObj<ELF64BE>; 109