• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DwarfStringPool.h"
11 #include "llvm/CodeGen/AsmPrinter.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCStreamer.h"
14 
15 using namespace llvm;
16 
DwarfStringPool(BumpPtrAllocator & A,AsmPrinter & Asm,StringRef Prefix)17 DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
18                                  StringRef Prefix)
19     : Pool(A), Prefix(Prefix),
20       ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
21 
getEntry(AsmPrinter & Asm,StringRef Str)22 DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
23                                                     StringRef Str) {
24   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
25   if (I.second) {
26     auto &Entry = I.first->second;
27     Entry.Index = Pool.size() - 1;
28     Entry.Offset = NumBytes;
29     Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
30 
31     NumBytes += Str.size() + 1;
32     assert(NumBytes > Entry.Offset && "Unexpected overflow");
33   }
34   return EntryRef(*I.first);
35 }
36 
emit(AsmPrinter & Asm,MCSection * StrSection,MCSection * OffsetSection)37 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
38                            MCSection *OffsetSection) {
39   if (Pool.empty())
40     return;
41 
42   // Start the dwarf str section.
43   Asm.OutStreamer->SwitchSection(StrSection);
44 
45   // Get all of the string pool entries and put them in an array by their ID so
46   // we can sort them.
47   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
48 
49   for (const auto &E : Pool)
50     Entries[E.getValue().Index] = &E;
51 
52   for (const auto &Entry : Entries) {
53     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
54            "Mismatch between setting and entry");
55 
56     // Emit a label for reference from debug information entries.
57     if (ShouldCreateSymbols)
58       Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
59 
60     // Emit the string itself with a terminating null byte.
61     Asm.OutStreamer->AddComment("string offset=" +
62                                 Twine(Entry->getValue().Offset));
63     Asm.OutStreamer->EmitBytes(
64         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
65   }
66 
67   // If we've got an offset section go ahead and emit that now as well.
68   if (OffsetSection) {
69     Asm.OutStreamer->SwitchSection(OffsetSection);
70     unsigned size = 4; // FIXME: DWARF64 is 8.
71     for (const auto &Entry : Entries)
72       Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
73   }
74 }
75