Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===// |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +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 | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DwarfStringPool.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SmallVector.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/ADT/Twine.h" |
Duncan P. N. Exon Smith | 9d50e82 | 2015-05-24 16:54:59 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/AsmPrinter.h" |
Duncan P. N. Exon Smith | 882a2b5 | 2015-05-24 16:58:59 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmInfo.h" |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 16 | #include <cassert> |
| 17 | #include <utility> |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
Duncan P. N. Exon Smith | 882a2b5 | 2015-05-24 16:58:59 +0000 | [diff] [blame] | 21 | DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, |
| 22 | StringRef Prefix) |
| 23 | : Pool(A), Prefix(Prefix), |
| 24 | ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {} |
| 25 | |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 26 | StringMapEntry<DwarfStringPool::EntryTy> & |
| 27 | DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) { |
Duncan P. N. Exon Smith | 03b7a1c | 2015-05-24 16:33:33 +0000 | [diff] [blame] | 28 | auto I = Pool.insert(std::make_pair(Str, EntryTy())); |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 29 | auto &Entry = I.first->second; |
Duncan P. N. Exon Smith | 03b7a1c | 2015-05-24 16:33:33 +0000 | [diff] [blame] | 30 | if (I.second) { |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 31 | Entry.Index = EntryTy::NotIndexed; |
Duncan P. N. Exon Smith | 1a65e4a | 2015-05-24 16:14:59 +0000 | [diff] [blame] | 32 | Entry.Offset = NumBytes; |
Duncan P. N. Exon Smith | 882a2b5 | 2015-05-24 16:58:59 +0000 | [diff] [blame] | 33 | Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr; |
Duncan P. N. Exon Smith | 1a65e4a | 2015-05-24 16:14:59 +0000 | [diff] [blame] | 34 | |
| 35 | NumBytes += Str.size() + 1; |
| 36 | assert(NumBytes > Entry.Offset && "Unexpected overflow"); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 37 | } |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 38 | return *I.first; |
| 39 | } |
| 40 | |
| 41 | DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm, |
| 42 | StringRef Str) { |
| 43 | auto &MapEntry = getEntryImpl(Asm, Str); |
| 44 | return EntryRef(MapEntry, false); |
| 45 | } |
| 46 | |
| 47 | DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm, |
| 48 | StringRef Str) { |
| 49 | auto &MapEntry = getEntryImpl(Asm, Str); |
| 50 | if (!MapEntry.getValue().isIndexed()) |
| 51 | MapEntry.getValue().Index = NumIndexedStrings++; |
| 52 | return EntryRef(MapEntry, true); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Pavel Labath | 7bfa5d6 | 2018-07-26 14:36:07 +0000 | [diff] [blame] | 55 | void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm, |
| 56 | MCSection *Section, |
| 57 | MCSymbol *StartSym) { |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 58 | if (getNumIndexedStrings() == 0) |
Pavel Labath | 7bfa5d6 | 2018-07-26 14:36:07 +0000 | [diff] [blame] | 59 | return; |
| 60 | Asm.OutStreamer->SwitchSection(Section); |
| 61 | unsigned EntrySize = 4; |
| 62 | // FIXME: DWARF64 |
| 63 | // We are emitting the header for a contribution to the string offsets |
| 64 | // table. The header consists of an entry with the contribution's |
| 65 | // size (not including the size of the length field), the DWARF version and |
| 66 | // 2 bytes of padding. |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 67 | Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4); |
Pavel Labath | 7bfa5d6 | 2018-07-26 14:36:07 +0000 | [diff] [blame] | 68 | Asm.emitInt16(Asm.getDwarfVersion()); |
| 69 | Asm.emitInt16(0); |
| 70 | // Define the symbol that marks the start of the contribution. It is |
| 71 | // referenced by most unit headers via DW_AT_str_offsets_base. |
| 72 | // Split units do not use the attribute. |
| 73 | if (StartSym) |
| 74 | Asm.OutStreamer->EmitLabel(StartSym); |
| 75 | } |
| 76 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 77 | void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection, |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 78 | MCSection *OffsetSection, bool UseRelativeOffsets) { |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 79 | if (Pool.empty()) |
| 80 | return; |
| 81 | |
| 82 | // Start the dwarf str section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 83 | Asm.OutStreamer->SwitchSection(StrSection); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 84 | |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 85 | // Get all of the string pool entries and sort them by their offset. |
| 86 | SmallVector<const StringMapEntry<EntryTy> *, 64> Entries; |
| 87 | Entries.reserve(Pool.size()); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 88 | |
| 89 | for (const auto &E : Pool) |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 90 | Entries.push_back(&E); |
| 91 | |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 92 | llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A, |
| 93 | const StringMapEntry<EntryTy> *B) { |
| 94 | return A->getValue().Offset < B->getValue().Offset; |
| 95 | }); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 96 | |
| 97 | for (const auto &Entry : Entries) { |
Duncan P. N. Exon Smith | 882a2b5 | 2015-05-24 16:58:59 +0000 | [diff] [blame] | 98 | assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) && |
| 99 | "Mismatch between setting and entry"); |
| 100 | |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 101 | // Emit a label for reference from debug information entries. |
Duncan P. N. Exon Smith | 882a2b5 | 2015-05-24 16:58:59 +0000 | [diff] [blame] | 102 | if (ShouldCreateSymbols) |
| 103 | Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 104 | |
| 105 | // Emit the string itself with a terminating null byte. |
Duncan P. N. Exon Smith | 1a65e4a | 2015-05-24 16:14:59 +0000 | [diff] [blame] | 106 | Asm.OutStreamer->AddComment("string offset=" + |
| 107 | Twine(Entry->getValue().Offset)); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 108 | Asm.OutStreamer->EmitBytes( |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 109 | StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); |
| 110 | } |
| 111 | |
| 112 | // If we've got an offset section go ahead and emit that now as well. |
| 113 | if (OffsetSection) { |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 114 | // Now only take the indexed entries and put them in an array by their ID so |
| 115 | // we can emit them in order. |
| 116 | Entries.resize(NumIndexedStrings); |
| 117 | for (const auto &Entry : Pool) { |
| 118 | if (Entry.getValue().isIndexed()) |
| 119 | Entries[Entry.getValue().Index] = &Entry; |
| 120 | } |
| 121 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 122 | Asm.OutStreamer->SwitchSection(OffsetSection); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 123 | unsigned size = 4; // FIXME: DWARF64 is 8. |
Duncan P. N. Exon Smith | 1a65e4a | 2015-05-24 16:14:59 +0000 | [diff] [blame] | 124 | for (const auto &Entry : Entries) |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 125 | if (UseRelativeOffsets) |
| 126 | Asm.emitDwarfStringOffset(Entry->getValue()); |
| 127 | else |
| 128 | Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 129 | } |
| 130 | } |