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