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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DwarfFile.h" |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 10 | #include "DwarfCompileUnit.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 11 | #include "DwarfDebug.h" |
| 12 | #include "DwarfUnit.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/CodeGen/AsmPrinter.h" |
| 15 | #include "llvm/CodeGen/DIE.h" |
| 16 | #include "llvm/IR/DebugInfoMetadata.h" |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | #include <cstdint> |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 20 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 23 | DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA) |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 24 | : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {} |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 25 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 26 | void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) { |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 27 | CUs.push_back(std::move(U)); |
| 28 | } |
| 29 | |
| 30 | // Emit the various dwarf units to the unit section USection with |
| 31 | // the abbreviations going into ASection. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 32 | void DwarfFile::emitUnits(bool UseOffsets) { |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 33 | for (const auto &TheU : CUs) |
| 34 | emitUnit(TheU.get(), UseOffsets); |
| 35 | } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 36 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 37 | void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) { |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 38 | if (TheU->getCUNode()->isDebugDirectivesOnly()) |
| 39 | return; |
| 40 | |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 41 | MCSection *S = TheU->getSection(); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 42 | |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 43 | if (!S) |
| 44 | return; |
| 45 | |
David Blaikie | 104dcb3 | 2019-02-12 00:00:38 +0000 | [diff] [blame] | 46 | // Skip CUs that ended up not being needed (split CUs that were abandoned |
| 47 | // because they added no information beyond the non-split CU) |
| 48 | if (llvm::empty(TheU->getUnitDie().values())) |
| 49 | return; |
| 50 | |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 51 | Asm->OutStreamer->SwitchSection(S); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 52 | TheU->emitHeader(UseOffsets); |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 53 | Asm->emitDwarfDIE(TheU->getUnitDie()); |
David Blaikie | c4e08fe | 2018-12-18 01:06:09 +0000 | [diff] [blame] | 54 | |
| 55 | if (MCSymbol *EndLabel = TheU->getEndLabel()) |
| 56 | Asm->OutStreamer->EmitLabel(EndLabel); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 57 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 58 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 59 | // Compute the size and offset for each DIE. |
| 60 | void DwarfFile::computeSizeAndOffsets() { |
| 61 | // Offset from the first CU in the debug info section is 0 initially. |
| 62 | unsigned SecOffset = 0; |
| 63 | |
| 64 | // Iterate over each compile unit and set the size and offsets for each |
| 65 | // DIE within each compile unit. All offsets are CU relative. |
| 66 | for (const auto &TheU : CUs) { |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 67 | if (TheU->getCUNode()->isDebugDirectivesOnly()) |
| 68 | continue; |
| 69 | |
David Blaikie | 104dcb3 | 2019-02-12 00:00:38 +0000 | [diff] [blame] | 70 | // Skip CUs that ended up not being needed (split CUs that were abandoned |
| 71 | // because they added no information beyond the non-split CU) |
| 72 | if (llvm::empty(TheU->getUnitDie().values())) |
| 73 | return; |
| 74 | |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 75 | TheU->setDebugSectionOffset(SecOffset); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 76 | SecOffset += computeSizeAndOffsetsForUnit(TheU.get()); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 79 | |
| 80 | unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) { |
| 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 | // The return value here is CU-relative, after laying out |
| 86 | // all of the CU DIE. |
| 87 | return computeSizeAndOffset(TheU->getUnitDie(), Offset); |
| 88 | } |
| 89 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 90 | // Compute the size and offset of a DIE. The offset is relative to start of the |
| 91 | // CU. It returns the offset after laying out the DIE. |
| 92 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 93 | return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 94 | } |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 95 | |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 96 | void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 97 | |
| 98 | // Emit strings into a string section. |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 99 | void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection, |
| 100 | bool UseRelativeOffsets) { |
| 101 | StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 102 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame] | 103 | |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 104 | bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 105 | auto &ScopeVars = ScopeVariables[LS]; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 106 | const DILocalVariable *DV = Var->getVariable(); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 107 | if (unsigned ArgNum = DV->getArg()) { |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 108 | auto Cached = ScopeVars.Args.find(ArgNum); |
| 109 | if (Cached == ScopeVars.Args.end()) |
| 110 | ScopeVars.Args[ArgNum] = Var; |
| 111 | else { |
| 112 | Cached->second->addMMIEntry(*Var); |
| 113 | return false; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 114 | } |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 115 | } else { |
| 116 | ScopeVars.Locals.push_back(Var); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 117 | } |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 118 | return true; |
David Blaikie | 3b5c840 | 2014-10-23 22:04:30 +0000 | [diff] [blame] | 119 | } |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 120 | |
| 121 | void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) { |
| 122 | SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS]; |
| 123 | Labels.push_back(Label); |
| 124 | } |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 125 | |
| 126 | std::pair<uint32_t, RangeSpanList *> |
David Blaikie | c8f7e6c | 2018-11-08 00:35:54 +0000 | [diff] [blame] | 127 | DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) { |
| 128 | CURangeLists.push_back( |
| 129 | RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R))); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 130 | return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back()); |
| 131 | } |