blob: e3c9095d13438819a690c5f39415783566c90559 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework -----------------===//
David Blaikie85f80d72014-04-23 18:54:00 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Blaikie85f80d72014-04-23 18:54:00 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "DwarfFile.h"
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000010#include "DwarfCompileUnit.h"
David Blaikie85f80d72014-04-23 18:54:00 +000011#include "DwarfDebug.h"
12#include "DwarfUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/CodeGen/DIE.h"
16#include "llvm/IR/DebugInfoMetadata.h"
David Blaikie85f80d72014-04-23 18:54:00 +000017#include "llvm/MC/MCStreamer.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000018#include <algorithm>
19#include <cstdint>
David Blaikie85f80d72014-04-23 18:54:00 +000020
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000021using namespace llvm;
22
Frederic Riss9412d632015-03-04 02:30:17 +000023DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Greg Clayton3462a422016-12-08 01:03:48 +000024 : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000025
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000026void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
David Blaikie85f80d72014-04-23 18:54:00 +000027 CUs.push_back(std::move(U));
28}
29
30// Emit the various dwarf units to the unit section USection with
31// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000032void DwarfFile::emitUnits(bool UseOffsets) {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000033 for (const auto &TheU : CUs)
34 emitUnit(TheU.get(), UseOffsets);
35}
David Blaikie85f80d72014-04-23 18:54:00 +000036
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000037void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
Alexey Bataevd4dd7212018-08-01 19:38:20 +000038 if (TheU->getCUNode()->isDebugDirectivesOnly())
39 return;
40
David Blaikie560ff352018-12-14 22:44:46 +000041 MCSection *S = TheU->getSection();
David Blaikie85f80d72014-04-23 18:54:00 +000042
David Blaikie560ff352018-12-14 22:44:46 +000043 if (!S)
44 return;
45
David Blaikie104dcb32019-02-12 00:00:38 +000046 // Skip CUs that ended up not being needed (split CUs that were abandoned
47 // because they added no information beyond the non-split CU)
48 if (llvm::empty(TheU->getUnitDie().values()))
49 return;
50
David Blaikie560ff352018-12-14 22:44:46 +000051 Asm->OutStreamer->SwitchSection(S);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000052 TheU->emitHeader(UseOffsets);
David Blaikie560ff352018-12-14 22:44:46 +000053 Asm->emitDwarfDIE(TheU->getUnitDie());
David Blaikiec4e08fe2018-12-18 01:06:09 +000054
55 if (MCSymbol *EndLabel = TheU->getEndLabel())
56 Asm->OutStreamer->EmitLabel(EndLabel);
David Blaikie85f80d72014-04-23 18:54:00 +000057}
David Blaikief2999472014-10-23 00:16:05 +000058
David Blaikie85f80d72014-04-23 18:54:00 +000059// Compute the size and offset for each DIE.
60void DwarfFile::computeSizeAndOffsets() {
61 // Offset from the first CU in the debug info section is 0 initially.
62 unsigned SecOffset = 0;
63
64 // Iterate over each compile unit and set the size and offsets for each
65 // DIE within each compile unit. All offsets are CU relative.
66 for (const auto &TheU : CUs) {
Alexey Bataevd4dd7212018-08-01 19:38:20 +000067 if (TheU->getCUNode()->isDebugDirectivesOnly())
68 continue;
69
David Blaikie104dcb32019-02-12 00:00:38 +000070 // Skip CUs that ended up not being needed (split CUs that were abandoned
71 // because they added no information beyond the non-split CU)
72 if (llvm::empty(TheU->getUnitDie().values()))
73 return;
74
Greg Clayton35630c32016-12-01 18:56:29 +000075 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000076 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000077 }
78}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000079
80unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
81 // CU-relative offset is reset to 0 here.
82 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
83 TheU->getHeaderSize(); // Unit-specific headers
84
85 // The return value here is CU-relative, after laying out
86 // all of the CU DIE.
87 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
88}
89
David Blaikie85f80d72014-04-23 18:54:00 +000090// Compute the size and offset of a DIE. The offset is relative to start of the
91// CU. It returns the offset after laying out the DIE.
92unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
Greg Clayton3462a422016-12-08 01:03:48 +000093 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000094}
Frederic Riss9412d632015-03-04 02:30:17 +000095
Greg Clayton3462a422016-12-08 01:03:48 +000096void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
David Blaikie85f80d72014-04-23 18:54:00 +000097
98// Emit strings into a string section.
Wolfgang Pieb456b5552018-01-26 18:52:58 +000099void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
100 bool UseRelativeOffsets) {
101 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +0000102}
David Blaikief2999472014-10-23 00:16:05 +0000103
Adrian Prantlca7e4702015-02-10 23:18:28 +0000104bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000105 auto &ScopeVars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000106 const DILocalVariable *DV = Var->getVariable();
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000107 if (unsigned ArgNum = DV->getArg()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000108 auto Cached = ScopeVars.Args.find(ArgNum);
109 if (Cached == ScopeVars.Args.end())
110 ScopeVars.Args[ArgNum] = Var;
111 else {
112 Cached->second->addMMIEntry(*Var);
113 return false;
David Blaikie3b5c8402014-10-23 22:04:30 +0000114 }
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000115 } else {
116 ScopeVars.Locals.push_back(Var);
Fangrui Songf78650a2018-07-30 19:41:25 +0000117 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000118 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000119}
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000120
121void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
122 SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
123 Labels.push_back(Label);
124}
David Blaikiec4af8bf2018-10-20 07:36:39 +0000125
126std::pair<uint32_t, RangeSpanList *>
David Blaikiec8f7e6c2018-11-08 00:35:54 +0000127DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
128 CURangeLists.push_back(
129 RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R)));
David Blaikiec4af8bf2018-10-20 07:36:39 +0000130 return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
131}