1 //===- GdbIndex.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
19 #include "lld/Common/Memory.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
21 #include "llvm/Object/ELFObjectFile.h"
24 using namespace llvm::object;
26 using namespace lld::elf;
28 template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *Obj) {
29 for (InputSectionBase *Sec : Obj->getSections()) {
32 if (LLDDWARFSection *M = StringSwitch<LLDDWARFSection *>(Sec->Name)
33 .Case(".debug_info", &InfoSection)
34 .Case(".debug_ranges", &RangeSection)
35 .Case(".debug_line", &LineSection)
37 Sec->maybeUncompress();
38 M->Data = toStringRef(Sec->Data);
42 if (Sec->Name == ".debug_abbrev")
43 AbbrevSection = toStringRef(Sec->Data);
44 else if (Sec->Name == ".debug_gnu_pubnames")
45 GnuPubNamesSection = toStringRef(Sec->Data);
46 else if (Sec->Name == ".debug_gnu_pubtypes")
47 GnuPubTypesSection = toStringRef(Sec->Data);
48 else if (Sec->Name == ".debug_str")
49 StrSection = toStringRef(Sec->Data);
53 // Find if there is a relocation at Pos in Sec. The code is a bit
54 // more complicated than usual because we need to pass a section index
55 // to llvm since it has no idea about InputSection.
57 template <class RelTy>
58 Optional<RelocAddrEntry>
59 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &Sec, uint64_t Pos,
60 ArrayRef<RelTy> Rels) const {
61 auto It = std::lower_bound(
62 Rels.begin(), Rels.end(), Pos,
63 [](const RelTy &A, uint64_t B) { return A.r_offset < B; });
64 if (It == Rels.end() || It->r_offset != Pos)
66 const RelTy &Rel = *It;
68 const ObjFile<ELFT> *File = Sec.getFile<ELFT>();
69 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
70 const typename ELFT::Sym &Sym = File->getELFSyms()[SymIndex];
71 uint32_t SecIndex = File->getSectionIndex(Sym);
73 // Broken debug info can point to a non-Defined symbol.
74 auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
76 error("unsupported relocation target while parsing debug info");
79 uint64_t Val = DR->Value + getAddend<ELFT>(Rel);
81 // FIXME: We should be consistent about always adding the file
83 if (DR->Section->Flags & ELF::SHF_ALLOC)
84 Val += cast<InputSection>(DR->Section)->getOffsetInFile();
86 return RelocAddrEntry{SecIndex, Val};
90 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &S,
92 auto &Sec = static_cast<const LLDDWARFSection &>(S);
93 if (Sec.Sec->AreRelocsRela)
94 return findAux(*Sec.Sec, Pos, Sec.Sec->template relas<ELFT>());
95 return findAux(*Sec.Sec, Pos, Sec.Sec->template rels<ELFT>());
98 template class elf::LLDDwarfObj<ELF32LE>;
99 template class elf::LLDDwarfObj<ELF32BE>;
100 template class elf::LLDDwarfObj<ELF64LE>;
101 template class elf::LLDDwarfObj<ELF64BE>;