Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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/Core/LLVM.h"
15
#include "llvm/ADT/ArrayRef.h"
16
#include "llvm/ADT/DenseSet.h"
17
#include "llvm/LTO/LTO.h"
18
#include "llvm/Object/Archive.h"
19
#include "llvm/Object/COFF.h"
20
#include "llvm/Support/StringSaver.h"
21
#include <memory>
22
#include <set>
23
#include <vector>
24
25
namespace llvm {
26
namespace pdb {
27
class DbiModuleDescriptorBuilder;
28
}
29
}
30
31
namespace lld {
32
namespace coff {
33
34
std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File);
35
36
using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
37
using llvm::COFF::MachineTypes;
38
using llvm::object::Archive;
39
using llvm::object::COFFObjectFile;
40
using llvm::object::COFFSymbolRef;
41
using llvm::object::coff_import_header;
42
using llvm::object::coff_section;
43
44
class Chunk;
45
class Defined;
46
class DefinedImportData;
47
class DefinedImportThunk;
48
class Lazy;
49
class SectionChunk;
50
struct Symbol;
51
class SymbolBody;
52
class Undefined;
53
54
// The root class of input files.
55
class InputFile {
56
public:
57
  enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
58
1.48k
  Kind kind() const { return FileKind; }
59
488
  virtual ~InputFile() {}
60
61
  // Returns the filename.
62
1.07k
  StringRef getName() { 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
121
  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
445
  StringRef getDirectives() { return StringRef(Directives).trim(); }
77
78
protected:
79
488
  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
318
  explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
109
445
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
110
  void parse() override;
111
  MachineTypes getMachineType() override;
112
923
  std::vector<Chunk *> &getChunks() { return Chunks; }
113
110
  std::vector<SectionChunk *> &getDebugChunks() { return DebugChunks; }
114
67
  std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
115
116
  // Returns a SymbolBody object for the SymbolIndex'th symbol in the
117
  // underlying object file.
118
1.13k
  SymbolBody *getSymbolBody(uint32_t SymbolIndex) {
119
1.13k
    return SparseSymbolBodies[SymbolIndex];
120
1.13k
  }
121
122
  // Returns the underying COFF file.
123
2.89k
  COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
124
125
  static std::vector<ObjFile *> Instances;
126
127
  // True if this object file is compatible with SEH.
128
  // COFF-specific and x86-only.
129
  bool SEHCompat = false;
130
131
  // The list of safe exception handlers listed in .sxdata section.
132
  // COFF-specific and x86-only.
133
  std::set<SymbolBody *> SEHandlers;
134
135
  // Pointer to the PDB module descriptor builder. Various debug info records
136
  // will reference object files by "module index", which is here. Things like
137
  // source files and section contributions are also recorded here. Will be null
138
  // if we are not producing a PDB.
139
  llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
140
141
private:
142
  void initializeChunks();
143
  void initializeSymbols();
144
  void initializeSEH();
145
146
  SymbolBody *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
147
  SymbolBody *createUndefined(COFFSymbolRef Sym);
148
149
  std::unique_ptr<COFFObjectFile> COFFObj;
150
  const coff_section *SXData = nullptr;
151
152
  // List of all chunks defined by this file. This includes both section
153
  // chunks and non-section chunks for common symbols.
154
  std::vector<Chunk *> Chunks;
155
156
  // CodeView debug info sections.
157
  std::vector<SectionChunk *> DebugChunks;
158
159
  // This vector contains the same chunks as Chunks, but they are
160
  // indexed such that you can get a SectionChunk by section index.
161
  // Nonexistent section indices are filled with null pointers.
162
  // (Because section number is 1-based, the first slot is always a
163
  // null pointer.)
164
  std::vector<Chunk *> SparseChunks;
165
166
  // List of all symbols referenced or defined by this file.
167
  std::vector<SymbolBody *> SymbolBodies;
168
169
  // This vector contains the same symbols as SymbolBodies, but they
170
  // are indexed such that you can get a SymbolBody by symbol
171
  // index. Nonexistent indices (which are occupied by auxiliary
172
  // symbols in the real symbol table) are filled with null pointers.
173
  std::vector<SymbolBody *> SparseSymbolBodies;
174
};
175
176
// This type represents import library members that contain DLL names
177
// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
178
// for details about the format.
179
class ImportFile : public InputFile {
180
public:
181
  explicit ImportFile(MemoryBufferRef M)
182
66
      : InputFile(ImportKind, M), Live(!Config->DoGC) {}
183
184
121
  static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
185
186
  static std::vector<ImportFile *> Instances;
187
188
  DefinedImportData *ImpSym = nullptr;
189
  DefinedImportThunk *ThunkSym = nullptr;
190
  std::string DLLName;
191
192
private:
193
  void parse() override;
194
195
public:
196
  StringRef ExternalName;
197
  const coff_import_header *Hdr;
198
  Chunk *Location = nullptr;
199
200
  // We want to eliminate dllimported symbols if no one actually refers them.
201
  // This "Live" bit is used to keep track of which import library members
202
  // are actually in use.
203
  //
204
  // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
205
  // symbols provided by this import library member.
206
  bool Live;
207
};
208
209
// Used for LTO.
210
class BitcodeFile : public InputFile {
211
public:
212
49
  explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
213
916
  static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
214
49
  std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
215
  MachineTypes getMachineType() override;
216
  static std::vector<BitcodeFile *> Instances;
217
  std::unique_ptr<llvm::lto::InputFile> Obj;
218
219
private:
220
  void parse() override;
221
222
  std::vector<SymbolBody *> SymbolBodies;
223
};
224
} // namespace coff
225
226
std::string toString(coff::InputFile *File);
227
} // namespace lld
228
229
#endif