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/MC/MCStreamer.h"
15 static std::pair<MCSymbol *, unsigned> &
16 getEntry(AsmPrinter &Asm,
17 StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool,
18 StringRef Prefix, StringRef Str) {
19 std::pair<MCSymbol *, unsigned> &Entry =
20 Pool.GetOrCreateValue(Str).getValue();
22 Entry.second = Pool.size() - 1;
23 Entry.first = Asm.GetTempSymbol(Prefix, Entry.second);
28 MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
29 return getEntry(Asm, Pool, Prefix, Str).first;
32 unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
33 return getEntry(Asm, Pool, Prefix, Str).second;
36 void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
37 const MCSection *OffsetSection) {
41 // Start the dwarf str section.
42 Asm.OutStreamer.SwitchSection(StrSection);
44 // Get all of the string pool entries and put them in an array by their ID so
46 SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
49 for (const auto &E : Pool)
50 Entries[E.getValue().second] = &E;
52 for (const auto &Entry : Entries) {
53 // Emit a label for reference from debug information entries.
54 Asm.OutStreamer.EmitLabel(Entry->getValue().first);
56 // Emit the string itself with a terminating null byte.
57 Asm.OutStreamer.EmitBytes(
58 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
61 // If we've got an offset section go ahead and emit that now as well.
63 Asm.OutStreamer.SwitchSection(OffsetSection);
65 unsigned size = 4; // FIXME: DWARF64 is 8.
66 for (const auto &Entry : Entries) {
67 Asm.OutStreamer.EmitIntValue(offset, size);
68 offset += Entry->getKeyLength() + 1;