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/MC/MCStreamer.h" 12 13 using namespace llvm; 14 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(); 21 if (!Entry.first) { 22 Entry.second = Pool.size() - 1; 23 Entry.first = Asm.GetTempSymbol(Prefix, Entry.second); 24 } 25 return Entry; 26 } 27 28 MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) { 29 return getEntry(Asm, Pool, Prefix, Str).first; 30 } 31 32 unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) { 33 return getEntry(Asm, Pool, Prefix, Str).second; 34 } 35 36 void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection, 37 const MCSection *OffsetSection) { 38 if (Pool.empty()) 39 return; 40 41 // Start the dwarf str section. 42 Asm.OutStreamer.SwitchSection(StrSection); 43 44 // Get all of the string pool entries and put them in an array by their ID so 45 // we can sort them. 46 SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64> 47 Entries(Pool.size()); 48 49 for (const auto &E : Pool) 50 Entries[E.getValue().second] = &E; 51 52 for (const auto &Entry : Entries) { 53 // Emit a label for reference from debug information entries. 54 Asm.OutStreamer.EmitLabel(Entry->getValue().first); 55 56 // Emit the string itself with a terminating null byte. 57 Asm.OutStreamer.EmitBytes( 58 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); 59 } 60 61 // If we've got an offset section go ahead and emit that now as well. 62 if (OffsetSection) { 63 Asm.OutStreamer.SwitchSection(OffsetSection); 64 unsigned offset = 0; 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; 69 } 70 } 71 } 72