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 
2703b7a1cfSDuncan P. N. Exon Smith DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
288c6499faSDuncan P. N. Exon Smith                                                     StringRef Str) {
2903b7a1cfSDuncan P. N. Exon Smith   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
3003b7a1cfSDuncan P. N. Exon Smith   if (I.second) {
3103b7a1cfSDuncan P. N. Exon Smith     auto &Entry = I.first->second;
321a65e4adSDuncan P. N. Exon Smith     Entry.Index = Pool.size() - 1;
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   }
3903b7a1cfSDuncan P. N. Exon Smith   return EntryRef(*I.first);
40daefdbf3SDavid Blaikie }
41daefdbf3SDavid Blaikie 
42*7bfa5d65SPavel Labath void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
43*7bfa5d65SPavel Labath                                                    MCSection *Section,
44*7bfa5d65SPavel Labath                                                    MCSymbol *StartSym) {
45*7bfa5d65SPavel Labath   if (empty())
46*7bfa5d65SPavel Labath     return;
47*7bfa5d65SPavel Labath   Asm.OutStreamer->SwitchSection(Section);
48*7bfa5d65SPavel Labath   unsigned EntrySize = 4;
49*7bfa5d65SPavel Labath   // FIXME: DWARF64
50*7bfa5d65SPavel Labath   // We are emitting the header for a contribution to the string offsets
51*7bfa5d65SPavel Labath   // table. The header consists of an entry with the contribution's
52*7bfa5d65SPavel Labath   // size (not including the size of the length field), the DWARF version and
53*7bfa5d65SPavel Labath   // 2 bytes of padding.
54*7bfa5d65SPavel Labath   Asm.emitInt32(size() * EntrySize + 4);
55*7bfa5d65SPavel Labath   Asm.emitInt16(Asm.getDwarfVersion());
56*7bfa5d65SPavel Labath   Asm.emitInt16(0);
57*7bfa5d65SPavel Labath   // Define the symbol that marks the start of the contribution. It is
58*7bfa5d65SPavel Labath   // referenced by most unit headers via DW_AT_str_offsets_base.
59*7bfa5d65SPavel Labath   // Split units do not use the attribute.
60*7bfa5d65SPavel Labath   if (StartSym)
61*7bfa5d65SPavel Labath     Asm.OutStreamer->EmitLabel(StartSym);
62*7bfa5d65SPavel Labath }
63*7bfa5d65SPavel Labath 
640709a7bdSRafael Espindola void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
65456b555fSWolfgang Pieb                            MCSection *OffsetSection, bool UseRelativeOffsets) {
66daefdbf3SDavid Blaikie   if (Pool.empty())
67daefdbf3SDavid Blaikie     return;
68daefdbf3SDavid Blaikie 
69daefdbf3SDavid Blaikie   // Start the dwarf str section.
709ff69c8fSLang Hames   Asm.OutStreamer->SwitchSection(StrSection);
71daefdbf3SDavid Blaikie 
72daefdbf3SDavid Blaikie   // Get all of the string pool entries and put them in an array by their ID so
73daefdbf3SDavid Blaikie   // we can sort them.
741a65e4adSDuncan P. N. Exon Smith   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
75daefdbf3SDavid Blaikie 
76daefdbf3SDavid Blaikie   for (const auto &E : Pool)
771a65e4adSDuncan P. N. Exon Smith     Entries[E.getValue().Index] = &E;
78daefdbf3SDavid Blaikie 
79daefdbf3SDavid Blaikie   for (const auto &Entry : Entries) {
80882a2b5aSDuncan P. N. Exon Smith     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
81882a2b5aSDuncan P. N. Exon Smith            "Mismatch between setting and entry");
82882a2b5aSDuncan P. N. Exon Smith 
83daefdbf3SDavid Blaikie     // Emit a label for reference from debug information entries.
84882a2b5aSDuncan P. N. Exon Smith     if (ShouldCreateSymbols)
851a65e4adSDuncan P. N. Exon Smith       Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
86daefdbf3SDavid Blaikie 
87daefdbf3SDavid Blaikie     // Emit the string itself with a terminating null byte.
881a65e4adSDuncan P. N. Exon Smith     Asm.OutStreamer->AddComment("string offset=" +
891a65e4adSDuncan P. N. Exon Smith                                 Twine(Entry->getValue().Offset));
909ff69c8fSLang Hames     Asm.OutStreamer->EmitBytes(
91daefdbf3SDavid Blaikie         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
92daefdbf3SDavid Blaikie   }
93daefdbf3SDavid Blaikie 
94daefdbf3SDavid Blaikie   // If we've got an offset section go ahead and emit that now as well.
95daefdbf3SDavid Blaikie   if (OffsetSection) {
969ff69c8fSLang Hames     Asm.OutStreamer->SwitchSection(OffsetSection);
97daefdbf3SDavid Blaikie     unsigned size = 4; // FIXME: DWARF64 is 8.
981a65e4adSDuncan P. N. Exon Smith     for (const auto &Entry : Entries)
99456b555fSWolfgang Pieb       if (UseRelativeOffsets)
100456b555fSWolfgang Pieb         Asm.emitDwarfStringOffset(Entry->getValue());
101456b555fSWolfgang Pieb       else
1021a65e4adSDuncan P. N. Exon Smith         Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
103daefdbf3SDavid Blaikie   }
104daefdbf3SDavid Blaikie }
105