| 1 | //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DwarfStringPool.h" |
| 10 | #include "llvm/ADT/SmallVector.h" |
| 11 | #include "llvm/ADT/Twine.h" |
| 12 | #include "llvm/CodeGen/AsmPrinter.h" |
| 13 | #include "llvm/MC/MCAsmInfo.h" |
| 14 | #include "llvm/MC/MCStreamer.h" |
| 15 | #include <cassert> |
| 16 | #include <utility> |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, |
| 21 | StringRef Prefix) |
| 22 | : Pool(A), Prefix(Prefix), |
| 23 | ShouldCreateSymbols(Asm.doesDwarfUseRelocationsAcrossSections()) {} |
| 24 | |
| 25 | StringMapEntry<DwarfStringPool::EntryTy> & |
| 26 | DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) { |
| 27 | auto I = Pool.try_emplace(Key: Str); |
| 28 | auto &Entry = I.first->second; |
| 29 | if (I.second) { |
| 30 | Entry.Index = EntryTy::NotIndexed; |
| 31 | Entry.Offset = NumBytes; |
| 32 | Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Name: Prefix) : nullptr; |
| 33 | |
| 34 | NumBytes += Str.size() + 1; |
| 35 | } |
| 36 | return *I.first; |
| 37 | } |
| 38 | |
| 39 | DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm, |
| 40 | StringRef Str) { |
| 41 | auto &MapEntry = getEntryImpl(Asm, Str); |
| 42 | return EntryRef(MapEntry); |
| 43 | } |
| 44 | |
| 45 | DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm, |
| 46 | StringRef Str) { |
| 47 | auto &MapEntry = getEntryImpl(Asm, Str); |
| 48 | if (!MapEntry.getValue().isIndexed()) |
| 49 | MapEntry.getValue().Index = NumIndexedStrings++; |
| 50 | return EntryRef(MapEntry); |
| 51 | } |
| 52 | |
| 53 | void DwarfStringPool::(AsmPrinter &Asm, |
| 54 | MCSection *Section, |
| 55 | MCSymbol *StartSym) { |
| 56 | if (getNumIndexedStrings() == 0) |
| 57 | return; |
| 58 | Asm.OutStreamer->switchSection(Section); |
| 59 | unsigned EntrySize = Asm.getDwarfOffsetByteSize(); |
| 60 | // We are emitting the header for a contribution to the string offsets |
| 61 | // table. The header consists of an entry with the contribution's |
| 62 | // size (not including the size of the length field), the DWARF version and |
| 63 | // 2 bytes of padding. |
| 64 | Asm.emitDwarfUnitLength(Length: getNumIndexedStrings() * EntrySize + 4, |
| 65 | Comment: "Length of String Offsets Set" ); |
| 66 | Asm.emitInt16(Value: Asm.getDwarfVersion()); |
| 67 | Asm.emitInt16(Value: 0); |
| 68 | // Define the symbol that marks the start of the contribution. It is |
| 69 | // referenced by most unit headers via DW_AT_str_offsets_base. |
| 70 | // Split units do not use the attribute. |
| 71 | if (StartSym) |
| 72 | Asm.OutStreamer->emitLabel(Symbol: StartSym); |
| 73 | } |
| 74 | |
| 75 | void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection, |
| 76 | MCSection *OffsetSection, bool UseRelativeOffsets) { |
| 77 | if (Pool.empty()) |
| 78 | return; |
| 79 | |
| 80 | // Start the dwarf str section. |
| 81 | Asm.OutStreamer->switchSection(Section: StrSection); |
| 82 | |
| 83 | // Get all of the string pool entries and sort them by their offset. |
| 84 | SmallVector<const StringMapEntry<EntryTy> *, 64> Entries( |
| 85 | llvm::make_pointer_range(Range&: Pool)); |
| 86 | |
| 87 | llvm::sort(C&: Entries, Comp: [](const StringMapEntry<EntryTy> *A, |
| 88 | const StringMapEntry<EntryTy> *B) { |
| 89 | return A->getValue().Offset < B->getValue().Offset; |
| 90 | }); |
| 91 | |
| 92 | for (const auto &Entry : Entries) { |
| 93 | assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) && |
| 94 | "Mismatch between setting and entry" ); |
| 95 | |
| 96 | // Emit a label for reference from debug information entries. |
| 97 | if (ShouldCreateSymbols) |
| 98 | Asm.OutStreamer->emitLabel(Symbol: Entry->getValue().Symbol); |
| 99 | |
| 100 | // Emit the string itself with a terminating null byte. |
| 101 | Asm.OutStreamer->AddComment(T: "string offset=" + |
| 102 | Twine(Entry->getValue().Offset)); |
| 103 | Asm.OutStreamer->emitBytes( |
| 104 | Data: StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); |
| 105 | } |
| 106 | |
| 107 | // If we've got an offset section go ahead and emit that now as well. |
| 108 | if (OffsetSection) { |
| 109 | // Now only take the indexed entries and put them in an array by their ID so |
| 110 | // we can emit them in order. |
| 111 | Entries.resize(N: NumIndexedStrings); |
| 112 | for (const auto &Entry : Pool) { |
| 113 | if (Entry.getValue().isIndexed()) |
| 114 | Entries[Entry.getValue().Index] = &Entry; |
| 115 | } |
| 116 | |
| 117 | Asm.OutStreamer->switchSection(Section: OffsetSection); |
| 118 | unsigned size = Asm.getDwarfOffsetByteSize(); |
| 119 | for (const auto &Entry : Entries) |
| 120 | if (UseRelativeOffsets) |
| 121 | Asm.emitDwarfStringOffset(S: Entry->getValue()); |
| 122 | else |
| 123 | Asm.OutStreamer->emitIntValue(Value: Entry->getValue().Offset, Size: size); |
| 124 | } |
| 125 | } |
| 126 | |