1*0b57cec5SDimitry Andric //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "DwarfStringPool.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
11*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
12*0b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
13*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
14*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
15*0b57cec5SDimitry Andric #include <cassert>
16*0b57cec5SDimitry Andric #include <utility>
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric using namespace llvm;
19*0b57cec5SDimitry Andric 
DwarfStringPool(BumpPtrAllocator & A,AsmPrinter & Asm,StringRef Prefix)20*0b57cec5SDimitry Andric DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
21*0b57cec5SDimitry Andric                                  StringRef Prefix)
22*0b57cec5SDimitry Andric     : Pool(A), Prefix(Prefix),
23*0b57cec5SDimitry Andric       ShouldCreateSymbols(Asm.doesDwarfUseRelocationsAcrossSections()) {}
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric StringMapEntry<DwarfStringPool::EntryTy> &
getEntryImpl(AsmPrinter & Asm,StringRef Str)26*0b57cec5SDimitry Andric DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
27*0b57cec5SDimitry Andric   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
28*0b57cec5SDimitry Andric   auto &Entry = I.first->second;
29*0b57cec5SDimitry Andric   if (I.second) {
30*0b57cec5SDimitry Andric     Entry.Index = EntryTy::NotIndexed;
31*0b57cec5SDimitry Andric     Entry.Offset = NumBytes;
32*0b57cec5SDimitry Andric     Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
33*0b57cec5SDimitry Andric 
34*0b57cec5SDimitry Andric     NumBytes += Str.size() + 1;
35*0b57cec5SDimitry Andric   }
36*0b57cec5SDimitry Andric   return *I.first;
37*0b57cec5SDimitry Andric }
38*0b57cec5SDimitry Andric 
getEntry(AsmPrinter & Asm,StringRef Str)39*0b57cec5SDimitry Andric DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
40*0b57cec5SDimitry Andric                                                     StringRef Str) {
41*0b57cec5SDimitry Andric   auto &MapEntry = getEntryImpl(Asm, Str);
42*0b57cec5SDimitry Andric   return EntryRef(MapEntry);
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric 
getIndexedEntry(AsmPrinter & Asm,StringRef Str)45*0b57cec5SDimitry Andric DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
46*0b57cec5SDimitry Andric                                                            StringRef Str) {
47*0b57cec5SDimitry Andric   auto &MapEntry = getEntryImpl(Asm, Str);
48*0b57cec5SDimitry Andric   if (!MapEntry.getValue().isIndexed())
49*0b57cec5SDimitry Andric     MapEntry.getValue().Index = NumIndexedStrings++;
50*0b57cec5SDimitry Andric   return EntryRef(MapEntry);
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric 
emitStringOffsetsTableHeader(AsmPrinter & Asm,MCSection * Section,MCSymbol * StartSym)53*0b57cec5SDimitry Andric void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
54*0b57cec5SDimitry Andric                                                    MCSection *Section,
55*0b57cec5SDimitry Andric                                                    MCSymbol *StartSym) {
56*0b57cec5SDimitry Andric   if (getNumIndexedStrings() == 0)
57*0b57cec5SDimitry Andric     return;
58*0b57cec5SDimitry Andric   Asm.OutStreamer->switchSection(Section);
59*0b57cec5SDimitry Andric   unsigned EntrySize = Asm.getDwarfOffsetByteSize();
60*0b57cec5SDimitry Andric   // We are emitting the header for a contribution to the string offsets
61*0b57cec5SDimitry Andric   // table. The header consists of an entry with the contribution's
62*0b57cec5SDimitry Andric   // size (not including the size of the length field), the DWARF version and
63*0b57cec5SDimitry Andric   // 2 bytes of padding.
64*0b57cec5SDimitry Andric   Asm.emitDwarfUnitLength(getNumIndexedStrings() * EntrySize + 4,
65*0b57cec5SDimitry Andric                           "Length of String Offsets Set");
66*0b57cec5SDimitry Andric   Asm.emitInt16(Asm.getDwarfVersion());
67*0b57cec5SDimitry Andric   Asm.emitInt16(0);
68*0b57cec5SDimitry Andric   // Define the symbol that marks the start of the contribution. It is
69*0b57cec5SDimitry Andric   // referenced by most unit headers via DW_AT_str_offsets_base.
70*0b57cec5SDimitry Andric   // Split units do not use the attribute.
71*0b57cec5SDimitry Andric   if (StartSym)
72*0b57cec5SDimitry Andric     Asm.OutStreamer->emitLabel(StartSym);
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric 
emit(AsmPrinter & Asm,MCSection * StrSection,MCSection * OffsetSection,bool UseRelativeOffsets)75*0b57cec5SDimitry Andric void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
76*0b57cec5SDimitry Andric                            MCSection *OffsetSection, bool UseRelativeOffsets) {
77*0b57cec5SDimitry Andric   if (Pool.empty())
78*0b57cec5SDimitry Andric     return;
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric   // Start the dwarf str section.
81*0b57cec5SDimitry Andric   Asm.OutStreamer->switchSection(StrSection);
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric   // Get all of the string pool entries and sort them by their offset.
84*0b57cec5SDimitry Andric   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
85*0b57cec5SDimitry Andric   Entries.reserve(Pool.size());
86*0b57cec5SDimitry Andric 
87*0b57cec5SDimitry Andric   for (const auto &E : Pool)
88*0b57cec5SDimitry Andric     Entries.push_back(&E);
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric   llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
91*0b57cec5SDimitry Andric                          const StringMapEntry<EntryTy> *B) {
92*0b57cec5SDimitry Andric     return A->getValue().Offset < B->getValue().Offset;
93*0b57cec5SDimitry Andric   });
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   for (const auto &Entry : Entries) {
96*0b57cec5SDimitry Andric     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
97*0b57cec5SDimitry Andric            "Mismatch between setting and entry");
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric     // Emit a label for reference from debug information entries.
100*0b57cec5SDimitry Andric     if (ShouldCreateSymbols)
101*0b57cec5SDimitry Andric       Asm.OutStreamer->emitLabel(Entry->getValue().Symbol);
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric     // Emit the string itself with a terminating null byte.
104*0b57cec5SDimitry Andric     Asm.OutStreamer->AddComment("string offset=" +
105*0b57cec5SDimitry Andric                                 Twine(Entry->getValue().Offset));
106*0b57cec5SDimitry Andric     Asm.OutStreamer->emitBytes(
107*0b57cec5SDimitry Andric         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
108*0b57cec5SDimitry Andric   }
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric   // If we've got an offset section go ahead and emit that now as well.
111*0b57cec5SDimitry Andric   if (OffsetSection) {
112*0b57cec5SDimitry Andric     // Now only take the indexed entries and put them in an array by their ID so
113*0b57cec5SDimitry Andric     // we can emit them in order.
114*0b57cec5SDimitry Andric     Entries.resize(NumIndexedStrings);
115*0b57cec5SDimitry Andric     for (const auto &Entry : Pool) {
116*0b57cec5SDimitry Andric       if (Entry.getValue().isIndexed())
117*0b57cec5SDimitry Andric         Entries[Entry.getValue().Index] = &Entry;
118*0b57cec5SDimitry Andric     }
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric     Asm.OutStreamer->switchSection(OffsetSection);
121*0b57cec5SDimitry Andric     unsigned size = Asm.getDwarfOffsetByteSize();
122*0b57cec5SDimitry Andric     for (const auto &Entry : Entries)
123*0b57cec5SDimitry Andric       if (UseRelativeOffsets)
124*0b57cec5SDimitry Andric         Asm.emitDwarfStringOffset(Entry->getValue());
125*0b57cec5SDimitry Andric       else
126*0b57cec5SDimitry Andric         Asm.OutStreamer->emitIntValue(Entry->getValue().Offset, size);
127*0b57cec5SDimitry Andric   }
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric