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 = Pool[Str];
21 Entry.second = Pool.size() - 1;
22 Entry.first = Asm.createTempSymbol(Prefix);
27 MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
28 return getEntry(Asm, Pool, Prefix, Str).first;
31 unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
32 return getEntry(Asm, Pool, Prefix, Str).second;
35 void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
36 const MCSection *OffsetSection) {
40 // Start the dwarf str section.
41 Asm.OutStreamer->SwitchSection(StrSection);
43 // Get all of the string pool entries and put them in an array by their ID so
45 SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
48 for (const auto &E : Pool)
49 Entries[E.getValue().second] = &E;
51 for (const auto &Entry : Entries) {
52 // Emit a label for reference from debug information entries.
53 Asm.OutStreamer->EmitLabel(Entry->getValue().first);
55 // Emit the string itself with a terminating null byte.
56 Asm.OutStreamer->EmitBytes(
57 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
60 // If we've got an offset section go ahead and emit that now as well.
62 Asm.OutStreamer->SwitchSection(OffsetSection);
64 unsigned size = 4; // FIXME: DWARF64 is 8.
65 for (const auto &Entry : Entries) {
66 Asm.OutStreamer->EmitIntValue(offset, size);
67 offset += Entry->getKeyLength() + 1;