16e07bfd0SEugene Zelenko //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===// 2daefdbf3SDavid Blaikie // 3daefdbf3SDavid Blaikie // The LLVM Compiler Infrastructure 4daefdbf3SDavid Blaikie // 5daefdbf3SDavid Blaikie // This file is distributed under the University of Illinois Open Source 6daefdbf3SDavid Blaikie // License. See LICENSE.TXT for details. 7daefdbf3SDavid Blaikie // 8daefdbf3SDavid Blaikie //===----------------------------------------------------------------------===// 9daefdbf3SDavid Blaikie 10daefdbf3SDavid Blaikie #include "DwarfStringPool.h" 116e07bfd0SEugene Zelenko #include "llvm/ADT/SmallVector.h" 126e07bfd0SEugene Zelenko #include "llvm/ADT/StringRef.h" 136e07bfd0SEugene Zelenko #include "llvm/ADT/Twine.h" 149d50e82fSDuncan P. N. Exon Smith #include "llvm/CodeGen/AsmPrinter.h" 15882a2b5aSDuncan P. N. Exon Smith #include "llvm/MC/MCAsmInfo.h" 16daefdbf3SDavid Blaikie #include "llvm/MC/MCStreamer.h" 176e07bfd0SEugene Zelenko #include <cassert> 186e07bfd0SEugene Zelenko #include <utility> 19daefdbf3SDavid Blaikie 20daefdbf3SDavid Blaikie using namespace llvm; 21daefdbf3SDavid Blaikie 22882a2b5aSDuncan P. N. Exon Smith DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, 23882a2b5aSDuncan P. N. Exon Smith StringRef Prefix) 24882a2b5aSDuncan P. N. Exon Smith : Pool(A), Prefix(Prefix), 25882a2b5aSDuncan P. N. Exon Smith ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {} 26882a2b5aSDuncan P. N. Exon Smith 27*2f088116SPavel Labath StringMapEntry<DwarfStringPool::EntryTy> & 28*2f088116SPavel Labath DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) { 2903b7a1cfSDuncan P. N. Exon Smith auto I = Pool.insert(std::make_pair(Str, EntryTy())); 3003b7a1cfSDuncan P. N. Exon Smith auto &Entry = I.first->second; 31*2f088116SPavel Labath if (I.second) { 32*2f088116SPavel Labath Entry.Index = EntryTy::NotIndexed; 331a65e4adSDuncan P. N. Exon Smith Entry.Offset = NumBytes; 34882a2b5aSDuncan P. N. Exon Smith Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr; 351a65e4adSDuncan P. N. Exon Smith 361a65e4adSDuncan P. N. Exon Smith NumBytes += Str.size() + 1; 371a65e4adSDuncan P. N. Exon Smith assert(NumBytes > Entry.Offset && "Unexpected overflow"); 38daefdbf3SDavid Blaikie } 39*2f088116SPavel Labath return *I.first; 40*2f088116SPavel Labath } 41*2f088116SPavel Labath 42*2f088116SPavel Labath DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm, 43*2f088116SPavel Labath StringRef Str) { 44*2f088116SPavel Labath auto &MapEntry = getEntryImpl(Asm, Str); 45*2f088116SPavel Labath return EntryRef(MapEntry, false); 46*2f088116SPavel Labath } 47*2f088116SPavel Labath 48*2f088116SPavel Labath DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm, 49*2f088116SPavel Labath StringRef Str) { 50*2f088116SPavel Labath auto &MapEntry = getEntryImpl(Asm, Str); 51*2f088116SPavel Labath if (!MapEntry.getValue().isIndexed()) 52*2f088116SPavel Labath MapEntry.getValue().Index = NumIndexedStrings++; 53*2f088116SPavel Labath return EntryRef(MapEntry, true); 54daefdbf3SDavid Blaikie } 55daefdbf3SDavid Blaikie 567bfa5d65SPavel Labath void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm, 577bfa5d65SPavel Labath MCSection *Section, 587bfa5d65SPavel Labath MCSymbol *StartSym) { 59*2f088116SPavel Labath if (getNumIndexedStrings() == 0) 607bfa5d65SPavel Labath return; 617bfa5d65SPavel Labath Asm.OutStreamer->SwitchSection(Section); 627bfa5d65SPavel Labath unsigned EntrySize = 4; 637bfa5d65SPavel Labath // FIXME: DWARF64 647bfa5d65SPavel Labath // We are emitting the header for a contribution to the string offsets 657bfa5d65SPavel Labath // table. The header consists of an entry with the contribution's 667bfa5d65SPavel Labath // size (not including the size of the length field), the DWARF version and 677bfa5d65SPavel Labath // 2 bytes of padding. 68*2f088116SPavel Labath Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4); 697bfa5d65SPavel Labath Asm.emitInt16(Asm.getDwarfVersion()); 707bfa5d65SPavel Labath Asm.emitInt16(0); 717bfa5d65SPavel Labath // Define the symbol that marks the start of the contribution. It is 727bfa5d65SPavel Labath // referenced by most unit headers via DW_AT_str_offsets_base. 737bfa5d65SPavel Labath // Split units do not use the attribute. 747bfa5d65SPavel Labath if (StartSym) 757bfa5d65SPavel Labath Asm.OutStreamer->EmitLabel(StartSym); 767bfa5d65SPavel Labath } 777bfa5d65SPavel Labath 780709a7bdSRafael Espindola void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection, 79456b555fSWolfgang Pieb MCSection *OffsetSection, bool UseRelativeOffsets) { 80daefdbf3SDavid Blaikie if (Pool.empty()) 81daefdbf3SDavid Blaikie return; 82daefdbf3SDavid Blaikie 83daefdbf3SDavid Blaikie // Start the dwarf str section. 849ff69c8fSLang Hames Asm.OutStreamer->SwitchSection(StrSection); 85daefdbf3SDavid Blaikie 86*2f088116SPavel Labath // Get all of the string pool entries and sort them by their offset. 87*2f088116SPavel Labath SmallVector<const StringMapEntry<EntryTy> *, 64> Entries; 88*2f088116SPavel Labath Entries.reserve(Pool.size()); 89daefdbf3SDavid Blaikie 90daefdbf3SDavid Blaikie for (const auto &E : Pool) 91*2f088116SPavel Labath Entries.push_back(&E); 92*2f088116SPavel Labath 93*2f088116SPavel Labath llvm::sort( 94*2f088116SPavel Labath Entries.begin(), Entries.end(), 95*2f088116SPavel Labath [](const StringMapEntry<EntryTy> *A, const StringMapEntry<EntryTy> *B) { 96*2f088116SPavel Labath return A->getValue().Offset < B->getValue().Offset; 97*2f088116SPavel Labath }); 98daefdbf3SDavid Blaikie 99daefdbf3SDavid Blaikie for (const auto &Entry : Entries) { 100882a2b5aSDuncan P. N. Exon Smith assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) && 101882a2b5aSDuncan P. N. Exon Smith "Mismatch between setting and entry"); 102882a2b5aSDuncan P. N. Exon Smith 103daefdbf3SDavid Blaikie // Emit a label for reference from debug information entries. 104882a2b5aSDuncan P. N. Exon Smith if (ShouldCreateSymbols) 1051a65e4adSDuncan P. N. Exon Smith Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol); 106daefdbf3SDavid Blaikie 107daefdbf3SDavid Blaikie // Emit the string itself with a terminating null byte. 1081a65e4adSDuncan P. N. Exon Smith Asm.OutStreamer->AddComment("string offset=" + 1091a65e4adSDuncan P. N. Exon Smith Twine(Entry->getValue().Offset)); 1109ff69c8fSLang Hames Asm.OutStreamer->EmitBytes( 111daefdbf3SDavid Blaikie StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); 112daefdbf3SDavid Blaikie } 113daefdbf3SDavid Blaikie 114daefdbf3SDavid Blaikie // If we've got an offset section go ahead and emit that now as well. 115daefdbf3SDavid Blaikie if (OffsetSection) { 116*2f088116SPavel Labath // Now only take the indexed entries and put them in an array by their ID so 117*2f088116SPavel Labath // we can emit them in order. 118*2f088116SPavel Labath Entries.resize(NumIndexedStrings); 119*2f088116SPavel Labath for (const auto &Entry : Pool) { 120*2f088116SPavel Labath if (Entry.getValue().isIndexed()) 121*2f088116SPavel Labath Entries[Entry.getValue().Index] = &Entry; 122*2f088116SPavel Labath } 123*2f088116SPavel Labath 1249ff69c8fSLang Hames Asm.OutStreamer->SwitchSection(OffsetSection); 125daefdbf3SDavid Blaikie unsigned size = 4; // FIXME: DWARF64 is 8. 1261a65e4adSDuncan P. N. Exon Smith for (const auto &Entry : Entries) 127456b555fSWolfgang Pieb if (UseRelativeOffsets) 128456b555fSWolfgang Pieb Asm.emitDwarfStringOffset(Entry->getValue()); 129456b555fSWolfgang Pieb else 1301a65e4adSDuncan P. N. Exon Smith Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size); 131daefdbf3SDavid Blaikie } 132daefdbf3SDavid Blaikie } 133