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/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
19 #include "llvm/LTO/LTO.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/Support/StringSaver.h"
29 class DbiModuleDescriptorBuilder;
36 std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File);
38 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
39 using llvm::COFF::MachineTypes;
40 using llvm::object::Archive;
41 using llvm::object::COFFObjectFile;
42 using llvm::object::COFFSymbolRef;
43 using llvm::object::coff_import_header;
44 using llvm::object::coff_section;
48 class DefinedImportData;
49 class DefinedImportThunk;
55 // The root class of input files.
58 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
59 Kind kind() const { return FileKind; }
60 virtual ~InputFile() {}
62 // Returns the filename.
63 StringRef getName() const { return MB.getBufferIdentifier(); }
65 // Reads a file (the constructor doesn't do that).
66 virtual void parse() = 0;
68 // Returns the CPU type this file was compiled to.
69 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
73 // An archive file name if this file is created from an archive.
76 // Returns .drectve section contents if exist.
77 StringRef getDirectives() { return StringRef(Directives).trim(); }
80 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
82 std::string Directives;
89 class ArchiveFile : public InputFile {
91 explicit ArchiveFile(MemoryBufferRef M);
92 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
93 void parse() override;
95 // Enqueues an archive member load for the given symbol. If we've already
96 // enqueued a load for the same archive member, this function does nothing,
97 // which ensures that we don't load the same member more than once.
98 void addMember(const Archive::Symbol *Sym);
101 std::unique_ptr<Archive> File;
102 std::string Filename;
103 llvm::DenseSet<uint64_t> Seen;
106 // .obj or .o file. This may be a member of an archive file.
107 class ObjFile : public InputFile {
109 explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
110 static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
111 void parse() override;
112 MachineTypes getMachineType() override;
113 ArrayRef<Chunk *> getChunks() { return Chunks; }
114 ArrayRef<SectionChunk *> getDebugChunks() { return DebugChunks; }
115 ArrayRef<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
116 ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
117 ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
118 ArrayRef<Symbol *> getSymbols() { return Symbols; }
120 // Returns a Symbol object for the SymbolIndex'th symbol in the
121 // underlying object file.
122 Symbol *getSymbol(uint32_t SymbolIndex) {
123 return Symbols[SymbolIndex];
126 // Returns the underlying COFF file.
127 COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
129 // Whether the object was already merged into the final PDB or not
130 bool wasProcessedForPDB() const { return !!ModuleDBI; }
132 static std::vector<ObjFile *> Instances;
134 // Flags in the absolute @feat.00 symbol if it is present. These usually
135 // indicate if an object was compiled with certain security features enabled
136 // like stack guard, safeseh, /guard:cf, or other things.
137 uint32_t Feat00Flags = 0;
139 // True if this object file is compatible with SEH. COFF-specific and
140 // x86-only. COFF spec 5.10.1. The .sxdata section.
141 bool hasSafeSEH() { return Feat00Flags & 0x1; }
143 // True if this file was compiled with /guard:cf.
144 bool hasGuardCF() { return Feat00Flags & 0x800; }
146 // Pointer to the PDB module descriptor builder. Various debug info records
147 // will reference object files by "module index", which is here. Things like
148 // source files and section contributions are also recorded here. Will be null
149 // if we are not producing a PDB.
150 llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
152 const coff_section *AddrsigSec = nullptr;
154 // When using Microsoft precompiled headers, this is the PCH's key.
155 // The same key is used by both the precompiled object, and objects using the
156 // precompiled object. Any difference indicates out-of-date objects.
157 llvm::Optional<uint32_t> PCHSignature;
160 void initializeChunks();
161 void initializeSymbols();
164 readSection(uint32_t SectionNumber,
165 const llvm::object::coff_aux_section_definition *Def,
166 StringRef LeaderName);
168 void readAssociativeDefinition(
169 COFFSymbolRef COFFSym,
170 const llvm::object::coff_aux_section_definition *Def);
172 void readAssociativeDefinition(
173 COFFSymbolRef COFFSym,
174 const llvm::object::coff_aux_section_definition *Def,
175 uint32_t ParentSection);
177 void recordPrevailingSymbolForMingw(
178 COFFSymbolRef COFFSym,
179 llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
181 void maybeAssociateSEHForMingw(
182 COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def,
183 const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
185 llvm::Optional<Symbol *>
186 createDefined(COFFSymbolRef Sym,
187 std::vector<const llvm::object::coff_aux_section_definition *>
189 bool &PrevailingComdat);
190 Symbol *createRegular(COFFSymbolRef Sym);
191 Symbol *createUndefined(COFFSymbolRef Sym);
193 std::unique_ptr<COFFObjectFile> COFFObj;
195 // List of all chunks defined by this file. This includes both section
196 // chunks and non-section chunks for common symbols.
197 std::vector<Chunk *> Chunks;
199 // CodeView debug info sections.
200 std::vector<SectionChunk *> DebugChunks;
202 // Chunks containing symbol table indices of exception handlers. Only used for
204 std::vector<SectionChunk *> SXDataChunks;
206 // Chunks containing symbol table indices of address taken symbols and longjmp
207 // targets. These are not linked into the final binary when /guard:cf is set.
208 std::vector<SectionChunk *> GuardFidChunks;
209 std::vector<SectionChunk *> GuardLJmpChunks;
211 // This vector contains the same chunks as Chunks, but they are
212 // indexed such that you can get a SectionChunk by section index.
213 // Nonexistent section indices are filled with null pointers.
214 // (Because section number is 1-based, the first slot is always a
216 std::vector<SectionChunk *> SparseChunks;
218 // This vector contains a list of all symbols defined or referenced by this
219 // file. They are indexed such that you can get a Symbol by symbol
220 // index. Nonexistent indices (which are occupied by auxiliary
221 // symbols in the real symbol table) are filled with null pointers.
222 std::vector<Symbol *> Symbols;
225 // This type represents import library members that contain DLL names
226 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
227 // for details about the format.
228 class ImportFile : public InputFile {
230 explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
232 static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
234 static std::vector<ImportFile *> Instances;
236 Symbol *ImpSym = nullptr;
237 Symbol *ThunkSym = nullptr;
241 void parse() override;
244 StringRef ExternalName;
245 const coff_import_header *Hdr;
246 Chunk *Location = nullptr;
248 // We want to eliminate dllimported symbols if no one actually refers them.
249 // These "Live" bits are used to keep track of which import library members
250 // are actually in use.
252 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
253 // symbols provided by this import library member. We also track whether the
254 // imported symbol is used separately from whether the thunk is used in order
255 // to avoid creating unnecessary thunks.
256 bool Live = !Config->DoGC;
257 bool ThunkLive = !Config->DoGC;
261 class BitcodeFile : public InputFile {
263 explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
264 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
265 ArrayRef<Symbol *> getSymbols() { return Symbols; }
266 MachineTypes getMachineType() override;
267 static std::vector<BitcodeFile *> Instances;
268 std::unique_ptr<llvm::lto::InputFile> Obj;
271 void parse() override;
273 std::vector<Symbol *> Symbols;
277 std::string toString(const coff::InputFile *File);