DwarfStringPool.cpp revision 288943
1254721Semaste//===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "DwarfStringPool.h"
11254721Semaste#include "llvm/CodeGen/AsmPrinter.h"
12254721Semaste#include "llvm/MC/MCAsmInfo.h"
13254721Semaste#include "llvm/MC/MCStreamer.h"
14254721Semaste
15254721Semasteusing namespace llvm;
16254721Semaste
17254721SemasteDwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
18254721Semaste                                 StringRef Prefix)
19254721Semaste    : Pool(A), Prefix(Prefix),
20254721Semaste      ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
21254721Semaste
22254721SemasteDwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
23254721Semaste                                                    StringRef Str) {
24254721Semaste  auto I = Pool.insert(std::make_pair(Str, EntryTy()));
25254721Semaste  if (I.second) {
26254721Semaste    auto &Entry = I.first->second;
27254721Semaste    Entry.Index = Pool.size() - 1;
28254721Semaste    Entry.Offset = NumBytes;
29254721Semaste    Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
30296417Sdim
31254721Semaste    NumBytes += Str.size() + 1;
32254721Semaste    assert(NumBytes > Entry.Offset && "Unexpected overflow");
33254721Semaste  }
34254721Semaste  return EntryRef(*I.first);
35296417Sdim}
36
37void 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