Coverage Report

Created: 2018-08-19 21:11

/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.39k
  Kind kind() const { return FileKind; }
59
715
  virtual ~InputFile() {}
60
61
  // Returns the filename.
62
1.13k
  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
145
  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
664
  StringRef getDirectives() { return StringRef(Directives).trim(); }
77
78
protected:
79
715
  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
518
  explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
109
664
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
110
  void parse() override;
111
  MachineTypes getMachineType() override;
112
1.36k
  ArrayRef<Chunk *> getChunks() { return Chunks; }
113
212
  ArrayRef<SectionChunk *> getDebugChunks() { return DebugChunks; }
114
29
  ArrayRef<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
115
8
  ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
116
8
  ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
117
108
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
118
119
  // Returns a Symbol object for the SymbolIndex'th symbol in the
120
  // underlying object file.
121
1.94k
  Symbol *getSymbol(uint32_t SymbolIndex) {
122
1.94k
    return Symbols[SymbolIndex];
123
1.94k
  }
124
125
  // Returns the underying COFF file.
126
5.59k
  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
82
  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
private:
149
  void initializeChunks();
150
  void initializeSymbols();
151
152
  SectionChunk *
153
  readSection(uint32_t SectionNumber,
154
              const llvm::object::coff_aux_section_definition *Def,
155
              StringRef LeaderName);
156
157
  void readAssociativeDefinition(
158
      COFFSymbolRef COFFSym,
159
      const llvm::object::coff_aux_section_definition *Def);
160
161
  void readAssociativeDefinition(
162
      COFFSymbolRef COFFSym,
163
      const llvm::object::coff_aux_section_definition *Def,
164
      uint32_t ParentSection);
165
166
  void recordPrevailingSymbolForMingw(
167
      COFFSymbolRef COFFSym,
168
      llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
169
170
  void maybeAssociateSEHForMingw(
171
      COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def,
172
      const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
173
174
  llvm::Optional<Symbol *>
175
  createDefined(COFFSymbolRef Sym,
176
                std::vector<const llvm::object::coff_aux_section_definition *>
177
                    &ComdatDefs,
178
                bool &PrevailingComdat);
179
  Symbol *createRegular(COFFSymbolRef Sym);
180
  Symbol *createUndefined(COFFSymbolRef Sym);
181
182
  std::unique_ptr<COFFObjectFile> COFFObj;
183
184
  // List of all chunks defined by this file. This includes both section
185
  // chunks and non-section chunks for common symbols.
186
  std::vector<Chunk *> Chunks;
187
188
  // CodeView debug info sections.
189
  std::vector<SectionChunk *> DebugChunks;
190
191
  // Chunks containing symbol table indices of exception handlers. Only used for
192
  // 32-bit x86.
193
  std::vector<SectionChunk *> SXDataChunks;
194
195
  // Chunks containing symbol table indices of address taken symbols and longjmp
196
  // targets.  These are not linked into the final binary when /guard:cf is set.
197
  std::vector<SectionChunk *> GuardFidChunks;
198
  std::vector<SectionChunk *> GuardLJmpChunks;
199
200
  // This vector contains the same chunks as Chunks, but they are
201
  // indexed such that you can get a SectionChunk by section index.
202
  // Nonexistent section indices are filled with null pointers.
203
  // (Because section number is 1-based, the first slot is always a
204
  // null pointer.)
205
  std::vector<SectionChunk *> SparseChunks;
206
207
  // This vector contains a list of all symbols defined or referenced by this
208
  // file. They are indexed such that you can get a Symbol by symbol
209
  // index. Nonexistent indices (which are occupied by auxiliary
210
  // symbols in the real symbol table) are filled with null pointers.
211
  std::vector<Symbol *> Symbols;
212
};
213
214
// This type represents import library members that contain DLL names
215
// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
216
// for details about the format.
217
class ImportFile : public InputFile {
218
public:
219
80
  explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
220
221
145
  static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
222
223
  static std::vector<ImportFile *> Instances;
224
225
  Symbol *ImpSym = nullptr;
226
  Symbol *ThunkSym = nullptr;
227
  std::string DLLName;
228
229
private:
230
  void parse() override;
231
232
public:
233
  StringRef ExternalName;
234
  const coff_import_header *Hdr;
235
  Chunk *Location = nullptr;
236
237
  // We want to eliminate dllimported symbols if no one actually refers them.
238
  // These "Live" bits are used to keep track of which import library members
239
  // are actually in use.
240
  //
241
  // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
242
  // symbols provided by this import library member. We also track whether the
243
  // imported symbol is used separately from whether the thunk is used in order
244
  // to avoid creating unnecessary thunks.
245
  bool Live = !Config->DoGC;
246
  bool ThunkLive = !Config->DoGC;
247
};
248
249
// Used for LTO.
250
class BitcodeFile : public InputFile {
251
public:
252
52
  explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
253
1.58k
  static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
254
52
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
255
  MachineTypes getMachineType() override;
256
  static std::vector<BitcodeFile *> Instances;
257
  std::unique_ptr<llvm::lto::InputFile> Obj;
258
259
private:
260
  void parse() override;
261
262
  std::vector<Symbol *> Symbols;
263
};
264
} // namespace coff
265
266
std::string toString(const coff::InputFile *File);
267
} // namespace lld
268
269
#endif