Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/InputFiles.h
Line
Count
Source (jump to first uncovered line)
1
//===- InputFiles.h ---------------------------------------------*- 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
#ifndef LLD_COFF_INPUT_FILES_H
10
#define LLD_COFF_INPUT_FILES_H
11
12
#include "Config.h"
13
#include "lld/Common/LLVM.h"
14
#include "llvm/ADT/ArrayRef.h"
15
#include "llvm/ADT/DenseMap.h"
16
#include "llvm/ADT/DenseSet.h"
17
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
18
#include "llvm/LTO/LTO.h"
19
#include "llvm/Object/Archive.h"
20
#include "llvm/Object/COFF.h"
21
#include "llvm/Support/StringSaver.h"
22
#include <memory>
23
#include <set>
24
#include <vector>
25
26
namespace llvm {
27
namespace pdb {
28
class DbiModuleDescriptorBuilder;
29
}
30
}
31
32
namespace lld {
33
namespace coff {
34
35
std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File);
36
37
using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
38
using llvm::COFF::MachineTypes;
39
using llvm::object::Archive;
40
using llvm::object::COFFObjectFile;
41
using llvm::object::COFFSymbolRef;
42
using llvm::object::coff_import_header;
43
using llvm::object::coff_section;
44
45
class Chunk;
46
class Defined;
47
class DefinedImportData;
48
class DefinedImportThunk;
49
class DefinedRegular;
50
class Lazy;
51
class SectionChunk;
52
class Symbol;
53
class Undefined;
54
class TpiSource;
55
56
// The root class of input files.
57
class InputFile {
58
public:
59
  enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
60
3.50k
  Kind kind() const { return FileKind; }
61
1.03k
  virtual ~InputFile() {}
62
63
  // Returns the filename.
64
1.83k
  StringRef getName() const { return MB.getBufferIdentifier(); }
65
66
  // Reads a file (the constructor doesn't do that).
67
  virtual void parse() = 0;
68
69
  // Returns the CPU type this file was compiled to.
70
201
  virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
71
72
  MemoryBufferRef MB;
73
74
  // An archive file name if this file is created from an archive.
75
  StringRef ParentName;
76
77
  // Returns .drectve section contents if exist.
78
981
  StringRef getDirectives() { return Directives; }
79
80
protected:
81
1.03k
  InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
82
83
  StringRef Directives;
84
85
private:
86
  const Kind FileKind;
87
};
88
89
// .lib or .a file.
90
class ArchiveFile : public InputFile {
91
public:
92
  explicit ArchiveFile(MemoryBufferRef M);
93
0
  static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
94
  void parse() override;
95
96
  // Enqueues an archive member load for the given symbol. If we've already
97
  // enqueued a load for the same archive member, this function does nothing,
98
  // which ensures that we don't load the same member more than once.
99
  void addMember(const Archive::Symbol *Sym);
100
101
private:
102
  std::unique_ptr<Archive> File;
103
  llvm::DenseSet<uint64_t> Seen;
104
};
105
106
// .obj or .o file. This may be a member of an archive file.
107
class ObjFile : public InputFile {
108
public:
109
778
  explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
110
981
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
111
  void parse() override;
112
  MachineTypes getMachineType() override;
113
2.03k
  ArrayRef<Chunk *> getChunks() { return Chunks; }
114
174
  ArrayRef<SectionChunk *> getDebugChunks() { return DebugChunks; }
115
45
  ArrayRef<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
116
8
  ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
117
8
  ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
118
711
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
119
120
  ArrayRef<uint8_t> getDebugSection(StringRef SecName);
121
122
  // Returns a Symbol object for the SymbolIndex'th symbol in the
123
  // underlying object file.
124
3.22k
  Symbol *getSymbol(uint32_t SymbolIndex) {
125
3.22k
    return Symbols[SymbolIndex];
126
3.22k
  }
127
128
  // Returns the underlying COFF file.
129
8.58k
  COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
130
131
  // Add a symbol for a range extension thunk. Return the new symbol table
132
  // index. This index can be used to modify a relocation.
133
51
  uint32_t addRangeThunkSymbol(Symbol *Thunk) {
134
51
    Symbols.push_back(Thunk);
135
51
    return Symbols.size() - 1;
136
51
  }
137
138
  static std::vector<ObjFile *> Instances;
139
140
  // Flags in the absolute @feat.00 symbol if it is present. These usually
141
  // indicate if an object was compiled with certain security features enabled
142
  // like stack guard, safeseh, /guard:cf, or other things.
143
  uint32_t Feat00Flags = 0;
144
145
  // True if this object file is compatible with SEH.  COFF-specific and
146
  // x86-only. COFF spec 5.10.1. The .sxdata section.
147
101
  bool hasSafeSEH() { return Feat00Flags & 0x1; }
148
149
  // True if this file was compiled with /guard:cf.
150
16
  bool hasGuardCF() { return Feat00Flags & 0x800; }
151
152
  // Pointer to the PDB module descriptor builder. Various debug info records
153
  // will reference object files by "module index", which is here. Things like
154
  // source files and section contributions are also recorded here. Will be null
155
  // if we are not producing a PDB.
156
  llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
157
158
  const coff_section *AddrsigSec = nullptr;
159
160
  // When using Microsoft precompiled headers, this is the PCH's key.
161
  // The same key is used by both the precompiled object, and objects using the
162
  // precompiled object. Any difference indicates out-of-date objects.
163
  llvm::Optional<uint32_t> PCHSignature;
164
165
  // Tells whether this file was compiled with /hotpatch
166
  bool HotPatchable = false;
167
168
  // Whether the object was already merged into the final PDB or not
169
  bool MergedIntoPDB = false;
170
171
  // If the OBJ has a .debug$T stream, this tells how it will be handled.
172
  TpiSource *DebugTypesObj = nullptr;
173
174
  // The .debug$T stream if there's one.
175
  llvm::Optional<llvm::codeview::CVTypeArray> DebugTypes;
176
177
private:
178
  const coff_section* getSection(uint32_t I);
179
17
  const coff_section *getSection(COFFSymbolRef Sym) {
180
17
    return getSection(Sym.getSectionNumber());
181
17
  }
182
183
  void initializeChunks();
184
  void initializeSymbols();
185
  void initializeFlags();
186
  void initializeDependencies();
187
188
  SectionChunk *
189
  readSection(uint32_t SectionNumber,
190
              const llvm::object::coff_aux_section_definition *Def,
191
              StringRef LeaderName);
192
193
  void readAssociativeDefinition(
194
      COFFSymbolRef COFFSym,
195
      const llvm::object::coff_aux_section_definition *Def);
196
197
  void readAssociativeDefinition(
198
      COFFSymbolRef COFFSym,
199
      const llvm::object::coff_aux_section_definition *Def,
200
      uint32_t ParentSection);
201
202
  void recordPrevailingSymbolForMingw(
203
      COFFSymbolRef COFFSym,
204
      llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
205
206
  void maybeAssociateSEHForMingw(
207
      COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def,
208
      const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
209
210
  // Given a new symbol Sym with comdat selection Selection, if the new
211
  // symbol is not (yet) Prevailing and the existing comdat leader set to
212
  // Leader, emits a diagnostic if the new symbol and its selection doesn't
213
  // match the existing symbol and its selection. If either old or new
214
  // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
215
  // the existing leader. In that case, Prevailing is set to true.
216
  void handleComdatSelection(COFFSymbolRef Sym,
217
                             llvm::COFF::COMDATType &Selection,
218
                             bool &Prevailing, DefinedRegular *Leader);
219
220
  llvm::Optional<Symbol *>
221
  createDefined(COFFSymbolRef Sym,
222
                std::vector<const llvm::object::coff_aux_section_definition *>
223
                    &ComdatDefs,
224
                bool &PrevailingComdat);
225
  Symbol *createRegular(COFFSymbolRef Sym);
226
  Symbol *createUndefined(COFFSymbolRef Sym);
227
228
  std::unique_ptr<COFFObjectFile> COFFObj;
229
230
  // List of all chunks defined by this file. This includes both section
231
  // chunks and non-section chunks for common symbols.
232
  std::vector<Chunk *> Chunks;
233
234
  // CodeView debug info sections.
235
  std::vector<SectionChunk *> DebugChunks;
236
237
  // Chunks containing symbol table indices of exception handlers. Only used for
238
  // 32-bit x86.
239
  std::vector<SectionChunk *> SXDataChunks;
240
241
  // Chunks containing symbol table indices of address taken symbols and longjmp
242
  // targets.  These are not linked into the final binary when /guard:cf is set.
243
  std::vector<SectionChunk *> GuardFidChunks;
244
  std::vector<SectionChunk *> GuardLJmpChunks;
245
246
  // This vector contains the same chunks as Chunks, but they are
247
  // indexed such that you can get a SectionChunk by section index.
248
  // Nonexistent section indices are filled with null pointers.
249
  // (Because section number is 1-based, the first slot is always a
250
  // null pointer.)
251
  std::vector<SectionChunk *> SparseChunks;
252
253
  // This vector contains a list of all symbols defined or referenced by this
254
  // file. They are indexed such that you can get a Symbol by symbol
255
  // index. Nonexistent indices (which are occupied by auxiliary
256
  // symbols in the real symbol table) are filled with null pointers.
257
  std::vector<Symbol *> Symbols;
258
};
259
260
// This type represents import library members that contain DLL names
261
// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
262
// for details about the format.
263
class ImportFile : public InputFile {
264
public:
265
109
  explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
266
267
201
  static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
268
269
  static std::vector<ImportFile *> Instances;
270
271
  Symbol *ImpSym = nullptr;
272
  Symbol *ThunkSym = nullptr;
273
  std::string DLLName;
274
275
private:
276
  void parse() override;
277
278
public:
279
  StringRef ExternalName;
280
  const coff_import_header *Hdr;
281
  Chunk *Location = nullptr;
282
283
  // We want to eliminate dllimported symbols if no one actually refers them.
284
  // These "Live" bits are used to keep track of which import library members
285
  // are actually in use.
286
  //
287
  // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
288
  // symbols provided by this import library member. We also track whether the
289
  // imported symbol is used separately from whether the thunk is used in order
290
  // to avoid creating unnecessary thunks.
291
  bool Live = !Config->DoGC;
292
  bool ThunkLive = !Config->DoGC;
293
};
294
295
// Used for LTO.
296
class BitcodeFile : public InputFile {
297
public:
298
  BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
299
              uint64_t OffsetInArchive);
300
2.11k
  static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
301
58
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
302
  MachineTypes getMachineType() override;
303
  static std::vector<BitcodeFile *> Instances;
304
  std::unique_ptr<llvm::lto::InputFile> Obj;
305
306
private:
307
  void parse() override;
308
309
  std::vector<Symbol *> Symbols;
310
};
311
} // namespace coff
312
313
std::string toString(const coff::InputFile *File);
314
} // namespace lld
315
316
#endif