David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 1 | //===-- 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" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 11 | #include "DwarfDebug.h" |
| 12 | #include "DwarfUnit.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
| 14 | #include "llvm/IR/DataLayout.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCStreamer.h" |
| 16 | #include "llvm/Support/LEB128.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 18 | |
| 19 | namespace llvm { |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 20 | DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA) |
| 21 | : Asm(AP), StrPool(DA, *Asm, Pref) {} |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 22 | |
Benjamin Kramer | 5188a2a | 2015-05-28 12:55:43 +0000 | [diff] [blame] | 23 | DwarfFile::~DwarfFile() { |
| 24 | for (DIEAbbrev *Abbrev : Abbreviations) |
| 25 | Abbrev->~DIEAbbrev(); |
| 26 | } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 27 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 28 | // Define a unique number for the abbreviation. |
| 29 | // |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 30 | DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) { |
| 31 | FoldingSetNodeID ID; |
| 32 | DIEAbbrev Abbrev = Die.generateAbbrev(); |
| 33 | Abbrev.Profile(ID); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 34 | |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 35 | void *InsertPos; |
| 36 | if (DIEAbbrev *Existing = |
| 37 | AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) { |
| 38 | Die.setAbbrevNumber(Existing->getNumber()); |
| 39 | return *Existing; |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 40 | } |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 41 | |
| 42 | // Move the abbreviation to the heap and assign a number. |
| 43 | DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev)); |
| 44 | Abbreviations.push_back(New); |
| 45 | New->setNumber(Abbreviations.size()); |
| 46 | Die.setAbbrevNumber(Abbreviations.size()); |
| 47 | |
| 48 | // Store it for lookup. |
| 49 | AbbreviationsSet.InsertNode(New, InsertPos); |
| 50 | return *New; |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) { |
| 54 | CUs.push_back(std::move(U)); |
| 55 | } |
| 56 | |
| 57 | // Emit the various dwarf units to the unit section USection with |
| 58 | // the abbreviations going into ASection. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 59 | void DwarfFile::emitUnits(bool UseOffsets) { |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 60 | for (const auto &TheU : CUs) { |
David Blaikie | adcde36 | 2014-04-25 18:35:57 +0000 | [diff] [blame] | 61 | DIE &Die = TheU->getUnitDie(); |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 62 | MCSection *USection = TheU->getSection(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 63 | Asm->OutStreamer->SwitchSection(USection); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 64 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 65 | TheU->emitHeader(UseOffsets); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 66 | |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 67 | Asm->emitDwarfDIE(Die); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 70 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 71 | // Compute the size and offset for each DIE. |
| 72 | void DwarfFile::computeSizeAndOffsets() { |
| 73 | // Offset from the first CU in the debug info section is 0 initially. |
| 74 | unsigned SecOffset = 0; |
| 75 | |
| 76 | // Iterate over each compile unit and set the size and offsets for each |
| 77 | // DIE within each compile unit. All offsets are CU relative. |
| 78 | for (const auto &TheU : CUs) { |
| 79 | TheU->setDebugInfoOffset(SecOffset); |
| 80 | |
| 81 | // CU-relative offset is reset to 0 here. |
| 82 | unsigned Offset = sizeof(int32_t) + // Length of Unit Info |
| 83 | TheU->getHeaderSize(); // Unit-specific headers |
| 84 | |
| 85 | // EndOffset here is CU-relative, after laying out |
| 86 | // all of the CU DIE. |
David Blaikie | adcde36 | 2014-04-25 18:35:57 +0000 | [diff] [blame] | 87 | unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 88 | SecOffset += EndOffset; |
| 89 | } |
| 90 | } |
| 91 | // 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. |
| 93 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
| 94 | // Record the abbreviation. |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 95 | const DIEAbbrev &Abbrev = assignAbbrevNumber(Die); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 96 | |
| 97 | // Set DIE offset |
| 98 | Die.setOffset(Offset); |
| 99 | |
| 100 | // Start the size with the size of abbreviation code. |
| 101 | Offset += getULEB128Size(Die.getAbbrevNumber()); |
| 102 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 103 | // Size the DIE attribute values. |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 104 | for (const auto &V : Die.values()) |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 105 | // Size attribute value. |
Duncan P. N. Exon Smith | 9dbb501 | 2015-06-24 18:48:11 +0000 | [diff] [blame] | 106 | Offset += V.SizeOf(Asm); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 107 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 108 | // Size the DIE children if any. |
Duncan P. N. Exon Smith | 8d3197f | 2015-05-28 19:56:34 +0000 | [diff] [blame] | 109 | if (Die.hasChildren()) { |
Duncan P. N. Exon Smith | a68b880 | 2015-05-27 23:02:36 +0000 | [diff] [blame] | 110 | (void)Abbrev; |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 111 | assert(Abbrev.hasChildren() && "Children flag not set"); |
| 112 | |
Duncan P. N. Exon Smith | 8d3197f | 2015-05-28 19:56:34 +0000 | [diff] [blame] | 113 | for (auto &Child : Die.children()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 114 | Offset = computeSizeAndOffset(Child, Offset); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 115 | |
| 116 | // End of children marker. |
| 117 | Offset += sizeof(int8_t); |
| 118 | } |
| 119 | |
| 120 | Die.setSize(Offset - Die.getOffset()); |
| 121 | return Offset; |
| 122 | } |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 123 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 124 | void DwarfFile::emitAbbrevs(MCSection *Section) { |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 125 | // Check to see if it is worth the effort. |
| 126 | if (!Abbreviations.empty()) { |
| 127 | // Start the debug abbrev section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 128 | Asm->OutStreamer->SwitchSection(Section); |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 129 | Asm->emitDwarfAbbrevs(Abbreviations); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | // Emit strings into a string section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 134 | void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 135 | StrPool.emit(*Asm, StrSection, OffsetSection); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 136 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 137 | |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 138 | bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 139 | SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS]; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 140 | const DILocalVariable *DV = Var->getVariable(); |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 141 | // Variables with positive arg numbers are parameters. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 142 | if (unsigned ArgNum = DV->getArg()) { |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 143 | // Keep all parameters in order at the start of the variable list to ensure |
| 144 | // function types are correct (no out-of-order parameters) |
| 145 | // |
| 146 | // This could be improved by only doing it for optimized builds (unoptimized |
| 147 | // builds have the right order to begin with), searching from the back (this |
| 148 | // would catch the unoptimized case quickly), or doing a binary search |
| 149 | // rather than linear search. |
| 150 | auto I = Vars.begin(); |
| 151 | while (I != Vars.end()) { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 152 | unsigned CurNum = (*I)->getVariable()->getArg(); |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 153 | // A local (non-parameter) variable has been found, insert immediately |
| 154 | // before it. |
| 155 | if (CurNum == 0) |
| 156 | break; |
| 157 | // A later indexed parameter has been found, insert immediately before it. |
| 158 | if (CurNum > ArgNum) |
| 159 | break; |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 160 | if (CurNum == ArgNum) { |
| 161 | (*I)->addMMIEntry(*Var); |
| 162 | return false; |
| 163 | } |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 164 | ++I; |
| 165 | } |
| 166 | Vars.insert(I, Var); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 167 | return true; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | Vars.push_back(Var); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 171 | return true; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 172 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 173 | } |