LLVM  10.0.0svn
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"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include <cassert>
17 #include <utility>
18 
19 using namespace llvm;
20 
23  : Pool(A), Prefix(Prefix),
24  ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
25 
27 DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
28  auto I = Pool.insert(std::make_pair(Str, EntryTy()));
29  auto &Entry = I.first->second;
30  if (I.second) {
31  Entry.Index = EntryTy::NotIndexed;
32  Entry.Offset = NumBytes;
33  Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
34 
35  NumBytes += Str.size() + 1;
36  assert(NumBytes > Entry.Offset && "Unexpected overflow");
37  }
38  return *I.first;
39 }
40 
42  StringRef Str) {
43  auto &MapEntry = getEntryImpl(Asm, Str);
44  return EntryRef(MapEntry, false);
45 }
46 
48  StringRef Str) {
49  auto &MapEntry = getEntryImpl(Asm, Str);
50  if (!MapEntry.getValue().isIndexed())
51  MapEntry.getValue().Index = NumIndexedStrings++;
52  return EntryRef(MapEntry, true);
53 }
54 
57  MCSymbol *StartSym) {
58  if (getNumIndexedStrings() == 0)
59  return;
60  Asm.OutStreamer->SwitchSection(Section);
61  unsigned EntrySize = 4;
62  // FIXME: DWARF64
63  // We are emitting the header for a contribution to the string offsets
64  // table. The header consists of an entry with the contribution's
65  // size (not including the size of the length field), the DWARF version and
66  // 2 bytes of padding.
67  Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
68  Asm.emitInt16(Asm.getDwarfVersion());
69  Asm.emitInt16(0);
70  // Define the symbol that marks the start of the contribution. It is
71  // referenced by most unit headers via DW_AT_str_offsets_base.
72  // Split units do not use the attribute.
73  if (StartSym)
74  Asm.OutStreamer->EmitLabel(StartSym);
75 }
76 
78  MCSection *OffsetSection, bool UseRelativeOffsets) {
79  if (Pool.empty())
80  return;
81 
82  // Start the dwarf str section.
83  Asm.OutStreamer->SwitchSection(StrSection);
84 
85  // Get all of the string pool entries and sort them by their offset.
87  Entries.reserve(Pool.size());
88 
89  for (const auto &E : Pool)
90  Entries.push_back(&E);
91 
92  llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
93  const StringMapEntry<EntryTy> *B) {
94  return A->getValue().Offset < B->getValue().Offset;
95  });
96 
97  for (const auto &Entry : Entries) {
98  assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
99  "Mismatch between setting and entry");
100 
101  // Emit a label for reference from debug information entries.
102  if (ShouldCreateSymbols)
103  Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
104 
105  // Emit the string itself with a terminating null byte.
106  Asm.OutStreamer->AddComment("string offset=" +
107  Twine(Entry->getValue().Offset));
108  Asm.OutStreamer->EmitBytes(
109  StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
110  }
111 
112  // If we've got an offset section go ahead and emit that now as well.
113  if (OffsetSection) {
114  // Now only take the indexed entries and put them in an array by their ID so
115  // we can emit them in order.
116  Entries.resize(NumIndexedStrings);
117  for (const auto &Entry : Pool) {
118  if (Entry.getValue().isIndexed())
119  Entries[Entry.getValue().Index] = &Entry;
120  }
121 
122  Asm.OutStreamer->SwitchSection(OffsetSection);
123  unsigned size = 4; // FIXME: DWARF64 is 8.
124  for (const auto &Entry : Entries)
125  if (UseRelativeOffsets)
126  Asm.emitDwarfStringOffset(Entry->getValue());
127  else
128  Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
129  }
130 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:125
unsigned getNumIndexedStrings() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
uint16_t getDwarfVersion() const
void reserve(size_type N)
Definition: SmallVector.h:369
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
const ValueTy & getValue() const
Definition: StringMap.h:140
static constexpr unsigned NotIndexed
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...
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
Data for a string pool entry.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
unsigned size() const
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1095
void emitDwarfStringOffset(DwarfStringPoolEntry S) const
Emit the 4-byte offset of a string from the start of its section.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
void emitInt32(int Value) const
Emit a long directive and value.
DwarfStringPoolEntryRef EntryRef
String pool entry reference.
void emitStringOffsetsTableHeader(AsmPrinter &Asm, MCSection *OffsetSection, MCSymbol *StartSym)
EntryRef getEntry(AsmPrinter &Asm, StringRef Str)
Get a reference to an entry in the string pool.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MCSymbol * createTempSymbol(const Twine &Name) const
DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
void emitInt16(int Value) const
Emit a short directive and value.