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/CodeGen/AsmPrinter.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCStreamer.h"
17 DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
19 : Pool(A), Prefix(Prefix),
20 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
22 DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
24 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
26 auto &Entry = I.first->second;
27 Entry.Index = Pool.size() - 1;
28 Entry.Offset = NumBytes;
29 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
31 NumBytes += Str.size() + 1;
32 assert(NumBytes > Entry.Offset && "Unexpected overflow");
34 return EntryRef(*I.first);
37 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
38 MCSection *OffsetSection) {
42 // Start the dwarf str section.
43 Asm.OutStreamer->SwitchSection(StrSection);
45 // Get all of the string pool entries and put them in an array by their ID so
47 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
49 for (const auto &E : Pool)
50 Entries[E.getValue().Index] = &E;
52 for (const auto &Entry : Entries) {
53 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
54 "Mismatch between setting and entry");
56 // Emit a label for reference from debug information entries.
57 if (ShouldCreateSymbols)
58 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
60 // Emit the string itself with a terminating null byte.
61 Asm.OutStreamer->AddComment("string offset=" +
62 Twine(Entry->getValue().Offset));
63 Asm.OutStreamer->EmitBytes(
64 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
67 // If we've got an offset section go ahead and emit that now as well.
69 Asm.OutStreamer->SwitchSection(OffsetSection);
70 unsigned size = 4; // FIXME: DWARF64 is 8.
71 for (const auto &Entry : Entries)
72 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);