Coverage Report

Created: 2019-02-20 00:17

/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
5.49k
  StringRef get(StringRef Strtab) const {
57
5.49k
    return {Strtab.data() + Offset, Size};
58
5.49k
  }
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
2.58k
  ArrayRef<T> get(StringRef Symtab) const {
66
2.58k
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
2.58k
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Module>::get(llvm::StringRef) const
Line
Count
Source
65
646
  ArrayRef<T> get(StringRef Symtab) const {
66
646
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
646
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Comdat>::get(llvm::StringRef) const
Line
Count
Source
65
646
  ArrayRef<T> get(StringRef Symtab) const {
66
646
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
646
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Symbol>::get(llvm::StringRef) const
Line
Count
Source
65
646
  ArrayRef<T> get(StringRef Symtab) const {
66
646
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
646
  }
llvm::irsymtab::storage::Range<llvm::irsymtab::storage::Uncommon>::get(llvm::StringRef) const
Line
Count
Source
65
646
  ArrayRef<T> get(StringRef Symtab) const {
66
646
    return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
67
646
  }
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
struct Header {
129
  /// Version number of the symtab format. This number should be incremented
130
  /// when the format changes, but it does not need to be incremented if a
131
  /// change to LLVM would cause it to create a different symbol table.
132
  Word Version;
133
  enum { kCurrentVersion = 1 };
134
135
  /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
136
  /// Consumers should rebuild the symbol table from IR if the producer's
137
  /// version does not match the consumer's version due to potential differences
138
  /// in symbol table format, symbol enumeration order and so on.
139
  Str Producer;
140
141
  Range<Module> Modules;
142
  Range<Comdat> Comdats;
143
  Range<Symbol> Symbols;
144
  Range<Uncommon> Uncommons;
145
146
  Str TargetTriple, SourceFileName;
147
148
  /// COFF-specific: linker directives.
149
  Str COFFLinkerOpts;
150
};
151
152
} // end namespace storage
153
154
/// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
155
/// Mods.
156
Error build(ArrayRef<Module *> Mods, SmallVector<char, 0> &Symtab,
157
            StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc);
158
159
/// This represents a symbol that has been read from a storage::Symbol and
160
/// possibly a storage::Uncommon.
161
struct Symbol {
162
  // Copied from storage::Symbol.
163
  StringRef Name, IRName;
164
  int ComdatIndex;
165
  uint32_t Flags;
166
167
  // Copied from storage::Uncommon.
168
  uint32_t CommonSize, CommonAlign;
169
  StringRef COFFWeakExternFallbackName;
170
  StringRef SectionName;
171
172
  /// Returns the mangled symbol name.
173
3.04k
  StringRef getName() const { return Name; }
174
175
  /// Returns the unmangled symbol name, or the empty string if this is not an
176
  /// IR symbol.
177
2.33k
  StringRef getIRName() const { return IRName; }
178
179
  /// Returns the index into the comdat table (see Reader::getComdatTable()), or
180
  /// -1 if not a comdat member.
181
  int getComdatIndex() const { return ComdatIndex; }
182
183
  using S = storage::Symbol;
184
185
  GlobalValue::VisibilityTypes getVisibility() const {
186
    return GlobalValue::VisibilityTypes((Flags >> S::FB_visibility) & 3);
187
  }
188
189
1.54k
  bool isUndefined() const { return (Flags >> S::FB_undefined) & 1; }
190
  bool isWeak() const { return (Flags >> S::FB_weak) & 1; }
191
997
  bool isCommon() const { return (Flags >> S::FB_common) & 1; }
192
  bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
193
1.11k
  bool isUsed() const { return (Flags >> S::FB_used) & 1; }
194
  bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
195
196
  bool canBeOmittedFromSymbolTable() const {
197
    return (Flags >> S::FB_may_omit) & 1;
198
  }
199
200
1.39k
  bool isGlobal() const { return (Flags >> S::FB_global) & 1; }
201
1.31k
  bool isFormatSpecific() const { return (Flags >> S::FB_format_specific) & 1; }
202
1.20k
  bool isUnnamedAddr() const { return (Flags >> S::FB_unnamed_addr) & 1; }
203
  bool isExecutable() const { return (Flags >> S::FB_executable) & 1; }
204
205
24
  uint64_t getCommonSize() const {
206
24
    assert(isCommon());
207
24
    return CommonSize;
208
24
  }
209
210
24
  uint32_t getCommonAlignment() const {
211
24
    assert(isCommon());
212
24
    return CommonAlign;
213
24
  }
214
215
  /// COFF-specific: for weak externals, returns the name of the symbol that is
216
  /// used as a fallback if the weak external remains undefined.
217
  StringRef getCOFFWeakExternalFallback() const {
218
    assert(isWeak() && isIndirect());
219
    return COFFWeakExternFallbackName;
220
  }
221
222
  StringRef getSectionName() const { return SectionName; }
223
};
224
225
/// This class can be used to read a Symtab and Strtab produced by
226
/// irsymtab::build.
227
class Reader {
228
  StringRef Symtab, Strtab;
229
230
  ArrayRef<storage::Module> Modules;
231
  ArrayRef<storage::Comdat> Comdats;
232
  ArrayRef<storage::Symbol> Symbols;
233
  ArrayRef<storage::Uncommon> Uncommons;
234
235
4.85k
  StringRef str(storage::Str S) const { return S.get(Strtab); }
236
237
2.58k
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
238
2.58k
    return R.get(Symtab);
239
2.58k
  }
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
237
646
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
238
646
    return R.get(Symtab);
239
646
  }
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
237
646
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
238
646
    return R.get(Symtab);
239
646
  }
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
237
646
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
238
646
    return R.get(Symtab);
