blob: 87f39167c85b96a4cec09ce7d0be26ddc268962e [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) {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000106 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000107 const DILocalVariable *DV = Var->getVariable();
David Blaikie3b5c8402014-10-23 22:04:30 +0000108 // Variables with positive arg numbers are parameters.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000109 if (unsigned ArgNum = DV->getArg()) {
David Blaikie3b5c8402014-10-23 22:04:30 +0000110 // Keep all parameters in order at the start of the variable list to ensure
111 // function types are correct (no out-of-order parameters)
112 //
113 // This could be improved by only doing it for optimized builds (unoptimized
114 // builds have the right order to begin with), searching from the back (this
115 // would catch the unoptimized case quickly), or doing a binary search
116 // rather than linear search.
117 auto I = Vars.begin();
118 while (I != Vars.end()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000119 unsigned CurNum = (*I)->getVariable()->getArg();
David Blaikie3b5c8402014-10-23 22:04:30 +0000120 // A local (non-parameter) variable has been found, insert immediately
121 // before it.
122 if (CurNum == 0)
123 break;
124 // A later indexed parameter has been found, insert immediately before it.
125 if (CurNum > ArgNum)
126 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000127 if (CurNum == ArgNum) {
128 (*I)->addMMIEntry(*Var);
129 return false;
130 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000131 ++I;
132 }
133 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000134 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000135 }
136
137 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000138 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000139}