xref: /freebsd/contrib/llvm-project/lld/ELF/DWARF.cpp (revision 994297b01b98816bea1abf45ae4bac1bc69ee7a0)
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   // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.
30   ArrayRef<typename ELFT::Shdr> objSections =
31       CHECK(obj->getObj().sections(), obj);
32   assert(objSections.size() == obj->getSections().size());
33   for (auto it : llvm::enumerate(obj->getSections())) {
34     InputSectionBase *sec = it.value();
35     if (!sec)
36       continue;
37 
38     if (LLDDWARFSection *m =
39             StringSwitch<LLDDWARFSection *>(sec->name)
40                 .Case(".debug_addr", &addrSection)
41                 .Case(".debug_gnu_pubnames", &gnuPubnamesSection)
42                 .Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
43                 .Case(".debug_loclists", &loclistsSection)
44                 .Case(".debug_ranges", &rangesSection)
45                 .Case(".debug_rnglists", &rnglistsSection)
46                 .Case(".debug_str_offsets", &strOffsetsSection)
47                 .Case(".debug_line", &lineSection)
48                 .Default(nullptr)) {
49       m->Data = toStringRef(sec->data());
50       m->sec = sec;
51       continue;
52     }
53 
54     if (sec->name == ".debug_abbrev")
55       abbrevSection = toStringRef(sec->data());
56     else if (sec->name == ".debug_str")
57       strSection = toStringRef(sec->data());
58     else if (sec->name == ".debug_line_str")
59       lineStrSection = toStringRef(sec->data());
60     else if (sec->name == ".debug_info" &&
61              !(objSections[it.index()].sh_flags & ELF::SHF_GROUP)) {
62       // In DWARF v5, -fdebug-types-section places type units in .debug_info
63       // sections in COMDAT groups. They are not compile units and thus should
64       // be ignored for .gdb_index/diagnostics purposes.
65       //
66       // We use a simple heuristic: the compile unit does not have the SHF_GROUP
67       // flag. If we place compile units in COMDAT groups in the future, we may
68       // need to perform a lightweight parsing. We drop the SHF_GROUP flag when
69       // the InputSection was created, so we need to retrieve sh_flags from the
70       // associated ELF section header.
71       infoSection.Data = toStringRef(sec->data());
72       infoSection.sec = sec;
73     }
74   }
75 }
76 
77 namespace {
78 template <class RelTy> struct LLDRelocationResolver {
79   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
80   // entry. For Rela, the addend is stored as part of the relocation entry and
81   // is provided by the `findAux` method.
82   // In resolve() methods, the `type` and `offset` arguments would always be 0,
83   // because we don't set an owning object for the `RelocationRef` instance that
84   // we create in `findAux()`.
85   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
86                           uint64_t /*locData*/, int64_t addend) {
87     return s + addend;
88   }
89 };
90 
91 template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
92   // For Rel, the addend is extracted from the relocated location and is
93   // supplied by the caller.
94   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
95                           uint64_t locData, int64_t /*addend*/) {
96     return s + locData;
97   }
98 };
99 } // namespace
100 
101 // Find if there is a relocation at Pos in Sec.  The code is a bit
102 // more complicated than usual because we need to pass a section index
103 // to llvm since it has no idea about InputSection.
104 template <class ELFT>
105 template <class RelTy>
106 Optional<RelocAddrEntry>
107 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
108                            ArrayRef<RelTy> rels) const {
109   auto it =
110       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
111   if (it == rels.end() || it->r_offset != pos)
112     return None;
113   const RelTy &rel = *it;
114 
115   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
116   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
117   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
118   uint32_t secIndex = file->getSectionIndex(sym);
119 
120   // An undefined symbol may be a symbol defined in a discarded section. We
121   // shall still resolve it. This is important for --gdb-index: the end address
122   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
123   // its zero value will terminate the decoding of .debug_ranges prematurely.
124   Symbol &s = file->getRelocTargetSym(rel);
125   uint64_t val = 0;
126   if (auto *dr = dyn_cast<Defined>(&s))
127     val = dr->value;
128 
129   DataRefImpl d;
130   d.p = getAddend<ELFT>(rel);
131   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
132                         val,      Optional<object::RelocationRef>(),
133                         0,        LLDRelocationResolver<RelTy>::resolve};
134 }
135 
136 template <class ELFT>
137 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
138                                                  uint64_t pos) const {
139   auto &sec = static_cast<const LLDDWARFSection &>(s);
140   if (sec.sec->areRelocsRela)
141     return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
142   return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
143 }
144 
145 template class elf::LLDDwarfObj<ELF32LE>;
146 template class elf::LLDDwarfObj<ELF32BE>;
147 template class elf::LLDDwarfObj<ELF64LE>;
148 template class elf::LLDDwarfObj<ELF64BE>;
149