blob: 78ccad4814111b77bfff2801c6d786c19e70fe63 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DwarfFile.h"
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000011#include "DwarfCompileUnit.h"
David Blaikie85f80d72014-04-23 18:54:00 +000012#include "DwarfDebug.h"
13#include "DwarfUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000014#include "llvm/ADT/SmallVector.h"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/DIE.h"
17#include "llvm/IR/DebugInfoMetadata.h"
David Blaikie85f80d72014-04-23 18:54:00 +000018#include "llvm/MC/MCStreamer.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000019#include <algorithm>
20#include <cstdint>
David Blaikie85f80d72014-04-23 18:54:00 +000021
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000022using namespace llvm;
23
Frederic Riss9412d632015-03-04 02:30:17 +000024DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Greg Clayton3462a422016-12-08 01:03:48 +000025 : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000026
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000027void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
David Blaikie85f80d72014-04-23 18:54:00 +000028 CUs.push_back(std::move(U));
29}
30
31// Emit the various dwarf units to the unit section USection with
32// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000033void DwarfFile::emitUnits(bool UseOffsets) {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000034 for (const auto &TheU : CUs)
35 emitUnit(TheU.get(), UseOffsets);
36}
David Blaikie85f80d72014-04-23 18:54:00 +000037
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000038void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
Alexey Bataevd4dd7212018-08-01 19:38:20 +000039 if (TheU->getCUNode()->isDebugDirectivesOnly())
40 return;
41
David Blaikie560ff352018-12-14 22:44:46 +000042 MCSection *S = TheU->getSection();
David Blaikie85f80d72014-04-23 18:54:00 +000043
David Blaikie560ff352018-12-14 22:44:46 +000044 if (!S)
45 return;
46
47 Asm->OutStreamer->SwitchSection(S);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000048 TheU->emitHeader(UseOffsets);
David Blaikie560ff352018-12-14 22:44:46 +000049 Asm->emitDwarfDIE(TheU->getUnitDie());
David Blaikiec4e08fe2018-12-18 01:06:09 +000050
51 if (MCSymbol *EndLabel = TheU->getEndLabel())
52 Asm->OutStreamer->EmitLabel(EndLabel);
David Blaikie85f80d72014-04-23 18:54:00 +000053}
David Blaikief2999472014-10-23 00:16:05 +000054
David Blaikie85f80d72014-04-23 18:54:00 +000055// 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.
58 unsigned SecOffset = 0;
59
60 // Iterate over each compile unit and set the size and offsets for each
61 // DIE within each compile unit. All offsets are CU relative.
62 for (const auto &TheU : CUs) {
Alexey Bataevd4dd7212018-08-01 19:38:20 +000063 if (TheU->getCUNode()->isDebugDirectivesOnly())
64 continue;
65
Greg Clayton35630c32016-12-01 18:56:29 +000066 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000067 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000068 }
69}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000070
71unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
72 // CU-relative offset is reset to 0 here.
73 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
74 TheU->getHeaderSize(); // Unit-specific headers
75
76 // The return value here is CU-relative, after laying out
77 // all of the CU DIE.
78 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
79}
80
David Blaikie85f80d72014-04-23 18:54:00 +000081// Compute the size and offset of a DIE. The offset is relative to start of the
82// CU. It returns the offset after laying out the DIE.
83unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
Greg Clayton3462a422016-12-08 01:03:48 +000084 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000085}
Frederic Riss9412d632015-03-04 02:30:17 +000086
Greg Clayton3462a422016-12-08 01:03:48 +000087void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
David Blaikie85f80d72014-04-23 18:54:00 +000088
89// Emit strings into a string section.
Wolfgang Pieb456b5552018-01-26 18:52:58 +000090void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
91 bool UseRelativeOffsets) {
92 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000093}
David Blaikief2999472014-10-23 00:16:05 +000094
Adrian Prantlca7e4702015-02-10 23:18:28 +000095bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +000096 auto &ScopeVars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000097 const DILocalVariable *DV = Var->getVariable();
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000098 if (unsigned ArgNum = DV->getArg()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +000099 auto Cached = ScopeVars.Args.find(ArgNum);
100 if (Cached == ScopeVars.Args.end())
101 ScopeVars.Args[ArgNum] = Var;
102 else {
103 Cached->second->addMMIEntry(*Var);
104 return false;
David Blaikie3b5c8402014-10-23 22:04:30 +0000105 }
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000106 } else {
107 ScopeVars.Locals.push_back(Var);
Fangrui Songf78650a2018-07-30 19:41:25 +0000108 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000109 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000110}
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000111
112void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
113 SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
114 Labels.push_back(Label);
115}
David Blaikiec4af8bf2018-10-20 07:36:39 +0000116
117std::pair<uint32_t, RangeSpanList *>
David Blaikiec8f7e6c2018-11-08 00:35:54 +0000118DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
119 CURangeLists.push_back(
120 RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R)));
David Blaikiec4af8bf2018-10-20 07:36:39 +0000121 return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
122}