LLVM 24.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"
13#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCStreamer.h"
16#include <cassert>
17
18using namespace llvm;
19
21 StringRef Prefix)
22 : Pool(A), Prefix(Prefix),
23 ShouldCreateSymbols(Asm.doesDwarfUseRelocationsAcrossSections()) {}
24
26DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
27 auto I = Pool.try_emplace(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(Prefix) : nullptr;
33
34 NumBytes += Str.size() + 1;
35 }
36 return *I.first;
37}
38
40 StringRef Str) {
41 auto &MapEntry = getEntryImpl(Asm, Str);
42 return EntryRef(MapEntry);
43}
44
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
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(getNumIndexedStrings() * EntrySize + 4,
65 "Length of String Offsets Set");
66 Asm.emitInt16(Asm.getDwarfVersion());
67 Asm.emitInt16(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(StartSym);
73}
74
76 MCSection *OffsetSection, bool UseRelativeOffsets) {
77 if (Pool.empty())
78 return;
79
80 // Start the dwarf str section.
81 Asm.OutStreamer->switchSection(StrSection);
82
83 // Get all of the string pool entries and sort them by their offset.
86
87 llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
89 return A->getValue().Offset < B->getValue().Offset;
90 });
91
92 SmallString<128> Comment;
93 for (const auto &Entry : Entries) {
94 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
95 "Mismatch between setting and entry");
96
97 // Emit a label for reference from debug information entries.
98 if (ShouldCreateSymbols)
99 Asm.OutStreamer->emitLabel(Entry->getValue().Symbol);
100
101 // Emit a comment with the string offset and the string itself. The string
102 // is escaped: it can contain arbitrary bytes (e.g. non-ASCII identifiers),
103 // and some assemblers, like ptxas, reject those even inside comments.
104 if (Asm.isVerbose()) {
105 Comment.clear();
106 raw_svector_ostream CommentOS(Comment);
107 CommentOS << "string offset=" << Entry->getValue().Offset << " ; ";
108 CommentOS.write_escaped(
109 StringRef(Entry->getKeyData(), Entry->getKeyLength()),
110 /*UseHexEscapes=*/true);
111 Asm.OutStreamer->AddComment(Comment);
112 }
113
114 // Emit the string itself with a terminating null byte.
115 Asm.OutStreamer->emitBytes(
116 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
117 }
118
119 // If we've got an offset section go ahead and emit that now as well.
120 if (OffsetSection) {
121 // Now only take the indexed entries and put them in an array by their ID so
122 // we can emit them in order.
123 Entries.resize(NumIndexedStrings);
124 for (const auto &Entry : Pool) {
125 if (Entry.getValue().isIndexed())
126 Entries[Entry.getValue().Index] = &Entry;
127 }
128
129 Asm.OutStreamer->switchSection(OffsetSection);
130 unsigned size = Asm.getDwarfOffsetByteSize();
131 for (const auto &Entry : Entries)
132 if (UseRelativeOffsets)
133 Asm.emitDwarfStringOffset(Entry->getValue());
134 else
135 Asm.OutStreamer->emitIntValue(Entry->getValue().Offset, size);
136 }
137}
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 SmallString class.
This file defines the SmallVector class.
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:95
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:580
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an SmallVector or SmallString.
This is an optimization pass for GlobalISel generic memory operations.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
static constexpr unsigned NotIndexed