Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
Line
Count
Source
1
//===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "DwarfStringPool.h"
10
#include "llvm/ADT/SmallVector.h"
11
#include "llvm/ADT/StringRef.h"
12
#include "llvm/ADT/Twine.h"
13
#include "llvm/CodeGen/AsmPrinter.h"
14
#include "llvm/MC/MCAsmInfo.h"
15
#include "llvm/MC/MCStreamer.h"
16
#include <cassert>
17
#include <utility>
18
19
using namespace llvm;
20
21
DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
22
                                 StringRef Prefix)
23
    : Pool(A), Prefix(Prefix),
24
71.6k
      ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
25
26
StringMapEntry<DwarfStringPool::EntryTy> &
27
419k
DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
28
419k
  auto I = Pool.insert(std::make_pair(Str, EntryTy()));
29
419k
  auto &Entry = I.first->second;
30
419k
  if (I.second) {
31
82.8k
    Entry.Index = EntryTy::NotIndexed;
32
82.8k
    Entry.Offset = NumBytes;
33
82.8k
    Entry.Symbol = ShouldCreateSymbols ? 
Asm.createTempSymbol(Prefix)4.93k
:
nullptr77.8k
;
34
82.8k
35
82.8k
    NumBytes += Str.size() + 1;
36
82.8k
    assert(NumBytes > Entry.Offset && "Unexpected overflow");
37
82.8k
  }
38
419k
  return *I.first;
39
419k
}
40
41
DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
42
418k
                                                    StringRef Str) {
43
418k
  auto &MapEntry = getEntryImpl(Asm, Str);
44
418k
  return EntryRef(MapEntry, false);
45
418k
}
46
47
DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
48
1.18k
                                                           StringRef Str) {
49
1.18k
  auto &MapEntry = getEntryImpl(Asm, Str);
50
1.18k
  if (!MapEntry.getValue().isIndexed())
51
1.06k
    MapEntry.getValue().Index = NumIndexedStrings++;
52
1.18k
  return EntryRef(MapEntry, true);
53
1.18k
}
54
55
void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
56
                                                   MCSection *Section,
57
129
                                                   MCSymbol *StartSym) {
58
129
  if (getNumIndexedStrings() == 0)
59
64
    return;
60
65
  Asm.OutStreamer->SwitchSection(Section);
61
65
  unsigned EntrySize = 4;
62
65
  // FIXME: DWARF64
63
65
  // We are emitting the header for a contribution to the string offsets
64
65
  // table. The header consists of an entry with the contribution's
65
65
  // size (not including the size of the length field), the DWARF version and
66
65
  // 2 bytes of padding.
67
65
  Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
68
65
  Asm.emitInt16(Asm.getDwarfVersion());
69
65
  Asm.emitInt16(0);
70
65
  // Define the symbol that marks the start of the contribution. It is
71
65
  // referenced by most unit headers via DW_AT_str_offsets_base.
72
65
  // Split units do not use the attribute.
73
65
  if (StartSym)
74
58
    Asm.OutStreamer->EmitLabel(StartSym);
75
65
}
76
77
void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
78
3.33k
                           MCSection *OffsetSection, bool UseRelativeOffsets) {
79
3.33k
  if (Pool.empty())
80
913
    return;
81
2.41k
82
2.41k
  // Start the dwarf str section.
83
2.41k
  Asm.OutStreamer->SwitchSection(StrSection);
84
2.41k
85
2.41k
  // Get all of the string pool entries and sort them by their offset.
86
2.41k
  SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
87
2.41k
  Entries.reserve(Pool.size());
88
2.41k
89
2.41k
  for (const auto &E : Pool)
90
82.8k
    Entries.push_back(&E);
91
2.41k
92
2.41k
  llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
93
634k
                         const StringMapEntry<EntryTy> *B) {
94
634k
    return A->getValue().Offset < B->getValue().Offset;
95
634k
  });
96
2.41k
97
82.8k
  for (const auto &Entry : Entries) {
98
82.8k
    assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
99
82.8k
           "Mismatch between setting and entry");
100
82.8k
101
82.8k
    // Emit a label for reference from debug information entries.
102
82.8k
    if (ShouldCreateSymbols)
103
4.92k
      Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
104
82.8k
105
82.8k
    // Emit the string itself with a terminating null byte.
106
82.8k
    Asm.OutStreamer->AddComment("string offset=" +
107
82.8k
                                Twine(Entry->getValue().Offset));
108
82.8k
    Asm.OutStreamer->EmitBytes(
109
82.8k
        StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
110
82.8k
  }
111
2.41k
112
2.41k
  // If we've got an offset section go ahead and emit that now as well.
113
2.41k
  if (OffsetSection) {
114
134
    // Now only take the indexed entries and put them in an array by their ID so
115
134
    // we can emit them in order.
116
134
    Entries.resize(NumIndexedStrings);
117
1.23k
    for (const auto &Entry : Pool) {
118
1.23k
      if (Entry.getValue().isIndexed())
119
1.06k
        Entries[Entry.getValue().Index] = &Entry;
120
1.23k
    }
121
134
122
134
    Asm.OutStreamer->SwitchSection(OffsetSection);
123
134
    unsigned size = 4; // FIXME: DWARF64 is 8.
124
134
    for (const auto &Entry : Entries)
125
1.06k
      if (UseRelativeOffsets)
126
667
        Asm.emitDwarfStringOffset(Entry->getValue());
127
400
      else
128
400
        Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
129
134
  }
130
2.41k
}