blob: 595f1d91c4bfbe6dff3eefaffb50f587f97de144 [file] [log] [blame]
David Blaikie85f80d72014-04-23 18:54:00 +00001//===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
2//
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"
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/ADT/STLExtras.h"
15#include "llvm/IR/DataLayout.h"
David Blaikie85f80d72014-04-23 18:54:00 +000016#include "llvm/MC/MCStreamer.h"
17#include "llvm/Support/LEB128.h"
David Blaikie85f80d72014-04-23 18:54:00 +000018#include "llvm/Target/TargetLoweringObjectFile.h"
19
20namespace llvm {
Frederic Riss9412d632015-03-04 02:30:17 +000021DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Greg Clayton3462a422016-12-08 01:03:48 +000022 : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000023
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000024void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
David Blaikie85f80d72014-04-23 18:54:00 +000025 CUs.push_back(std::move(U));
26}
27
28// Emit the various dwarf units to the unit section USection with
29// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000030void DwarfFile::emitUnits(bool UseOffsets) {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000031 for (const auto &TheU : CUs)
32 emitUnit(TheU.get(), UseOffsets);
33}
David Blaikie85f80d72014-04-23 18:54:00 +000034
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000035void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
36 DIE &Die = TheU->getUnitDie();
37 MCSection *USection = TheU->getSection();
38 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000039
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000040 TheU->emitHeader(UseOffsets);
41
42 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000043}
David Blaikief2999472014-10-23 00:16:05 +000044
David Blaikie85f80d72014-04-23 18:54:00 +000045// Compute the size and offset for each DIE.
46void DwarfFile::computeSizeAndOffsets() {
47 // Offset from the first CU in the debug info section is 0 initially.
48 unsigned SecOffset = 0;
49
50 // Iterate over each compile unit and set the size and offsets for each
51 // DIE within each compile unit. All offsets are CU relative.
52 for (const auto &TheU : CUs) {
Greg Clayton35630c32016-12-01 18:56:29 +000053 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000054 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000055 }
56}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000057
58unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
59 // CU-relative offset is reset to 0 here.
60 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
61 TheU->getHeaderSize(); // Unit-specific headers
62
63 // The return value here is CU-relative, after laying out
64 // all of the CU DIE.
65 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
66}
67
David Blaikie85f80d72014-04-23 18:54:00 +000068// Compute the size and offset of a DIE. The offset is relative to start of the
69// CU. It returns the offset after laying out the DIE.
70unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
Greg Clayton3462a422016-12-08 01:03:48 +000071 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000072}
Frederic Riss9412d632015-03-04 02:30:17 +000073
Greg Clayton3462a422016-12-08 01:03:48 +000074void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
David Blaikie85f80d72014-04-23 18:54:00 +000075
76// Emit strings into a string section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000077void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
David Blaikie6741bb02014-09-11 21:12:48 +000078 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +000079}
David Blaikief2999472014-10-23 00:16:05 +000080
Adrian Prantlca7e4702015-02-10 23:18:28 +000081bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie80e5b1e2014-10-24 17:57:34 +000082 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000083 const DILocalVariable *DV = Var->getVariable();
David Blaikie3b5c8402014-10-23 22:04:30 +000084 // Variables with positive arg numbers are parameters.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000085 if (unsigned ArgNum = DV->getArg()) {
David Blaikie3b5c8402014-10-23 22:04:30 +000086 // Keep all parameters in order at the start of the variable list to ensure
87 // function types are correct (no out-of-order parameters)
88 //
89 // This could be improved by only doing it for optimized builds (unoptimized
90 // builds have the right order to begin with), searching from the back (this
91 // would catch the unoptimized case quickly), or doing a binary search
92 // rather than linear search.
93 auto I = Vars.begin();
94 while (I != Vars.end()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000095 unsigned CurNum = (*I)->getVariable()->getArg();
David Blaikie3b5c8402014-10-23 22:04:30 +000096 // A local (non-parameter) variable has been found, insert immediately
97 // before it.
98 if (CurNum == 0)
99 break;
100 // A later indexed parameter has been found, insert immediately before it.
101 if (CurNum > ArgNum)
102 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000103 if (CurNum == ArgNum) {
104 (*I)->addMMIEntry(*Var);
105 return false;
106 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000107 ++I;
108 }
109 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000110 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000111 }
112
113 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000114 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000115}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000116}