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