Coverage Report

Created: 2019-07-24 05:18

/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.72k
  Kind kind() const { return fileKind; }
61
1.11k
  virtual ~InputFile() {}
62
63
  // Returns the filename.
64
1.98k
  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
206
  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
1.04k
  StringRef getDirectives() { return directives; }
79
80
protected:
81
1.11k
  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
828
  explicit ObjFile(MemoryBufferRef m) : InputFile(ObjectKind, m) {}
110
1.04k
  static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
111
  void parse() override;
112
  MachineTypes getMachineType() override;
113
2.18k
  ArrayRef<Chunk *> getChunks() { return chunks; }
114
204
  ArrayRef<SectionChunk *> getDebugChunks() { return debugChunks; }
115
50
  ArrayRef<SectionChunk *> getSXDataChunks() { return sXDataChunks; }
116
8
  ArrayRef<SectionChunk *> getGuardFidChunks() { return guardFidChunks; }
117
8
  ArrayRef<SectionChunk *> getGuardLJmpChunks() { return guardLJmpChunks; }
118
771
  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.36k
  Symbol *getSymbol(uint32_t symbolIndex) {
125
3.36k
    return symbols[symbolIndex];
126
3.36k
  }
127
128
  // Returns the underlying COFF file.
129
9.10k
  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
50
  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
  // Whether this is an object file created from .res files.
166
  bool isResourceObjFile = false;
167
168
  // Whether this file was compiled with /hotpatch.
169
  bool hotPatchable = false;
170
171
  // Whether the object was already merged into the final PDB.
172
  bool mergedIntoPDB = false;
173
174
  // If the OBJ has a .debug$T stream, this tells how it will be handled.
175
  TpiSource *debugTypesObj = nullptr;
176
177
  // The .debug$T stream if there's one.
178
  llvm::Optional<llvm::codeview::CVTypeArray> debugTypes;
179
180
private:
181
  const coff_section* getSection(uint32_t i);
182
17
  const coff_section *getSection(COFFSymbolRef sym) {
183
17
    return getSection(sym.getSectionNumber());
184
17
  }
185
186
  void initializeChunks();
187
  void initializeSymbols();
188
  void initializeFlags();
189
  void initializeDependencies();
190
191
  SectionChunk *
192
  readSection(uint32_t sectionNumber,
193
              const llvm::object::coff_aux_section_definition *def,
194
              StringRef leaderName);
195
196
  void readAssociativeDefinition(
197
      COFFSymbolRef coffSym,
198
      const llvm::object::coff_aux_section_definition *def);
199
200
  void readAssociativeDefinition(
201
      COFFSymbolRef coffSym,
202
      const llvm::object::coff_aux_section_definition *def,
203
      uint32_t parentSection);
204
205
  void recordPrevailingSymbolForMingw(
206
      COFFSymbolRef coffSym,
207
      llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
208
209
  void maybeAssociateSEHForMingw(
210
      COFFSymbolRef sym, const llvm::object::coff_aux_section_definition *def,
211
      const llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
212
213
  // Given a new symbol Sym with comdat selection Selection, if the new
214
  // symbol is not (yet) Prevailing and the existing comdat leader set to
215
  // Leader, emits a diagnostic if the new symbol and its selection doesn't
216
  // match the existing symbol and its selection. If either old or new
217
  // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
218
  // the existing leader. In that case, Prevailing is set to true.
219
  void handleComdatSelection(COFFSymbolRef sym,
220
                             llvm::COFF::COMDATType &selection,
221
                             bool &prevailing, DefinedRegular *leader);
222
223
  llvm::Optional<Symbol *>
224
  createDefined(COFFSymbolRef sym,
225
                std::vector<const llvm::object::coff_aux_section_definition *>
226
                    &comdatDefs,
227
                bool &prevailingComdat);
228
  Symbol *createRegular(COFFSymbolRef sym);
229
  Symbol *createUndefined(COFFSymbolRef sym);
230
231
  std::unique_ptr<COFFObjectFile> coffObj;
232
233
  // List of all chunks defined by this file. This includes both section
234
  // chunks and non-section chunks for common symbols.
235
  std::vector<Chunk *> chunks;
236
237
  // CodeView debug info sections.
238
  std::vector<SectionChunk *> debugChunks;
239
240
  // Chunks containing symbol table indices of exception handlers. Only used for
241
  // 32-bit x86.
242
  std::vector<SectionChunk *> sXDataChunks;
243
244
  // Chunks containing symbol table indices of address taken symbols and longjmp
245
  // targets.  These are not linked into the final binary when /guard:cf is set.
246
  std::vector<SectionChunk *> guardFidChunks;
247
  std::vector<SectionChunk *> guardLJmpChunks;
248
249
  // This vector contains the same chunks as Chunks, but they are
250
  // indexed such that you can get a SectionChunk by section index.
251
  // Nonexistent section indices are filled with null pointers.
252
  // (Because section number is 1-based, the first slot is always a
253
  // null pointer.)
254
  std::vector<SectionChunk *> sparseChunks;
255
256
  // This vector contains a list of all symbols defined or referenced by this
257
  // file. They are indexed such that you can get a Symbol by symbol
258
  // index. Nonexistent indices (which are occupied by auxiliary
259
  // symbols in the real symbol table) are filled with null pointers.
260
  std::vector<Symbol *> symbols;
261
};
262
263
// This type represents import library members that contain DLL names
264
// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
265
// for details about the format.
266
class ImportFile : public InputFile {
267
public:
268
109
  explicit ImportFile(MemoryBufferRef m) : InputFile(ImportKind, m) {}
269
270
206
  static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
271
272
  static std::vector<ImportFile *> instances;
273
274
  Symbol *impSym = nullptr;
275
  Symbol *thunkSym = nullptr;
276
  std::string dllName;
277
278
private:
279
  void parse() override;
280
281
public:
282
  StringRef externalName;
283
  const coff_import_header *hdr;
284
  Chunk *location = nullptr;
285
286
  // We want to eliminate dllimported symbols if no one actually refers them.
287
  // These "Live" bits are used to keep track of which import library members
288
  // are actually in use.
289
  //
290
  // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
291
  // symbols provided by this import library member. We also track whether the
292
  // imported symbol is used separately from whether the thunk is used in order
293
  // to avoid creating unnecessary thunks.
294
  bool live = !config->doGC;
295
  bool thunkLive = !config->doGC;
296
};
297
298
// Used for LTO.
299
class BitcodeFile : public InputFile {
300
public:
301
  BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
302
              uint64_t offsetInArchive);
303
2.23k
  static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
304
74
  ArrayRef<Symbol *> getSymbols() { return symbols; }
305
  MachineTypes getMachineType() override;
306
  static std::vector<BitcodeFile *> instances;
307
  std::unique_ptr<llvm::lto::InputFile> obj;
308
309
private:
310
  void parse() override;
311
312
  std::vector<Symbol *> symbols;
313
};
314
315
std::string replaceThinLTOSuffix(StringRef path);
316
} // namespace coff
317
318
std::string toString(const coff::InputFile *file);
319
} // namespace lld
320
321
#endif