Path: blob/main/contrib/llvm-project/lld/ELF/DWARF.cpp
34870 views
//===- DWARF.cpp ----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// The --gdb-index option instructs the linker to emit a .gdb_index section.9// The section contains information to make gdb startup faster.10// The format of the section is described at11// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html.12//13//===----------------------------------------------------------------------===//1415#include "DWARF.h"16#include "InputSection.h"17#include "Symbols.h"18#include "lld/Common/Memory.h"19#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"20#include "llvm/Object/ELFObjectFile.h"2122using namespace llvm;23using namespace llvm::object;24using namespace lld;25using namespace lld::elf;2627template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {28// Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below.29ArrayRef<typename ELFT::Shdr> objSections = obj->template getELFShdrs<ELFT>();30assert(objSections.size() == obj->getSections().size());31for (auto [i, sec] : llvm::enumerate(obj->getSections())) {32if (!sec)33continue;3435if (LLDDWARFSection *m =36StringSwitch<LLDDWARFSection *>(sec->name)37.Case(".debug_addr", &addrSection)38.Case(".debug_gnu_pubnames", &gnuPubnamesSection)39.Case(".debug_gnu_pubtypes", &gnuPubtypesSection)40.Case(".debug_line", &lineSection)41.Case(".debug_loclists", &loclistsSection)42.Case(".debug_names", &namesSection)43.Case(".debug_ranges", &rangesSection)44.Case(".debug_rnglists", &rnglistsSection)45.Case(".debug_str_offsets", &strOffsetsSection)46.Default(nullptr)) {47m->Data = toStringRef(sec->contentMaybeDecompress());48m->sec = sec;49continue;50}5152if (sec->name == ".debug_abbrev")53abbrevSection = toStringRef(sec->contentMaybeDecompress());54else if (sec->name == ".debug_str")55strSection = toStringRef(sec->contentMaybeDecompress());56else if (sec->name == ".debug_line_str")57lineStrSection = toStringRef(sec->contentMaybeDecompress());58else if (sec->name == ".debug_info" &&59!(objSections[i].sh_flags & ELF::SHF_GROUP)) {60// In DWARF v5, -fdebug-types-section places type units in .debug_info61// sections in COMDAT groups. They are not compile units and thus should62// be ignored for .gdb_index/diagnostics purposes.63//64// We use a simple heuristic: the compile unit does not have the SHF_GROUP65// flag. If we place compile units in COMDAT groups in the future, we may66// need to perform a lightweight parsing. We drop the SHF_GROUP flag when67// the InputSection was created, so we need to retrieve sh_flags from the68// associated ELF section header.69infoSection.Data = toStringRef(sec->contentMaybeDecompress());70infoSection.sec = sec;71}72}73}7475namespace {76template <class RelTy> struct LLDRelocationResolver {77// In the ELF ABIs, S sepresents the value of the symbol in the relocation78// entry. For Rela, the addend is stored as part of the relocation entry and79// is provided by the `findAux` method.80// In resolve() methods, the `type` and `offset` arguments would always be 0,81// because we don't set an owning object for the `RelocationRef` instance that82// we create in `findAux()`.83static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,84uint64_t /*locData*/, int64_t addend) {85return s + addend;86}87};8889template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {90// For Rel, the addend is extracted from the relocated location and is91// supplied by the caller.92static uint64_t resolve(uint64_t /*type*/, uint64_t /*offset*/, uint64_t s,93uint64_t locData, int64_t /*addend*/) {94return s + locData;95}96};97} // namespace9899// Find if there is a relocation at Pos in Sec. The code is a bit100// more complicated than usual because we need to pass a section index101// to llvm since it has no idea about InputSection.102template <class ELFT>103template <class RelTy>104std::optional<RelocAddrEntry>105LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,106ArrayRef<RelTy> rels) const {107auto it =108partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });109if (it == rels.end() || it->r_offset != pos)110return std::nullopt;111const RelTy &rel = *it;112113const ObjFile<ELFT> *file = sec.getFile<ELFT>();114uint32_t symIndex = rel.getSymbol(config->isMips64EL);115const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];116uint32_t secIndex = file->getSectionIndex(sym);117118// An undefined symbol may be a symbol defined in a discarded section. We119// shall still resolve it. This is important for --gdb-index: the end address120// offset of an entry in .debug_ranges is relocated. If it is not resolved,121// its zero value will terminate the decoding of .debug_ranges prematurely.122Symbol &s = file->getRelocTargetSym(rel);123uint64_t val = 0;124if (auto *dr = dyn_cast<Defined>(&s))125val = dr->value;126127DataRefImpl d;128d.p = getAddend<ELFT>(rel);129return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),130val, std::optional<object::RelocationRef>(),1310, LLDRelocationResolver<RelTy>::resolve};132}133134template <class ELFT>135std::optional<RelocAddrEntry>136LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {137auto &sec = static_cast<const LLDDWARFSection &>(s);138const RelsOrRelas<ELFT> rels =139sec.sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);140if (rels.areRelocsRel())141return findAux(*sec.sec, pos, rels.rels);142return findAux(*sec.sec, pos, rels.relas);143}144145template class elf::LLDDwarfObj<ELF32LE>;146template class elf::LLDDwarfObj<ELF32BE>;147template class elf::LLDDwarfObj<ELF64LE>;148template class elf::LLDDwarfObj<ELF64BE>;149150151