]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/ELF/DWARF.cpp
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / ELF / DWARF.cpp
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.
81   static uint64_t resolve(object::RelocationRef ref, uint64_t s,
82                           uint64_t /* A */) {
83     return s + ref.getRawDataRefImpl().p;
84   }
85 };
86
87 template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
88   // For Rel, the addend A is supplied by the caller.
89   static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s,
90                           uint64_t a) {
91     return s + a;
92   }
93 };
94 } // namespace
95
96 // Find if there is a relocation at Pos in Sec.  The code is a bit
97 // more complicated than usual because we need to pass a section index
98 // to llvm since it has no idea about InputSection.
99 template <class ELFT>
100 template <class RelTy>
101 Optional<RelocAddrEntry>
102 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
103                            ArrayRef<RelTy> rels) const {
104   auto it =
105       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
106   if (it == rels.end() || it->r_offset != pos)
107     return None;
108   const RelTy &rel = *it;
109
110   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
111   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
112   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
113   uint32_t secIndex = file->getSectionIndex(sym);
114
115   // An undefined symbol may be a symbol defined in a discarded section. We
116   // shall still resolve it. This is important for --gdb-index: the end address
117   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
118   // its zero value will terminate the decoding of .debug_ranges prematurely.
119   Symbol &s = file->getRelocTargetSym(rel);
120   uint64_t val = 0;
121   if (auto *dr = dyn_cast<Defined>(&s))
122     val = dr->value;
123
124   DataRefImpl d;
125   d.p = getAddend<ELFT>(rel);
126   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
127                         val,      Optional<object::RelocationRef>(),
128                         0,        LLDRelocationResolver<RelTy>::resolve};
129 }
130
131 template <class ELFT>
132 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
133                                                  uint64_t pos) const {
134   auto &sec = static_cast<const LLDDWARFSection &>(s);
135   if (sec.sec->areRelocsRela)
136     return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
137   return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
138 }
139
140 template class elf::LLDDwarfObj<ELF32LE>;
141 template class elf::LLDDwarfObj<ELF32BE>;
142 template class elf::LLDDwarfObj<ELF64LE>;
143 template class elf::LLDDwarfObj<ELF64BE>;