Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/IRSymtab.h
Line
Count
Source (jump to first uncovered line)
1
//===- IRSymtab.h - data definitions for IR symbol tables -------*- C++ -*-===//
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
// This file contains data definitions and a reader and builder for a symbol
10
// table for LLVM IR. Its purpose is to allow linkers and other consumers of
11
// bitcode files to efficiently read the symbol table for symbol resolution
12
// purposes without needing to construct a module in memory.
13
//
14
// As with most object files the symbol table has two parts: the symbol table
15
// itself and a string table which is referenced by the symbol table.
16
//
17
// A symbol table corresponds to a single bitcode file, which may consist of
18
// multiple modules, so symbol tables may likewise contain symbols for multiple
19
// modules.
20
//
21
//===----------------------------------------------------------------------===//
22
23
#ifndef LLVM_OBJECT_IRSYMTAB_H
24
#define LLVM_OBJECT_IRSYMTAB_H
25
26
#include "llvm/ADT/ArrayRef.h"
27
#include "llvm/ADT/StringRef.h"
28
#include "llvm/ADT/iterator_range.h"
29
#include "llvm/IR/GlobalValue.h"
30
#include "llvm/Object/SymbolicFile.h"
31
#include "llvm/Support/Endian.h"
32
#include "llvm/Support/Error.h"
33
#include <cassert>
34
#include <cstdint>
35
#include <vector>
36
37
namespace llvm {
38
39
struct BitcodeFileContents;
40
class StringTableBuilder;
41
42
namespace irsymtab {
43
44
namespace storage {
45
46
// The data structures in this namespace define the low-level serialization
47
// format. Clients that just want to read a symbol table should use the
48
// irsymtab::Reader class.
49
50
using Word = support::ulittle32_t;
51
52
/// A reference to a string in the string table.
53
struct Str {
54
  Word Offset, Size;
55
56
9.08k
  StringRef get(StringRef Strtab) const {
57
9.08k
    return {Strtab.data() + Offset, Size};
58
9.08k
  }
59
};
60
61
/// A reference to a range of objects in the symbol table.
62
template <typename T> struct Range {
63
  Word Offset, Size;
64
65
4.69k
  ArrayRef<T> get(StringRef Symtab) const {
66
4.69k
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
4.69k
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Module>::get(llvm::StringRef) const
Line
Count
Source
65
938
  ArrayRef<T> get(StringRef Symtab) const {
66
938
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
938
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Comdat>::get(llvm::StringRef) const
Line
Count
Source
65
938
  ArrayRef<T> get(StringRef Symtab) const {
66
938
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
938
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Symbol>::get(llvm::StringRef) const
Line
Count
Source
65
938
  ArrayRef<T> get(StringRef Symtab) const {
66
938
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
938
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Uncommon>::get(llvm::StringRef) const
Line
Count
Source
65
938
  ArrayRef<T> get(StringRef Symtab) const {
66
938
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
938
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Str>::get(llvm::StringRef) const
Line
Count
Source
65
938
  ArrayRef<T> get(StringRef Symtab) const {
66
938
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
938
  }
68
};
69
70
/// Describes the range of a particular module's symbols within the symbol
71
/// table.
72
struct Module {
73
  Word Begin, End;
74
75
  /// The index of the first Uncommon for this Module.
76
  Word UncBegin;
77
};
78
79
/// This is equivalent to an IR comdat.
80
struct Comdat {
81
  Str Name;
82
};
83
84
/// Contains the information needed by linkers for symbol resolution, as well as
85
/// by the LTO implementation itself.
86
struct Symbol {
87
  /// The mangled symbol name.
88
  Str Name;
89
90
  /// The unmangled symbol name, or the empty string if this is not an IR
91
  /// symbol.
92
  Str IRName;
93
94
  /// The index into Header::Comdats, or -1 if not a comdat member.
95
  Word ComdatIndex;
96
97
  Word Flags;
98
  enum FlagBits {
99
    FB_visibility, // 2 bits
100
    FB_has_uncommon = FB_visibility + 2,
101
    FB_undefined,
102
    FB_weak,
103
    FB_common,
104
    FB_indirect,
105
    FB_used,
106
    FB_tls,
107
    FB_may_omit,
108
    FB_global,
109
    FB_format_specific,
110
    FB_unnamed_addr,
111
    FB_executable,
112
  };
113
};
114
115
/// This data structure contains rarely used symbol fields and is optionally
116
/// referenced by a Symbol.
117
struct Uncommon {
118
  Word CommonSize, CommonAlign;
119
120
  /// COFF-specific: the name of the symbol that a weak external resolves to
121
  /// if not defined.
122
  Str COFFWeakExternFallbackName;
123
124
  /// Specified section name, if any.
125
  Str SectionName;
126
};
127
128
129
struct Header {
130
  /// Version number of the symtab format. This number should be incremented
131
  /// when the format changes, but it does not need to be incremented if a
132
  /// change to LLVM would cause it to create a different symbol table.
133
  Word Version;
134
  enum { kCurrentVersion = 2 };
135
136
  /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
137
  /// Consumers should rebuild the symbol table from IR if the producer's
138
  /// version does not match the consumer's version due to potential differences
139
  /// in symbol table format, symbol enumeration order and so on.
140
  Str Producer;
141
142
  Range<Module> Modules;
143
  Range<Comdat> Comdats;
144
  Range<Symbol> Symbols;
145
  Range<Uncommon> Uncommons;
146
147
  Str TargetTriple, SourceFileName;
148
149
  /// COFF-specific: linker directives.
150
  Str COFFLinkerOpts;
151
152
  /// Dependent Library Specifiers
153
  Range<Str> DependentLibraries;
154
};
155
156
} // end namespace storage
157
158
/// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
159
/// Mods.
160
Error build(ArrayRef<Module *> Mods, SmallVector<char, 0> &Symtab,
161
            StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc);
162
163
/// This represents a symbol that has been read from a storage::Symbol and
164
/// possibly a storage::Uncommon.
165
struct Symbol {
166
  // Copied from storage::Symbol.
167
  StringRef Name, IRName;
168
  int ComdatIndex;
169
  uint32_t Flags;
170
171
  // Copied from storage::Uncommon.
172
  uint32_t CommonSize, CommonAlign;
173
  StringRef COFFWeakExternFallbackName;
174
  StringRef SectionName;
175
176
  /// Returns the mangled symbol name.
177
3.47k
  StringRef getName() const { return Name; }
178
179
  /// Returns the unmangled symbol name, or the empty string if this is not an
180
  /// IR symbol.
181
2.69k
  StringRef getIRName() const { return IRName; }
182
183
  /// Returns the index into the comdat table (see Reader::getComdatTable()), or
184
  /// -1 if not a comdat member.
185
  int getComdatIndex() const { return ComdatIndex; }
186
187
  using S = storage::Symbol;
188
189
  GlobalValue::VisibilityTypes getVisibility() const {
190
    return GlobalValue::VisibilityTypes((Flags >> S::FB_visibility) & 3);
191
  }
192
193
1.70k
  bool isUndefined() const { return (Flags >> S::FB_undefined) & 1; }
194
  bool isWeak() const { return (Flags >> S::FB_weak) & 1; }
195
1.09k
  bool isCommon() const { return (Flags >> S::FB_common) & 1; }
196
  bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
197
1.77k
  bool isUsed() const { return (Flags >> S::FB_used) & 1; }
198
  bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
199
200
  bool canBeOmittedFromSymbolTable() const {
201
    return (Flags >> S::FB_may_omit) & 1;
202
  }
203
204
2.57k
  bool isGlobal() const { return (Flags >> S::FB_global) & 1; }
205
2.38k
  bool isFormatSpecific() const { return (Flags >> S::FB_format_specific) & 1; }
206
1.36k
  bool isUnnamedAddr() const { return (Flags >> S::FB_unnamed_addr) & 1; }
207
  bool isExecutable() const { return (Flags >> S::FB_executable) & 1; }
208
209
28
  uint64_t getCommonSize() const {
210
28
    assert(isCommon());
211
28
    return CommonSize;
212
28
  }
213
214
28
  uint32_t getCommonAlignment() const {
215
28
    assert(isCommon());
216
28
    return CommonAlign;
217
28
  }
218
219
  /// COFF-specific: for weak externals, returns the name of the symbol that is
220
  /// used as a fallback if the weak external remains undefined.
221
  StringRef getCOFFWeakExternalFallback() const {
222
    assert(isWeak() && isIndirect());
223
    return COFFWeakExternFallbackName;
224
  }
225
226
  StringRef getSectionName() const { return SectionName; }
227
};
228
229
/// This class can be used to read a Symtab and Strtab produced by
230
/// irsymtab::build.
231
class Reader {
232
  StringRef Symtab, Strtab;
233
234
  ArrayRef<storage::Module> Modules;
235
  ArrayRef<storage::Comdat> Comdats;
236
  ArrayRef<storage::Symbol> Symbols;
237
  ArrayRef<storage::Uncommon> Uncommons;
238
  ArrayRef<storage::Str> DependentLibraries;
239
240
8.15k
  StringRef str(storage::Str S) const { return S.get(Strtab); }
241
242
4.69k
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
243
4.69k
    return R.get(Symtab);
244
4.69k
  }
llvm::ArrayRef<llvm::irsymtab::storage::Module> llvm::irsymtab::Reader::range<llvm::irsymtab::storage::Module>(llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Module>) const
Line
Count
Source
242
938
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
243
938
    return R.get(Symtab);
244
938
  }
llvm::ArrayRef<llvm::irsymtab::storage::Comdat> llvm::irsymtab::Reader::range<llvm::irsymtab::storage::Comdat>(llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Comdat>) const
Line
Count
Source
242
938
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
243
938
    return R.get(Symtab);
244
938
  }
llvm::ArrayRef<llvm::irsymtab::storage::Symbol> llvm::irsymtab::Reader::range<llvm::irsymtab::storage::Symbol>(llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Symbol>) const
Line
Count
Source
242
938
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
243
938
    return R.get(Symtab);
244
938
  }
llvm::ArrayRef<llvm::irsymtab::storage::Uncommon> llvm::irsymtab::Reader::range<llvm::irsymtab::storage::Uncommon>(llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Uncommon>) const
Line
Count
Source
242
938
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
243
938
    return R.get(Symtab);
244
938
  }
llvm::ArrayRef<llvm::irsymtab::storage::Str> llvm::irsymtab::Reader::range<llvm::irsymtab::storage::Str>(llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Str>) const
Line
Count
Source
242
938
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
243
938
    return R.get(Symtab);
244
938
  }
245
246
7.50k
  const storage::Header &header() const {
247
7.50k
    return *reinterpret_cast<const storage::Header *>(Symtab.data());
248
7.50k
  }
249
250
public:
251
  class SymbolRef;
252
253
1.88k
  Reader() = default;
254
938
  Reader(StringRef Symtab, StringRef Strtab) : Symtab(Symtab), Strtab(Strtab) {
255
938
    Modules = range(header().Modules);
256
938
    Comdats = range(header().Comdats);
257
938
    Symbols = range(header().Symbols);
258
938
    Uncommons = range(header().Uncommons);
259
938
    DependentLibraries = range(header().DependentLibraries);
260
938
  }
261
262
  using symbol_range = iterator_range<object::content_iterator<SymbolRef>>;
263
264
  /// Returns the symbol table for the entire bitcode file.
265
  /// The symbols enumerated by this method are ephemeral, but they can be
266
  /// copied into an irsymtab::Symbol object.
267
  symbol_range symbols() const;
268
269
926
  size_t getNumModules() const { return Modules.size(); }
270
271
  /// Returns a slice of the symbol table for the I'th module in the file.
272
  /// The symbols enumerated by this method are ephemeral, but they can be
273
  /// copied into an irsymtab::Symbol object.
274
  symbol_range module_symbols(unsigned I) const;
275
276
938
  StringRef getTargetTriple() const { return str(header().TargetTriple); }
277
278
  /// Returns the source file path specified at compile time.
279
938
  StringRef getSourceFileName() const { return str(header().SourceFileName); }
280
281
  /// Returns a table with all the comdats used by this file.
282
938
  std::vector<StringRef> getComdatTable() const {
283
938
    std::vector<StringRef> ComdatTable;
284
938
    ComdatTable.reserve(Comdats.size());
285
938
    for (auto C : Comdats)
286
65
      ComdatTable.push_back(str(C.Name));
287
938
    return ComdatTable;
288
938
  }
289
290
  /// COFF-specific: returns linker options specified in the input file.
291
938
  StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); }
