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/LTO/LTO.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Support/StringSaver.h"
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;
37 class DefinedImportData;
38 class DefinedImportThunk;
45 // The root class of input files.
48 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
49 Kind kind() const { return FileKind; }
50 virtual ~InputFile() {}
52 // Returns the filename.
53 StringRef getName() { return MB.getBufferIdentifier(); }
55 // Reads a file (the constructor doesn't do that).
56 virtual void parse() = 0;
58 // Returns the CPU type this file was compiled to.
59 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) {}
72 std::string Directives;
79 class ArchiveFile : public InputFile {
81 explicit ArchiveFile(MemoryBufferRef M);
82 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
83 void parse() override;
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);
91 std::unique_ptr<Archive> File;
93 llvm::DenseSet<uint64_t> Seen;
96 // .obj or .o file. This may be a member of an archive file.
97 class ObjectFile : public InputFile {
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; }
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];
113 // Returns the underying COFF file.
114 COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
116 // True if this object file is compatible with SEH.
117 // COFF-specific and x86-only.
118 bool SEHCompat = false;
120 // The list of safe exception handlers listed in .sxdata section.
121 // COFF-specific and x86-only.
122 std::set<SymbolBody *> SEHandlers;
125 void initializeChunks();
126 void initializeSymbols();
127 void initializeSEH();
129 SymbolBody *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
130 SymbolBody *createUndefined(COFFSymbolRef Sym);
132 std::unique_ptr<COFFObjectFile> COFFObj;
133 llvm::BumpPtrAllocator Alloc;
134 const coff_section *SXData = nullptr;
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;
140 // CodeView debug info sections.
141 std::vector<SectionChunk *> DebugChunks;
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
148 std::vector<Chunk *> SparseChunks;
150 // List of all symbols referenced or defined by this file.
151 std::vector<SymbolBody *> SymbolBodies;
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;
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 {
165 explicit ImportFile(MemoryBufferRef M)
166 : InputFile(ImportKind, M), StringAlloc(StringAllocAux) {}
167 static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
169 DefinedImportData *ImpSym = nullptr;
170 DefinedImportThunk *ThunkSym = nullptr;
174 void parse() override;
176 llvm::BumpPtrAllocator StringAllocAux;
177 llvm::StringSaver StringAlloc;
180 StringRef ExternalName;
181 const coff_import_header *Hdr;
182 Chunk *Location = nullptr;
186 class BitcodeFile : public InputFile {
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;
195 void parse() override;
197 std::vector<SymbolBody *> SymbolBodies;
201 std::string toString(coff::InputFile *File);