LLVM  8.0.0svn
DwarfStringPool.cpp
Go to the documentation of this file.
1 //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DwarfStringPool.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include <cassert>
18 #include <utility>
19 
20 using namespace llvm;
21 
24  : Pool(A), Prefix(Prefix),
25  ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
26 
28 DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
29  auto I = Pool.insert(std::make_pair(Str, EntryTy()));
30  auto &Entry = I.first->second;
31  if (I.second) {
32  Entry.Index = EntryTy::NotIndexed;
33  Entry.Offset = NumBytes;
34  Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
35 
36  NumBytes += Str.size() + 1;
37  assert(NumBytes > Entry.Offset && "Unexpected overflow");
38  }
39  return *I.first;
40 }
41 
43  StringRef Str) {
44  auto &MapEntry = getEntryImpl(Asm, Str);
45  return EntryRef(MapEntry, false);
46 }
47 
49  StringRef Str) {
50  auto &MapEntry = getEntryImpl(Asm, Str);
51  if (!MapEntry.getValue().isIndexed())
52  MapEntry.getValue().Index = NumIndexedStrings++;
53  return EntryRef(MapEntry, true);
54 }
55 
58  MCSymbol *StartSym) {
59  if (getNumIndexedStrings() == 0)
60  return;
61  Asm.OutStreamer->SwitchSection(Section);
62  unsigned EntrySize = 4;
63  // FIXME: DWARF64
64  // We are emitting the header for a contribution to the string offsets
65  // table. The header consists of an entry with the contribution's
66  // size (not including the size of the length field), the DWARF version and
67  // 2 bytes of padding.
68  Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
69  Asm.emitInt16(Asm.getDwarfVersion());
70  Asm.emitInt16(0);
71  // Define the symbol that marks the start of the contribution. It is
72  // referenced by most unit headers via DW_AT_str_offsets_base.
73  // Split units do not use the attribute.
74  if (StartSym)
75  Asm.OutStreamer->EmitLabel(StartSym);
76 }
77 
79  MCSection *OffsetSection, bool UseRelativeOffsets) {
80  if (Pool.empty())
81  return;
82 
83  // Start the dwarf str section.
84  Asm.OutStreamer->SwitchSection(StrSection);
85 
86  // Get all of the string pool entries and sort them by their offset.
88  Entries.reserve(Pool.size());
89 
90  for (const auto &E : Pool)
91  Entries.push_back(&E);
92 
93  llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
94  const StringMapEntry<EntryTy> *B) {
95  return A->getValue().Offset < B->getValue().Offset;
96  });
97 
98  for (const auto &Entry : Entries) {
99  assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
100  "Mismatch between setting and entry");
101 
102  // Emit a label for reference from debug information entries.
103  if (ShouldCreateSymbols)
104  Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
105 
106  // Emit the string itself with a terminating null byte.
107  Asm.OutStreamer->AddComment("string offset=" +
108  Twine(Entry->getValue().Offset));
109  Asm.OutStreamer->EmitBytes(
110  StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
111  }
112 
113  // If we've got an offset section go ahead and emit that now as well.
114  if (OffsetSection) {
115  // Now only take the indexed entries and put them in an array by their ID so
116  // we can emit them in order.
117  Entries.resize(NumIndexedStrings);
118  for (const auto &Entry : Pool) {
119  if (Entry.getValue().isIndexed())
120  Entries[Entry.getValue().Index] = &Entry;
121  }
122 
123  Asm.OutStreamer->SwitchSection(OffsetSection);
124  unsigned size = 4; // FIXME: DWARF64 is 8.
125  for (const auto &Entry : Entries)
126  if (UseRelativeOffsets)
127  Asm.emitDwarfStringOffset(Entry->getValue());
128  else
129  Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
130  }
131 }
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:94
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:126
unsigned getNumIndexedStrings() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
uint16_t getDwarfVersion() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
void reserve(size_type N)
Definition: SmallVector.h:376
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:81
const ValueTy & getValue() const
Definition: StringMap.h:141
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...
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:141
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:79
unsigned size() const
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1110
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:847
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:49
MCSymbol * createTempSymbol(const Twine &Name) const
DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
void emitInt16(int Value) const
Emit a short directive and value.