1//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_COFF_INPUT_FILES_H
10#define LLD_COFF_INPUT_FILES_H
11
12#include "Config.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/ADT/StringSet.h"
18#include "llvm/BinaryFormat/Magic.h"
19#include "llvm/Object/Archive.h"
20#include "llvm/Object/COFF.h"
21#include "llvm/Support/StringSaver.h"
22#include <memory>
23#include <set>
24#include <vector>
25
26namespace llvm {
27struct DILineInfo;
28namespace pdb {
29class DbiModuleDescriptorBuilder;
30class NativeSession;
31}
32namespace lto {
33class InputFile;
34}
35}
36
37namespace lld {
38class DWARFCache;
39
40namespace coff {
41class COFFLinkerContext;
42
43const COFFSyncStream &operator<<(const COFFSyncStream &, const InputFile *);
44
45std::vector<MemoryBufferRef> getArchiveMembers(COFFLinkerContext &,
46 llvm::object::Archive *file);
47
48using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
49using llvm::COFF::MachineTypes;
50using llvm::object::Archive;
51using llvm::object::COFFObjectFile;
52using llvm::object::COFFSymbolRef;
53using llvm::object::coff_import_header;
54using llvm::object::coff_section;
55
56class Chunk;
57class Defined;
58class DefinedImportData;
59class DefinedImportThunk;
60class DefinedRegular;
61class ImportThunkChunk;
62class ImportThunkChunkARM64EC;
63class SectionChunk;
64class Symbol;
65class SymbolTable;
66class Undefined;
67class TpiSource;
68
69// The root class of input files.
70class InputFile {
71public:
72 enum Kind {
73 ArchiveKind,
74 ObjectKind,
75 PDBKind,
76 ImportKind,
77 BitcodeKind,
78 DLLKind
79 };
80 Kind kind() const { return fileKind; }
81 virtual ~InputFile() {}
82
83 // Returns the filename.
84 StringRef getName() const { return mb.getBufferIdentifier(); }
85
86 // Reads a file (the constructor doesn't do that).
87 virtual void parse() = 0;
88
89 // Returns the CPU type this file was compiled to.
90 virtual MachineTypes getMachineType() const {
91 return IMAGE_FILE_MACHINE_UNKNOWN;
92 }
93
94 MemoryBufferRef mb;
95
96 // An archive file name if this file is created from an archive.
97 StringRef parentName;
98
99 // Returns .drectve section contents if exist.
100 StringRef getDirectives() { return directives; }
101
102 SymbolTable &symtab;
103
104protected:
105 InputFile(SymbolTable &s, Kind k, MemoryBufferRef m, bool lazy = false)
106 : mb(m), symtab(s), fileKind(k), lazy(lazy) {}
107
108 StringRef directives;
109
110private:
111 const Kind fileKind;
112
113public:
114 // True if this is a lazy ObjFile or BitcodeFile.
115 bool lazy = false;
116};
117
118// .lib or .a file.
119class ArchiveFile : public InputFile {
120public:
121 explicit ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef mb,
122 std::unique_ptr<Archive> &f);
123 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
124 void parse() override;
125
126 // Enqueues an archive member load for the given symbol. If we've already
127 // enqueued a load for the same archive member, this function does nothing,
128 // which ensures that we don't load the same member more than once.
129 void addMember(const Archive::Symbol &sym);
130
131private:
132 std::unique_ptr<Archive> file;
133 llvm::DenseSet<uint64_t> seen;
134};
135
136// .obj or .o file. This may be a member of an archive file.
137class ObjFile : public InputFile {
138public:
139 static ObjFile *create(COFFLinkerContext &ctx, COFFObjectFile *coffObj,
140 bool lazy = false);
141 static ObjFile *create(COFFLinkerContext &ctx, MemoryBufferRef mb,
142 bool lazy = false) {
143 return ObjFile::create(ctx, coffObj: ObjFile::createCOFFObject(ctx, mb).release(),
144 lazy);
145 }
146 explicit ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy);
147
148 static std::unique_ptr<COFFObjectFile>
149 createCOFFObject(COFFLinkerContext &ctx, MemoryBufferRef mb);
150
151 static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
152 void parse() override;
153 void parseLazy();
154 MachineTypes getMachineType() const override;
155 ArrayRef<Chunk *> getChunks() { return chunks; }
156 ArrayRef<SectionChunk *> getDebugChunks() { return debugChunks; }
157 ArrayRef<SectionChunk *> getSXDataChunks() { return sxDataChunks; }
158 ArrayRef<SectionChunk *> getGuardFidChunks() { return guardFidChunks; }
159 ArrayRef<SectionChunk *> getGuardIATChunks() { return guardIATChunks; }
160 ArrayRef<SectionChunk *> getGuardLJmpChunks() { return guardLJmpChunks; }
161 ArrayRef<SectionChunk *> getGuardEHContChunks() { return guardEHContChunks; }
162 ArrayRef<Symbol *> getSymbols() { return symbols; }
163
164 MutableArrayRef<Symbol *> getMutableSymbols() { return symbols; }
165
166 ArrayRef<uint8_t> getDebugSection(StringRef secName);
167
168 // Returns a Symbol object for the symbolIndex'th symbol in the
169 // underlying object file.
170 Symbol *getSymbol(uint32_t symbolIndex) {
171 return symbols[symbolIndex];
172 }
173
174 // Returns the underlying COFF file.
175 COFFObjectFile *getCOFFObj() { return coffObj.get(); }
176
177 // Add a symbol for a range extension thunk. Return the new symbol table
178 // index. This index can be used to modify a relocation.
179 uint32_t addRangeThunkSymbol(Symbol *thunk) {
180 symbols.push_back(x: thunk);
181 return symbols.size() - 1;
182 }
183
184 void includeResourceChunks();
185
186 bool isResourceObjFile() const { return !resourceChunks.empty(); }
187
188 // Flags in the absolute @feat.00 symbol if it is present. These usually
189 // indicate if an object was compiled with certain security features enabled
190 // like stack guard, safeseh, /guard:cf, or other things.
191 uint32_t feat00Flags = 0;
192
193 // True if this object file is compatible with SEH. COFF-specific and
194 // x86-only. COFF spec 5.10.1. The .sxdata section.
195 bool hasSafeSEH() { return feat00Flags & 0x1; }
196
197 // True if this file was compiled with /guard:cf.
198 bool hasGuardCF() { return feat00Flags & 0x800; }
199
200 // True if this file was compiled with /guard:ehcont.
201 bool hasGuardEHCont() { return feat00Flags & 0x4000; }
202
203 // Pointer to the PDB module descriptor builder. Various debug info records
204 // will reference object files by "module index", which is here. Things like
205 // source files and section contributions are also recorded here. Will be null
206 // if we are not producing a PDB.
207 llvm::pdb::DbiModuleDescriptorBuilder *moduleDBI = nullptr;
208
209 const coff_section *addrsigSec = nullptr;
210
211 const coff_section *callgraphSec = nullptr;
212
213 // When using Microsoft precompiled headers, this is the PCH's key.
214 // The same key is used by both the precompiled object, and objects using the
215 // precompiled object. Any difference indicates out-of-date objects.
216 std::optional<uint32_t> pchSignature;
217
218 // Whether this file was compiled with /hotpatch.
219 bool hotPatchable = false;
220
221 // Whether the object was already merged into the final PDB.
222 bool mergedIntoPDB = false;
223
224 // If the OBJ has a .debug$T stream, this tells how it will be handled.
225 TpiSource *debugTypesObj = nullptr;
226
227 // The .debug$P or .debug$T section data if present. Empty otherwise.
228 ArrayRef<uint8_t> debugTypes;
229
230 std::optional<std::pair<StringRef, uint32_t>>
231 getVariableLocation(StringRef var);
232
233 std::optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
234 uint32_t sectionIndex);
235
236private:
237 const coff_section* getSection(uint32_t i);
238 const coff_section *getSection(COFFSymbolRef sym) {
239 return getSection(i: sym.getSectionNumber());
240 }
241
242 void enqueuePdbFile(StringRef path, ObjFile *fromFile);
243
244 void initializeChunks();
245 void initializeSymbols();
246 void initializeFlags();
247 void initializeDependencies();
248 void initializeECThunks();
249
250 SectionChunk *
251 readSection(uint32_t sectionNumber,
252 const llvm::object::coff_aux_section_definition *def,
253 StringRef leaderName);
254
255 void readAssociativeDefinition(
256 COFFSymbolRef coffSym,
257 const llvm::object::coff_aux_section_definition *def);
258
259 void readAssociativeDefinition(
260 COFFSymbolRef coffSym,
261 const llvm::object::coff_aux_section_definition *def,
262 uint32_t parentSection);
263
264 void recordPrevailingSymbolForMingw(
265 COFFSymbolRef coffSym,
266 llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
267
268 void maybeAssociateSEHForMingw(
269 COFFSymbolRef sym, const llvm::object::coff_aux_section_definition *def,
270 const llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
271
272 // Given a new symbol Sym with comdat selection Selection, if the new
273 // symbol is not (yet) Prevailing and the existing comdat leader set to
274 // Leader, emits a diagnostic if the new symbol and its selection doesn't
275 // match the existing symbol and its selection. If either old or new
276 // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
277 // the existing leader. In that case, Prevailing is set to true.
278 void
279 handleComdatSelection(COFFSymbolRef sym, llvm::COFF::COMDATType &selection,
280 bool &prevailing, DefinedRegular *leader,
281 const llvm::object::coff_aux_section_definition *def);
282
283 std::optional<Symbol *>
284 createDefined(COFFSymbolRef sym,
285 std::vector<const llvm::object::coff_aux_section_definition *>
286 &comdatDefs,
287 bool &prevailingComdat);
288 Symbol *createRegular(COFFSymbolRef sym);
289 Symbol *createUndefined(COFFSymbolRef sym, bool overrideLazy);
290
291 std::unique_ptr<COFFObjectFile> coffObj;
292
293 // List of all chunks defined by this file. This includes both section
294 // chunks and non-section chunks for common symbols.
295 std::vector<Chunk *> chunks;
296
297 std::vector<SectionChunk *> resourceChunks;
298
299 // CodeView debug info sections.
300 std::vector<SectionChunk *> debugChunks;
301
302 // Chunks containing symbol table indices of exception handlers. Only used for
303 // 32-bit x86.
304 std::vector<SectionChunk *> sxDataChunks;
305
306 // Chunks containing symbol table indices of address taken symbols, address
307 // taken IAT entries, longjmp and ehcont targets. These are not linked into
308 // the final binary when /guard:cf is set.
309 std::vector<SectionChunk *> guardFidChunks;
310 std::vector<SectionChunk *> guardIATChunks;
311 std::vector<SectionChunk *> guardLJmpChunks;
312 std::vector<SectionChunk *> guardEHContChunks;
313
314 std::vector<SectionChunk *> hybmpChunks;
315
316 // This vector contains a list of all symbols defined or referenced by this
317 // file. They are indexed such that you can get a Symbol by symbol
318 // index. Nonexistent indices (which are occupied by auxiliary
319 // symbols in the real symbol table) are filled with null pointers.
320 std::vector<Symbol *> symbols;
321
322 // This vector contains the same chunks as Chunks, but they are
323 // indexed such that you can get a SectionChunk by section index.
324 // Nonexistent section indices are filled with null pointers.
325 // (Because section number is 1-based, the first slot is always a
326 // null pointer.) This vector is only valid during initialization.
327 std::vector<SectionChunk *> sparseChunks;
328
329 DWARFCache *dwarf = nullptr;
330};
331
332// This is a PDB type server dependency, that is not a input file per se, but
333// needs to be treated like one. Such files are discovered from the debug type
334// stream.
335class PDBInputFile : public InputFile {
336public:
337 explicit PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m);
338 ~PDBInputFile();
339 static bool classof(const InputFile *f) { return f->kind() == PDBKind; }
340 void parse() override;
341
342 static PDBInputFile *findFromRecordPath(const COFFLinkerContext &ctx,
343 StringRef path, ObjFile *fromFile);
344
345 // Record possible errors while opening the PDB file
346 std::optional<std::string> loadErrorStr;
347
348 // This is the actual interface to the PDB (if it was opened successfully)
349 std::unique_ptr<llvm::pdb::NativeSession> session;
350
351 // If the PDB has a .debug$T stream, this tells how it will be handled.
352 TpiSource *debugTypesObj = nullptr;
353};
354
355// This type represents import library members that contain DLL names
356// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
357// for details about the format.
358class ImportFile : public InputFile {
359public:
360 explicit ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m);
361
362 static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
363 MachineTypes getMachineType() const override { return getMachineType(m: mb); }
364 static MachineTypes getMachineType(MemoryBufferRef m);
365 bool isSameImport(const ImportFile *other) const;
366 bool isEC() const { return impECSym != nullptr; }
367
368 DefinedImportData *impSym = nullptr;
369 Defined *thunkSym = nullptr;
370 ImportThunkChunkARM64EC *impchkThunk = nullptr;
371 ImportFile *hybridFile = nullptr;
372 std::string dllName;
373
374private:
375 void parse() override;
376 ImportThunkChunk *makeImportThunk();
377
378public:
379 StringRef externalName;
380 const coff_import_header *hdr;
381 Chunk *location = nullptr;
382
383 // Auxiliary IAT symbols and chunks on ARM64EC.
384 DefinedImportData *impECSym = nullptr;
385 Chunk *auxLocation = nullptr;
386 Defined *auxThunkSym = nullptr;
387 DefinedImportData *auxImpCopySym = nullptr;
388 Chunk *auxCopyLocation = nullptr;
389
390 // We want to eliminate dllimported symbols if no one actually refers to them.
391 // These "Live" bits are used to keep track of which import library members
392 // are actually in use.
393 //
394 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
395 // symbols provided by this import library member.
396 bool live;
397};
398
399// Used for LTO.
400class BitcodeFile : public InputFile {
401public:
402 explicit BitcodeFile(SymbolTable &symtab, MemoryBufferRef mb,
403 std::unique_ptr<llvm::lto::InputFile> &obj, bool lazy);
404 ~BitcodeFile();
405
406 static BitcodeFile *create(COFFLinkerContext &ctx, MemoryBufferRef mb,
407 StringRef archiveName, uint64_t offsetInArchive,
408 bool lazy);
409 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
410 ArrayRef<Symbol *> getSymbols() { return symbols; }
411 MachineTypes getMachineType() const override {
412 return getMachineType(obj: obj.get());
413 }
414 static MachineTypes getMachineType(const llvm::lto::InputFile *obj);
415 void parseLazy();
416 std::unique_ptr<llvm::lto::InputFile> obj;
417
418private:
419 void parse() override;
420
421 std::vector<Symbol *> symbols;
422};
423
424// .dll file. MinGW only.
425class DLLFile : public InputFile {
426public:
427 explicit DLLFile(SymbolTable &symtab, std::unique_ptr<COFFObjectFile> &obj)
428 : InputFile(symtab, DLLKind, obj->getMemoryBufferRef()) {
429 coffObj.swap(u&: obj);
430 }
431 static bool classof(const InputFile *f) { return f->kind() == DLLKind; }
432 void parse() override;
433 MachineTypes getMachineType() const override;
434
435 struct Symbol {
436 StringRef dllName;
437 StringRef symbolName;
438 llvm::COFF::ImportNameType nameType;
439 llvm::COFF::ImportType importType;
440 };
441
442 void makeImport(Symbol *s);
443
444private:
445 std::unique_ptr<COFFObjectFile> coffObj;
446 llvm::StringSet<> seen;
447};
448
449inline bool isBitcode(MemoryBufferRef mb) {
450 return identify_magic(magic: mb.getBuffer()) == llvm::file_magic::bitcode;
451}
452
453std::string replaceThinLTOSuffix(StringRef path, StringRef suffix,
454 StringRef repl);
455} // namespace coff
456
457std::string toString(const coff::InputFile *file);
458} // namespace lld
459
460#endif
461