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" |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 11 | #include "DwarfCompileUnit.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 12 | #include "DwarfDebug.h" |
| 13 | #include "DwarfUnit.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | #include "llvm/IR/DataLayout.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCStreamer.h" |
| 17 | #include "llvm/Support/LEB128.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 19 | |
| 20 | namespace llvm { |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 21 | DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA) |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 22 | : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {} |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 23 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 24 | void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) { |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 25 | 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 Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 30 | void DwarfFile::emitUnits(bool UseOffsets) { |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 31 | for (const auto &TheU : CUs) |
| 32 | emitUnit(TheU.get(), UseOffsets); |
| 33 | } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 34 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 35 | void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) { |
| 36 | DIE &Die = TheU->getUnitDie(); |
| 37 | MCSection *USection = TheU->getSection(); |
| 38 | Asm->OutStreamer->SwitchSection(USection); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 39 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 40 | TheU->emitHeader(UseOffsets); |
| 41 | |
| 42 | Asm->emitDwarfDIE(Die); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 43 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 44 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 45 | // Compute the size and offset for each DIE. |
| 46 | void 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 Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 53 | TheU->setDebugSectionOffset(SecOffset); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 54 | SecOffset += computeSizeAndOffsetsForUnit(TheU.get()); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 57 | |
| 58 | unsigned 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 Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 68 | // 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. |
| 70 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 71 | return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 72 | } |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 73 | |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 74 | void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 75 | |
| 76 | // Emit strings into a string section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 77 | void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 78 | StrPool.emit(*Asm, StrSection, OffsetSection); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 79 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 80 | |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 81 | bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 82 | SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS]; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 83 | const DILocalVariable *DV = Var->getVariable(); |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 84 | // Variables with positive arg numbers are parameters. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 85 | if (unsigned ArgNum = DV->getArg()) { |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 86 | // 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 Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 95 | unsigned CurNum = (*I)->getVariable()->getArg(); |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 96 | // 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 Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 103 | if (CurNum == ArgNum) { |
| 104 | (*I)->addMMIEntry(*Var); |
| 105 | return false; |
| 106 | } |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 107 | ++I; |
| 108 | } |
| 109 | Vars.insert(I, Var); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 110 | return true; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | Vars.push_back(Var); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 114 | return true; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 115 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 116 | } |