239
646
  }
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
237
646
  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
238
646
    return R.get(Symtab);
239
646
  }
240
241
4.52k
  const storage::Header &header() const {
242
4.52k
    return *reinterpret_cast<const storage::Header *>(Symtab.data());
243
4.52k
  }
244
245
public:
246
  class SymbolRef;
247
248
1.29k
  Reader() = default;
249
646
  Reader(StringRef Symtab, StringRef Strtab) : Symtab(Symtab), Strtab(Strtab) {
250
646
    Modules = range(header().Modules);
251
646
    Comdats = range(header().Comdats);
252
646
    Symbols = range(header().Symbols);
253
646
    Uncommons = range(header().Uncommons);
254
646
  }
255
256
  using symbol_range = iterator_range<object::content_iterator<SymbolRef>>;
257
258
  /// Returns the symbol table for the entire bitcode file.
259
  /// The symbols enumerated by this method are ephemeral, but they can be
260
  /// copied into an irsymtab::Symbol object.
261
  symbol_range symbols() const;
262
263
636
  size_t getNumModules() const { return Modules.size(); }
264
265
  /// Returns a slice of the symbol table for the I'th module in the file.
266
  /// The symbols enumerated by this method are ephemeral, but they can be
267
  /// copied into an irsymtab::Symbol object.
268
  symbol_range module_symbols(unsigned I) const;
269
270
646
  StringRef getTargetTriple() const { return str(header().TargetTriple); }
271
272
  /// Returns the source file path specified at compile time.
273
646
  StringRef getSourceFileName() const { return str(header().SourceFileName); }
274
275
  /// Returns a table with all the comdats used by this file.
276
646
  std::vector<StringRef> getComdatTable() const {
277
646
    std::vector<StringRef> ComdatTable;
278
646
    ComdatTable.reserve(Comdats.size());
279
646
    for (auto C : Comdats)
280
43
      ComdatTable.push_back(str(C.Name));
281
646
    return ComdatTable;
282
646
  }
283
284
  /// COFF-specific: returns linker options specified in the input file.
285
646
  StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); }
286
};
287
288
/// Ephemeral symbols produced by Reader::symbols() and
289
/// Reader::module_symbols().
290
class Reader::SymbolRef : public Symbol {
291
  const storage::Symbol *SymI, *SymE;
292
  const storage::Uncommon *UncI;
293
  const Reader *R;
294
295
2.73k
  void read() {
296
2.73k
    if (SymI == SymE)
297
1.33k
      return;
298
1.39k
299
1.39k
    Name = R->str(SymI->Name);
300
1.39k
    IRName = R->str(SymI->IRName);
301
1.39k
    ComdatIndex = SymI->ComdatIndex;
302
1.39k
    Flags = SymI->Flags;
303
1.39k
304
1.39k
    if (Flags & (1 << storage::Symbol::FB_has_uncommon)) {
305
38
      CommonSize = UncI->CommonSize;
306
38
      CommonAlign = UncI->CommonAlign;
307
38
      COFFWeakExternFallbackName = R->str(UncI->COFFWeakExternFallbackName);
308
38
      SectionName = R->str(UncI->SectionName);
309
38
    } else
310
1.36k
      // Reset this field so it can be queried unconditionally for all symbols.
311
1.36k
      SectionName = "";
312
1.39k
  }
313
314
public:
315
  SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE,
316
            const storage::Uncommon *UncI, const Reader *R)
317
1.33k
      : SymI(SymI), SymE(SymE), UncI(UncI), R(R) {
318
1.33k
    read();
319
1.33k
  }
320
321
1.39k
  void moveNext() {
322
1.39k
    ++SymI;
323
1.39k
    if (Flags & (1 << storage::Symbol::FB_has_uncommon))
324
38
      ++UncI;
325
1.39k
    read();
326
1.39k
  }
327
328
2.06k
  bool operator==(const SymbolRef &Other) const { return SymI == Other.SymI; }
329
};
330
331
0
inline Reader::symbol_range Reader::symbols() const {
332
0
  return {SymbolRef(Symbols.begin(), Symbols.end(), Uncommons.begin(), this),
333
0
          SymbolRef(Symbols.end(), Symbols.end(), nullptr, this)};
334
0
}
335
336
666
inline Reader::symbol_range Reader::module_symbols(unsigned I) const {
337
666
  const storage::Module &M = Modules[I];
338
666
  const storage::Symbol *MBegin = Symbols.begin() + M.Begin,
339
666
                        *MEnd = Symbols.begin() + M.End;
340
666
  return {SymbolRef(MBegin, MEnd, Uncommons.begin() + M.UncBegin, this),
341
666
          SymbolRef(MEnd, MEnd, nullptr, this)};
342
666
}
343
344
/// The contents of the irsymtab in a bitcode file. Any underlying data for the
345
/// irsymtab are owned by Symtab and Strtab.
346
struct FileContents {
347
  SmallVector<char, 0> Symtab, Strtab;
348
  Reader TheReader;
349
};
350
351
/// Reads the contents of a bitcode file, creating its irsymtab if necessary.
352
Expected<FileContents> readBitcode(const BitcodeFileContents &BFC);
353
354
} // end namespace irsymtab
355
} // end namespace llvm
356
357
#endif // LLVM_OBJECT_IRSYMTAB_H