1 //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
24 : Pool(A), Prefix(Prefix),
25 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
27 DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
29 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
31 auto &Entry = I.first->second;
32 Entry.Index = Pool.size() - 1;
33 Entry.Offset = NumBytes;
34 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
36 NumBytes += Str.size() + 1;
37 assert(NumBytes > Entry.Offset && "Unexpected overflow");
39 return EntryRef(*I.first);
42 void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
47 Asm.OutStreamer->SwitchSection(Section);
48 unsigned EntrySize = 4;
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());
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.
61 Asm.OutStreamer->EmitLabel(StartSym);
64 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
65 MCSection *OffsetSection, bool UseRelativeOffsets) {
69 // Start the dwarf str section.
70 Asm.OutStreamer->SwitchSection(StrSection);
72 // Get all of the string pool entries and put them in an array by their ID so
74 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
76 for (const auto &E : Pool)
77 Entries[E.getValue().Index] = &E;
79 for (const auto &Entry : Entries) {
80 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
81 "Mismatch between setting and entry");
83 // Emit a label for reference from debug information entries.
84 if (ShouldCreateSymbols)
85 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
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));
94 // If we've got an offset section go ahead and emit that now as well.
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());
102 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);