]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/ELF/DWARF.cpp
Fix a memory leak in if_delgroups() introduced in r334118.
[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   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_ranges", &rangeSection)
40                 .Case(".debug_rnglists", &rngListsSection)
41                 .Case(".debug_line", &lineSection)
42                 .Default(nullptr)) {
43       m->Data = toStringRef(sec->data());
44       m->sec = sec;
45       continue;
46     }
47
48     if (sec->name == ".debug_abbrev")
49       abbrevSection = toStringRef(sec->data());
50     else if (sec->name == ".debug_str")
51       strSection = toStringRef(sec->data());
52     else if (sec->name == ".debug_line_str")
53       lineStringSection = toStringRef(sec->data());
54   }
55 }
56
57 namespace {
58 template <class RelTy> struct LLDRelocationResolver {
59   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
60   // entry. For Rela, the addend is stored as part of the relocation entry.
61   static uint64_t resolve(object::RelocationRef ref, uint64_t s,
62                           uint64_t /* A */) {
63     return s + ref.getRawDataRefImpl().p;
64   }
65 };
66
67 template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
68   // For Rel, the addend A is supplied by the caller.
69   static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s,
70                           uint64_t a) {
71     return s + a;
72   }
73 };
74 } // namespace
75
76 // Find if there is a relocation at Pos in Sec.  The code is a bit
77 // more complicated than usual because we need to pass a section index
78 // to llvm since it has no idea about InputSection.
79 template <class ELFT>
80 template <class RelTy>
81 Optional<RelocAddrEntry>
82 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
83                            ArrayRef<RelTy> rels) const {
84   auto it =
85       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
86   if (it == rels.end() || it->r_offset != pos)
87     return None;
88   const RelTy &rel = *it;
89
90   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
91   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
92   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
93   uint32_t secIndex = file->getSectionIndex(sym);
94
95   // An undefined symbol may be a symbol defined in a discarded section. We
96   // shall still resolve it. This is important for --gdb-index: the end address
97   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
98   // its zero value will terminate the decoding of .debug_ranges prematurely.
99   Symbol &s = file->getRelocTargetSym(rel);
100   uint64_t val = 0;
101   if (auto *dr = dyn_cast<Defined>(&s)) {
102     val = dr->value;
103
104     // FIXME: We should be consistent about always adding the file
105     // offset or not.
106     if (dr->section->flags & ELF::SHF_ALLOC)
107       val += cast<InputSection>(dr->section)->getOffsetInFile();
108   }
109
110   DataRefImpl d;
111   d.p = getAddend<ELFT>(rel);
112   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
113                         val,      Optional<object::RelocationRef>(),
114                         0,        LLDRelocationResolver<RelTy>::resolve};
115 }
116
117 template <class ELFT>
118 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
119                                                  uint64_t pos) const {
120   auto &sec = static_cast<const LLDDWARFSection &>(s);
121   if (sec.sec->areRelocsRela)
122     return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
123   return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
124 }
125
126 template class elf::LLDDwarfObj<ELF32LE>;
127 template class elf::LLDDwarfObj<ELF32BE>;
128 template class elf::LLDDwarfObj<ELF64LE>;
129 template class elf::LLDDwarfObj<ELF64BE>;