1 //===- DWARF.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 //===----------------------------------------------------------------------===//
20 #include "lld/Common/Memory.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
22 #include "llvm/Object/ELFObjectFile.h"
25 using namespace llvm::object;
27 using namespace lld::elf;
29 template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *Obj) {
30 for (InputSectionBase *Sec : Obj->getSections()) {
34 if (LLDDWARFSection *M =
35 StringSwitch<LLDDWARFSection *>(Sec->Name)
36 .Case(".debug_addr", &AddrSection)
37 .Case(".debug_gnu_pubnames", &GnuPubNamesSection)
38 .Case(".debug_gnu_pubtypes", &GnuPubTypesSection)
39 .Case(".debug_info", &InfoSection)
40 .Case(".debug_ranges", &RangeSection)
41 .Case(".debug_rnglists", &RngListsSection)
42 .Case(".debug_line", &LineSection)
44 M->Data = toStringRef(Sec->data());
49 if (Sec->Name == ".debug_abbrev")
50 AbbrevSection = toStringRef(Sec->data());
51 else if (Sec->Name == ".debug_str")
52 StrSection = toStringRef(Sec->data());
53 else if (Sec->Name == ".debug_line_str")
54 LineStringSection = toStringRef(Sec->data());
58 // Find if there is a relocation at Pos in Sec. The code is a bit
59 // more complicated than usual because we need to pass a section index
60 // to llvm since it has no idea about InputSection.
62 template <class RelTy>
63 Optional<RelocAddrEntry>
64 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &Sec, uint64_t Pos,
65 ArrayRef<RelTy> Rels) const {
66 auto It = std::lower_bound(
67 Rels.begin(), Rels.end(), Pos,
68 [](const RelTy &A, uint64_t B) { return A.r_offset < B; });
69 if (It == Rels.end() || It->r_offset != Pos)
71 const RelTy &Rel = *It;
73 const ObjFile<ELFT> *File = Sec.getFile<ELFT>();
74 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
75 const typename ELFT::Sym &Sym = File->getELFSyms()[SymIndex];
76 uint32_t SecIndex = File->getSectionIndex(Sym);
78 // Broken debug info can point to a non-Defined symbol.
79 auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
81 RelType Type = Rel.getType(Config->IsMips64EL);
82 if (Type != Target->NoneRel)
83 error(toString(File) + ": relocation " + lld::toString(Type) + " at 0x" +
84 llvm::utohexstr(Rel.r_offset) + " has unsupported target");
87 uint64_t Val = DR->Value + getAddend<ELFT>(Rel);
89 // FIXME: We should be consistent about always adding the file
91 if (DR->Section->Flags & ELF::SHF_ALLOC)
92 Val += cast<InputSection>(DR->Section)->getOffsetInFile();
94 return RelocAddrEntry{SecIndex, Val};
98 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &S,
100 auto &Sec = static_cast<const LLDDWARFSection &>(S);
101 if (Sec.Sec->AreRelocsRela)
102 return findAux(*Sec.Sec, Pos, Sec.Sec->template relas<ELFT>());
103 return findAux(*Sec.Sec, Pos, Sec.Sec->template rels<ELFT>());
106 template class elf::LLDDwarfObj<ELF32LE>;
107 template class elf::LLDDwarfObj<ELF32BE>;
108 template class elf::LLDDwarfObj<ELF64LE>;
109 template class elf::LLDDwarfObj<ELF64BE>;