]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/InputFiles.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[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 "lld/Core/LLVM.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/LTO/LTO.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Support/StringSaver.h"
20 #include <memory>
21 #include <set>
22 #include <vector>
23
24 namespace lld {
25 namespace coff {
26
27 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
28 using llvm::COFF::MachineTypes;
29 using llvm::object::Archive;
30 using llvm::object::COFFObjectFile;
31 using llvm::object::COFFSymbolRef;
32 using llvm::object::coff_import_header;
33 using llvm::object::coff_section;
34
35 class Chunk;
36 class Defined;
37 class DefinedImportData;
38 class DefinedImportThunk;
39 class Lazy;
40 class SectionChunk;
41 struct Symbol;
42 class SymbolBody;
43 class Undefined;
44
45 // The root class of input files.
46 class InputFile {
47 public:
48   enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
49   Kind kind() const { return FileKind; }
50   virtual ~InputFile() {}
51
52   // Returns the filename.
53   StringRef getName() { return MB.getBufferIdentifier(); }
54
55   // Reads a file (the constructor doesn't do that).
56   virtual void parse() = 0;
57
58   // Returns the CPU type this file was compiled to.
59   virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
60
61   MemoryBufferRef MB;
62
63   // An archive file name if this file is created from an archive.
64   StringRef ParentName;
65
66   // Returns .drectve section contents if exist.
67   StringRef getDirectives() { return StringRef(Directives).trim(); }
68
69 protected:
70   InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
71
72   std::string Directives;
73
74 private:
75   const Kind FileKind;
76 };
77
78 // .lib or .a file.
79 class ArchiveFile : public InputFile {
80 public:
81   explicit ArchiveFile(MemoryBufferRef M);
82   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
83   void parse() override;
84
85   // Enqueues an archive member load for the given symbol. If we've already
86   // enqueued a load for the same archive member, this function does nothing,
87   // which ensures that we don't load the same member more than once.
88   void addMember(const Archive::Symbol *Sym);
89
90 private:
91   std::unique_ptr<Archive> File;
92   std::string Filename;
93   llvm::DenseSet<uint64_t> Seen;
94 };
95
96 // .obj or .o file. This may be a member of an archive file.
97 class ObjectFile : public InputFile {
98 public:
99   explicit ObjectFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
100   static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
101   void parse() override;
102   MachineTypes getMachineType() override;
103   std::vector<Chunk *> &getChunks() { return Chunks; }
104   std::vector<SectionChunk *> &getDebugChunks() { return DebugChunks; }
105   std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
106
107   // Returns a SymbolBody object for the SymbolIndex'th symbol in the
108   // underlying object file.
109   SymbolBody *getSymbolBody(uint32_t SymbolIndex) {
110     return SparseSymbolBodies[SymbolIndex];
111   }
112
113   // Returns the underying COFF file.
114   COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
115
116   // True if this object file is compatible with SEH.
117   // COFF-specific and x86-only.
118   bool SEHCompat = false;
119
120   // The list of safe exception handlers listed in .sxdata section.
121   // COFF-specific and x86-only.
122   std::set<SymbolBody *> SEHandlers;
123
124 private:
125   void initializeChunks();
126   void initializeSymbols();
127   void initializeSEH();
128
129   SymbolBody *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
130   SymbolBody *createUndefined(COFFSymbolRef Sym);
131
132   std::unique_ptr<COFFObjectFile> COFFObj;
133   llvm::BumpPtrAllocator Alloc;
134   const coff_section *SXData = nullptr;
135
136   // List of all chunks defined by this file. This includes both section
137   // chunks and non-section chunks for common symbols.
138   std::vector<Chunk *> Chunks;
139
140   // CodeView debug info sections.
141   std::vector<SectionChunk *> DebugChunks;
142
143   // This vector contains the same chunks as Chunks, but they are
144   // indexed such that you can get a SectionChunk by section index.
145   // Nonexistent section indices are filled with null pointers.
146   // (Because section number is 1-based, the first slot is always a
147   // null pointer.)
148   std::vector<Chunk *> SparseChunks;
149
150   // List of all symbols referenced or defined by this file.
151   std::vector<SymbolBody *> SymbolBodies;
152
153   // This vector contains the same symbols as SymbolBodies, but they
154   // are indexed such that you can get a SymbolBody by symbol
155   // index. Nonexistent indices (which are occupied by auxiliary
156   // symbols in the real symbol table) are filled with null pointers.
157   std::vector<SymbolBody *> SparseSymbolBodies;
158 };
159
160 // This type represents import library members that contain DLL names
161 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
162 // for details about the format.
163 class ImportFile : public InputFile {
164 public:
165   explicit ImportFile(MemoryBufferRef M)
166       : InputFile(ImportKind, M), StringAlloc(StringAllocAux) {}
167   static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
168
169   DefinedImportData *ImpSym = nullptr;
170   DefinedImportThunk *ThunkSym = nullptr;
171   std::string DLLName;
172
173 private:
174   void parse() override;
175
176   llvm::BumpPtrAllocator StringAllocAux;
177   llvm::StringSaver StringAlloc;
178
179 public:
180   StringRef ExternalName;
181   const coff_import_header *Hdr;
182   Chunk *Location = nullptr;
183 };
184
185 // Used for LTO.
186 class BitcodeFile : public InputFile {
187 public:
188   explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
189   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
190   std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
191   MachineTypes getMachineType() override;
192   std::unique_ptr<llvm::lto::InputFile> Obj;
193
194 private:
195   void parse() override;
196
197   std::vector<SymbolBody *> SymbolBodies;
198 };
199 } // namespace coff
200
201 std::string toString(coff::InputFile *File);
202 } // namespace lld
203
204 #endif