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"
14 #include "llvm/CodeGen/AsmPrinter.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
DwarfStringPool(BumpPtrAllocator & A,AsmPrinter & Asm,StringRef Prefix)22 DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
23 StringRef Prefix)
24 : Pool(A), Prefix(Prefix),
25 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
26
getEntry(AsmPrinter & Asm,StringRef Str)27 DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
28 StringRef Str) {
29 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
30 if (I.second) {
31 auto &Entry = I.first->second;
32 Entry.Index = Pool.size() - 1;
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 EntryRef(*I.first);
40 }
41
emitStringOffsetsTableHeader(AsmPrinter & Asm,MCSection * Section,MCSymbol * StartSym)42 void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
43 MCSection *Section,
44 MCSymbol *StartSym) {
45 if (empty())
46 return;
47 Asm.OutStreamer->SwitchSection(Section);
48 unsigned EntrySize = 4;
49 // FIXME: DWARF64
50 // We are emitting the header for a contribution to the string offsets
51 // table. The header consists of an entry with the contribution's
52 // size (not including the size of the length field), the DWARF version and
53 // 2 bytes of padding.
54 Asm.emitInt32(size() * EntrySize + 4);
55 Asm.emitInt16(Asm.getDwarfVersion());
56 Asm.emitInt16(0);
57 // Define the symbol that marks the start of the contribution. It is
58 // referenced by most unit headers via DW_AT_str_offsets_base.
59 // Split units do not use the attribute.
60 if (StartSym)
61 Asm.OutStreamer->EmitLabel(StartSym);
62 }
63
emit(AsmPrinter & Asm,MCSection * StrSection,MCSection * OffsetSection,bool UseRelativeOffsets)64 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
65 MCSection *OffsetSection, bool UseRelativeOffsets) {
66 if (Pool.empty())
67 return;
68
69 // Start the dwarf str section.
70 Asm.OutStreamer->SwitchSection(StrSection);
71
72 // Get all of the string pool entries and put them in an array by their ID so
73 // we can sort them.
74 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
75
76 for (const auto &E : Pool)
77 Entries[E.getValue().Index] = &E;
78
79 for (const auto &Entry : Entries) {
80 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
81 "Mismatch between setting and entry");
82
83 // Emit a label for reference from debug information entries.
84 if (ShouldCreateSymbols)
85 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
86
87 // Emit the string itself with a terminating null byte.
88 Asm.OutStreamer->AddComment("string offset=" +
89 Twine(Entry->getValue().Offset));
90 Asm.OutStreamer->EmitBytes(
91 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
92 }
93
94 // If we've got an offset section go ahead and emit that now as well.
95 if (OffsetSection) {
96 Asm.OutStreamer->SwitchSection(OffsetSection);
97 unsigned size = 4; // FIXME: DWARF64 is 8.
98 for (const auto &Entry : Entries)
99 if (UseRelativeOffsets)
100 Asm.emitDwarfStringOffset(Entry->getValue());
101 else
102 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
103 }
104 }
105