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" 17*81ad6265SDimitry 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()); 3216d6b3b3SDimitry Andric for (auto it : llvm::enumerate(obj->getSections())) { 3316d6b3b3SDimitry Andric InputSectionBase *sec = it.value(); 340b57cec5SDimitry Andric if (!sec) 350b57cec5SDimitry Andric continue; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric if (LLDDWARFSection *m = 380b57cec5SDimitry Andric StringSwitch<LLDDWARFSection *>(sec->name) 390b57cec5SDimitry Andric .Case(".debug_addr", &addrSection) 4085868e8aSDimitry Andric .Case(".debug_gnu_pubnames", &gnuPubnamesSection) 4185868e8aSDimitry Andric .Case(".debug_gnu_pubtypes", &gnuPubtypesSection) 425ffd83dbSDimitry Andric .Case(".debug_loclists", &loclistsSection) 4385868e8aSDimitry Andric .Case(".debug_ranges", &rangesSection) 4485868e8aSDimitry Andric .Case(".debug_rnglists", &rnglistsSection) 4585868e8aSDimitry Andric .Case(".debug_str_offsets", &strOffsetsSection) 460b57cec5SDimitry Andric .Case(".debug_line", &lineSection) 470b57cec5SDimitry Andric .Default(nullptr)) { 480b57cec5SDimitry Andric m->Data = toStringRef(sec->data()); 490b57cec5SDimitry Andric m->sec = sec; 500b57cec5SDimitry Andric continue; 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric if (sec->name == ".debug_abbrev") 540b57cec5SDimitry Andric abbrevSection = toStringRef(sec->data()); 550b57cec5SDimitry Andric else if (sec->name == ".debug_str") 560b57cec5SDimitry Andric strSection = toStringRef(sec->data()); 570b57cec5SDimitry Andric else if (sec->name == ".debug_line_str") 5885868e8aSDimitry Andric lineStrSection = toStringRef(sec->data()); 5916d6b3b3SDimitry Andric else if (sec->name == ".debug_info" && 6016d6b3b3SDimitry Andric !(objSections[it.index()].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. 7016d6b3b3SDimitry Andric infoSection.Data = toStringRef(sec->data()); 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()`. 84e8d8bef9SDimitry 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. 93e8d8bef9SDimitry 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> 1050b57cec5SDimitry Andric Optional<RelocAddrEntry> 1060b57cec5SDimitry 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) 1110b57cec5SDimitry Andric return None; 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), 1310b57cec5SDimitry Andric val, Optional<object::RelocationRef>(), 1320b57cec5SDimitry Andric 0, LLDRelocationResolver<RelTy>::resolve}; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric template <class ELFT> 1360b57cec5SDimitry Andric Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, 1370b57cec5SDimitry Andric uint64_t pos) const { 1380b57cec5SDimitry Andric auto &sec = static_cast<const LLDDWARFSection &>(s); 139349cc55cSDimitry Andric const RelsOrRelas<ELFT> rels = sec.sec->template relsOrRelas<ELFT>(); 140349cc55cSDimitry Andric if (rels.areRelocsRel()) 141349cc55cSDimitry Andric return findAux(*sec.sec, pos, rels.rels); 142349cc55cSDimitry Andric return findAux(*sec.sec, pos, rels.relas); 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1455ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32LE>; 1465ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF32BE>; 1475ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64LE>; 1485ffd83dbSDimitry Andric template class elf::LLDDwarfObj<ELF64BE>; 149