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
13 #include "lld/Core/LLVM.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/LTO/legacy/LTOModule.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Support/StringSaver.h"
28 using llvm::LTOModule;
29 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
30 using llvm::COFF::MachineTypes;
31 using llvm::object::Archive;
32 using llvm::object::COFFObjectFile;
33 using llvm::object::COFFSymbolRef;
34 using llvm::object::coff_import_header;
35 using llvm::object::coff_section;
39 class DefinedImportData;
40 class DefinedImportThunk;
47 // The root class of input files.
50 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
51 Kind kind() const { return FileKind; }
52 virtual ~InputFile() {}
54 // Returns the filename.
55 StringRef getName() { return MB.getBufferIdentifier(); }
57 // Reads a file (the constructor doesn't do that).
58 virtual void parse() = 0;
60 // Returns the CPU type this file was compiled to.
61 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
63 // An archive file name if this file is created from an archive.
66 // Returns .drectve section contents if exist.
67 StringRef getDirectives() { return StringRef(Directives).trim(); }
70 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
73 std::string Directives;
80 class ArchiveFile : public InputFile {
82 explicit ArchiveFile(MemoryBufferRef M);
83 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
84 void parse() override;
86 // Enqueues an archive member load for the given symbol. If we've already
87 // enqueued a load for the same archive member, this function does nothing,
88 // which ensures that we don't load the same member more than once.
89 void addMember(const Archive::Symbol *Sym);
92 std::unique_ptr<Archive> File;
94 llvm::DenseSet<uint64_t> Seen;
97 // .obj or .o file. This may be a member of an archive file.
98 class ObjectFile : public InputFile {
100 explicit ObjectFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
101 static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
102 void parse() override;
103 MachineTypes getMachineType() override;
104 std::vector<Chunk *> &getChunks() { return Chunks; }
105 std::vector<SectionChunk *> &getDebugChunks() { return DebugChunks; }
106 std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
108 // Returns a SymbolBody object for the SymbolIndex'th symbol in the
109 // underlying object file.
110 SymbolBody *getSymbolBody(uint32_t SymbolIndex) {
111 return SparseSymbolBodies[SymbolIndex];
114 // Returns the underying COFF file.
115 COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
117 // True if this object file is compatible with SEH.
118 // COFF-specific and x86-only.
119 bool SEHCompat = false;
121 // The list of safe exception handlers listed in .sxdata section.
122 // COFF-specific and x86-only.
123 std::set<SymbolBody *> SEHandlers;
126 void initializeChunks();
127 void initializeSymbols();
128 void initializeSEH();
130 SymbolBody *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
131 SymbolBody *createUndefined(COFFSymbolRef Sym);
133 std::unique_ptr<COFFObjectFile> COFFObj;
134 llvm::BumpPtrAllocator Alloc;
135 const coff_section *SXData = nullptr;
137 // List of all chunks defined by this file. This includes both section
138 // chunks and non-section chunks for common symbols.
139 std::vector<Chunk *> Chunks;
141 // CodeView debug info sections.
142 std::vector<SectionChunk *> DebugChunks;
144 // This vector contains the same chunks as Chunks, but they are
145 // indexed such that you can get a SectionChunk by section index.
146 // Nonexistent section indices are filled with null pointers.
147 // (Because section number is 1-based, the first slot is always a
149 std::vector<Chunk *> SparseChunks;
151 // List of all symbols referenced or defined by this file.
152 std::vector<SymbolBody *> SymbolBodies;
154 // This vector contains the same symbols as SymbolBodies, but they
155 // are indexed such that you can get a SymbolBody by symbol
156 // index. Nonexistent indices (which are occupied by auxiliary
157 // symbols in the real symbol table) are filled with null pointers.
158 std::vector<SymbolBody *> SparseSymbolBodies;
161 // This type represents import library members that contain DLL names
162 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
163 // for details about the format.
164 class ImportFile : public InputFile {
166 explicit ImportFile(MemoryBufferRef M)
167 : InputFile(ImportKind, M), StringAlloc(StringAllocAux) {}
168 static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
170 DefinedImportData *ImpSym = nullptr;
171 DefinedImportThunk *ThunkSym = nullptr;
175 void parse() override;
177 llvm::BumpPtrAllocator Alloc;
178 llvm::BumpPtrAllocator StringAllocAux;
179 llvm::StringSaver StringAlloc;
182 StringRef ExternalName;
183 const coff_import_header *Hdr;
184 Chunk *Location = nullptr;
188 class BitcodeFile : public InputFile {
190 explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
191 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
192 std::vector<SymbolBody *> &getSymbols() { return SymbolBodies; }
193 MachineTypes getMachineType() override;
194 std::unique_ptr<LTOModule> takeModule() { return std::move(M); }
196 static llvm::LLVMContext Context;
199 void parse() override;
201 std::vector<SymbolBody *> SymbolBodies;
202 llvm::BumpPtrAllocator Alloc;
203 std::unique_ptr<LTOModule> M;
207 std::string toString(coff::InputFile *File);