1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_INPUT_FILES_H
10 #define LLD_COFF_INPUT_FILES_H
13 #include "lld/Common/LLVM.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
18 #include "llvm/LTO/LTO.h"
19 #include "llvm/Object/Archive.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Support/StringSaver.h"
28 class DbiModuleDescriptorBuilder;
35 std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *file);
37 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
38 using llvm::COFF::MachineTypes;
39 using llvm::object::Archive;
40 using llvm::object::COFFObjectFile;
41 using llvm::object::COFFSymbolRef;
42 using llvm::object::coff_import_header;
43 using llvm::object::coff_section;
47 class DefinedImportData;
48 class DefinedImportThunk;
56 // The root class of input files.
59 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
60 Kind kind() const { return fileKind; }
61 virtual ~InputFile() {}
63 // Returns the filename.
64 StringRef getName() const { return mb.getBufferIdentifier(); }
66 // Reads a file (the constructor doesn't do that).
67 virtual void parse() = 0;
69 // Returns the CPU type this file was compiled to.
70 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
74 // An archive file name if this file is created from an archive.
77 // Returns .drectve section contents if exist.
78 StringRef getDirectives() { return directives; }
81 InputFile(Kind k, MemoryBufferRef m) : mb(m), fileKind(k) {}
90 class ArchiveFile : public InputFile {
92 explicit ArchiveFile(MemoryBufferRef m);
93 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
94 void parse() override;
96 // Enqueues an archive member load for the given symbol. If we've already
97 // enqueued a load for the same archive member, this function does nothing,
98 // which ensures that we don't load the same member more than once.
99 void addMember(const Archive::Symbol &sym);
102 std::unique_ptr<Archive> file;
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 ArrayRef<uint8_t> getDebugSection(StringRef secName);
122 // Returns a Symbol object for the symbolIndex'th symbol in the
123 // underlying object file.
124 Symbol *getSymbol(uint32_t symbolIndex) {
125 return symbols[symbolIndex];
128 // Returns the underlying COFF file.
129 COFFObjectFile *getCOFFObj() { return coffObj.get(); }
131 // Add a symbol for a range extension thunk. Return the new symbol table
132 // index. This index can be used to modify a relocation.
133 uint32_t addRangeThunkSymbol(Symbol *thunk) {
134 symbols.push_back(thunk);
135 return symbols.size() - 1;
138 static std::vector<ObjFile *> instances;
140 // Flags in the absolute @feat.00 symbol if it is present. These usually
141 // indicate if an object was compiled with certain security features enabled
142 // like stack guard, safeseh, /guard:cf, or other things.
143 uint32_t feat00Flags = 0;
145 // True if this object file is compatible with SEH. COFF-specific and
146 // x86-only. COFF spec 5.10.1. The .sxdata section.
147 bool hasSafeSEH() { return feat00Flags & 0x1; }
149 // True if this file was compiled with /guard:cf.
150 bool hasGuardCF() { return feat00Flags & 0x800; }
152 // Pointer to the PDB module descriptor builder. Various debug info records
153 // will reference object files by "module index", which is here. Things like
154 // source files and section contributions are also recorded here. Will be null
155 // if we are not producing a PDB.
156 llvm::pdb::DbiModuleDescriptorBuilder *moduleDBI = nullptr;
158 const coff_section *addrsigSec = nullptr;
160 // When using Microsoft precompiled headers, this is the PCH's key.
161 // The same key is used by both the precompiled object, and objects using the
162 // precompiled object. Any difference indicates out-of-date objects.
163 llvm::Optional<uint32_t> pchSignature;
165 // Whether this is an object file created from .res files.
166 bool isResourceObjFile = false;
168 // Whether this file was compiled with /hotpatch.
169 bool hotPatchable = false;
171 // Whether the object was already merged into the final PDB.
172 bool mergedIntoPDB = false;
174 // If the OBJ has a .debug$T stream, this tells how it will be handled.
175 TpiSource *debugTypesObj = nullptr;
177 // The .debug$T stream if there's one.
178 llvm::Optional<llvm::codeview::CVTypeArray> debugTypes;
181 const coff_section* getSection(uint32_t i);
182 const coff_section *getSection(COFFSymbolRef sym) {
183 return getSection(sym.getSectionNumber());
186 void initializeChunks();
187 void initializeSymbols();
188 void initializeFlags();
189 void initializeDependencies();
192 readSection(uint32_t sectionNumber,
193 const llvm::object::coff_aux_section_definition *def,
194 StringRef leaderName);
196 void readAssociativeDefinition(
197 COFFSymbolRef coffSym,
198 const llvm::object::coff_aux_section_definition *def);
200 void readAssociativeDefinition(
201 COFFSymbolRef coffSym,
202 const llvm::object::coff_aux_section_definition *def,
203 uint32_t parentSection);
205 void recordPrevailingSymbolForMingw(
206 COFFSymbolRef coffSym,
207 llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
209 void maybeAssociateSEHForMingw(
210 COFFSymbolRef sym, const llvm::object::coff_aux_section_definition *def,
211 const llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
213 // Given a new symbol Sym with comdat selection Selection, if the new
214 // symbol is not (yet) Prevailing and the existing comdat leader set to
215 // Leader, emits a diagnostic if the new symbol and its selection doesn't
216 // match the existing symbol and its selection. If either old or new
217 // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
218 // the existing leader. In that case, Prevailing is set to true.
219 void handleComdatSelection(COFFSymbolRef sym,
220 llvm::COFF::COMDATType &selection,
221 bool &prevailing, DefinedRegular *leader);
223 llvm::Optional<Symbol *>
224 createDefined(COFFSymbolRef sym,
225 std::vector<const llvm::object::coff_aux_section_definition *>
227 bool &prevailingComdat);
228 Symbol *createRegular(COFFSymbolRef sym);
229 Symbol *createUndefined(COFFSymbolRef sym);
231 std::unique_ptr<COFFObjectFile> coffObj;
233 // List of all chunks defined by this file. This includes both section
234 // chunks and non-section chunks for common symbols.
235 std::vector<Chunk *> chunks;
237 // CodeView debug info sections.
238 std::vector<SectionChunk *> debugChunks;
240 // Chunks containing symbol table indices of exception handlers. Only used for
242 std::vector<SectionChunk *> sXDataChunks;
244 // Chunks containing symbol table indices of address taken symbols and longjmp
245 // targets. These are not linked into the final binary when /guard:cf is set.
246 std::vector<SectionChunk *> guardFidChunks;
247 std::vector<SectionChunk *> guardLJmpChunks;
249 // This vector contains the same chunks as Chunks, but they are
250 // indexed such that you can get a SectionChunk by section index.
251 // Nonexistent section indices are filled with null pointers.
252 // (Because section number is 1-based, the first slot is always a
254 std::vector<SectionChunk *> sparseChunks;
256 // This vector contains a list of all symbols defined or referenced by this
257 // file. They are indexed such that you can get a Symbol by symbol
258 // index. Nonexistent indices (which are occupied by auxiliary
259 // symbols in the real symbol table) are filled with null pointers.
260 std::vector<Symbol *> symbols;
263 // This type represents import library members that contain DLL names
264 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
265 // for details about the format.
266 class ImportFile : public InputFile {
268 explicit ImportFile(MemoryBufferRef m) : InputFile(ImportKind, m) {}
270 static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
272 static std::vector<ImportFile *> instances;
274 Symbol *impSym = nullptr;
275 Symbol *thunkSym = nullptr;
279 void parse() override;
282 StringRef externalName;
283 const coff_import_header *hdr;
284 Chunk *location = nullptr;
286 // We want to eliminate dllimported symbols if no one actually refers them.
287 // These "Live" bits are used to keep track of which import library members
288 // are actually in use.
290 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
291 // symbols provided by this import library member. We also track whether the
292 // imported symbol is used separately from whether the thunk is used in order
293 // to avoid creating unnecessary thunks.
294 bool live = !config->doGC;
295 bool thunkLive = !config->doGC;
299 class BitcodeFile : public InputFile {
301 BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
302 uint64_t offsetInArchive);
303 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
304 ArrayRef<Symbol *> getSymbols() { return symbols; }
305 MachineTypes getMachineType() override;
306 static std::vector<BitcodeFile *> instances;
307 std::unique_ptr<llvm::lto::InputFile> obj;
310 void parse() override;
312 std::vector<Symbol *> symbols;
315 std::string replaceThinLTOSuffix(StringRef path);
318 std::string toString(const coff::InputFile *file);