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