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/StringRef.h"
126e07bfd0SEugene Zelenko #include "llvm/ADT/Twine.h"
139d50e82fSDuncan P. N. Exon Smith #include "llvm/CodeGen/AsmPrinter.h"
14882a2b5aSDuncan P. N. Exon Smith #include "llvm/MC/MCAsmInfo.h"
15daefdbf3SDavid Blaikie #include "llvm/MC/MCStreamer.h"
166e07bfd0SEugene Zelenko #include <cassert>
176e07bfd0SEugene Zelenko #include <utility>
18daefdbf3SDavid Blaikie 
19daefdbf3SDavid Blaikie using namespace llvm;
20daefdbf3SDavid Blaikie 
21882a2b5aSDuncan P. N. Exon Smith DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
22882a2b5aSDuncan P. N. Exon Smith                                  StringRef Prefix)
23882a2b5aSDuncan P. N. Exon Smith     : Pool(A), Prefix(Prefix),
24882a2b5aSDuncan P. N. Exon Smith       ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
25882a2b5aSDuncan P. N. Exon Smith 
262f088116SPavel Labath StringMapEntry<DwarfStringPool::EntryTy> &
272f088116SPavel Labath DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
2803b7a1cfSDuncan P. N. Exon Smith   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
2903b7a1cfSDuncan P. N. Exon Smith   auto &Entry = I.first->second;
302f088116SPavel Labath   if (I.second) {
312f088116SPavel Labath     Entry.Index = EntryTy::NotIndexed;
321a65e4adSDuncan P. N. Exon Smith     Entry.Offset = NumBytes;
33882a2b5aSDuncan P. N. Exon Smith     Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
341a65e4adSDuncan P. N. Exon Smith 
351a65e4adSDuncan P. N. Exon Smith     NumBytes += Str.size() + 1;
361a65e4adSDuncan P. N. Exon Smith     assert(NumBytes > Entry.Offset && "Unexpected overflow");
37daefdbf3SDavid Blaikie   }
382f088116SPavel Labath   return *I.first;
392f088116SPavel Labath }
402f088116SPavel Labath 
412f088116SPavel Labath DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
422f088116SPavel Labath                                                     StringRef Str) {
432f088116SPavel Labath   auto &MapEntry = getEntryImpl(Asm, Str);
442f088116SPavel Labath   return EntryRef(MapEntry, false);
452f088116SPavel Labath }
462f088116SPavel Labath 
472f088116SPavel Labath DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
482f088116SPavel Labath                                                            StringRef Str) {
492f088116SPavel Labath   auto &MapEntry = getEntryImpl(Asm, Str);
502f088116SPavel Labath   if (!MapEntry.getValue().isIndexed())
512f088116SPavel Labath     MapEntry.getValue().Index = NumIndexedStrings++;
522f088116SPavel Labath   return EntryRef(MapEntry, true);
53daefdbf3SDavid Blaikie }
54daefdbf3SDavid Blaikie 
557bfa5d65SPavel Labath void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
567bfa5d65SPavel Labath                                                    MCSection *Section,
577bfa5d65SPavel Labath                                                    MCSymbol *StartSym) {
582f088116SPavel Labath   if (getNumIndexedStrings() == 0)
597bfa5d65SPavel Labath     return;
607bfa5d65SPavel Labath   Asm.OutStreamer->SwitchSection(Section);
617bfa5d65SPavel Labath   unsigned EntrySize = 4;
627bfa5d65SPavel Labath   // FIXME: DWARF64
637bfa5d65SPavel Labath   // We are emitting the header for a contribution to the string offsets
647bfa5d65SPavel Labath   // table. The header consists of an entry with the contribution's
657bfa5d65SPavel Labath   // size (not including the size of the length field), the DWARF version and
667bfa5d65SPavel Labath   // 2 bytes of padding.
672f088116SPavel Labath   Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
687bfa5d65SPavel Labath   Asm.emitInt16(Asm.getDwarfVersion());
697bfa5d65SPavel Labath   Asm.emitInt16(0);
707bfa5d65SPavel Labath   // Define the symbol that marks the start of the contribution. It is
717bfa5d65SPavel Labath   // referenced by most unit headers via DW_AT_str_offsets_base.
727bfa5d65SPavel Labath   // Split units do not use the attribute.
737bfa5d65SPavel Labath   if (StartSym)
747bfa5d65SPavel Labath     Asm.OutStreamer->EmitLabel(StartSym);
757bfa5d65SPavel Labath }
767bfa5d65SPavel Labath 
770709a7bdSRafael Espindola void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
78456b555fSWolfgang Pieb                            MCSection *OffsetSection, bool UseRelativeOffsets) {
79daefdbf3SDavid Blaikie   if (Pool.empty())
80daefdbf3SDavid Blaikie     return;
81daefdbf3SDavid Blaikie 
82daefdbf3SDavid Blaikie   // Start the dwarf str section.
839ff69c8fSLang Hames   Asm.OutStreamer->SwitchSection(StrSection);
84daefdbf3SDavid Blaikie 
852f088116SPavel Labath   // Get all of the string pool entries and sort them by their offset.
862f088116SPavel Labath   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
872f088116SPavel Labath   Entries.reserve(Pool.size());
88daefdbf3SDavid Blaikie 
89daefdbf3SDavid Blaikie   for (const auto &E : Pool)
902f088116SPavel Labath     Entries.push_back(&E);
912f088116SPavel Labath 
923507c6e8SFangrui Song   llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
933507c6e8SFangrui Song                          const StringMapEntry<EntryTy> *B) {
942f088116SPavel Labath     return A->getValue().Offset < B->getValue().Offset;
952f088116SPavel Labath   });
96daefdbf3SDavid Blaikie 
97daefdbf3SDavid Blaikie   for (const auto &Entry : Entries) {
98882a2b5aSDuncan P. N. Exon Smith     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
99882a2b5aSDuncan P. N. Exon Smith            "Mismatch between setting and entry");
100882a2b5aSDuncan P. N. Exon Smith 
101daefdbf3SDavid Blaikie     // Emit a label for reference from debug information entries.
102882a2b5aSDuncan P. N. Exon Smith     if (ShouldCreateSymbols)
1031a65e4adSDuncan P. N. Exon Smith       Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
104daefdbf3SDavid Blaikie 
105daefdbf3SDavid Blaikie     // Emit the string itself with a terminating null byte.
1061a65e4adSDuncan P. N. Exon Smith     Asm.OutStreamer->AddComment("string offset=" +
1071a65e4adSDuncan P. N. Exon Smith                                 Twine(Entry->getValue().Offset));
108*a55daa14SFangrui Song     Asm.OutStreamer->emitBytes(
109daefdbf3SDavid Blaikie         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
110daefdbf3SDavid Blaikie   }
111daefdbf3SDavid Blaikie 
112daefdbf3SDavid Blaikie   // If we've got an offset section go ahead and emit that now as well.
113daefdbf3SDavid Blaikie   if (OffsetSection) {
1142f088116SPavel Labath     // Now only take the indexed entries and put them in an array by their ID so
1152f088116SPavel Labath     // we can emit them in order.
1162f088116SPavel Labath     Entries.resize(NumIndexedStrings);
1172f088116SPavel Labath     for (const auto &Entry : Pool) {
1182f088116SPavel Labath       if (Entry.getValue().isIndexed())
1192f088116SPavel Labath         Entries[Entry.getValue().Index] = &Entry;
1202f088116SPavel Labath     }
1212f088116SPavel Labath 
1229ff69c8fSLang Hames     Asm.OutStreamer->SwitchSection(OffsetSection);
123daefdbf3SDavid Blaikie     unsigned size = 4; // FIXME: DWARF64 is 8.
1241a65e4adSDuncan P. N. Exon Smith     for (const auto &Entry : Entries)
125456b555fSWolfgang Pieb       if (UseRelativeOffsets)
126456b555fSWolfgang Pieb         Asm.emitDwarfStringOffset(Entry->getValue());
127456b555fSWolfgang Pieb       else
1281a65e4adSDuncan P. N. Exon Smith         Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
129daefdbf3SDavid Blaikie   }
130daefdbf3SDavid Blaikie }
131