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