xref: /freebsd/contrib/llvm-project/lld/ELF/DWARF.cpp (revision 6132212808e8dccedc9e5d85fea4390c2f38059a)
1 //===- DWARF.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The -gdb-index option instructs the linker to emit a .gdb_index section.
10 // The section contains information to make gdb startup faster.
11 // The format of the section is described at
12 // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "DWARF.h"
17 #include "Symbols.h"
18 #include "Target.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 
33     if (LLDDWARFSection *m =
34             StringSwitch<LLDDWARFSection *>(sec->name)
35                 .Case(".debug_addr", &addrSection)
36                 .Case(".debug_gnu_pubnames", &gnuPubnamesSection)
37                 .Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
38                 .Case(".debug_info", &infoSection)
39                 .Case(".debug_loclists", &loclistsSection)
40                 .Case(".debug_ranges", &rangesSection)
41                 .Case(".debug_rnglists", &rnglistsSection)
42                 .Case(".debug_str_offsets", &strOffsetsSection)
43                 .Case(".debug_line", &lineSection)
44                 .Default(nullptr)) {
45       m->Data = toStringRef(sec->data());
46       m->sec = sec;
47       continue;
48     }
49 
50     if (sec->name == ".debug_abbrev")
51       abbrevSection = toStringRef(sec->data());
52     else if (sec->name == ".debug_str")
53       strSection = toStringRef(sec->data());
54     else if (sec->name == ".debug_line_str")
55       lineStrSection = toStringRef(sec->data());
56   }
57 }
58 
59 namespace {
60 template <class RelTy> struct LLDRelocationResolver {
61   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
62   // entry. For Rela, the addend is stored as part of the relocation entry.
63   static uint64_t resolve(object::RelocationRef ref, uint64_t s,
64                           uint64_t /* A */) {
65     return s + ref.getRawDataRefImpl().p;
66   }
67 };
68 
69 template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
70   // For Rel, the addend A is supplied by the caller.
71   static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s,
72                           uint64_t a) {
73     return s + a;
74   }
75 };
76 } // namespace
77 
78 // Find if there is a relocation at Pos in Sec.  The code is a bit
79 // more complicated than usual because we need to pass a section index
80 // to llvm since it has no idea about InputSection.
81 template <class ELFT>
82 template <class RelTy>
83 Optional<RelocAddrEntry>
84 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
85                            ArrayRef<RelTy> rels) const {
86   auto it =
87       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
88   if (it == rels.end() || it->r_offset != pos)
89     return None;
90   const RelTy &rel = *it;
91 
92   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
93   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
94   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
95   uint32_t secIndex = file->getSectionIndex(sym);
96 
97   // An undefined symbol may be a symbol defined in a discarded section. We
98   // shall still resolve it. This is important for --gdb-index: the end address
99   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
100   // its zero value will terminate the decoding of .debug_ranges prematurely.
101   Symbol &s = file->getRelocTargetSym(rel);
102   uint64_t val = 0;
103   if (auto *dr = dyn_cast<Defined>(&s))
104     val = dr->value;
105 
106   DataRefImpl d;
107   d.p = getAddend<ELFT>(rel);
108   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
109                         val,      Optional<object::RelocationRef>(),
110                         0,        LLDRelocationResolver<RelTy>::resolve};
111 }
112 
113 template <class ELFT>
114 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
115                                                  uint64_t pos) const {
116   auto &sec = static_cast<const LLDDWARFSection &>(s);
117   if (sec.sec->areRelocsRela)
118     return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
119   return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
120 }
121 
122 template class elf::LLDDwarfObj<ELF32LE>;
123 template class elf::LLDDwarfObj<ELF32BE>;
124 template class elf::LLDDwarfObj<ELF64LE>;
125 template class elf::LLDDwarfObj<ELF64BE>;
126