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