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