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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of lld/ELF/DWARF.cpp