LLVM 22.0.0git
DwarfStringPool.cpp
Go to the documentation of this file.
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"
11#include "llvm/ADT/Twine.h"
13#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCStreamer.h"
15#include <cassert>
16
17using namespace llvm;
18
20 StringRef Prefix)
21 : Pool(A), Prefix(Prefix),
22 ShouldCreateSymbols(Asm.doesDwarfUseRelocationsAcrossSections()) {}
23
25DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
26 auto I = Pool.try_emplace(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(Prefix) : nullptr;
32
33 NumBytes += Str.size() + 1;
34 }
35 return *I.first;
36}
37
39 StringRef Str) {
40 auto &MapEntry = getEntryImpl(Asm, Str);
41 return EntryRef(MapEntry);
42}
43
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
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(getNumIndexedStrings() * EntrySize + 4,
64 "Length of String Offsets Set");
65 Asm.emitInt16(Asm.getDwarfVersion());
66 Asm.emitInt16(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(StartSym);
72}
73
75 MCSection *OffsetSection, bool UseRelativeOffsets) {
76 if (Pool.empty())
77 return;
78
79 // Start the dwarf str section.
80 Asm.OutStreamer->switchSection(StrSection);
81
82 // Get all of the string pool entries and sort them by their offset.
85
86 llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
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(Entry->getValue().Symbol);
98
99 // Emit the string itself with a terminating null byte.
100 Asm.OutStreamer->AddComment("string offset=" +
101 Twine(Entry->getValue().Offset));
102 Asm.OutStreamer->emitBytes(
103 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
104 }
105
106 // If we've got an offset section go ahead and emit that now as well.
107 if (OffsetSection) {
108 // Now only take the indexed entries and put them in an array by their ID so
109 // we can emit them in order.
110 Entries.resize(NumIndexedStrings);
111 for (const auto &Entry : Pool) {
112 if (Entry.getValue().isIndexed())
113 Entries[Entry.getValue().Index] = &Entry;
114 }
115
116 Asm.OutStreamer->switchSection(OffsetSection);
117 unsigned size = Asm.getDwarfOffsetByteSize();
118 for (const auto &Entry : Entries)
119 if (UseRelativeOffsets)
120 Asm.emitDwarfStringOffset(Entry->getValue());
121 else
122 Asm.OutStreamer->emitIntValue(Entry->getValue().Offset, size);
123 }
124}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallVector class.
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:96
LLVM_ABI_FOR_TEST EntryRef getEntry(AsmPrinter &Asm, StringRef Str)
Get a reference to an entry in the string pool.
LLVM_ABI_FOR_TEST EntryRef getIndexedEntry(AsmPrinter &Asm, StringRef Str)
Same as getEntry, except that you can use EntryRef::getIndex to obtain a unique ID of this entry (e....
DwarfStringPoolEntryRef EntryRef
unsigned getNumIndexedStrings() const
LLVM_ABI_FOR_TEST void emitStringOffsetsTableHeader(AsmPrinter &Asm, MCSection *OffsetSection, MCSymbol *StartSym)
unsigned size() const
LLVM_ABI_FOR_TEST DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
LLVM_ABI_FOR_TEST void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:517
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
void resize(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This is an optimization pass for GlobalISel generic memory operations.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:363
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
static constexpr unsigned NotIndexed