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; 250b57cec5SDimitry Andric 26*85868e8aSDimitry Andric namespace lld { 27*85868e8aSDimitry Andric namespace elf { 280b57cec5SDimitry Andric template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) { 290b57cec5SDimitry Andric for (InputSectionBase *sec : obj->getSections()) { 300b57cec5SDimitry Andric if (!sec) 310b57cec5SDimitry Andric continue; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric if (LLDDWARFSection *m = 340b57cec5SDimitry Andric StringSwitch<LLDDWARFSection *>(sec->name) 350b57cec5SDimitry Andric .Case(".debug_addr", &addrSection) 36*85868e8aSDimitry Andric .Case(".debug_gnu_pubnames", &gnuPubnamesSection) 37*85868e8aSDimitry Andric .Case(".debug_gnu_pubtypes", &gnuPubtypesSection) 380b57cec5SDimitry Andric .Case(".debug_info", &infoSection) 39*85868e8aSDimitry Andric .Case(".debug_ranges", &rangesSection) 40*85868e8aSDimitry Andric .Case(".debug_rnglists", &rnglistsSection) 41*85868e8aSDimitry Andric .Case(".debug_str_offsets", &strOffsetsSection) 420b57cec5SDimitry Andric .Case(".debug_line", &lineSection) 430b57cec5SDimitry Andric .Default(nullptr)) { 440b57cec5SDimitry Andric m->Data = toStringRef(sec->data()); 450b57cec5SDimitry Andric m->sec = sec; 460b57cec5SDimitry Andric continue; 470b57cec5SDimitry Andric } 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric if (sec->name == ".debug_abbrev") 500b57cec5SDimitry Andric abbrevSection = toStringRef(sec->data()); 510b57cec5SDimitry Andric else if (sec->name == ".debug_str") 520b57cec5SDimitry Andric strSection = toStringRef(sec->data()); 530b57cec5SDimitry Andric else if (sec->name == ".debug_line_str") 54*85868e8aSDimitry Andric lineStrSection = toStringRef(sec->data()); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric namespace { 590b57cec5SDimitry Andric template <class RelTy> struct LLDRelocationResolver { 600b57cec5SDimitry Andric // In the ELF ABIs, S sepresents the value of the symbol in the relocation 610b57cec5SDimitry Andric // entry. For Rela, the addend is stored as part of the relocation entry. 620b57cec5SDimitry Andric static uint64_t resolve(object::RelocationRef ref, uint64_t s, 630b57cec5SDimitry Andric uint64_t /* A */) { 640b57cec5SDimitry Andric return s + ref.getRawDataRefImpl().p; 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric }; 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> { 690b57cec5SDimitry Andric // For Rel, the addend A is supplied by the caller. 700b57cec5SDimitry Andric static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s, 710b57cec5SDimitry Andric uint64_t a) { 720b57cec5SDimitry Andric return s + a; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric }; 750b57cec5SDimitry Andric } // namespace 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric // Find if there is a relocation at Pos in Sec. The code is a bit 780b57cec5SDimitry Andric // more complicated than usual because we need to pass a section index 790b57cec5SDimitry Andric // to llvm since it has no idea about InputSection. 800b57cec5SDimitry Andric template <class ELFT> 810b57cec5SDimitry Andric template <class RelTy> 820b57cec5SDimitry Andric Optional<RelocAddrEntry> 830b57cec5SDimitry Andric LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos, 840b57cec5SDimitry Andric ArrayRef<RelTy> rels) const { 850b57cec5SDimitry Andric auto it = 860b57cec5SDimitry Andric partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; }); 870b57cec5SDimitry Andric if (it == rels.end() || it->r_offset != pos) 880b57cec5SDimitry Andric return None; 890b57cec5SDimitry Andric const RelTy &rel = *it; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric const ObjFile<ELFT> *file = sec.getFile<ELFT>(); 920b57cec5SDimitry Andric uint32_t symIndex = rel.getSymbol(config->isMips64EL); 930b57cec5SDimitry Andric const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex]; 940b57cec5SDimitry Andric uint32_t secIndex = file->getSectionIndex(sym); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric // An undefined symbol may be a symbol defined in a discarded section. We 970b57cec5SDimitry Andric // shall still resolve it. This is important for --gdb-index: the end address 980b57cec5SDimitry Andric // offset of an entry in .debug_ranges is relocated. If it is not resolved, 990b57cec5SDimitry Andric // its zero value will terminate the decoding of .debug_ranges prematurely. 1000b57cec5SDimitry Andric Symbol &s = file->getRelocTargetSym(rel); 1010b57cec5SDimitry Andric uint64_t val = 0; 1020b57cec5SDimitry Andric if (auto *dr = dyn_cast<Defined>(&s)) { 1030b57cec5SDimitry Andric val = dr->value; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric // FIXME: We should be consistent about always adding the file 1060b57cec5SDimitry Andric // offset or not. 1070b57cec5SDimitry Andric if (dr->section->flags & ELF::SHF_ALLOC) 1080b57cec5SDimitry Andric val += cast<InputSection>(dr->section)->getOffsetInFile(); 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric DataRefImpl d; 1120b57cec5SDimitry Andric d.p = getAddend<ELFT>(rel); 1130b57cec5SDimitry Andric return RelocAddrEntry{secIndex, RelocationRef(d, nullptr), 1140b57cec5SDimitry Andric val, Optional<object::RelocationRef>(), 1150b57cec5SDimitry Andric 0, LLDRelocationResolver<RelTy>::resolve}; 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric template <class ELFT> 1190b57cec5SDimitry Andric Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, 1200b57cec5SDimitry Andric uint64_t pos) const { 1210b57cec5SDimitry Andric auto &sec = static_cast<const LLDDWARFSection &>(s); 1220b57cec5SDimitry Andric if (sec.sec->areRelocsRela) 1230b57cec5SDimitry Andric return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>()); 1240b57cec5SDimitry Andric return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>()); 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 127*85868e8aSDimitry Andric template class LLDDwarfObj<ELF32LE>; 128*85868e8aSDimitry Andric template class LLDDwarfObj<ELF32BE>; 129*85868e8aSDimitry Andric template class LLDDwarfObj<ELF64LE>; 130*85868e8aSDimitry Andric template class LLDDwarfObj<ELF64BE>; 131*85868e8aSDimitry Andric 132*85868e8aSDimitry Andric } // namespace elf 133*85868e8aSDimitry Andric } // namespace lld 134