292
293
  /// Returns dependent library specifiers
294
938
  std::vector<StringRef> getDependentLibraries() const {
295
938
    std::vector<StringRef> Specifiers;
296
938
    Specifiers.reserve(DependentLibraries.size());
297
938
    for (auto S : DependentLibraries) {
298
13
      Specifiers.push_back(str(S));
299
13
    }
300
938
    return Specifiers;
301
938
  }
302
};
303
304
/// Ephemeral symbols produced by Reader::symbols() and
305
/// Reader::module_symbols().
306
class Reader::SymbolRef : public Symbol {
307
  const storage::Symbol *SymI, *SymE;
308
  const storage::Uncommon *UncI;
309
  const Reader *R;
310
311
4.49k
  void read() {
312
4.49k
    if (SymI == SymE)
313
1.92k
      return;
314
2.57k
315
2.57k
    Name = R->str(SymI->Name);
316
2.57k
    IRName = R->str(SymI->IRName);
317
2.57k
    ComdatIndex = SymI->ComdatIndex;
318
2.57k
    Flags = SymI->Flags;
319
2.57k
320
2.57k
    if (Flags & (1 << storage::Symbol::FB_has_uncommon)) {
321
61
      CommonSize = UncI->CommonSize;
322
61
      CommonAlign = UncI->CommonAlign;
323
61
      COFFWeakExternFallbackName = R->str(UncI->COFFWeakExternFallbackName);
324
61
      SectionName = R->str(UncI->SectionName);
325
61
    } else
326
2.51k
      // Reset this field so it can be queried unconditionally for all symbols.
327
2.51k
      SectionName = "";
328
2.57k
  }
329
330
public:
331
  SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE,
332
            const storage::Uncommon *UncI, const Reader *R)
