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<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
114 ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
115 ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
116 ArrayRef<Symbol *> getSymbols() { return Symbols; }
118 // Returns a Symbol object for the SymbolIndex'th symbol in the
119 // underlying object file.
120 Symbol *getSymbol(uint32_t SymbolIndex) {
121 return Symbols[SymbolIndex];
124 // Returns the underying COFF file.
125 COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
127 static std::vector<ObjFile *> Instances;
129 // Flags in the absolute @feat.00 symbol if it is present. These usually
130 // indicate if an object was compiled with certain security features enabled
131 // like stack guard, safeseh, /guard:cf, or other things.
132 uint32_t Feat00Flags = 0;
134 // True if this object file is compatible with SEH. COFF-specific and
135 // x86-only. COFF spec 5.10.1. The .sxdata section.
136 bool hasSafeSEH() { return Feat00Flags & 0x1; }
138 // True if this file was compiled with /guard:cf.
139 bool hasGuardCF() { return Feat00Flags & 0x800; }
141 // Pointer to the PDB module descriptor builder. Various debug info records
142 // will reference object files by "module index", which is here. Things like
143 // source files and section contributions are also recorded here. Will be null
144 // if we are not producing a PDB.
145 llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
148 void initializeChunks();
149 void initializeSymbols();
152 readSection(uint32_t SectionNumber,
153 const llvm::object::coff_aux_section_definition *Def,
154 StringRef LeaderName);
156 void readAssociativeDefinition(
157 COFFSymbolRef COFFSym,
158 const llvm::object::coff_aux_section_definition *Def);
160 llvm::Optional<Symbol *>
161 createDefined(COFFSymbolRef Sym,
162 std::vector<const llvm::object::coff_aux_section_definition *>
164 Symbol *createRegular(COFFSymbolRef Sym);
165 Symbol *createUndefined(COFFSymbolRef Sym);
167 std::unique_ptr<COFFObjectFile> COFFObj;
169 // List of all chunks defined by this file. This includes both section
170 // chunks and non-section chunks for common symbols.
171 std::vector<Chunk *> Chunks;
173 // CodeView debug info sections.
174 std::vector<SectionChunk *> DebugChunks;
176 // Chunks containing symbol table indices of exception handlers. Only used for
178 std::vector<SectionChunk *> SXDataChunks;
180 // Chunks containing symbol table indices of address taken symbols and longjmp
181 // targets. These are not linked into the final binary when /guard:cf is set.
182 std::vector<SectionChunk *> GuardFidChunks;
183 std::vector<SectionChunk *> GuardLJmpChunks;
185 // This vector contains the same chunks as Chunks, but they are
186 // indexed such that you can get a SectionChunk by section index.
187 // Nonexistent section indices are filled with null pointers.
188 // (Because section number is 1-based, the first slot is always a
190 std::vector<SectionChunk *> SparseChunks;
192 // This vector contains a list of all symbols defined or referenced by this
193 // file. They are indexed such that you can get a Symbol by symbol
194 // index. Nonexistent indices (which are occupied by auxiliary
195 // symbols in the real symbol table) are filled with null pointers.
196 std::vector<Symbol *> Symbols;
199 // This type represents import library members that contain DLL names
200 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
201 // for details about the format.
202 class ImportFile : public InputFile {
204 explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
206 static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
208 static std::vector<ImportFile *> Instances;
210 Symbol *ImpSym = nullptr;
211 Symbol *ThunkSym = nullptr;
215 void parse() override;
218 StringRef ExternalName;
219 const coff_import_header *Hdr;
220 Chunk *Location = nullptr;
222 // We want to eliminate dllimported symbols if no one actually refers them.
223 // These "Live" bits are used to keep track of which import library members
224 // are actually in use.
226 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
227 // symbols provided by this import library member. We also track whether the
228 // imported symbol is used separately from whether the thunk is used in order
229 // to avoid creating unnecessary thunks.
230 bool Live = !Config->DoGC;
231 bool ThunkLive = !Config->DoGC;
235 class BitcodeFile : public InputFile {
237 explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
238 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
239 ArrayRef<Symbol *> getSymbols() { return Symbols; }
240 MachineTypes getMachineType() override;
241 static std::vector<BitcodeFile *> Instances;
242 std::unique_ptr<llvm::lto::InputFile> Obj;
245 void parse() override;
247 std::vector<Symbol *> Symbols;
251 std::string toString(const coff::InputFile *File);