1*0b57cec5SDimitry Andric //===- DWARF.cpp ----------------------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // The -gdb-index option instructs the linker to emit a .gdb_index section. 10*0b57cec5SDimitry Andric // The section contains information to make gdb startup faster. 11*0b57cec5SDimitry Andric // The format of the section is described at 12*0b57cec5SDimitry Andric // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html. 13*0b57cec5SDimitry Andric // 14*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include "DWARF.h" 17*0b57cec5SDimitry Andric #include "Symbols.h" 18*0b57cec5SDimitry Andric #include "Target.h" 19*0b57cec5SDimitry Andric #include "lld/Common/Memory.h" 20*0b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 21*0b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric using namespace llvm; 24*0b57cec5SDimitry Andric using namespace llvm::object; 25*0b57cec5SDimitry Andric using namespace lld; 26*0b57cec5SDimitry Andric using namespace lld::elf; 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) { 29*0b57cec5SDimitry Andric for (InputSectionBase *sec : obj->getSections()) { 30*0b57cec5SDimitry Andric if (!sec) 31*0b57cec5SDimitry Andric continue; 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric if (LLDDWARFSection *m = 34*0b57cec5SDimitry Andric StringSwitch<LLDDWARFSection *>(sec->name) 35*0b57cec5SDimitry Andric .Case(".debug_addr", &addrSection) 36*0b57cec5SDimitry Andric .Case(".debug_gnu_pubnames", &gnuPubNamesSection) 37*0b57cec5SDimitry Andric .Case(".debug_gnu_pubtypes", &gnuPubTypesSection) 38*0b57cec5SDimitry Andric .Case(".debug_info", &infoSection) 39*0b57cec5SDimitry Andric .Case(".debug_ranges", &rangeSection) 40*0b57cec5SDimitry Andric .Case(".debug_rnglists", &rngListsSection) 41*0b57cec5SDimitry Andric .Case(".debug_line", &lineSection) 42*0b57cec5SDimitry Andric .Default(nullptr)) { 43*0b57cec5SDimitry Andric m->Data = toStringRef(sec->data()); 44*0b57cec5SDimitry Andric m->sec = sec; 45*0b57cec5SDimitry Andric continue; 46*0b57cec5SDimitry Andric } 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric if (sec->name == ".debug_abbrev") 49*0b57cec5SDimitry Andric abbrevSection = toStringRef(sec->data()); 50*0b57cec5SDimitry Andric else if (sec->name == ".debug_str") 51*0b57cec5SDimitry Andric strSection = toStringRef(sec->data()); 52*0b57cec5SDimitry Andric else if (sec->name == ".debug_line_str") 53*0b57cec5SDimitry Andric lineStringSection = toStringRef(sec->data()); 54*0b57cec5SDimitry Andric } 55*0b57cec5SDimitry Andric } 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric namespace { 58*0b57cec5SDimitry Andric template <class RelTy> struct LLDRelocationResolver { 59*0b57cec5SDimitry Andric // In the ELF ABIs, S sepresents the value of the symbol in the relocation 60*0b57cec5SDimitry Andric // entry. For Rela, the addend is stored as part of the relocation entry. 61*0b57cec5SDimitry Andric static uint64_t resolve(object::RelocationRef ref, uint64_t s, 62*0b57cec5SDimitry Andric uint64_t /* A */) { 63*0b57cec5SDimitry Andric return s + ref.getRawDataRefImpl().p; 64*0b57cec5SDimitry Andric } 65*0b57cec5SDimitry Andric }; 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> { 68*0b57cec5SDimitry Andric // For Rel, the addend A is supplied by the caller. 69*0b57cec5SDimitry Andric static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s, 70*0b57cec5SDimitry Andric uint64_t a) { 71*0b57cec5SDimitry Andric return s + a; 72*0b57cec5SDimitry Andric } 73*0b57cec5SDimitry Andric }; 74*0b57cec5SDimitry Andric } // namespace 75*0b57cec5SDimitry Andric 76*0b57cec5SDimitry Andric // Find if there is a relocation at Pos in Sec. The code is a bit 77*0b57cec5SDimitry Andric // more complicated than usual because we need to pass a section index 78*0b57cec5SDimitry Andric // to llvm since it has no idea about InputSection. 79*0b57cec5SDimitry Andric template <class ELFT> 80*0b57cec5SDimitry Andric template <class RelTy> 81*0b57cec5SDimitry Andric Optional<RelocAddrEntry> 82*0b57cec5SDimitry Andric LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos, 83*0b57cec5SDimitry Andric ArrayRef<RelTy> rels) const { 84*0b57cec5SDimitry Andric auto it = 85*0b57cec5SDimitry Andric partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; }); 86*0b57cec5SDimitry Andric if (it == rels.end() || it->r_offset != pos) 87*0b57cec5SDimitry Andric return None; 88*0b57cec5SDimitry Andric const RelTy &rel = *it; 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric const ObjFile<ELFT> *file = sec.getFile<ELFT>(); 91*0b57cec5SDimitry Andric uint32_t symIndex = rel.getSymbol(config->isMips64EL); 92*0b57cec5SDimitry Andric const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex]; 93*0b57cec5SDimitry Andric uint32_t secIndex = file->getSectionIndex(sym); 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric // An undefined symbol may be a symbol defined in a discarded section. We 96*0b57cec5SDimitry Andric // shall still resolve it. This is important for --gdb-index: the end address 97*0b57cec5SDimitry Andric // offset of an entry in .debug_ranges is relocated. If it is not resolved, 98*0b57cec5SDimitry Andric // its zero value will terminate the decoding of .debug_ranges prematurely. 99*0b57cec5SDimitry Andric Symbol &s = file->getRelocTargetSym(rel); 100*0b57cec5SDimitry Andric uint64_t val = 0; 101*0b57cec5SDimitry Andric if (auto *dr = dyn_cast<Defined>(&s)) { 102*0b57cec5SDimitry Andric val = dr->value; 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric // FIXME: We should be consistent about always adding the file 105*0b57cec5SDimitry Andric // offset or not. 106*0b57cec5SDimitry Andric if (dr->section->flags & ELF::SHF_ALLOC) 107*0b57cec5SDimitry Andric val += cast<InputSection>(dr->section)->getOffsetInFile(); 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric DataRefImpl d; 111*0b57cec5SDimitry Andric d.p = getAddend<ELFT>(rel); 112*0b57cec5SDimitry Andric return RelocAddrEntry{secIndex, RelocationRef(d, nullptr), 113*0b57cec5SDimitry Andric val, Optional<object::RelocationRef>(), 114*0b57cec5SDimitry Andric 0, LLDRelocationResolver<RelTy>::resolve}; 115*0b57cec5SDimitry Andric } 116*0b57cec5SDimitry Andric 117*0b57cec5SDimitry Andric template <class ELFT> 118*0b57cec5SDimitry Andric Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, 119*0b57cec5SDimitry Andric uint64_t pos) const { 120*0b57cec5SDimitry Andric auto &sec = static_cast<const LLDDWARFSection &>(s); 121*0b57cec5SDimitry Andric if (sec.sec->areRelocsRela) 122*0b57cec5SDimitry Andric return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>()); 123*0b57cec5SDimitry Andric return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>()); 124*0b57cec5SDimitry Andric } 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric template class elf::LLDDwarfObj<ELF32LE>; 127*0b57cec5SDimitry Andric template class elf::LLDDwarfObj<ELF32BE>; 128*0b57cec5SDimitry Andric template class elf::LLDDwarfObj<ELF64LE>; 129*0b57cec5SDimitry Andric template class elf::LLDDwarfObj<ELF64BE>; 130