xref: /llvm-project-15.0.7/lld/ELF/DWARF.cpp (revision 22bdb331)
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 =
35             StringSwitch<LLDDWARFSection *>(Sec->Name)
36                 .Case(".debug_gnu_pubnames", &GnuPubNamesSection)
37                 .Case(".debug_gnu_pubtypes", &GnuPubTypesSection)
38                 .Case(".debug_info", &InfoSection)
39                 .Case(".debug_ranges", &RangeSection)
40                 .Case(".debug_line", &LineSection)
41                 .Default(nullptr)) {
42       M->Data = toStringRef(Sec->data());
43       M->Sec = Sec;
44       continue;
45     }
46 
47     if (Sec->Name == ".debug_abbrev")
48       AbbrevSection = toStringRef(Sec->data());
49     else if (Sec->Name == ".debug_str")
50       StrSection = toStringRef(Sec->data());
51     else if (Sec->Name == ".debug_line_str")
52       LineStringSection = toStringRef(Sec->data());
53   }
54 }
55 
56 // Find if there is a relocation at Pos in Sec.  The code is a bit
57 // more complicated than usual because we need to pass a section index
58 // to llvm since it has no idea about InputSection.
59 template <class ELFT>
60 template <class RelTy>
61 Optional<RelocAddrEntry>
62 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &Sec, uint64_t Pos,
63                            ArrayRef<RelTy> Rels) const {
64   auto It = std::lower_bound(
65       Rels.begin(), Rels.end(), Pos,
66       [](const RelTy &A, uint64_t B) { return A.r_offset < B; });
67   if (It == Rels.end() || It->r_offset != Pos)
68     return None;
69   const RelTy &Rel = *It;
70 
71   const ObjFile<ELFT> *File = Sec.getFile<ELFT>();
72   uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
73   const typename ELFT::Sym &Sym = File->getELFSyms()[SymIndex];
74   uint32_t SecIndex = File->getSectionIndex(Sym);
75 
76   // Broken debug info can point to a non-Defined symbol.
77   auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
78   if (!DR) {
79     RelType Type = Rel.getType(Config->IsMips64EL);
80     if (Type != Target->NoneRel)
81       error(toString(File) + ": relocation " + lld::toString(Type) + " at 0x" +
82             llvm::utohexstr(Rel.r_offset) + " has unsupported target");
83     return None;
84   }
85   uint64_t Val = DR->Value + getAddend<ELFT>(Rel);
86 
87   // FIXME: We should be consistent about always adding the file
88   // offset or not.
89   if (DR->Section->Flags & ELF::SHF_ALLOC)
90     Val += cast<InputSection>(DR->Section)->getOffsetInFile();
91 
92   return RelocAddrEntry{SecIndex, Val};
93 }
94 
95 template <class ELFT>
96 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &S,
97                                                  uint64_t Pos) const {
98   auto &Sec = static_cast<const LLDDWARFSection &>(S);
99   if (Sec.Sec->AreRelocsRela)
100     return findAux(*Sec.Sec, Pos, Sec.Sec->template relas<ELFT>());
101   return findAux(*Sec.Sec, Pos, Sec.Sec->template rels<ELFT>());
102 }
103 
104 template class elf::LLDDwarfObj<ELF32LE>;
105 template class elf::LLDDwarfObj<ELF32BE>;
106 template class elf::LLDDwarfObj<ELF64LE>;
107 template class elf::LLDDwarfObj<ELF64BE>;
108