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;
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", &rangesSection)
40 .Case(".debug_rnglists", &rnglistsSection)
41 .Case(".debug_str_offsets", &strOffsetsSection)
42 .Case(".debug_line", &lineSection)
44 m->Data = toStringRef(sec->data());
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());
59 template <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,
64 return s + ref.getRawDataRefImpl().p;
68 template <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,
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.
81 template <class RelTy>
82 Optional<RelocAddrEntry>
83 LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
84 ArrayRef<RelTy> rels) const {
86 partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
87 if (it == rels.end() || it->r_offset != pos)
89 const RelTy &rel = *it;
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);
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);
102 if (auto *dr = dyn_cast<Defined>(&s)) {
105 // FIXME: We should be consistent about always adding the file
107 if (dr->section->flags & ELF::SHF_ALLOC)
108 val += cast<InputSection>(dr->section)->getOffsetInFile();
112 d.p = getAddend<ELFT>(rel);
113 return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
114 val, Optional<object::RelocationRef>(),
115 0, LLDRelocationResolver<RelTy>::resolve};
118 template <class ELFT>
119 Optional<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>());
127 template class LLDDwarfObj<ELF32LE>;
128 template class LLDDwarfObj<ELF32BE>;
129 template class LLDDwarfObj<ELF64LE>;
130 template class LLDDwarfObj<ELF64BE>;