1 //===- DWARF.cpp ----------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
19 #include "lld/Common/Memory.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
21 #include "llvm/Object/ELFObjectFile.h"
24 using namespace llvm::object;
26 using namespace lld::elf;
28 template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
29 for (InputSectionBase *sec : obj->getSections()) {
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", &rangeSection)
40 .Case(".debug_rnglists", &rngListsSection)
41 .Case(".debug_line", &lineSection)
43 m->Data = toStringRef(sec->data());
48 if (sec->name == ".debug_abbrev")
49 abbrevSection = toStringRef(sec->data());
50 else if (sec->name == ".debug_str")
51 strSection = toStringRef(sec->data());
52 else if (sec->name == ".debug_line_str")
53 lineStringSection = toStringRef(sec->data());
58 template <class RelTy> struct LLDRelocationResolver {
59 // In the ELF ABIs, S sepresents the value of the symbol in the relocation
60 // entry. For Rela, the addend is stored as part of the relocation entry.
61 static uint64_t resolve(object::RelocationRef ref, uint64_t s,
63 return s + ref.getRawDataRefImpl().p;
67 template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
68 // For Rel, the addend A is supplied by the caller.
69 static uint64_t resolve(object::RelocationRef /*Ref*/, uint64_t s,
76 // Find if there is a relocation at Pos in Sec. The code is a bit
77 // more complicated than usual because we need to pass a section index
78 // to llvm since it has no idea about InputSection.
80 template <class RelTy>
81 Optional<RelocAddrEntry>
82 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
83 ArrayRef<RelTy> rels) const {
85 partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
86 if (it == rels.end() || it->r_offset != pos)
88 const RelTy &rel = *it;
90 const ObjFile<ELFT> *file = sec.getFile<ELFT>();
91 uint32_t symIndex = rel.getSymbol(config->isMips64EL);
92 const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
93 uint32_t secIndex = file->getSectionIndex(sym);
95 // An undefined symbol may be a symbol defined in a discarded section. We
96 // shall still resolve it. This is important for --gdb-index: the end address
97 // offset of an entry in .debug_ranges is relocated. If it is not resolved,
98 // its zero value will terminate the decoding of .debug_ranges prematurely.
99 Symbol &s = file->getRelocTargetSym(rel);
101 if (auto *dr = dyn_cast<Defined>(&s)) {
104 // FIXME: We should be consistent about always adding the file
106 if (dr->section->flags & ELF::SHF_ALLOC)
107 val += cast<InputSection>(dr->section)->getOffsetInFile();
111 d.p = getAddend<ELFT>(rel);
112 return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
113 val, Optional<object::RelocationRef>(),
114 0, LLDRelocationResolver<RelTy>::resolve};
117 template <class ELFT>
118 Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
119 uint64_t pos) const {
120 auto &sec = static_cast<const LLDDWARFSection &>(s);
121 if (sec.sec->areRelocsRela)
122 return findAux(*sec.sec, pos, sec.sec->template relas<ELFT>());
123 return findAux(*sec.sec, pos, sec.sec->template rels<ELFT>());
126 template class elf::LLDDwarfObj<ELF32LE>;
127 template class elf::LLDDwarfObj<ELF32BE>;
128 template class elf::LLDDwarfObj<ELF64LE>;
129 template class elf::LLDDwarfObj<ELF64BE>;