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) { 2916d6b3b3SDimitry Andric // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below. 3016d6b3b3SDimitry Andric ArrayRef<typename ELFT::Shdr> objSections = 3116d6b3b3SDimitry Andric CHECK(obj->getObj().sections(), obj); 3216d6b3b3SDimitry Andric assert(objSections.size() == obj->getSections().size()); 3316d6b3b3SDimitry Andric for (auto it : llvm::enumerate(obj->getSections())) { 3416d6b3b3SDimitry 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()); 6016d6b3b3SDimitry Andric else if (sec->name == ".debug_info" && 6116d6b3b3SDimitry Andric !(objSections[it.index()].sh_flags & ELF::SHF_GROUP)) { 6216d6b3b3SDimitry Andric // In DWARF v5, -fdebug-types-section places type units in .debug_info 6316d6b3b3SDimitry Andric // sections in COMDAT groups. They are not compile units and thus should 6416d6b3b3SDimitry Andric // be ignored for .gdb_index/diagnostics purposes. 6516d6b3b3SDimitry Andric // 6616d6b3b3SDimitry Andric // We use a simple heuristic: the compile unit does not have the SHF_GROUP 6716d6b3b3SDimitry Andric // flag. If we place compile units in COMDAT groups in the future, we may 6816d6b3b3SDimitry Andric // need to perform a lightweight parsing. We drop the SHF_GROUP flag when 6916d6b3b3SDimitry Andric // the InputSection was created, so we need to retrieve sh_flags from the 7016d6b3b3SDimitry Andric // associated ELF section header. 7116d6b3b3SDimitry Andric infoSection.Data = toStringRef(sec->data()); 7216d6b3b3SDimitry Andric infoSection.sec = sec; 7316d6b3b3SDimitry 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 80*e8d8bef9SDimitry Andric // entry. For Rela, the addend is stored as part of the relocation entry and 81*e8d8bef9SDimitry Andric // is provided by the `findAux` method. 82*e8d8bef9SDimitry Andric // In resolve() methods, the `type` and `offset` arguments would always be 0, 83*e8d8bef9SDimitry Andric // because we don't set an owning object for the `RelocationRef` instance that 84*e8d8bef9SDimitry Andric // we create in `findAux()`. 85*e8d8bef9SDimitry Andric static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s, 86*e8d8bef9SDimitry Andric uint64_t /*locData*/, int64_t addend) { 87*e8d8bef9SDimitry Andric return s + addend; 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric }; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> { 92*e8d8bef9SDimitry Andric // For Rel, the addend is extracted from the relocated location and is 93*e8d8bef9SDimitry Andric // supplied by the caller. 94*e8d8bef9SDimitry Andric static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s, 95*e8d8bef9SDimitry Andric uint64_t locData, int64_t /*addend*/) { 96*e8d8bef9SDimitry Andric return s + locData; 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric }; 990b57cec5SDimitry Andric } // namespace 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // Find if there is a relocation at Pos in Sec. The code is a bit 1020b57cec5SDimitry Andric // more complicated than usual because we need to pass a section index 1030b57cec5SDimitry Andric // to llvm since it has no idea about InputSection. 1040b57cec5SDimitry Andric template <class ELFT> 1050b57cec5SDimitry Andric template <class RelTy> 1060b57cec5SDimitry Andric Optional<RelocAddrEntry> 1070b57cec5SDimitry Andric LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos, 1080b57cec5SDimitry Andric ArrayRef<RelTy> rels) const { 1090b57cec5SDimitry Andric auto it = 1100b57cec5SDimitry Andric partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; }); 1110b57cec5SDimitry Andric if (it == rels.end() || it->r_offset != pos) 1120b57cec5SDimitry Andric return None; 1130b57cec5SDimitry Andric const RelTy &rel = *it; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric const ObjFile<ELFT> *file = sec.getFile<ELFT>(); 1160b57cec5SDimitry Andric uint32_t symIndex = rel.getSymbol(config->isMips64EL); 1170b57cec5SDimitry Andric const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex]; 1180b57cec5SDimitry Andric uint32_t secIndex = file->getSectionIndex(sym); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric // An undefined symbol may be a symbol defined in a discarded section. We 1210b57cec5SDimitry Andric // shall still resolve it. This is important for --gdb-index: the end address 1220b57cec5SDimitry Andric // offset of an entry in .debug_ranges is relocated. If it is not resolved, 1230b57cec5SDimitry Andric // its zero value will terminate the decoding of .debug_ranges prematurely. 1240b57cec5SDimitry Andric Symbol &s = file->getRelocTargetSym(rel); 1250b57cec5SDimitry Andric uint64_t val = 0; 1265ffd83dbSDimitry Andric if (auto *dr = dyn_cast<Defined>(&s)) 1270b57cec5SDimitry Andric val = dr->value; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric DataRefImpl d; 1300b57cec5SDimitry Andric d.p = getAddend<ELFT>(rel); 1310b57cec5SDimitry Andric return RelocAddrEntry{secIndex, RelocationRef(d, nullptr), 1320b57cec5SDimitry Andric val, Optional<object::RelocationRef>(), 1330b57cec5SDimitry Andric 0, LLDRelocationResolver<RelTy>::resolve}; 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric template <class ELFT> 1370b57cec5SDimitry Andric Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, 1380b57cec5SDimitry Andric uint64_t pos) const { 1390b57cec5SDimitry Andric auto &sec = static_cast<const LLDDWARFSection &>(s); 1400b57cec5SDimitry Andric if (sec.sec->areRelocsRela) 1410b57cec5SDimitry Andric return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>()); 1420b57cec5SDimitry Andric return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>()); 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