12cab237bSDimitry Andric //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
291bc56edSDimitry Andric //
391bc56edSDimitry Andric //                     The LLVM Compiler Infrastructure
491bc56edSDimitry Andric //
591bc56edSDimitry Andric // This file is distributed under the University of Illinois Open Source
691bc56edSDimitry Andric // License. See LICENSE.TXT for details.
791bc56edSDimitry Andric //
891bc56edSDimitry Andric //===----------------------------------------------------------------------===//
991bc56edSDimitry Andric 
1091bc56edSDimitry Andric #include "DwarfStringPool.h"
112cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
122cab237bSDimitry Andric #include "llvm/ADT/StringRef.h"
132cab237bSDimitry Andric #include "llvm/ADT/Twine.h"
14ff0cc061SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
15ff0cc061SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
1691bc56edSDimitry Andric #include "llvm/MC/MCStreamer.h"
172cab237bSDimitry Andric #include <cassert>
182cab237bSDimitry Andric #include <utility>
1991bc56edSDimitry Andric 
2091bc56edSDimitry Andric using namespace llvm;
2191bc56edSDimitry Andric 
DwarfStringPool(BumpPtrAllocator & A,AsmPrinter & Asm,StringRef Prefix)22ff0cc061SDimitry Andric DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
23ff0cc061SDimitry Andric                                  StringRef Prefix)
24ff0cc061SDimitry Andric     : Pool(A), Prefix(Prefix),
25ff0cc061SDimitry Andric       ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
26ff0cc061SDimitry Andric 
27*b5893f02SDimitry Andric StringMapEntry<DwarfStringPool::EntryTy> &
getEntryImpl(AsmPrinter & Asm,StringRef Str)28*b5893f02SDimitry Andric DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
29ff0cc061SDimitry Andric   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
30ff0cc061SDimitry Andric   auto &Entry = I.first->second;
31*b5893f02SDimitry Andric   if (I.second) {
32*b5893f02SDimitry Andric     Entry.Index = EntryTy::NotIndexed;
33ff0cc061SDimitry Andric     Entry.Offset = NumBytes;
34ff0cc061SDimitry Andric     Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
35ff0cc061SDimitry Andric 
36ff0cc061SDimitry Andric     NumBytes += Str.size() + 1;
37ff0cc061SDimitry Andric     assert(NumBytes > Entry.Offset && "Unexpected overflow");
3891bc56edSDimitry Andric   }
39*b5893f02SDimitry Andric   return *I.first;
40*b5893f02SDimitry Andric }
41*b5893f02SDimitry Andric 
getEntry(AsmPrinter & Asm,StringRef Str)42*b5893f02SDimitry Andric DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
43*b5893f02SDimitry Andric                                                     StringRef Str) {
44*b5893f02SDimitry Andric   auto &MapEntry = getEntryImpl(Asm, Str);
45*b5893f02SDimitry Andric   return EntryRef(MapEntry, false);
46*b5893f02SDimitry Andric }
47*b5893f02SDimitry Andric 
getIndexedEntry(AsmPrinter & Asm,StringRef Str)48*b5893f02SDimitry Andric DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
49*b5893f02SDimitry Andric                                                            StringRef Str) {
50*b5893f02SDimitry Andric   auto &MapEntry = getEntryImpl(Asm, Str);
51*b5893f02SDimitry Andric   if (!MapEntry.getValue().isIndexed())
52*b5893f02SDimitry Andric     MapEntry.getValue().Index = NumIndexedStrings++;
53*b5893f02SDimitry Andric   return EntryRef(MapEntry, true);
5491bc56edSDimitry Andric }
5591bc56edSDimitry Andric 
emitStringOffsetsTableHeader(AsmPrinter & Asm,MCSection * Section,MCSymbol * StartSym)564ba319b5SDimitry Andric void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
574ba319b5SDimitry Andric                                                    MCSection *Section,
584ba319b5SDimitry Andric                                                    MCSymbol *StartSym) {
59*b5893f02SDimitry Andric   if (getNumIndexedStrings() == 0)
604ba319b5SDimitry Andric     return;
614ba319b5SDimitry Andric   Asm.OutStreamer->SwitchSection(Section);
624ba319b5SDimitry Andric   unsigned EntrySize = 4;
634ba319b5SDimitry Andric   // FIXME: DWARF64
644ba319b5SDimitry Andric   // We are emitting the header for a contribution to the string offsets
654ba319b5SDimitry Andric   // table. The header consists of an entry with the contribution's
664ba319b5SDimitry Andric   // size (not including the size of the length field), the DWARF version and
674ba319b5SDimitry Andric   // 2 bytes of padding.
68*b5893f02SDimitry Andric   Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
694ba319b5SDimitry Andric   Asm.emitInt16(Asm.getDwarfVersion());
704ba319b5SDimitry Andric   Asm.emitInt16(0);
714ba319b5SDimitry Andric   // Define the symbol that marks the start of the contribution. It is
724ba319b5SDimitry Andric   // referenced by most unit headers via DW_AT_str_offsets_base.
734ba319b5SDimitry Andric   // Split units do not use the attribute.
744ba319b5SDimitry Andric   if (StartSym)
754ba319b5SDimitry Andric     Asm.OutStreamer->EmitLabel(StartSym);
764ba319b5SDimitry Andric }
774ba319b5SDimitry Andric 
emit(AsmPrinter & Asm,MCSection * StrSection,MCSection * OffsetSection,bool UseRelativeOffsets)78ff0cc061SDimitry Andric void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
794ba319b5SDimitry Andric                            MCSection *OffsetSection, bool UseRelativeOffsets) {
8091bc56edSDimitry Andric   if (Pool.empty())
8191bc56edSDimitry Andric     return;
8291bc56edSDimitry Andric 
8391bc56edSDimitry Andric   // Start the dwarf str section.
84ff0cc061SDimitry Andric   Asm.OutStreamer->SwitchSection(StrSection);
8591bc56edSDimitry Andric 
86*b5893f02SDimitry Andric   // Get all of the string pool entries and sort them by their offset.
87*b5893f02SDimitry Andric   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
88*b5893f02SDimitry Andric   Entries.reserve(Pool.size());
8991bc56edSDimitry Andric 
9091bc56edSDimitry Andric   for (const auto &E : Pool)
91*b5893f02SDimitry Andric     Entries.push_back(&E);
92*b5893f02SDimitry Andric 
93*b5893f02SDimitry Andric   llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
94*b5893f02SDimitry Andric                          const StringMapEntry<EntryTy> *B) {
95*b5893f02SDimitry Andric     return A->getValue().Offset < B->getValue().Offset;
96*b5893f02SDimitry Andric   });
9791bc56edSDimitry Andric 
9891bc56edSDimitry Andric   for (const auto &Entry : Entries) {
99ff0cc061SDimitry Andric     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
100ff0cc061SDimitry Andric            "Mismatch between setting and entry");
101ff0cc061SDimitry Andric 
10291bc56edSDimitry Andric     // Emit a label for reference from debug information entries.
103ff0cc061SDimitry Andric     if (ShouldCreateSymbols)
104ff0cc061SDimitry Andric       Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
10591bc56edSDimitry Andric 
10691bc56edSDimitry Andric     // Emit the string itself with a terminating null byte.
107ff0cc061SDimitry Andric     Asm.OutStreamer->AddComment("string offset=" +
108ff0cc061SDimitry Andric                                 Twine(Entry->getValue().Offset));
109ff0cc061SDimitry Andric     Asm.OutStreamer->EmitBytes(
11091bc56edSDimitry Andric         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
11191bc56edSDimitry Andric   }
11291bc56edSDimitry Andric 
11391bc56edSDimitry Andric   // If we've got an offset section go ahead and emit that now as well.
11491bc56edSDimitry Andric   if (OffsetSection) {
115*b5893f02SDimitry Andric     // Now only take the indexed entries and put them in an array by their ID so
116*b5893f02SDimitry Andric     // we can emit them in order.
117*b5893f02SDimitry Andric     Entries.resize(NumIndexedStrings);
118*b5893f02SDimitry Andric     for (const auto &Entry : Pool) {
119*b5893f02SDimitry Andric       if (Entry.getValue().isIndexed())
120*b5893f02SDimitry Andric         Entries[Entry.getValue().Index] = &Entry;
121*b5893f02SDimitry Andric     }
122*b5893f02SDimitry Andric 
123ff0cc061SDimitry Andric     Asm.OutStreamer->SwitchSection(OffsetSection);
12491bc56edSDimitry Andric     unsigned size = 4; // FIXME: DWARF64 is 8.
125ff0cc061SDimitry Andric     for (const auto &Entry : Entries)
1264ba319b5SDimitry Andric       if (UseRelativeOffsets)
1274ba319b5SDimitry Andric         Asm.emitDwarfStringOffset(Entry->getValue());
1284ba319b5SDimitry Andric       else
129ff0cc061SDimitry Andric         Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
13091bc56edSDimitry Andric   }
13191bc56edSDimitry Andric }
132