xref: /llvm-project-15.0.7/lld/COFF/InputFiles.h (revision f2a8bccf)
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 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
35 using llvm::COFF::MachineTypes;
36 using llvm::object::Archive;
37 using llvm::object::COFFObjectFile;
38 using llvm::object::COFFSymbolRef;
39 using llvm::object::coff_import_header;
40 using llvm::object::coff_section;
41 
42 class Chunk;
43 class Defined;
44 class DefinedImportData;
45 class DefinedImportThunk;
46 class Lazy;
47 class SectionChunk;
48 struct Symbol;
49 class SymbolBody;
50 class Undefined;
51 
52 // The root class of input files.
53 class InputFile {
54 public:
55   enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
56   Kind kind() const { return FileKind; }
57   virtual ~InputFile() {}
58 
59   // Returns the filename.
60   StringRef getName() { return MB.getBufferIdentifier(); }
61 
62   // Reads a file (the constructor doesn't do that).
63   virtual void parse() = 0;
64 
65   // Returns the CPU type this file was compiled to.
66   virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
67 
68   MemoryBufferRef MB;
69 
70   // An archive file name if this file is created from an archive.
71   StringRef ParentName;
72 
73   // Returns .drectve section contents if exist.
74   StringRef getDirectives() { return StringRef(Directives).trim(); }
75 
76 protected:
77   InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
78 
79   std::string Directives;
80 
81 private:
82   const Kind FileKind;
83 };
84 
85 // .lib or .a file.
86 class ArchiveFile : public InputFile {
87 public:
88   explicit ArchiveFile(MemoryBufferRef M);
89   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
90   void parse() override;
91 
92   // Enqueues an archive member load for the given symbol. If we've already
93   // enqueued a load for the same archive member, this function does nothing,
94   // which ensures that we don't load the same member more than once.
95   void addMember(const Archive::Symbol *Sym);
96 
97 private:
98   std::unique_ptr<Archive> File;
99   std::string Filename;
100   llvm::DenseSet<uint64_t> Seen;
101 };
102 
103 // .obj or .o file. This may be a member of an archive file.
104 class ObjectFile : public InputFile {
105 public:
106   explicit ObjectFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
107   static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
108   void parse() override;
109   MachineTypes getMachineType() override;
110   std::vector<Chunk *> &getChunks() { return Chunks; }
111   std::vector<SectionChunk *> &getDebugChunks() { return DebugChunks; }
112   std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
113 
114   // Returns a SymbolBody object for the SymbolIndex'th symbol in the
115   // underlying object file.
116   SymbolBody *getSymbolBody(uint32_t SymbolIndex) {
117     return SparseSymbolBodies[SymbolIndex];
118   }
119 
120   // Returns the underying COFF file.
121   COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
122 
123   // True if this object file is compatible with SEH.
124   // COFF-specific and x86-only.
125   bool SEHCompat = false;
126 
127   // The list of safe exception handlers listed in .sxdata section.
128   // COFF-specific and x86-only.
129   std::set<SymbolBody *> SEHandlers;
130 
131   // Pointer to the PDB module descriptor builder. Various debug info records
132   // will reference object files by "module index", which is here. Things like
133   // source files and section contributions are also recorded here. Will be null
134   // if we are not producing a PDB.
135   llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
136 
137 private:
138   void initializeChunks();
139   void initializeSymbols();
140   void initializeSEH();
141 
142   SymbolBody *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
143   SymbolBody *createUndefined(COFFSymbolRef Sym);
144 
145   std::unique_ptr<COFFObjectFile> COFFObj;
146   const coff_section *SXData = nullptr;
147 
148   // List of all chunks defined by this file. This includes both section
149   // chunks and non-section chunks for common symbols.
150   std::vector<Chunk *> Chunks;
151 
152   // CodeView debug info sections.
153   std::vector<SectionChunk *> DebugChunks;
154 
155   // This vector contains the same chunks as Chunks, but they are
156   // indexed such that you can get a SectionChunk by section index.
157   // Nonexistent section indices are filled with null pointers.
158   // (Because section number is 1-based, the first slot is always a
159   // null pointer.)
160   std::vector<Chunk *> SparseChunks;
161 
162   // List of all symbols referenced or defined by this file.
163   std::vector<SymbolBody *> SymbolBodies;
164 
165   // This vector contains the same symbols as SymbolBodies, but they
166   // are indexed such that you can get a SymbolBody by symbol
167   // index. Nonexistent indices (which are occupied by auxiliary
168   // symbols in the real symbol table) are filled with null pointers.
169   std::vector<SymbolBody *> SparseSymbolBodies;
170 };
171 
172 // This type represents import library members that contain DLL names
173 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
174 // for details about the format.
175 class ImportFile : public InputFile {
176 public:
177   explicit ImportFile(MemoryBufferRef M)
178       : InputFile(ImportKind, M), Live(!Config->DoGC) {}
179 
180   static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
181 
182   DefinedImportData *ImpSym = nullptr;
183   DefinedImportData *ConstSym = nullptr;
184   DefinedImportThunk *ThunkSym = nullptr;
185   std::string DLLName;
186 
187 private:
188   void parse() override;
189 
190 public:
191   StringRef ExternalName;
192   const coff_import_header *Hdr;
193   Chunk *Location = nullptr;
194 
195   // We want to eliminate dllimported symbols if no one actually refers them.
196   // This "Live" bit is used to keep track of which import library members
197   // are actually in use.
198   //
199   // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
200   // symbols provided by this import library member.
201   bool Live;
202 };
203 
204 // Used for LTO.
205 class BitcodeFile : public InputFile {
206 public:
207   explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
208   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
209   std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
210   MachineTypes getMachineType() override;
211   std::unique_ptr<llvm::lto::InputFile> Obj;
212 
213 private:
214   void parse() override;
215 
216   std::vector<SymbolBody *> SymbolBodies;
217 };
218 } // namespace coff
219 
220 std::string toString(coff::InputFile *File);
221 } // namespace lld
222 
223 #endif
224