333
1.92k
      : SymI(SymI), SymE(SymE), UncI(UncI), R(R) {
334
1.92k
    read();
335
1.92k
  }
336
337
2.57k
  void moveNext() {
338
2.57k
    ++SymI;
339
2.57k
    if (Flags & (1 << storage::Symbol::FB_has_uncommon))
340
61
      ++UncI;
341
2.57k
    read();
342
2.57k
  }
343
344
3.53k
  bool operator==(const SymbolRef &Other) const { return SymI == Other.SymI; }
345
};
346
347
0
inline Reader::symbol_range Reader::symbols() const {
348
0
  return {SymbolRef(Symbols.begin(), Symbols.end(), Uncommons.begin(), this),
349
0
          SymbolRef(Symbols.end(), Symbols.end(), nullptr, this)};
350
0
}
351
352
962
inline Reader::symbol_range Reader::module_symbols(unsigned I) const {
353
962
  const storage::Module &M = Modules[I];
354
962
  const storage::Symbol *MBegin = Symbols.begin() + M.Begin,
355
962
                        *MEnd = Symbols.begin() + M.End;
356
962
  return {SymbolRef(MBegin, MEnd, Uncommons.begin() + M.UncBegin, this),
357
962
          SymbolRef(MEnd, MEnd, nullptr, this)};
358
962
}
359
360
/// The contents of the irsymtab in a bitcode file. Any underlying data for the
361
/// irsymtab are owned by Symtab and Strtab.
362
struct FileContents {
363
  SmallVector<char, 0> Symtab, Strtab;
364
  Reader TheReader;
365
};
366
367
/// Reads the contents of a bitcode file, creating its irsymtab if necessary.
368
Expected<FileContents> readBitcode(const BitcodeFileContents &BFC);
369
370
} // end namespace irsymtab
371
} // end namespace llvm
372
373
#endif // LLVM_OBJECT_IRSYMTAB_H