1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_COFF_INPUT_FILES_H
11 #define LLD_COFF_INPUT_FILES_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"
27 class DbiModuleDescriptorBuilder;
34 std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File);
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;
46 class DefinedImportData;
47 class DefinedImportThunk;
53 // The root class of input files.
56 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
57 Kind kind() const { return FileKind; }
58 virtual ~InputFile() {}
60 // Returns the filename.
61 StringRef getName() const { return MB.getBufferIdentifier(); }
63 // Reads a file (the constructor doesn't do that).
64 virtual void parse() = 0;
66 // Returns the CPU type this file was compiled to.
67 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
71 // An archive file name if this file is created from an archive.
74 // Returns .drectve section contents if exist.
75 StringRef getDirectives() { return StringRef(Directives).trim(); }
78 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
80 std::string Directives;
87 class ArchiveFile : public InputFile {
89 explicit ArchiveFile(MemoryBufferRef M);
90 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
91 void parse() override;
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);
99 std::unique_ptr<Archive> File;
100 std::string Filename;
101 llvm::DenseSet<uint64_t> Seen;
104 // .obj or .o file. This may be a member of an archive file.
105 class ObjFile : public InputFile {
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; }
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];
121 // Returns the underying COFF file.
122 COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
124 static std::vector<ObjFile *> Instances;
126 // True if this object file is compatible with SEH.
127 // COFF-specific and x86-only.
128 bool SEHCompat = false;
130 // The symbol table indexes of the safe exception handlers.
131 // COFF-specific and x86-only.
132 ArrayRef<llvm::support::ulittle32_t> SXData;
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;
141 void initializeChunks();
142 void initializeSymbols();
145 readSection(uint32_t SectionNumber,
146 const llvm::object::coff_aux_section_definition *Def);
148 void readAssociativeDefinition(
149 COFFSymbolRef COFFSym,
150 const llvm::object::coff_aux_section_definition *Def);
152 llvm::Optional<Symbol *>
153 createDefined(COFFSymbolRef Sym,
154 std::vector<const llvm::object::coff_aux_section_definition *>
156 Symbol *createRegular(COFFSymbolRef Sym);
157 Symbol *createUndefined(COFFSymbolRef Sym);
159 std::unique_ptr<COFFObjectFile> COFFObj;
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;
165 // CodeView debug info sections.
166 std::vector<SectionChunk *> DebugChunks;
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
173 std::vector<SectionChunk *> SparseChunks;
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;
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 {
187 explicit ImportFile(MemoryBufferRef M)
188 : InputFile(ImportKind, M), Live(!Config->DoGC) {}
190 static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
192 static std::vector<ImportFile *> Instances;
194 DefinedImportData *ImpSym = nullptr;
195 DefinedImportThunk *ThunkSym = nullptr;
199 void parse() override;
202 StringRef ExternalName;
203 const coff_import_header *Hdr;
204 Chunk *Location = nullptr;
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.
210 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
211 // symbols provided by this import library member.
216 class BitcodeFile : public InputFile {
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;
226 void parse() override;
228 std::vector<Symbol *> SymbolBodies;
232 std::string toString(const coff::InputFile *File);