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
LLDDwarfObj(ObjFile<ELFT> * Obj)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 =
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)
43 .Default(nullptr)) {
44 M->Data = toStringRef(Sec->data());
45 M->Sec = Sec;
46 continue;
47 }
48
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());
55 }
56 }
57
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.
61 template <class ELFT>
62 template <class RelTy>
63 Optional<RelocAddrEntry>
findAux(const InputSectionBase & Sec,uint64_t Pos,ArrayRef<RelTy> Rels) const64 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)
70 return None;
71 const RelTy &Rel = *It;
72
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);
77
78 // Broken debug info can point to a non-Defined symbol.
79 auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
80 if (!DR) {
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");
85 return None;
86 }
87 uint64_t Val = DR->Value + getAddend<ELFT>(Rel);
88
89 // FIXME: We should be consistent about always adding the file
90 // offset or not.
91 if (DR->Section->Flags & ELF::SHF_ALLOC)
92 Val += cast<InputSection>(DR->Section)->getOffsetInFile();
93
94 return RelocAddrEntry{SecIndex, Val};
95 }
96
97 template <class ELFT>
find(const llvm::DWARFSection & S,uint64_t Pos) const98 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &S,
99 uint64_t Pos) const {
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>());
104 }
105
106 template class elf::LLDDwarfObj<ELF32LE>;
107 template class elf::LLDDwarfObj<ELF32BE>;
108 template class elf::LLDDwarfObj<ELF64LE>;
109 template class elf::LLDDwarfObj<ELF64BE>;
110