Coverage Report

Created: 2018-10-20 06:24

/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
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#ifndef LLD_COFF_INPUT_FILES_H
11
#define LLD_COFF_INPUT_FILES_H
12
13
#include "Config.h"
14
#include "lld/Common/LLVM.h"
15
#include "llvm/ADT/ArrayRef.h"
16
#include "llvm/ADT/DenseMap.h"
17
#include "llvm/ADT/DenseSet.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 Lazy;
50
class SectionChunk;
51
class Symbol;
52
class Undefined;
53
54
// The root class of input files.
55
class InputFile {
56
public:
57
  enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
58
2.88k
  Kind kind() const { return FileKind; }
59
886
  virtual ~InputFile() {}
60
61
  // Returns the filename.
62
1.46k
  StringRef getName() const { return MB.getBufferIdentifier(); }
63
64
  // Reads a file (the constructor doesn't do that).
65
  virtual void parse() = 0;
66
67
  // Returns the CPU type this file was compiled to.
68
183
  virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
69
70
  MemoryBufferRef MB;
71
72
  // An archive file name if this file is created from an archive.
73
  StringRef ParentName;
74
75
  // Returns .drectve section contents if exist.
76
835
  StringRef getDirectives() { return StringRef(Directives).trim(); }
77
78
protected:
79
886
  InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
80
81
  std::string Directives;
82
83
private:
84
  const Kind FileKind;
85
};
86
87
// .lib or .a file.
88
class ArchiveFile : public InputFile {
89
public:
90
  explicit ArchiveFile(MemoryBufferRef M);
91
0
  static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
92
  void parse() override;
93
94
  // Enqueues an archive member load for the given symbol. If we've already
95
  // enqueued a load for the same archive member, this function does nothing,
96
  // which ensures that we don't load the same member more than once.
97
  void addMember(const Archive::Symbol *Sym);
98
99
private:
100
  std::unique_ptr<Archive> File;
101
  std::string Filename;
102
  llvm::DenseSet<uint64_t> Seen;
103
};
104
105
// .obj or .o file. This may be a member of an archive file.
106
class ObjFile : public InputFile {
107
public:
108
649
  explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
109
835
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
110
  void parse() override;
111
  MachineTypes getMachineType() override;
112
1.72k
  ArrayRef<Chunk *> getChunks() { return Chunks; }
113
249
  ArrayRef<SectionChunk *> getDebugChunks() { return DebugChunks; }
114
41
  ArrayRef<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
115
8
  ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
116
8
  ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
117
570
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
118
119
  // Returns a Symbol object for the SymbolIndex'th symbol in the
120
  // underlying object file.
121
2.31k
  Symbol *getSymbol(uint32_t SymbolIndex) {
122
2.31k
    return Symbols[SymbolIndex];
123
2.31k
  }
124
125
  // Returns the underying COFF file.
126
7.09k
  COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
127
128
  static std::vector<ObjFile *> Instances;
129
130
  // Flags in the absolute @feat.00 symbol if it is present. These usually
131
  // indicate if an object was compiled with certain security features enabled
132
  // like stack guard, safeseh, /guard:cf, or other things.
133
  uint32_t Feat00Flags = 0;
134
135
  // True if this object file is compatible with SEH.  COFF-specific and
136
  // x86-only. COFF spec 5.10.1. The .sxdata section.
137
96
  bool hasSafeSEH() { return Feat00Flags & 0x1; }
138
139
  // True if this file was compiled with /guard:cf.
140
14
  bool hasGuardCF() { return Feat00Flags & 0x800; }
141
142
  // Pointer to the PDB module descriptor builder. Various debug info records
143
  // will reference object files by "module index", which is here. Things like
144
  // source files and section contributions are also recorded here. Will be null
145
  // if we are not producing a PDB.
146
  llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
147
148
  const coff_section *AddrsigSec = nullptr;
149
150
private:
151
  void initializeChunks();
152
  void initializeSymbols();
153
154
  SectionChunk *
155
  readSection(uint32_t SectionNumber,
156
              const llvm::object::coff_aux_section_definition *Def,
157
              StringRef LeaderName);
158
159
  void readAssociativeDefinition(
160
      COFFSymbolRef COFFSym,
161
      const llvm::object::coff_aux_section_definition *Def);
162
163
  void readAssociativeDefinition(
164
      COFFSymbolRef COFFSym,
165
      const llvm::object::coff_aux_section_definition *Def,
166
      uint32_t ParentSection);
167
168
  void recordPrevailingSymbolForMingw(
169
      COFFSymbolRef COFFSym,
170
      llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
171
172
  void maybeAssociateSEHForMingw(
173
      COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def,
174
      const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
175
176
  llvm::Optional<Symbol *>
177
  createDefined(COFFSymbolRef Sym,
178
                std::vector<const llvm::object::coff_aux_section_definition *>
179
                    &ComdatDefs,
180
                bool &PrevailingComdat);
181
  Symbol *createRegular(COFFSymbolRef Sym);
182
  Symbol *createUndefined(COFFSymbolRef Sym);
183
184
  std::unique_ptr<COFFObjectFile> COFFObj;
185
186
  // List of all chunks defined by this file. This includes both section
187
  // chunks and non-section chunks for common symbols.
188
  std::vector<Chunk *> Chunks;
189
190
  // CodeView debug info sections.
191
  std::vector<SectionChunk *> DebugChunks;
192
193
  // Chunks containing symbol table indices of exception handlers. Only used for
194
  // 32-bit x86.
195
  std::vector<SectionChunk *> SXDataChunks;
196
197
  // Chunks containing symbol table indices of address taken symbols and longjmp
198
  // targets.  These are not linked into the final binary when /guard:cf is set.
199
  std::vector<SectionChunk *> GuardFidChunks;
200
  std::vector<SectionChunk *> GuardLJmpChunks;
201
202
  // This vector contains the same chunks as Chunks, but they are
203
  // indexed such that you can get a SectionChunk by section index.
204
  // Nonexistent section indices are filled with null pointers.
205
  // (Because section number is 1-based, the first slot is always a
206
  // null pointer.)
207
  std::vector<SectionChunk *> SparseChunks;
208
209
  // This vector contains a list of all symbols defined or referenced by this
210
  // file. They are indexed such that you can get a Symbol by symbol
211
  // index. Nonexistent indices (which are occupied by auxiliary
212
  // symbols in the real symbol table) are filled with null pointers.
213
  std::vector<Symbol *> Symbols;
214
};
215
216
// This type represents import library members that contain DLL names
217
// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
218
// for details about the format.
219
class ImportFile : public InputFile {
220
public:
221
99
  explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
222
223
183
  static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
224
225
  static std::vector<ImportFile *> Instances;
226
227
  Symbol *ImpSym = nullptr;
228
  Symbol *ThunkSym = nullptr;
229
  std::string DLLName;
230
231
private:
232
  void parse() override;
233
234
public:
235
  StringRef ExternalName;
236
  const coff_import_header *Hdr;
237
  Chunk *Location = nullptr;
238
239
  // We want to eliminate dllimported symbols if no one actually refers them.
240
  // These "Live" bits are used to keep track of which import library members
241
  // are actually in use.
242
  //
243
  // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
244
  // symbols provided by this import library member. We also track whether the
245
  // imported symbol is used separately from whether the thunk is used in order
246
  // to avoid creating unnecessary thunks.
247
  bool Live = !Config->DoGC;
248
  bool ThunkLive = !Config->DoGC;
249
};
250
251
// Used for LTO.
252
class BitcodeFile : public InputFile {
253
public:
254
54
  explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
255
1.86k
  static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
256
52
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
257
  MachineTypes getMachineType() override;
258
  static std::vector<BitcodeFile *> Instances;
259
  std::unique_ptr<llvm::lto::InputFile> Obj;
260
261
private:
262
  void parse() override;
263
264
  std::vector<Symbol *> Symbols;
265
};
266
} // namespace coff
267
268
std::string toString(const coff::InputFile *File);
269
} // namespace lld
270
271
#endif