blob: 96149fd63f19ec6af22d0018f2205d5fc8788c87 [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
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000042 DIE &Die = TheU->getUnitDie();
43 MCSection *USection = TheU->getSection();
44 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000045
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000046 TheU->emitHeader(UseOffsets);
47
48 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000049}
David Blaikief2999472014-10-23 00:16:05 +000050
David Blaikie85f80d72014-04-23 18:54:00 +000051// Compute the size and offset for each DIE.
52void DwarfFile::computeSizeAndOffsets() {
53 // Offset from the first CU in the debug info section is 0 initially.
54 unsigned SecOffset = 0;
55
56 // Iterate over each compile unit and set the size and offsets for each
57 // DIE within each compile unit. All offsets are CU relative.
58 for (const auto &TheU : CUs) {
Alexey Bataevd4dd7212018-08-01 19:38:20 +000059 if (TheU->getCUNode()->isDebugDirectivesOnly())
60 continue;
61
Greg Clayton35630c32016-12-01 18:56:29 +000062 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000063 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000064 }
65}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000066
67unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
68 // CU-relative offset is reset to 0 here.
69 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
70 TheU->getHeaderSize(); // Unit-specific headers
71
72 // The return value here is CU-relative, after laying out
73 // all of the CU DIE.
74 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
75}
76
David Blaikie85f80d72014-04-23 18:54:00 +000077// Compute the size and offset of a DIE. The offset is relative to start of the
78// CU. It returns the offset after laying out the DIE.
79unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
Greg Clayton3462a422016-12-08 01:03:48 +000080 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000081}
Frederic Riss9412d632015-03-04 02:30:17 +000082
Greg Clayton3462a422016-12-08 01:03:48 +000083void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
David Blaikie85f80d72014-04-23 18:54:00 +000084
85// Emit strings into a string section.
Wolfgang Pieb456b5552018-01-26 18:52:58 +000086void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
87 bool UseRelativeOffsets) {
88 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000089}
David Blaikief2999472014-10-23 00:16:05 +000090
Adrian Prantlca7e4702015-02-10 23:18:28 +000091bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +000092 auto &ScopeVars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000093 const DILocalVariable *DV = Var->getVariable();
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000094 if (unsigned ArgNum = DV->getArg()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +000095 auto Cached = ScopeVars.Args.find(ArgNum);
96 if (Cached == ScopeVars.Args.end())
97 ScopeVars.Args[ArgNum] = Var;
98 else {
99 Cached->second->addMMIEntry(*Var);
100 return false;
David Blaikie3b5c8402014-10-23 22:04:30 +0000101 }
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000102 } else {
103 ScopeVars.Locals.push_back(Var);
Fangrui Songf78650a2018-07-30 19:41:25 +0000104 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000105 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000106}