148 lines · cpp
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-exception6//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 at12// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html.13//14//===----------------------------------------------------------------------===//15 16#include "DWARF.h"17#include "InputSection.h"18#include "Symbols.h"19 20using namespace llvm;21using namespace llvm::object;22using namespace lld;23using namespace lld::elf;24 25template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {26 // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.27 ArrayRef<typename ELFT::Shdr> objSections = obj->template getELFShdrs<ELFT>();28 assert(objSections.size() == obj->getSections().size());29 for (auto [i, sec] : llvm::enumerate(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_line", &lineSection)39 .Case(".debug_loclists", &loclistsSection)40 .Case(".debug_names", &namesSection)41 .Case(".debug_ranges", &rangesSection)42 .Case(".debug_rnglists", &rnglistsSection)43 .Case(".debug_str_offsets", &strOffsetsSection)44 .Default(nullptr)) {45 m->Data = toStringRef(sec->contentMaybeDecompress());46 m->sec = sec;47 continue;48 }49 50 if (sec->name == ".debug_abbrev")51 abbrevSection = toStringRef(sec->contentMaybeDecompress());52 else if (sec->name == ".debug_str")53 strSection = toStringRef(sec->contentMaybeDecompress());54 else if (sec->name == ".debug_line_str")55 lineStrSection = toStringRef(sec->contentMaybeDecompress());56 else if (sec->name == ".debug_info" &&57 !(objSections[i].sh_flags & ELF::SHF_GROUP)) {58 // In DWARF v5, -fdebug-types-section places type units in .debug_info59 // sections in COMDAT groups. They are not compile units and thus should60 // be ignored for .gdb_index/diagnostics purposes.61 //62 // We use a simple heuristic: the compile unit does not have the SHF_GROUP63 // flag. If we place compile units in COMDAT groups in the future, we may64 // need to perform a lightweight parsing. We drop the SHF_GROUP flag when65 // the InputSection was created, so we need to retrieve sh_flags from the66 // associated ELF section header.67 infoSection.Data = toStringRef(sec->contentMaybeDecompress());68 infoSection.sec = sec;69 }70 }71}72 73namespace {74template <class RelTy> struct LLDRelocationResolver {75 // In the ELF ABIs, S sepresents the value of the symbol in the relocation76 // entry. For Rela, the addend is stored as part of the relocation entry and77 // is provided by the `findAux` method.78 // In resolve() methods, the `type` and `offset` arguments would always be 0,79 // because we don't set an owning object for the `RelocationRef` instance that80 // we create in `findAux()`.81 static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,82 uint64_t /*locData*/, int64_t addend) {83 return s + addend;84 }85};86 87template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {88 // For Rel, the addend is extracted from the relocated location and is89 // supplied by the caller.90 static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,91 uint64_t locData, int64_t /*addend*/) {92 return s + locData;93 }94};95} // namespace96 97// Find if there is a relocation at Pos in Sec. The code is a bit98// more complicated than usual because we need to pass a section index99// to llvm since it has no idea about InputSection.100template <class ELFT>101template <class RelTy>102std::optional<RelocAddrEntry>103LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,104 ArrayRef<RelTy> rels) const {105 auto it =106 partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });107 if (it == rels.end() || it->r_offset != pos)108 return std::nullopt;109 const RelTy &rel = *it;110 111 const ObjFile<ELFT> *file = sec.getFile<ELFT>();112 Ctx &ctx = sec.getCtx();113 uint32_t symIndex = rel.getSymbol(ctx.arg.isMips64EL);114 const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];115 uint32_t secIndex = file->getSectionIndex(sym);116 117 // An undefined symbol may be a symbol defined in a discarded section. We118 // shall still resolve it. This is important for --gdb-index: the end address119 // offset of an entry in .debug_ranges is relocated. If it is not resolved,120 // its zero value will terminate the decoding of .debug_ranges prematurely.121 Symbol &s = file->getRelocTargetSym(rel);122 uint64_t val = 0;123 if (auto *dr = dyn_cast<Defined>(&s))124 val = dr->value;125 126 DataRefImpl d;127 d.p = getAddend<ELFT>(rel);128 return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),129 val, std::optional<object::RelocationRef>(),130 0, LLDRelocationResolver<RelTy>::resolve};131}132 133template <class ELFT>134std::optional<RelocAddrEntry>135LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {136 auto &sec = static_cast<const LLDDWARFSection &>(s);137 const RelsOrRelas<ELFT> rels =138 sec.sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);139 if (rels.areRelocsRel())140 return findAux(*sec.sec, pos, rels.rels);141 return findAux(*sec.sec, pos, rels.relas);142}143 144template class elf::LLDDwarfObj<ELF32LE>;145template class elf::LLDDwarfObj<ELF32BE>;146template class elf::LLDDwarfObj<ELF64LE>;147template class elf::LLDDwarfObj<ELF64BE>;148