blob: 0bfa1d46008b28061f65671fda26a8238158532c [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 Blaikie85f80d72014-04-23 18:54:00 +000050}
David Blaikief2999472014-10-23 00:16:05 +000051
David Blaikie85f80d72014-04-23 18:54:00 +000052// Compute the size and offset for each DIE.
53void DwarfFile::computeSizeAndOffsets() {
54 // Offset from the first CU in the debug info section is 0 initially.
55 unsigned SecOffset = 0;
56
57 // Iterate over each compile unit and set the size and offsets for each
58 // DIE within each compile unit. All offsets are CU relative.
59 for (const auto &TheU : CUs) {
Alexey Bataevd4dd7212018-08-01 19:38:20 +000060 if (TheU->getCUNode()->isDebugDirectivesOnly())
61 continue;
62
Greg Clayton35630c32016-12-01 18:56:29 +000063 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000064 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000065 }
66}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000067
68unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
69 // CU-relative offset is reset to 0 here.
70 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
71 TheU->getHeaderSize(); // Unit-specific headers
72
73 // The return value here is CU-relative, after laying out
74 // all of the CU DIE.
75 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
76}
77
David Blaikie85f80d72014-04-23 18:54:00 +000078// Compute the size and offset of a DIE. The offset is relative to start of the
79// CU. It returns the offset after laying out the DIE.
80unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
Greg Clayton3462a422016-12-08 01:03:48 +000081 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000082}
Frederic Riss9412d632015-03-04 02:30:17 +000083
Greg Clayton3462a422016-12-08 01:03:48 +000084void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
David Blaikie85f80d72014-04-23 18:54:00 +000085
86// Emit strings into a string section.
Wolfgang Pieb456b5552018-01-26 18:52:58 +000087void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
88 bool UseRelativeOffsets) {
89 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000090}
David Blaikief2999472014-10-23 00:16:05 +000091
Adrian Prantlca7e4702015-02-10 23:18:28 +000092bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +000093 auto &ScopeVars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000094 const DILocalVariable *DV = Var->getVariable();
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000095 if (unsigned ArgNum = DV->getArg()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +000096 auto Cached = ScopeVars.Args.find(ArgNum);
97 if (Cached == ScopeVars.Args.end())
98 ScopeVars.Args[ArgNum] = Var;
99 else {
100 Cached->second->addMMIEntry(*Var);
101 return false;
David Blaikie3b5c8402014-10-23 22:04:30 +0000102 }
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000103 } else {
104 ScopeVars.Locals.push_back(Var);
Fangrui Songf78650a2018-07-30 19:41:25 +0000105 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000106 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000107}
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000108
109void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
110 SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
111 Labels.push_back(Label);
112}
David Blaikiec4af8bf2018-10-20 07:36:39 +0000113
114std::pair<uint32_t, RangeSpanList *>
David Blaikiec8f7e6c2018-11-08 00:35:54 +0000115DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
116 CURangeLists.push_back(
117 RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R)));
David Blaikiec4af8bf2018-10-20 07:36:39 +0000118 return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
119}