xref: /freebsd/contrib/llvm-project/lld/ELF/DWARF.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- DWARF.cpp ----------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
9349cc55cSDimitry Andric // The --gdb-index option instructs the linker to emit a .gdb_index section.
100b57cec5SDimitry Andric // The section contains information to make gdb startup faster.
110b57cec5SDimitry Andric // The format of the section is described at
120b57cec5SDimitry Andric // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "DWARF.h"
1781ad6265SDimitry Andric #include "InputSection.h"
180b57cec5SDimitry Andric #include "Symbols.h"
190b57cec5SDimitry Andric #include "lld/Common/Memory.h"
200b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
210b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric using namespace llvm::object;
255ffd83dbSDimitry Andric using namespace lld;
265ffd83dbSDimitry Andric using namespace lld::elf;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
2916d6b3b3SDimitry Andric   // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.
300eae32dcSDimitry Andric   ArrayRef<typename ELFT::Shdr> objSections = obj->template getELFShdrs<ELFT>();
3116d6b3b3SDimitry Andric   assert(objSections.size() == obj->getSections().size());
32*bdd1243dSDimitry Andric   for (auto [i, sec] : llvm::enumerate(obj->getSections())) {
330b57cec5SDimitry Andric     if (!sec)
340b57cec5SDimitry Andric       continue;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric     if (LLDDWARFSection *m =
370b57cec5SDimitry Andric             StringSwitch<LLDDWARFSection *>(sec->name)
380b57cec5SDimitry Andric                 .Case(".debug_addr", &addrSection)
3985868e8aSDimitry Andric                 .Case(".debug_gnu_pubnames", &gnuPubnamesSection)
4085868e8aSDimitry Andric                 .Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
415ffd83dbSDimitry Andric                 .Case(".debug_loclists", &loclistsSection)
4285868e8aSDimitry Andric                 .Case(".debug_ranges", &rangesSection)
4385868e8aSDimitry Andric                 .Case(".debug_rnglists", &rnglistsSection)
4485868e8aSDimitry Andric                 .Case(".debug_str_offsets", &strOffsetsSection)
450b57cec5SDimitry Andric                 .Case(".debug_line", &lineSection)
460b57cec5SDimitry Andric                 .Default(nullptr)) {
47*bdd1243dSDimitry Andric       m->Data = toStringRef(sec->contentMaybeDecompress());
480b57cec5SDimitry Andric       m->sec = sec;
490b57cec5SDimitry Andric       continue;
500b57cec5SDimitry Andric     }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric     if (sec->name == ".debug_abbrev")
53*bdd1243dSDimitry Andric       abbrevSection = toStringRef(sec->contentMaybeDecompress());
540b57cec5SDimitry Andric     else if (sec->name == ".debug_str")
55*bdd1243dSDimitry Andric       strSection = toStringRef(sec->contentMaybeDecompress());
560b57cec5SDimitry Andric     else if (sec->name == ".debug_line_str")
57*bdd1243dSDimitry Andric       lineStrSection = toStringRef(sec->contentMaybeDecompress());
5816d6b3b3SDimitry Andric     else if (sec->name == ".debug_info" &&
59*bdd1243dSDimitry Andric              !(objSections[i].sh_flags & ELF::SHF_GROUP)) {
6016d6b3b3SDimitry Andric       // In DWARF v5, -fdebug-types-section places type units in .debug_info
6116d6b3b3SDimitry Andric       // sections in COMDAT groups. They are not compile units and thus should
6216d6b3b3SDimitry Andric       // be ignored for .gdb_index/diagnostics purposes.
6316d6b3b3SDimitry Andric       //
6416d6b3b3SDimitry Andric       // We use a simple heuristic: the compile unit does not have the SHF_GROUP
6516d6b3b3SDimitry Andric       // flag. If we place compile units in COMDAT groups in the future, we may
6616d6b3b3SDimitry Andric       // need to perform a lightweight parsing. We drop the SHF_GROUP flag when
6716d6b3b3SDimitry Andric       // the InputSection was created, so we need to retrieve sh_flags from the
6816d6b3b3SDimitry Andric       // associated ELF section header.
69*bdd1243dSDimitry Andric       infoSection.Data = toStringRef(sec->contentMaybeDecompress());
7016d6b3b3SDimitry Andric       infoSection.sec = sec;
7116d6b3b3SDimitry Andric     }
720b57cec5SDimitry Andric   }
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric namespace {
760b57cec5SDimitry Andric template <class RelTy> struct LLDRelocationResolver {
770b57cec5SDimitry Andric   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
78e8d8bef9SDimitry Andric   // entry. For Rela, the addend is stored as part of the relocation entry and
79e8d8bef9SDimitry Andric   // is provided by the `findAux` method.
80e8d8bef9SDimitry Andric   // In resolve() methods, the `type` and `offset` arguments would always be 0,
81e8d8bef9SDimitry Andric   // because we don't set an owning object for the `RelocationRef` instance that
82e8d8bef9SDimitry Andric   // we create in `findAux()`.
83e8d8bef9SDimitry Andric   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
84e8d8bef9SDimitry Andric                           uint64_t /*locData*/, int64_t addend) {
85e8d8bef9SDimitry Andric     return s + addend;
860b57cec5SDimitry Andric   }
870b57cec5SDimitry Andric };
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
90e8d8bef9SDimitry Andric   // For Rel, the addend is extracted from the relocated location and is
91e8d8bef9SDimitry Andric   // supplied by the caller.
92e8d8bef9SDimitry Andric   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
93e8d8bef9SDimitry Andric                           uint64_t locData, int64_t /*addend*/) {
94e8d8bef9SDimitry Andric     return s + locData;
950b57cec5SDimitry Andric   }
960b57cec5SDimitry Andric };
970b57cec5SDimitry Andric } // namespace
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric // Find if there is a relocation at Pos in Sec.  The code is a bit
1000b57cec5SDimitry Andric // more complicated than usual because we need to pass a section index
1010b57cec5SDimitry Andric // to llvm since it has no idea about InputSection.
1020b57cec5SDimitry Andric template <class ELFT>
1030b57cec5SDimitry Andric template <class RelTy>
104*bdd1243dSDimitry Andric std::optional<RelocAddrEntry>
1050b57cec5SDimitry Andric LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
1060b57cec5SDimitry Andric                            ArrayRef<RelTy> rels) const {
1070b57cec5SDimitry Andric   auto it =
1080b57cec5SDimitry Andric       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
1090b57cec5SDimitry Andric   if (it == rels.end() || it->r_offset != pos)
110*bdd1243dSDimitry Andric     return std::nullopt;
1110b57cec5SDimitry Andric   const RelTy &rel = *it;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
1140b57cec5SDimitry Andric   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1150b57cec5SDimitry Andric   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
1160b57cec5SDimitry Andric   uint32_t secIndex = file->getSectionIndex(sym);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   // An undefined symbol may be a symbol defined in a discarded section. We
1190b57cec5SDimitry Andric   // shall still resolve it. This is important for --gdb-index: the end address
1200b57cec5SDimitry Andric   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
1210b57cec5SDimitry Andric   // its zero value will terminate the decoding of .debug_ranges prematurely.
1220b57cec5SDimitry Andric   Symbol &s = file->getRelocTargetSym(rel);
1230b57cec5SDimitry Andric   uint64_t val = 0;
1245ffd83dbSDimitry Andric   if (auto *dr = dyn_cast<Defined>(&s))
1250b57cec5SDimitry Andric     val = dr->value;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   DataRefImpl d;
1280b57cec5SDimitry Andric   d.p = getAddend<ELFT>(rel);
1290b57cec5SDimitry Andric   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
130*bdd1243dSDimitry Andric                         val,      std::optional<object::RelocationRef>(),
1310b57cec5SDimitry Andric                         0,        LLDRelocationResolver<RelTy>::resolve};
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric template <class ELFT>
135*bdd1243dSDimitry Andric std::optional<RelocAddrEntry>
136*bdd1243dSDimitry Andric LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {
1370b57cec5SDimitry Andric   auto &sec = static_cast<const LLDDWARFSection &>(s);
138349cc55cSDimitry Andric   const RelsOrRelas<ELFT> rels = sec.sec->template relsOrRelas<ELFT>();
139349cc55cSDimitry Andric   if (rels.areRelocsRel())
140349cc55cSDimitry Andric     return findAux(*sec.sec, pos, rels.rels);
141349cc55cSDimitry Andric   return findAux(*sec.sec, pos, rels.relas);
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1445ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32LE>;
1455ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32BE>;
1465ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64LE>;
1475ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64BE>;
148