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