]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/GdbIndex.cpp
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / GdbIndex.cpp
1 //===- GdbIndex.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 "GdbIndex.h"
18 #include "Symbols.h"
19 #include "lld/Common/Memory.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
21 #include "llvm/Object/ELFObjectFile.h"
22
23 using namespace llvm;
24 using namespace llvm::object;
25 using namespace lld;
26 using namespace lld::elf;
27
28 template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *Obj) {
29   for (InputSectionBase *Sec : Obj->getSections()) {
30     if (!Sec)
31       continue;
32     if (LLDDWARFSection *M = StringSwitch<LLDDWARFSection *>(Sec->Name)
33                                  .Case(".debug_info", &InfoSection)
34                                  .Case(".debug_ranges", &RangeSection)
35                                  .Case(".debug_line", &LineSection)
36                                  .Default(nullptr)) {
37       Sec->maybeUncompress();
38       M->Data = toStringRef(Sec->Data);
39       M->Sec = Sec;
40       continue;
41     }
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);
50   }
51 }
52
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.
56 template <class ELFT>
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)
65     return None;
66   const RelTy &Rel = *It;
67
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);
72
73   // Broken debug info can point to a non-Defined symbol.
74   auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
75   if (!DR) {
76     error("unsupported relocation target while parsing debug info");
77     return None;
78   }
79   uint64_t Val = DR->Value + getAddend<ELFT>(Rel);
80
81   // FIXME: We should be consistent about always adding the file
82   // offset or not.
83   if (DR->Section->Flags & ELF::SHF_ALLOC)
84     Val += cast<InputSection>(DR->Section)->getOffsetInFile();
85
86   return RelocAddrEntry{SecIndex, Val};
87 }
88
89 template <class ELFT>
90 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &S,
91                                                  uint64_t Pos) const {
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>());
96 }
97
98 template class elf::LLDDwarfObj<ELF32LE>;
99 template class elf::LLDDwarfObj<ELF32BE>;
100 template class elf::LLDDwarfObj<ELF64LE>;
101 template class elf::LLDDwarfObj<ELF64BE>;