blob: 19a1dc3c66ef009cb4a49c7a625357167976768e [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
Wolfgang Pieb456b5552018-01-26 18:52:58 +000031void DwarfFile::emitStringOffsetsTableHeader(MCSection *Section) {
32 if (StrPool.empty())
33 return;
34 Asm->OutStreamer->SwitchSection(Section);
35 unsigned EntrySize = 4;
36 // FIXME: DWARF64
37 // We are emitting the header for a contribution to the string offsets
38 // table. The header consists of an entry with the contribution's
39 // size (not including the size of the header), the DWARF version and
40 // 2 bytes of padding.
41 Asm->EmitInt32(StrPool.size() * EntrySize);
42 Asm->EmitInt16(Asm->getDwarfVersion());
43 Asm->EmitInt16(0);
44 // Define the symbol that marks the start of the contribution. It is
45 // referenced by most unit headers via DW_AT_str_offsets_base.
46 // Split units do not use the attribute.
47 if (StringOffsetsStartSym)
48 Asm->OutStreamer->EmitLabel(StringOffsetsStartSym);
49}
50
David Blaikie85f80d72014-04-23 18:54:00 +000051// Emit the various dwarf units to the unit section USection with
52// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000053void DwarfFile::emitUnits(bool UseOffsets) {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000054 for (const auto &TheU : CUs)
55 emitUnit(TheU.get(), UseOffsets);
56}
David Blaikie85f80d72014-04-23 18:54:00 +000057
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000058void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
59 DIE &Die = TheU->getUnitDie();
60 MCSection *USection = TheU->getSection();
61 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000062
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000063 TheU->emitHeader(UseOffsets);
64
65 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000066}
David Blaikief2999472014-10-23 00:16:05 +000067
David Blaikie85f80d72014-04-23 18:54:00 +000068// Compute the size and offset for each DIE.
69void DwarfFile::computeSizeAndOffsets() {
70 // Offset from the first CU in the debug info section is 0 initially.
71 unsigned SecOffset = 0;
72
73 // Iterate over each compile unit and set the size and offsets for each
74 // DIE within each compile unit. All offsets are CU relative.
75 for (const auto &TheU : CUs) {
Greg Clayton35630c32016-12-01 18:56:29 +000076 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000077 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000078 }
79}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000080
81unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
82 // CU-relative offset is reset to 0 here.
83 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
84 TheU->getHeaderSize(); // Unit-specific headers
85
86 // The return value here is CU-relative, after laying out
87 // all of the CU DIE.
88 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
89}
90
David Blaikie85f80d72014-04-23 18:54:00 +000091// Compute the size and offset of a DIE. The offset is relative to start of the
92// CU. It returns the offset after laying out the DIE.
93unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
Greg Clayton3462a422016-12-08 01:03:48 +000094 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000095}
Frederic Riss9412d632015-03-04 02:30:17 +000096
Greg Clayton3462a422016-12-08 01:03:48 +000097void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
David Blaikie85f80d72014-04-23 18:54:00 +000098
99// Emit strings into a string section.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000100void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
101 bool UseRelativeOffsets) {
102 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +0000103}
David Blaikief2999472014-10-23 00:16:05 +0000104
Adrian Prantlca7e4702015-02-10 23:18:28 +0000105bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000106 auto &ScopeVars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000107 const DILocalVariable *DV = Var->getVariable();
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000108 if (unsigned ArgNum = DV->getArg()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000109 auto Cached = ScopeVars.Args.find(ArgNum);
110 if (Cached == ScopeVars.Args.end())
111 ScopeVars.Args[ArgNum] = Var;
112 else {
113 Cached->second->addMMIEntry(*Var);
114 return false;
David Blaikie3b5c8402014-10-23 22:04:30 +0000115 }
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000116 } else {
117 ScopeVars.Locals.push_back(Var);
118 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000119 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000120}