xref: /openbsd-src/gnu/llvm/lld/ELF/DWARF.cpp (revision dfe94b169149f14cc1aee2cf6dad58a8d9a1860c)
1ece8a530Spatrick //===- DWARF.cpp ----------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick //
9*dfe94b16Srobert // The --gdb-index option instructs the linker to emit a .gdb_index section.
10ece8a530Spatrick // The section contains information to make gdb startup faster.
11ece8a530Spatrick // The format of the section is described at
12ece8a530Spatrick // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html.
13ece8a530Spatrick //
14ece8a530Spatrick //===----------------------------------------------------------------------===//
15ece8a530Spatrick 
16ece8a530Spatrick #include "DWARF.h"
17*dfe94b16Srobert #include "InputSection.h"
18ece8a530Spatrick #include "Symbols.h"
19ece8a530Spatrick #include "lld/Common/Memory.h"
20ece8a530Spatrick #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
21ece8a530Spatrick #include "llvm/Object/ELFObjectFile.h"
22ece8a530Spatrick 
23ece8a530Spatrick using namespace llvm;
24ece8a530Spatrick using namespace llvm::object;
25bb684c34Spatrick using namespace lld;
26bb684c34Spatrick using namespace lld::elf;
27ece8a530Spatrick 
LLDDwarfObj(ObjFile<ELFT> * obj)28ece8a530Spatrick template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
29bb684c34Spatrick   // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.
30*dfe94b16Srobert   ArrayRef<typename ELFT::Shdr> objSections = obj->template getELFShdrs<ELFT>();
31bb684c34Spatrick   assert(objSections.size() == obj->getSections().size());
32*dfe94b16Srobert   for (auto [i, sec] : llvm::enumerate(obj->getSections())) {
33ece8a530Spatrick     if (!sec)
34ece8a530Spatrick       continue;
35ece8a530Spatrick 
36ece8a530Spatrick     if (LLDDWARFSection *m =
37ece8a530Spatrick             StringSwitch<LLDDWARFSection *>(sec->name)
38ece8a530Spatrick                 .Case(".debug_addr", &addrSection)
39ece8a530Spatrick                 .Case(".debug_gnu_pubnames", &gnuPubnamesSection)
40ece8a530Spatrick                 .Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
41bb684c34Spatrick                 .Case(".debug_loclists", &loclistsSection)
42ece8a530Spatrick                 .Case(".debug_ranges", &rangesSection)
43ece8a530Spatrick                 .Case(".debug_rnglists", &rnglistsSection)
44ece8a530Spatrick                 .Case(".debug_str_offsets", &strOffsetsSection)
45ece8a530Spatrick                 .Case(".debug_line", &lineSection)
46ece8a530Spatrick                 .Default(nullptr)) {
47*dfe94b16Srobert       m->Data = toStringRef(sec->contentMaybeDecompress());
48ece8a530Spatrick       m->sec = sec;
49ece8a530Spatrick       continue;
50ece8a530Spatrick     }
51ece8a530Spatrick 
52ece8a530Spatrick     if (sec->name == ".debug_abbrev")
53*dfe94b16Srobert       abbrevSection = toStringRef(sec->contentMaybeDecompress());
54ece8a530Spatrick     else if (sec->name == ".debug_str")
55*dfe94b16Srobert       strSection = toStringRef(sec->contentMaybeDecompress());
56ece8a530Spatrick     else if (sec->name == ".debug_line_str")
57*dfe94b16Srobert       lineStrSection = toStringRef(sec->contentMaybeDecompress());
58bb684c34Spatrick     else if (sec->name == ".debug_info" &&
59*dfe94b16Srobert              !(objSections[i].sh_flags & ELF::SHF_GROUP)) {
60bb684c34Spatrick       // In DWARF v5, -fdebug-types-section places type units in .debug_info
61bb684c34Spatrick       // sections in COMDAT groups. They are not compile units and thus should
62bb684c34Spatrick       // be ignored for .gdb_index/diagnostics purposes.
63bb684c34Spatrick       //
64bb684c34Spatrick       // We use a simple heuristic: the compile unit does not have the SHF_GROUP
65bb684c34Spatrick       // flag. If we place compile units in COMDAT groups in the future, we may
66bb684c34Spatrick       // need to perform a lightweight parsing. We drop the SHF_GROUP flag when
67bb684c34Spatrick       // the InputSection was created, so we need to retrieve sh_flags from the
68bb684c34Spatrick       // associated ELF section header.
69*dfe94b16Srobert       infoSection.Data = toStringRef(sec->contentMaybeDecompress());
70bb684c34Spatrick       infoSection.sec = sec;
71bb684c34Spatrick     }
72ece8a530Spatrick   }
73ece8a530Spatrick }
74ece8a530Spatrick 
75ece8a530Spatrick namespace {
76ece8a530Spatrick template <class RelTy> struct LLDRelocationResolver {
77ece8a530Spatrick   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
781cf9926bSpatrick   // entry. For Rela, the addend is stored as part of the relocation entry and
791cf9926bSpatrick   // is provided by the `findAux` method.
801cf9926bSpatrick   // In resolve() methods, the `type` and `offset` arguments would always be 0,
811cf9926bSpatrick   // because we don't set an owning object for the `RelocationRef` instance that
821cf9926bSpatrick   // we create in `findAux()`.
resolve__anon6061d05c0111::LLDRelocationResolver831cf9926bSpatrick   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
841cf9926bSpatrick                           uint64_t /*locData*/, int64_t addend) {
851cf9926bSpatrick     return s + addend;
86ece8a530Spatrick   }
87ece8a530Spatrick };
88ece8a530Spatrick 
89ece8a530Spatrick template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
901cf9926bSpatrick   // For Rel, the addend is extracted from the relocated location and is
911cf9926bSpatrick   // supplied by the caller.
resolve__anon6061d05c0111::LLDRelocationResolver921cf9926bSpatrick   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
931cf9926bSpatrick                           uint64_t locData, int64_t /*addend*/) {
941cf9926bSpatrick     return s + locData;
95ece8a530Spatrick   }
96ece8a530Spatrick };
97ece8a530Spatrick } // namespace
98ece8a530Spatrick 
99ece8a530Spatrick // Find if there is a relocation at Pos in Sec.  The code is a bit
100ece8a530Spatrick // more complicated than usual because we need to pass a section index
101ece8a530Spatrick // to llvm since it has no idea about InputSection.
102ece8a530Spatrick template <class ELFT>
103ece8a530Spatrick template <class RelTy>
104*dfe94b16Srobert std::optional<RelocAddrEntry>
findAux(const InputSectionBase & sec,uint64_t pos,ArrayRef<RelTy> rels) const105ece8a530Spatrick LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
106ece8a530Spatrick                            ArrayRef<RelTy> rels) const {
107ece8a530Spatrick   auto it =
108ece8a530Spatrick       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
109ece8a530Spatrick   if (it == rels.end() || it->r_offset != pos)
110*dfe94b16Srobert     return std::nullopt;
111ece8a530Spatrick   const RelTy &rel = *it;
112ece8a530Spatrick 
113ece8a530Spatrick   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
114ece8a530Spatrick   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
115ece8a530Spatrick   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
116ece8a530Spatrick   uint32_t secIndex = file->getSectionIndex(sym);
117ece8a530Spatrick 
118ece8a530Spatrick   // An undefined symbol may be a symbol defined in a discarded section. We
119ece8a530Spatrick   // shall still resolve it. This is important for --gdb-index: the end address
120ece8a530Spatrick   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
121ece8a530Spatrick   // its zero value will terminate the decoding of .debug_ranges prematurely.
122ece8a530Spatrick   Symbol &s = file->getRelocTargetSym(rel);
123ece8a530Spatrick   uint64_t val = 0;
124bb684c34Spatrick   if (auto *dr = dyn_cast<Defined>(&s))
125ece8a530Spatrick     val = dr->value;
126ece8a530Spatrick 
127ece8a530Spatrick   DataRefImpl d;
128ece8a530Spatrick   d.p = getAddend<ELFT>(rel);
129ece8a530Spatrick   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
130*dfe94b16Srobert                         val,      std::optional<object::RelocationRef>(),
131ece8a530Spatrick                         0,        LLDRelocationResolver<RelTy>::resolve};
132ece8a530Spatrick }
133ece8a530Spatrick 
134ece8a530Spatrick template <class ELFT>
135*dfe94b16Srobert std::optional<RelocAddrEntry>
find(const llvm::DWARFSection & s,uint64_t pos) const136*dfe94b16Srobert LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {
137ece8a530Spatrick   auto &sec = static_cast<const LLDDWARFSection &>(s);
138*dfe94b16Srobert   const RelsOrRelas<ELFT> rels = sec.sec->template relsOrRelas<ELFT>();
139*dfe94b16Srobert   if (rels.areRelocsRel())
140*dfe94b16Srobert     return findAux(*sec.sec, pos, rels.rels);
141*dfe94b16Srobert   return findAux(*sec.sec, pos, rels.relas);
142ece8a530Spatrick }
143ece8a530Spatrick 
144bb684c34Spatrick template class elf::LLDDwarfObj<ELF32LE>;
145bb684c34Spatrick template class elf::LLDDwarfObj<ELF32BE>;
146bb684c34Spatrick template class elf::LLDDwarfObj<ELF64LE>;
147bb684c34Spatrick template class elf::LLDDwarfObj<ELF64BE>;
148