xref: /freebsd/contrib/llvm-project/lld/ELF/DWARF.cpp (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
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 
LLDDwarfObj(ObjFile<ELFT> * obj)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());
32bdd1243dSDimitry 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)
410fca6ea1SDimitry Andric                 .Case(".debug_line", &lineSection)
425ffd83dbSDimitry Andric                 .Case(".debug_loclists", &loclistsSection)
430fca6ea1SDimitry Andric                 .Case(".debug_names", &namesSection)
4485868e8aSDimitry Andric                 .Case(".debug_ranges", &rangesSection)
4585868e8aSDimitry Andric                 .Case(".debug_rnglists", &rnglistsSection)
4685868e8aSDimitry Andric                 .Case(".debug_str_offsets", &strOffsetsSection)
470b57cec5SDimitry Andric                 .Default(nullptr)) {
48bdd1243dSDimitry Andric       m->Data = toStringRef(sec->contentMaybeDecompress());
490b57cec5SDimitry Andric       m->sec = sec;
500b57cec5SDimitry Andric       continue;
510b57cec5SDimitry Andric     }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric     if (sec->name == ".debug_abbrev")
54bdd1243dSDimitry Andric       abbrevSection = toStringRef(sec->contentMaybeDecompress());
550b57cec5SDimitry Andric     else if (sec->name == ".debug_str")
56bdd1243dSDimitry Andric       strSection = toStringRef(sec->contentMaybeDecompress());
570b57cec5SDimitry Andric     else if (sec->name == ".debug_line_str")
58bdd1243dSDimitry Andric       lineStrSection = toStringRef(sec->contentMaybeDecompress());
5916d6b3b3SDimitry Andric     else if (sec->name == ".debug_info" &&
60bdd1243dSDimitry Andric              !(objSections[i].sh_flags & ELF::SHF_GROUP)) {
6116d6b3b3SDimitry Andric       // In DWARF v5, -fdebug-types-section places type units in .debug_info
6216d6b3b3SDimitry Andric       // sections in COMDAT groups. They are not compile units and thus should
6316d6b3b3SDimitry Andric       // be ignored for .gdb_index/diagnostics purposes.
6416d6b3b3SDimitry Andric       //
6516d6b3b3SDimitry Andric       // We use a simple heuristic: the compile unit does not have the SHF_GROUP
6616d6b3b3SDimitry Andric       // flag. If we place compile units in COMDAT groups in the future, we may
6716d6b3b3SDimitry Andric       // need to perform a lightweight parsing. We drop the SHF_GROUP flag when
6816d6b3b3SDimitry Andric       // the InputSection was created, so we need to retrieve sh_flags from the
6916d6b3b3SDimitry Andric       // associated ELF section header.
70bdd1243dSDimitry Andric       infoSection.Data = toStringRef(sec->contentMaybeDecompress());
7116d6b3b3SDimitry Andric       infoSection.sec = sec;
7216d6b3b3SDimitry Andric     }
730b57cec5SDimitry Andric   }
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric namespace {
770b57cec5SDimitry Andric template <class RelTy> struct LLDRelocationResolver {
780b57cec5SDimitry Andric   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
79e8d8bef9SDimitry Andric   // entry. For Rela, the addend is stored as part of the relocation entry and
80e8d8bef9SDimitry Andric   // is provided by the `findAux` method.
81e8d8bef9SDimitry Andric   // In resolve() methods, the `type` and `offset` arguments would always be 0,
82e8d8bef9SDimitry Andric   // because we don't set an owning object for the `RelocationRef` instance that
83e8d8bef9SDimitry Andric   // we create in `findAux()`.
resolve__anon1249abc50111::LLDRelocationResolver84e8d8bef9SDimitry Andric   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
85e8d8bef9SDimitry Andric                           uint64_t /*locData*/, int64_t addend) {
86e8d8bef9SDimitry Andric     return s + addend;
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric };
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
91e8d8bef9SDimitry Andric   // For Rel, the addend is extracted from the relocated location and is
92e8d8bef9SDimitry Andric   // supplied by the caller.
resolve__anon1249abc50111::LLDRelocationResolver93e8d8bef9SDimitry Andric   static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,
94e8d8bef9SDimitry Andric                           uint64_t locData, int64_t /*addend*/) {
95e8d8bef9SDimitry Andric     return s + locData;
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric };
980b57cec5SDimitry Andric } // namespace
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric // Find if there is a relocation at Pos in Sec.  The code is a bit
1010b57cec5SDimitry Andric // more complicated than usual because we need to pass a section index
1020b57cec5SDimitry Andric // to llvm since it has no idea about InputSection.
1030b57cec5SDimitry Andric template <class ELFT>
1040b57cec5SDimitry Andric template <class RelTy>
105bdd1243dSDimitry Andric std::optional<RelocAddrEntry>
findAux(const InputSectionBase & sec,uint64_t pos,ArrayRef<RelTy> rels) const1060b57cec5SDimitry Andric LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
1070b57cec5SDimitry Andric                            ArrayRef<RelTy> rels) const {
1080b57cec5SDimitry Andric   auto it =
1090b57cec5SDimitry Andric       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
1100b57cec5SDimitry Andric   if (it == rels.end() || it->r_offset != pos)
111bdd1243dSDimitry Andric     return std::nullopt;
1120b57cec5SDimitry Andric   const RelTy &rel = *it;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
1150b57cec5SDimitry Andric   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1160b57cec5SDimitry Andric   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
1170b57cec5SDimitry Andric   uint32_t secIndex = file->getSectionIndex(sym);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   // An undefined symbol may be a symbol defined in a discarded section. We
1200b57cec5SDimitry Andric   // shall still resolve it. This is important for --gdb-index: the end address
1210b57cec5SDimitry Andric   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
1220b57cec5SDimitry Andric   // its zero value will terminate the decoding of .debug_ranges prematurely.
1230b57cec5SDimitry Andric   Symbol &s = file->getRelocTargetSym(rel);
1240b57cec5SDimitry Andric   uint64_t val = 0;
1255ffd83dbSDimitry Andric   if (auto *dr = dyn_cast<Defined>(&s))
1260b57cec5SDimitry Andric     val = dr->value;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   DataRefImpl d;
1290b57cec5SDimitry Andric   d.p = getAddend<ELFT>(rel);
1300b57cec5SDimitry Andric   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
131bdd1243dSDimitry Andric                         val,      std::optional<object::RelocationRef>(),
1320b57cec5SDimitry Andric                         0,        LLDRelocationResolver<RelTy>::resolve};
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric template <class ELFT>
136bdd1243dSDimitry Andric std::optional<RelocAddrEntry>
find(const llvm::DWARFSection & s,uint64_t pos) const137bdd1243dSDimitry Andric LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {
1380b57cec5SDimitry Andric   auto &sec = static_cast<const LLDDWARFSection &>(s);
139*52418fc2SDimitry Andric   const RelsOrRelas<ELFT> rels =
140*52418fc2SDimitry Andric       sec.sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);
141349cc55cSDimitry Andric   if (rels.areRelocsRel())
142349cc55cSDimitry Andric     return findAux(*sec.sec, pos, rels.rels);
143349cc55cSDimitry Andric   return findAux(*sec.sec, pos, rels.relas);
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
1465ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32LE>;
1475ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32BE>;
1485ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64LE>;
1495ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64BE>;
150