xref: /freebsd/contrib/llvm-project/lld/ELF/DWARF.cpp (revision 16d6b3b3da62aa5baaf3c66c8d4e6f8c8f70aeb7)
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 //
90b57cec5SDimitry 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"
170b57cec5SDimitry Andric #include "Symbols.h"
180b57cec5SDimitry Andric #include "Target.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) {
29*16d6b3b3SDimitry Andric   // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.
30*16d6b3b3SDimitry Andric   ArrayRef<typename ELFT::Shdr> objSections =
31*16d6b3b3SDimitry Andric       CHECK(obj->getObj().sections(), obj);
32*16d6b3b3SDimitry Andric   assert(objSections.size() == obj->getSections().size());
33*16d6b3b3SDimitry Andric   for (auto it : llvm::enumerate(obj->getSections())) {
34*16d6b3b3SDimitry Andric     InputSectionBase *sec = it.value();
350b57cec5SDimitry Andric     if (!sec)
360b57cec5SDimitry Andric       continue;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric     if (LLDDWARFSection *m =
390b57cec5SDimitry Andric             StringSwitch<LLDDWARFSection *>(sec->name)
400b57cec5SDimitry Andric                 .Case(".debug_addr", &addrSection)
4185868e8aSDimitry Andric                 .Case(".debug_gnu_pubnames", &gnuPubnamesSection)
4285868e8aSDimitry Andric                 .Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
435ffd83dbSDimitry Andric                 .Case(".debug_loclists", &loclistsSection)
4485868e8aSDimitry Andric                 .Case(".debug_ranges", &rangesSection)
4585868e8aSDimitry Andric                 .Case(".debug_rnglists", &rnglistsSection)
4685868e8aSDimitry Andric                 .Case(".debug_str_offsets", &strOffsetsSection)
470b57cec5SDimitry Andric                 .Case(".debug_line", &lineSection)
480b57cec5SDimitry Andric                 .Default(nullptr)) {
490b57cec5SDimitry Andric       m->Data = toStringRef(sec->data());
500b57cec5SDimitry Andric       m->sec = sec;
510b57cec5SDimitry Andric       continue;
520b57cec5SDimitry Andric     }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric     if (sec->name == ".debug_abbrev")
550b57cec5SDimitry Andric       abbrevSection = toStringRef(sec->data());
560b57cec5SDimitry Andric     else if (sec->name == ".debug_str")
570b57cec5SDimitry Andric       strSection = toStringRef(sec->data());
580b57cec5SDimitry Andric     else if (sec->name == ".debug_line_str")
5985868e8aSDimitry Andric       lineStrSection = toStringRef(sec->data());
60*16d6b3b3SDimitry Andric     else if (sec->name == ".debug_info" &&
61*16d6b3b3SDimitry Andric              !(objSections[it.index()].sh_flags & ELF::SHF_GROUP)) {
62*16d6b3b3SDimitry Andric       // In DWARF v5, -fdebug-types-section places type units in .debug_info
63*16d6b3b3SDimitry Andric       // sections in COMDAT groups. They are not compile units and thus should
64*16d6b3b3SDimitry Andric       // be ignored for .gdb_index/diagnostics purposes.
65*16d6b3b3SDimitry Andric       //
66*16d6b3b3SDimitry Andric       // We use a simple heuristic: the compile unit does not have the SHF_GROUP
67*16d6b3b3SDimitry Andric       // flag. If we place compile units in COMDAT groups in the future, we may
68*16d6b3b3SDimitry Andric       // need to perform a lightweight parsing. We drop the SHF_GROUP flag when
69*16d6b3b3SDimitry Andric       // the InputSection was created, so we need to retrieve sh_flags from the
70*16d6b3b3SDimitry Andric       // associated ELF section header.
71*16d6b3b3SDimitry Andric       infoSection.Data = toStringRef(sec->data());
72*16d6b3b3SDimitry Andric       infoSection.sec = sec;
73*16d6b3b3SDimitry Andric     }
740b57cec5SDimitry Andric   }
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric namespace {
780b57cec5SDimitry Andric template <class RelTy> struct LLDRelocationResolver {
790b57cec5SDimitry Andric   // In the ELF ABIs, S sepresents the value of the symbol in the relocation
800b57cec5SDimitry Andric   // entry. For Rela, the addend is stored as part of the relocation entry.
810b57cec5SDimitry Andric   static uint64_t resolve(object::RelocationRef ref, uint64_t s,
820b57cec5SDimitry Andric                           uint64_t /* A */) {
830b57cec5SDimitry Andric     return s + ref.getRawDataRefImpl().p;
840b57cec5SDimitry Andric   }
850b57cec5SDimitry Andric };
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
880b57cec5SDimitry Andric   // For Rel, the addend A is supplied by the caller.
890b57cec5SDimitry Andric   static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s,
900b57cec5SDimitry Andric                           uint64_t a) {
910b57cec5SDimitry Andric     return s + a;
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric };
940b57cec5SDimitry Andric } // namespace
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric // Find if there is a relocation at Pos in Sec.  The code is a bit
970b57cec5SDimitry Andric // more complicated than usual because we need to pass a section index
980b57cec5SDimitry Andric // to llvm since it has no idea about InputSection.
990b57cec5SDimitry Andric template <class ELFT>
1000b57cec5SDimitry Andric template <class RelTy>
1010b57cec5SDimitry Andric Optional<RelocAddrEntry>
1020b57cec5SDimitry Andric LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
1030b57cec5SDimitry Andric                            ArrayRef<RelTy> rels) const {
1040b57cec5SDimitry Andric   auto it =
1050b57cec5SDimitry Andric       partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
1060b57cec5SDimitry Andric   if (it == rels.end() || it->r_offset != pos)
1070b57cec5SDimitry Andric     return None;
1080b57cec5SDimitry Andric   const RelTy &rel = *it;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   const ObjFile<ELFT> *file = sec.getFile<ELFT>();
1110b57cec5SDimitry Andric   uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1120b57cec5SDimitry Andric   const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
1130b57cec5SDimitry Andric   uint32_t secIndex = file->getSectionIndex(sym);
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // An undefined symbol may be a symbol defined in a discarded section. We
1160b57cec5SDimitry Andric   // shall still resolve it. This is important for --gdb-index: the end address
1170b57cec5SDimitry Andric   // offset of an entry in .debug_ranges is relocated. If it is not resolved,
1180b57cec5SDimitry Andric   // its zero value will terminate the decoding of .debug_ranges prematurely.
1190b57cec5SDimitry Andric   Symbol &s = file->getRelocTargetSym(rel);
1200b57cec5SDimitry Andric   uint64_t val = 0;
1215ffd83dbSDimitry Andric   if (auto *dr = dyn_cast<Defined>(&s))
1220b57cec5SDimitry Andric     val = dr->value;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   DataRefImpl d;
1250b57cec5SDimitry Andric   d.p = getAddend<ELFT>(rel);
1260b57cec5SDimitry Andric   return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
1270b57cec5SDimitry Andric                         val,      Optional<object::RelocationRef>(),
1280b57cec5SDimitry Andric                         0,        LLDRelocationResolver<RelTy>::resolve};
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric template <class ELFT>
1320b57cec5SDimitry Andric Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
1330b57cec5SDimitry Andric                                                  uint64_t pos) const {
1340b57cec5SDimitry Andric   auto &sec = static_cast<const LLDDWARFSection &>(s);
1350b57cec5SDimitry Andric   if (sec.sec->areRelocsRela)
1360b57cec5SDimitry Andric     return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
1370b57cec5SDimitry Andric   return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1405ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32LE>;
1415ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32BE>;
1425ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64LE>;
1435ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64BE>;
144