Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp
35271 views
//===- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework -----------------===//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//===----------------------------------------------------------------------===//78#include "DwarfFile.h"9#include "DwarfCompileUnit.h"10#include "DwarfDebug.h"11#include "DwarfUnit.h"12#include "llvm/CodeGen/AsmPrinter.h"13#include "llvm/IR/DebugInfoMetadata.h"14#include "llvm/MC/MCStreamer.h"15#include <cstdint>1617using namespace llvm;1819DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)20: Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}2122void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {23CUs.push_back(std::move(U));24}2526// Emit the various dwarf units to the unit section USection with27// the abbreviations going into ASection.28void DwarfFile::emitUnits(bool UseOffsets) {29for (const auto &TheU : CUs)30emitUnit(TheU.get(), UseOffsets);31}3233void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {34if (TheU->getCUNode()->isDebugDirectivesOnly())35return;3637MCSection *S = TheU->getSection();3839if (!S)40return;4142// Skip CUs that ended up not being needed (split CUs that were abandoned43// because they added no information beyond the non-split CU)44if (TheU->getUnitDie().values().empty())45return;4647Asm->OutStreamer->switchSection(S);48TheU->emitHeader(UseOffsets);49Asm->emitDwarfDIE(TheU->getUnitDie());5051if (MCSymbol *EndLabel = TheU->getEndLabel())52Asm->OutStreamer->emitLabel(EndLabel);53}5455// Compute the size and offset for each DIE.56void DwarfFile::computeSizeAndOffsets() {57// Offset from the first CU in the debug info section is 0 initially.58uint64_t SecOffset = 0;5960// Iterate over each compile unit and set the size and offsets for each61// DIE within each compile unit. All offsets are CU relative.62for (const auto &TheU : CUs) {63if (TheU->getCUNode()->isDebugDirectivesOnly())64continue;6566// Skip CUs that ended up not being needed (split CUs that were abandoned67// because they added no information beyond the non-split CU)68if (TheU->getUnitDie().values().empty())69return;7071TheU->setDebugSectionOffset(SecOffset);72SecOffset += computeSizeAndOffsetsForUnit(TheU.get());73}74if (SecOffset > UINT32_MAX && !Asm->isDwarf64())75report_fatal_error("The generated debug information is too large "76"for the 32-bit DWARF format.");77}7879unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {80// CU-relative offset is reset to 0 here.81unsigned Offset = Asm->getUnitLengthFieldByteSize() + // Length of Unit Info82TheU->getHeaderSize(); // Unit-specific headers8384// The return value here is CU-relative, after laying out85// all of the CU DIE.86return computeSizeAndOffset(TheU->getUnitDie(), Offset);87}8889// Compute the size and offset of a DIE. The offset is relative to start of the90// CU. It returns the offset after laying out the DIE.91unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {92return Die.computeOffsetsAndAbbrevs(Asm->getDwarfFormParams(), Abbrevs,93Offset);94}9596void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }9798// Emit strings into a string section.99void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,100bool UseRelativeOffsets) {101StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);102}103104void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {105auto &ScopeVars = ScopeVariables[LS];106const DILocalVariable *DV = Var->getVariable();107if (unsigned ArgNum = DV->getArg()) {108auto Ret = ScopeVars.Args.insert({ArgNum, Var});109assert(Ret.second);110(void)Ret;111} else {112ScopeVars.Locals.push_back(Var);113}114}115116void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {117SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];118Labels.push_back(Label);119}120121std::pair<uint32_t, RangeSpanList *>122DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {123CURangeLists.push_back(124RangeSpanList{Asm->createTempSymbol("debug_ranges"), &CU, std::move(R)});125return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());126}127128129