]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/InputFiles.h
MFV r339750:
[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<Symbol *> getSymbols() { return Symbols; }
114
115   // Returns a Symbol object for the SymbolIndex'th symbol in the
116   // underlying object file.
117   Symbol *getSymbol(uint32_t SymbolIndex) {
118     return Symbols[SymbolIndex];
119   }
120
121   // Returns the underying COFF file.
122   COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
123
124   static std::vector<ObjFile *> Instances;
125
126   // True if this object file is compatible with SEH.
127   // COFF-specific and x86-only.
128   bool SEHCompat = false;
129
130   // The symbol table indexes of the safe exception handlers.
131   // COFF-specific and x86-only.
132   ArrayRef<llvm::support::ulittle32_t> SXData;
133
134   // Pointer to the PDB module descriptor builder. Various debug info records
135   // will reference object files by "module index", which is here. Things like
136   // source files and section contributions are also recorded here. Will be null
137   // if we are not producing a PDB.
138   llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
139
140 private:
141   void initializeChunks();
142   void initializeSymbols();
143
144   SectionChunk *
145   readSection(uint32_t SectionNumber,
146               const llvm::object::coff_aux_section_definition *Def);
147
148   void readAssociativeDefinition(
149       COFFSymbolRef COFFSym,
150       const llvm::object::coff_aux_section_definition *Def);
151
152   llvm::Optional<Symbol *>
153   createDefined(COFFSymbolRef Sym,
154                 std::vector<const llvm::object::coff_aux_section_definition *>
155                     &ComdatDefs);
156   Symbol *createRegular(COFFSymbolRef Sym);
157   Symbol *createUndefined(COFFSymbolRef Sym);
158
159   std::unique_ptr<COFFObjectFile> COFFObj;
160
161   // List of all chunks defined by this file. This includes both section
162   // chunks and non-section chunks for common symbols.
163   std::vector<Chunk *> Chunks;
164
165   // CodeView debug info sections.
166   std::vector<SectionChunk *> DebugChunks;
167
168   // This vector contains the same chunks as Chunks, but they are
169   // indexed such that you can get a SectionChunk by section index.
170   // Nonexistent section indices are filled with null pointers.
171   // (Because section number is 1-based, the first slot is always a
172   // null pointer.)
173   std::vector<SectionChunk *> SparseChunks;
174
175   // This vector contains a list of all symbols defined or referenced by this
176   // file. They are indexed such that you can get a Symbol by symbol
177   // index. Nonexistent indices (which are occupied by auxiliary
178   // symbols in the real symbol table) are filled with null pointers.
179   std::vector<Symbol *> Symbols;
180 };
181
182 // This type represents import library members that contain DLL names
183 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
184 // for details about the format.
185 class ImportFile : public InputFile {
186 public:
187   explicit ImportFile(MemoryBufferRef M)
188       : InputFile(ImportKind, M), Live(!Config->DoGC) {}
189
190   static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
191
192   static std::vector<ImportFile *> Instances;
193
194   DefinedImportData *ImpSym = nullptr;
195   DefinedImportThunk *ThunkSym = nullptr;
196   std::string DLLName;
197
198 private:
199   void parse() override;
200
201 public:
202   StringRef ExternalName;
203   const coff_import_header *Hdr;
204   Chunk *Location = nullptr;
205
206   // We want to eliminate dllimported symbols if no one actually refers them.
207   // This "Live" bit is used to keep track of which import library members
208   // are actually in use.
209   //
210   // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
211   // symbols provided by this import library member.
212   bool Live;
213 };
214
215 // Used for LTO.
216 class BitcodeFile : public InputFile {
217 public:
218   explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
219   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
220   ArrayRef<Symbol *> getSymbols() { return SymbolBodies; }
221   MachineTypes getMachineType() override;
222   static std::vector<BitcodeFile *> Instances;
223   std::unique_ptr<llvm::lto::InputFile> Obj;
224
225 private:
226   void parse() override;
227
228   std::vector<Symbol *> SymbolBodies;
229 };
230 } // namespace coff
231
232 std::string toString(const coff::InputFile *File);
233 } // namespace lld
234
235 #endif