[LLD][COFF] Fix TypeServerSource matcher with more than one collision
[llvm-project.git] / lld / COFF / InputFiles.h
blob2cabb54cb386363db7a0e61678781297454565d5
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 SectionChunk;
59 class Symbol;
60 class Undefined;
61 class TpiSource;
63 // The root class of input files.
64 class InputFile {
65 public:
66 enum Kind {
67 ArchiveKind,
68 ObjectKind,
69 LazyObjectKind,
70 PDBKind,
71 ImportKind,
72 BitcodeKind,
73 DLLKind
75 Kind kind() const { return fileKind; }
76 virtual ~InputFile() {}
78 // Returns the filename.
79 StringRef getName() const { return mb.getBufferIdentifier(); }
81 // Reads a file (the constructor doesn't do that).
82 virtual void parse() = 0;
84 // Returns the CPU type this file was compiled to.
85 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
87 MemoryBufferRef mb;
89 // An archive file name if this file is created from an archive.
90 StringRef parentName;
92 // Returns .drectve section contents if exist.
93 StringRef getDirectives() { return directives; }
95 COFFLinkerContext &ctx;
97 protected:
98 InputFile(COFFLinkerContext &c, Kind k, MemoryBufferRef m, bool lazy = false)
99 : mb(m), ctx(c), fileKind(k), lazy(lazy) {}
101 StringRef directives;
103 private:
104 const Kind fileKind;
106 public:
107 // True if this is a lazy ObjFile or BitcodeFile.
108 bool lazy = false;
111 // .lib or .a file.
112 class ArchiveFile : public InputFile {
113 public:
114 explicit ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef m);
115 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
116 void parse() override;
118 // Enqueues an archive member load for the given symbol. If we've already
119 // enqueued a load for the same archive member, this function does nothing,
120 // which ensures that we don't load the same member more than once.
121 void addMember(const Archive::Symbol &sym);
123 private:
124 std::unique_ptr<Archive> file;
125 llvm::DenseSet<uint64_t> seen;
128 // .obj or .o file. This may be a member of an archive file.
129 class ObjFile : public InputFile {
130 public:
131 explicit ObjFile(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy = false)
132 : InputFile(ctx, ObjectKind, m, lazy) {}
133 static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
134 void parse() override;
135 void parseLazy();
136 MachineTypes getMachineType() override;
137 ArrayRef<Chunk *> getChunks() { return chunks; }
138 ArrayRef<SectionChunk *> getDebugChunks() { return debugChunks; }
139 ArrayRef<SectionChunk *> getSXDataChunks() { return sxDataChunks; }
140 ArrayRef<SectionChunk *> getGuardFidChunks() { return guardFidChunks; }
141 ArrayRef<SectionChunk *> getGuardIATChunks() { return guardIATChunks; }
142 ArrayRef<SectionChunk *> getGuardLJmpChunks() { return guardLJmpChunks; }
143 ArrayRef<SectionChunk *> getGuardEHContChunks() { return guardEHContChunks; }
144 ArrayRef<Symbol *> getSymbols() { return symbols; }
146 MutableArrayRef<Symbol *> getMutableSymbols() { return symbols; }
148 ArrayRef<uint8_t> getDebugSection(StringRef secName);
150 // Returns a Symbol object for the symbolIndex'th symbol in the
151 // underlying object file.
152 Symbol *getSymbol(uint32_t symbolIndex) {
153 return symbols[symbolIndex];
156 // Returns the underlying COFF file.
157 COFFObjectFile *getCOFFObj() { return coffObj.get(); }
159 // Add a symbol for a range extension thunk. Return the new symbol table
160 // index. This index can be used to modify a relocation.
161 uint32_t addRangeThunkSymbol(Symbol *thunk) {
162 symbols.push_back(thunk);
163 return symbols.size() - 1;
166 void includeResourceChunks();
168 bool isResourceObjFile() const { return !resourceChunks.empty(); }
170 // Flags in the absolute @feat.00 symbol if it is present. These usually
171 // indicate if an object was compiled with certain security features enabled
172 // like stack guard, safeseh, /guard:cf, or other things.
173 uint32_t feat00Flags = 0;
175 // True if this object file is compatible with SEH. COFF-specific and
176 // x86-only. COFF spec 5.10.1. The .sxdata section.
177 bool hasSafeSEH() { return feat00Flags & 0x1; }
179 // True if this file was compiled with /guard:cf.
180 bool hasGuardCF() { return feat00Flags & 0x4800; }
182 // Pointer to the PDB module descriptor builder. Various debug info records
183 // will reference object files by "module index", which is here. Things like
184 // source files and section contributions are also recorded here. Will be null
185 // if we are not producing a PDB.
186 llvm::pdb::DbiModuleDescriptorBuilder *moduleDBI = nullptr;
188 const coff_section *addrsigSec = nullptr;
190 const coff_section *callgraphSec = nullptr;
192 // When using Microsoft precompiled headers, this is the PCH's key.
193 // The same key is used by both the precompiled object, and objects using the
194 // precompiled object. Any difference indicates out-of-date objects.
195 llvm::Optional<uint32_t> pchSignature;
197 // Whether this file was compiled with /hotpatch.
198 bool hotPatchable = false;
200 // Whether the object was already merged into the final PDB.
201 bool mergedIntoPDB = false;
203 // If the OBJ has a .debug$T stream, this tells how it will be handled.
204 TpiSource *debugTypesObj = nullptr;
206 // The .debug$P or .debug$T section data if present. Empty otherwise.
207 ArrayRef<uint8_t> debugTypes;
209 llvm::Optional<std::pair<StringRef, uint32_t>>
210 getVariableLocation(StringRef var);
212 llvm::Optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
213 uint32_t sectionIndex);
215 private:
216 const coff_section* getSection(uint32_t i);
217 const coff_section *getSection(COFFSymbolRef sym) {
218 return getSection(sym.getSectionNumber());
221 void enqueuePdbFile(StringRef path, ObjFile *fromFile);
223 void initializeChunks();
224 void initializeSymbols();
225 void initializeFlags();
226 void initializeDependencies();
228 SectionChunk *
229 readSection(uint32_t sectionNumber,
230 const llvm::object::coff_aux_section_definition *def,
231 StringRef leaderName);
233 void readAssociativeDefinition(
234 COFFSymbolRef coffSym,
235 const llvm::object::coff_aux_section_definition *def);
237 void readAssociativeDefinition(
238 COFFSymbolRef coffSym,
239 const llvm::object::coff_aux_section_definition *def,
240 uint32_t parentSection);
242 void recordPrevailingSymbolForMingw(
243 COFFSymbolRef coffSym,
244 llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
246 void maybeAssociateSEHForMingw(
247 COFFSymbolRef sym, const llvm::object::coff_aux_section_definition *def,
248 const llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
250 // Given a new symbol Sym with comdat selection Selection, if the new
251 // symbol is not (yet) Prevailing and the existing comdat leader set to
252 // Leader, emits a diagnostic if the new symbol and its selection doesn't
253 // match the existing symbol and its selection. If either old or new
254 // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
255 // the existing leader. In that case, Prevailing is set to true.
256 void
257 handleComdatSelection(COFFSymbolRef sym, llvm::COFF::COMDATType &selection,
258 bool &prevailing, DefinedRegular *leader,
259 const llvm::object::coff_aux_section_definition *def);
261 llvm::Optional<Symbol *>
262 createDefined(COFFSymbolRef sym,
263 std::vector<const llvm::object::coff_aux_section_definition *>
264 &comdatDefs,
265 bool &prevailingComdat);
266 Symbol *createRegular(COFFSymbolRef sym);
267 Symbol *createUndefined(COFFSymbolRef sym);
269 std::unique_ptr<COFFObjectFile> coffObj;
271 // List of all chunks defined by this file. This includes both section
272 // chunks and non-section chunks for common symbols.
273 std::vector<Chunk *> chunks;
275 std::vector<SectionChunk *> resourceChunks;
277 // CodeView debug info sections.
278 std::vector<SectionChunk *> debugChunks;
280 // Chunks containing symbol table indices of exception handlers. Only used for
281 // 32-bit x86.
282 std::vector<SectionChunk *> sxDataChunks;
284 // Chunks containing symbol table indices of address taken symbols, address
285 // taken IAT entries, longjmp and ehcont targets. These are not linked into
286 // the final binary when /guard:cf is set.
287 std::vector<SectionChunk *> guardFidChunks;
288 std::vector<SectionChunk *> guardIATChunks;
289 std::vector<SectionChunk *> guardLJmpChunks;
290 std::vector<SectionChunk *> guardEHContChunks;
292 // This vector contains a list of all symbols defined or referenced by this
293 // file. They are indexed such that you can get a Symbol by symbol
294 // index. Nonexistent indices (which are occupied by auxiliary
295 // symbols in the real symbol table) are filled with null pointers.
296 std::vector<Symbol *> symbols;
298 // This vector contains the same chunks as Chunks, but they are
299 // indexed such that you can get a SectionChunk by section index.
300 // Nonexistent section indices are filled with null pointers.
301 // (Because section number is 1-based, the first slot is always a
302 // null pointer.) This vector is only valid during initialization.
303 std::vector<SectionChunk *> sparseChunks;
305 DWARFCache *dwarf = nullptr;
308 // This is a PDB type server dependency, that is not a input file per se, but
309 // needs to be treated like one. Such files are discovered from the debug type
310 // stream.
311 class PDBInputFile : public InputFile {
312 public:
313 explicit PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m);
314 ~PDBInputFile();
315 static bool classof(const InputFile *f) { return f->kind() == PDBKind; }
316 void parse() override;
318 static PDBInputFile *findFromRecordPath(const COFFLinkerContext &ctx,
319 StringRef path, ObjFile *fromFile);
321 // Record possible errors while opening the PDB file
322 llvm::Optional<Error> loadErr;
324 // This is the actual interface to the PDB (if it was opened successfully)
325 std::unique_ptr<llvm::pdb::NativeSession> session;
327 // If the PDB has a .debug$T stream, this tells how it will be handled.
328 TpiSource *debugTypesObj = nullptr;
331 // This type represents import library members that contain DLL names
332 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
333 // for details about the format.
334 class ImportFile : public InputFile {
335 public:
336 explicit ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
337 : InputFile(ctx, ImportKind, m) {}
339 static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
341 Symbol *impSym = nullptr;
342 Symbol *thunkSym = nullptr;
343 std::string dllName;
345 private:
346 void parse() override;
348 public:
349 StringRef externalName;
350 const coff_import_header *hdr;
351 Chunk *location = nullptr;
353 // We want to eliminate dllimported symbols if no one actually refers to them.
354 // These "Live" bits are used to keep track of which import library members
355 // are actually in use.
357 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
358 // symbols provided by this import library member. We also track whether the
359 // imported symbol is used separately from whether the thunk is used in order
360 // to avoid creating unnecessary thunks.
361 bool live = !config->doGC;
362 bool thunkLive = !config->doGC;
365 // Used for LTO.
366 class BitcodeFile : public InputFile {
367 public:
368 explicit BitcodeFile(COFFLinkerContext &ctx, MemoryBufferRef mb,
369 StringRef archiveName, uint64_t offsetInArchive,
370 bool lazy);
371 ~BitcodeFile();
372 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
373 ArrayRef<Symbol *> getSymbols() { return symbols; }
374 MachineTypes getMachineType() override;
375 void parseLazy();
376 std::unique_ptr<llvm::lto::InputFile> obj;
378 private:
379 void parse() override;
381 std::vector<Symbol *> symbols;
384 // .dll file. MinGW only.
385 class DLLFile : public InputFile {
386 public:
387 explicit DLLFile(COFFLinkerContext &ctx, MemoryBufferRef m)
388 : InputFile(ctx, DLLKind, m) {}
389 static bool classof(const InputFile *f) { return f->kind() == DLLKind; }
390 void parse() override;
391 MachineTypes getMachineType() override;
393 struct Symbol {
394 StringRef dllName;
395 StringRef symbolName;
396 llvm::COFF::ImportNameType nameType;
397 llvm::COFF::ImportType importType;
400 void makeImport(Symbol *s);
402 private:
403 std::unique_ptr<COFFObjectFile> coffObj;
404 llvm::StringSet<> seen;
407 inline bool isBitcode(MemoryBufferRef mb) {
408 return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
411 std::string replaceThinLTOSuffix(StringRef path);
412 } // namespace coff
414 std::string toString(const coff::InputFile *file);
415 } // namespace lld
417 #endif