Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework -----------------===// |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 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" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
| 16 | #include "llvm/CodeGen/DIE.h" |
| 17 | #include "llvm/IR/DebugInfoMetadata.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <cstdint> |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 21 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 24 | DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA) |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 25 | : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {} |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 26 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 27 | void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) { |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 28 | CUs.push_back(std::move(U)); |
| 29 | } |
| 30 | |
| 31 | // Emit the various dwarf units to the unit section USection with |
| 32 | // the abbreviations going into ASection. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 33 | void DwarfFile::emitUnits(bool UseOffsets) { |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 34 | for (const auto &TheU : CUs) |
| 35 | emitUnit(TheU.get(), UseOffsets); |
| 36 | } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 37 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 38 | void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) { |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame^] | 39 | if (TheU->getCUNode()->isDebugDirectivesOnly()) |
| 40 | return; |
| 41 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 42 | DIE &Die = TheU->getUnitDie(); |
| 43 | MCSection *USection = TheU->getSection(); |
| 44 | Asm->OutStreamer->SwitchSection(USection); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 45 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 46 | TheU->emitHeader(UseOffsets); |
| 47 | |
| 48 | Asm->emitDwarfDIE(Die); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 49 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 50 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 51 | // Compute the size and offset for each DIE. |
| 52 | void DwarfFile::computeSizeAndOffsets() { |
| 53 | // Offset from the first CU in the debug info section is 0 initially. |
| 54 | unsigned SecOffset = 0; |
| 55 | |
| 56 | // Iterate over each compile unit and set the size and offsets for each |
| 57 | // DIE within each compile unit. All offsets are CU relative. |
| 58 | for (const auto &TheU : CUs) { |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame^] | 59 | if (TheU->getCUNode()->isDebugDirectivesOnly()) |
| 60 | continue; |
| 61 | |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 62 | TheU->setDebugSectionOffset(SecOffset); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 63 | SecOffset += computeSizeAndOffsetsForUnit(TheU.get()); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 66 | |
| 67 | unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) { |
| 68 | // CU-relative offset is reset to 0 here. |
| 69 | unsigned Offset = sizeof(int32_t) + // Length of Unit Info |
| 70 | TheU->getHeaderSize(); // Unit-specific headers |
| 71 | |
| 72 | // The return value here is CU-relative, after laying out |
| 73 | // all of the CU DIE. |
| 74 | return computeSizeAndOffset(TheU->getUnitDie(), Offset); |
| 75 | } |
| 76 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 77 | // Compute the size and offset of a DIE. The offset is relative to start of the |
| 78 | // CU. It returns the offset after laying out the DIE. |
| 79 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 80 | return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 81 | } |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 82 | |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 83 | void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 84 | |
| 85 | // Emit strings into a string section. |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 86 | void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection, |
| 87 | bool UseRelativeOffsets) { |
| 88 | StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 89 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 90 | |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 91 | bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 92 | auto &ScopeVars = ScopeVariables[LS]; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 93 | const DILocalVariable *DV = Var->getVariable(); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 94 | if (unsigned ArgNum = DV->getArg()) { |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 95 | auto Cached = ScopeVars.Args.find(ArgNum); |
| 96 | if (Cached == ScopeVars.Args.end()) |
| 97 | ScopeVars.Args[ArgNum] = Var; |
| 98 | else { |
| 99 | Cached->second->addMMIEntry(*Var); |
| 100 | return false; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 101 | } |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 102 | } else { |
| 103 | ScopeVars.Locals.push_back(Var); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 104 | } |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 105 | return true; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 106 | } |