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