16e07bfd0SEugene Zelenko //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===// 2daefdbf3SDavid Blaikie // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6daefdbf3SDavid Blaikie // 7daefdbf3SDavid Blaikie //===----------------------------------------------------------------------===// 8daefdbf3SDavid Blaikie 9daefdbf3SDavid Blaikie #include "DwarfStringPool.h" 106e07bfd0SEugene Zelenko #include "llvm/ADT/SmallVector.h" 116e07bfd0SEugene Zelenko #include "llvm/ADT/Twine.h" 129d50e82fSDuncan P. N. Exon Smith #include "llvm/CodeGen/AsmPrinter.h" 13882a2b5aSDuncan P. N. Exon Smith #include "llvm/MC/MCAsmInfo.h" 14daefdbf3SDavid Blaikie #include "llvm/MC/MCStreamer.h" 156e07bfd0SEugene Zelenko #include <cassert> 166e07bfd0SEugene Zelenko #include <utility> 17daefdbf3SDavid Blaikie 18daefdbf3SDavid Blaikie using namespace llvm; 19daefdbf3SDavid Blaikie 20882a2b5aSDuncan P. N. Exon Smith DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, 21882a2b5aSDuncan P. N. Exon Smith StringRef Prefix) 22882a2b5aSDuncan P. N. Exon Smith : Pool(A), Prefix(Prefix), 23882a2b5aSDuncan P. N. Exon Smith ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {} 24882a2b5aSDuncan P. N. Exon Smith 252f088116SPavel Labath StringMapEntry<DwarfStringPool::EntryTy> & 262f088116SPavel Labath DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) { 2703b7a1cfSDuncan P. N. Exon Smith auto I = Pool.insert(std::make_pair(Str, EntryTy())); 2803b7a1cfSDuncan P. N. Exon Smith auto &Entry = I.first->second; 292f088116SPavel Labath if (I.second) { 302f088116SPavel Labath Entry.Index = EntryTy::NotIndexed; 311a65e4adSDuncan P. N. Exon Smith Entry.Offset = NumBytes; 32882a2b5aSDuncan P. N. Exon Smith Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr; 331a65e4adSDuncan P. N. Exon Smith 341a65e4adSDuncan P. N. Exon Smith NumBytes += Str.size() + 1; 35daefdbf3SDavid Blaikie } 362f088116SPavel Labath return *I.first; 372f088116SPavel Labath } 382f088116SPavel Labath 392f088116SPavel Labath DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm, 402f088116SPavel Labath StringRef Str) { 412f088116SPavel Labath auto &MapEntry = getEntryImpl(Asm, Str); 42*501d5b24SAlexey Lapshin return EntryRef(MapEntry); 432f088116SPavel Labath } 442f088116SPavel Labath 452f088116SPavel Labath DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm, 462f088116SPavel Labath StringRef Str) { 472f088116SPavel Labath auto &MapEntry = getEntryImpl(Asm, Str); 482f088116SPavel Labath if (!MapEntry.getValue().isIndexed()) 492f088116SPavel Labath MapEntry.getValue().Index = NumIndexedStrings++; 50*501d5b24SAlexey Lapshin return EntryRef(MapEntry); 51daefdbf3SDavid Blaikie } 52daefdbf3SDavid Blaikie 537bfa5d65SPavel Labath void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm, 547bfa5d65SPavel Labath MCSection *Section, 557bfa5d65SPavel Labath MCSymbol *StartSym) { 562f088116SPavel Labath if (getNumIndexedStrings() == 0) 577bfa5d65SPavel Labath return; 587bfa5d65SPavel Labath Asm.OutStreamer->SwitchSection(Section); 59383d34c0SIgor Kudrin unsigned EntrySize = Asm.getDwarfOffsetByteSize(); 607bfa5d65SPavel Labath // We are emitting the header for a contribution to the string offsets 617bfa5d65SPavel Labath // table. The header consists of an entry with the contribution's 627bfa5d65SPavel Labath // size (not including the size of the length field), the DWARF version and 637bfa5d65SPavel Labath // 2 bytes of padding. 64383d34c0SIgor Kudrin Asm.emitDwarfUnitLength(getNumIndexedStrings() * EntrySize + 4, 65383d34c0SIgor Kudrin "Length of String Offsets Set"); 667bfa5d65SPavel Labath Asm.emitInt16(Asm.getDwarfVersion()); 677bfa5d65SPavel Labath Asm.emitInt16(0); 687bfa5d65SPavel Labath // Define the symbol that marks the start of the contribution. It is 697bfa5d65SPavel Labath // referenced by most unit headers via DW_AT_str_offsets_base. 707bfa5d65SPavel Labath // Split units do not use the attribute. 717bfa5d65SPavel Labath if (StartSym) 726d2d589bSFangrui Song Asm.OutStreamer->emitLabel(StartSym); 737bfa5d65SPavel Labath } 747bfa5d65SPavel Labath 750709a7bdSRafael Espindola void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection, 76456b555fSWolfgang Pieb MCSection *OffsetSection, bool UseRelativeOffsets) { 77daefdbf3SDavid Blaikie if (Pool.empty()) 78daefdbf3SDavid Blaikie return; 79daefdbf3SDavid Blaikie 80daefdbf3SDavid Blaikie // Start the dwarf str section. 819ff69c8fSLang Hames Asm.OutStreamer->SwitchSection(StrSection); 82daefdbf3SDavid Blaikie 832f088116SPavel Labath // Get all of the string pool entries and sort them by their offset. 842f088116SPavel Labath SmallVector<const StringMapEntry<EntryTy> *, 64> Entries; 852f088116SPavel Labath Entries.reserve(Pool.size()); 86daefdbf3SDavid Blaikie 87daefdbf3SDavid Blaikie for (const auto &E : Pool) 882f088116SPavel Labath Entries.push_back(&E); 892f088116SPavel Labath 903507c6e8SFangrui Song llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A, 913507c6e8SFangrui Song const StringMapEntry<EntryTy> *B) { 922f088116SPavel Labath return A->getValue().Offset < B->getValue().Offset; 932f088116SPavel Labath }); 94daefdbf3SDavid Blaikie 95daefdbf3SDavid Blaikie for (const auto &Entry : Entries) { 96882a2b5aSDuncan P. N. Exon Smith assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) && 97882a2b5aSDuncan P. N. Exon Smith "Mismatch between setting and entry"); 98882a2b5aSDuncan P. N. Exon Smith 99daefdbf3SDavid Blaikie // Emit a label for reference from debug information entries. 100882a2b5aSDuncan P. N. Exon Smith if (ShouldCreateSymbols) 1016d2d589bSFangrui Song Asm.OutStreamer->emitLabel(Entry->getValue().Symbol); 102daefdbf3SDavid Blaikie 103daefdbf3SDavid Blaikie // Emit the string itself with a terminating null byte. 1041a65e4adSDuncan P. N. Exon Smith Asm.OutStreamer->AddComment("string offset=" + 1051a65e4adSDuncan P. N. Exon Smith Twine(Entry->getValue().Offset)); 106a55daa14SFangrui Song Asm.OutStreamer->emitBytes( 107daefdbf3SDavid Blaikie StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); 108daefdbf3SDavid Blaikie } 109daefdbf3SDavid Blaikie 110daefdbf3SDavid Blaikie // If we've got an offset section go ahead and emit that now as well. 111daefdbf3SDavid Blaikie if (OffsetSection) { 1122f088116SPavel Labath // Now only take the indexed entries and put them in an array by their ID so 1132f088116SPavel Labath // we can emit them in order. 1142f088116SPavel Labath Entries.resize(NumIndexedStrings); 1152f088116SPavel Labath for (const auto &Entry : Pool) { 1162f088116SPavel Labath if (Entry.getValue().isIndexed()) 1172f088116SPavel Labath Entries[Entry.getValue().Index] = &Entry; 1182f088116SPavel Labath } 1192f088116SPavel Labath 1209ff69c8fSLang Hames Asm.OutStreamer->SwitchSection(OffsetSection); 121924dc580SIgor Kudrin unsigned size = Asm.getDwarfOffsetByteSize(); 1221a65e4adSDuncan P. N. Exon Smith for (const auto &Entry : Entries) 123456b555fSWolfgang Pieb if (UseRelativeOffsets) 124456b555fSWolfgang Pieb Asm.emitDwarfStringOffset(Entry->getValue()); 125456b555fSWolfgang Pieb else 12677497103SFangrui Song Asm.OutStreamer->emitIntValue(Entry->getValue().Offset, size); 127daefdbf3SDavid Blaikie } 128daefdbf3SDavid Blaikie } 129