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/BinaryFormat/Magic.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Support/StringSaver.h"
28 class DbiModuleDescriptorBuilder
;
40 std::vector
<MemoryBufferRef
> getArchiveMembers(llvm::object::Archive
*file
);
42 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN
;
43 using llvm::COFF::MachineTypes
;
44 using llvm::object::Archive
;
45 using llvm::object::COFFObjectFile
;
46 using llvm::object::COFFSymbolRef
;
47 using llvm::object::coff_import_header
;
48 using llvm::object::coff_section
;
52 class DefinedImportData
;
53 class DefinedImportThunk
;
60 // The root class of input files.
70 Kind
kind() const { return fileKind
; }
71 virtual ~InputFile() {}
73 // Returns the filename.
74 StringRef
getName() const { return mb
.getBufferIdentifier(); }
76 // Reads a file (the constructor doesn't do that).
77 virtual void parse() = 0;
79 // Returns the CPU type this file was compiled to.
80 virtual MachineTypes
getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN
; }
84 // An archive file name if this file is created from an archive.
87 // Returns .drectve section contents if exist.
88 StringRef
getDirectives() { return directives
; }
91 InputFile(Kind k
, MemoryBufferRef m
) : mb(m
), fileKind(k
) {}
100 class ArchiveFile
: public InputFile
{
102 explicit ArchiveFile(MemoryBufferRef m
);
103 static bool classof(const InputFile
*f
) { return f
->kind() == ArchiveKind
; }
104 void parse() override
;
106 // Enqueues an archive member load for the given symbol. If we've already
107 // enqueued a load for the same archive member, this function does nothing,
108 // which ensures that we don't load the same member more than once.
109 void addMember(const Archive::Symbol
&sym
);
112 std::unique_ptr
<Archive
> file
;
113 llvm::DenseSet
<uint64_t> seen
;
116 // .obj or .o file between -start-lib and -end-lib.
117 class LazyObjFile
: public InputFile
{
119 explicit LazyObjFile(MemoryBufferRef m
) : InputFile(LazyObjectKind
, m
) {}
120 static bool classof(const InputFile
*f
) {
121 return f
->kind() == LazyObjectKind
;
123 // Makes this object file part of the link.
125 // Adds the symbols in this file to the symbol table as LazyObject symbols.
126 void parse() override
;
129 std::vector
<Symbol
*> symbols
;
132 // .obj or .o file. This may be a member of an archive file.
133 class ObjFile
: public InputFile
{
135 explicit ObjFile(MemoryBufferRef m
) : InputFile(ObjectKind
, m
) {}
136 explicit ObjFile(MemoryBufferRef m
, std::vector
<Symbol
*> &&symbols
)
137 : InputFile(ObjectKind
, m
), symbols(std::move(symbols
)) {}
138 static bool classof(const InputFile
*f
) { return f
->kind() == ObjectKind
; }
139 void parse() override
;
140 MachineTypes
getMachineType() 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
*> getGuardLJmpChunks() { return guardLJmpChunks
; }
146 ArrayRef
<Symbol
*> getSymbols() { 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 static std::vector
<ObjFile
*> instances
;
172 // Flags in the absolute @feat.00 symbol if it is present. These usually
173 // indicate if an object was compiled with certain security features enabled
174 // like stack guard, safeseh, /guard:cf, or other things.
175 uint32_t feat00Flags
= 0;
177 // True if this object file is compatible with SEH. COFF-specific and
178 // x86-only. COFF spec 5.10.1. The .sxdata section.
179 bool hasSafeSEH() { return feat00Flags
& 0x1; }
181 // True if this file was compiled with /guard:cf.
182 bool hasGuardCF() { return feat00Flags
& 0x800; }
184 // Pointer to the PDB module descriptor builder. Various debug info records
185 // will reference object files by "module index", which is here. Things like
186 // source files and section contributions are also recorded here. Will be null
187 // if we are not producing a PDB.
188 llvm::pdb::DbiModuleDescriptorBuilder
*moduleDBI
= nullptr;
190 const coff_section
*addrsigSec
= 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
);
216 const coff_section
* getSection(uint32_t i
);
217 const coff_section
*getSection(COFFSymbolRef sym
) {
218 return getSection(sym
.getSectionNumber());
221 void initializeChunks();
222 void initializeSymbols();
223 void initializeFlags();
224 void initializeDependencies();
227 readSection(uint32_t sectionNumber
,
228 const llvm::object::coff_aux_section_definition
*def
,
229 StringRef leaderName
);
231 void readAssociativeDefinition(
232 COFFSymbolRef coffSym
,
233 const llvm::object::coff_aux_section_definition
*def
);
235 void readAssociativeDefinition(
236 COFFSymbolRef coffSym
,
237 const llvm::object::coff_aux_section_definition
*def
,
238 uint32_t parentSection
);
240 void recordPrevailingSymbolForMingw(
241 COFFSymbolRef coffSym
,
242 llvm::DenseMap
<StringRef
, uint32_t> &prevailingSectionMap
);
244 void maybeAssociateSEHForMingw(
245 COFFSymbolRef sym
, const llvm::object::coff_aux_section_definition
*def
,
246 const llvm::DenseMap
<StringRef
, uint32_t> &prevailingSectionMap
);
248 // Given a new symbol Sym with comdat selection Selection, if the new
249 // symbol is not (yet) Prevailing and the existing comdat leader set to
250 // Leader, emits a diagnostic if the new symbol and its selection doesn't
251 // match the existing symbol and its selection. If either old or new
252 // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
253 // the existing leader. In that case, Prevailing is set to true.
254 void handleComdatSelection(COFFSymbolRef sym
,
255 llvm::COFF::COMDATType
&selection
,
256 bool &prevailing
, DefinedRegular
*leader
);
258 llvm::Optional
<Symbol
*>
259 createDefined(COFFSymbolRef sym
,
260 std::vector
<const llvm::object::coff_aux_section_definition
*>
262 bool &prevailingComdat
);
263 Symbol
*createRegular(COFFSymbolRef sym
);
264 Symbol
*createUndefined(COFFSymbolRef sym
);
266 std::unique_ptr
<COFFObjectFile
> coffObj
;
268 // List of all chunks defined by this file. This includes both section
269 // chunks and non-section chunks for common symbols.
270 std::vector
<Chunk
*> chunks
;
272 std::vector
<SectionChunk
*> resourceChunks
;
274 // CodeView debug info sections.
275 std::vector
<SectionChunk
*> debugChunks
;
277 // Chunks containing symbol table indices of exception handlers. Only used for
279 std::vector
<SectionChunk
*> sXDataChunks
;
281 // Chunks containing symbol table indices of address taken symbols and longjmp
282 // targets. These are not linked into the final binary when /guard:cf is set.
283 std::vector
<SectionChunk
*> guardFidChunks
;
284 std::vector
<SectionChunk
*> guardLJmpChunks
;
286 // This vector contains the same chunks as Chunks, but they are
287 // indexed such that you can get a SectionChunk by section index.
288 // Nonexistent section indices are filled with null pointers.
289 // (Because section number is 1-based, the first slot is always a
291 std::vector
<SectionChunk
*> sparseChunks
;
293 // This vector contains a list of all symbols defined or referenced by this
294 // file. They are indexed such that you can get a Symbol by symbol
295 // index. Nonexistent indices (which are occupied by auxiliary
296 // symbols in the real symbol table) are filled with null pointers.
297 std::vector
<Symbol
*> symbols
;
299 DWARFCache
*dwarf
= nullptr;
302 // This type represents import library members that contain DLL names
303 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
304 // for details about the format.
305 class ImportFile
: public InputFile
{
307 explicit ImportFile(MemoryBufferRef m
) : InputFile(ImportKind
, m
) {}
309 static bool classof(const InputFile
*f
) { return f
->kind() == ImportKind
; }
311 static std::vector
<ImportFile
*> instances
;
313 Symbol
*impSym
= nullptr;
314 Symbol
*thunkSym
= nullptr;
318 void parse() override
;
321 StringRef externalName
;
322 const coff_import_header
*hdr
;
323 Chunk
*location
= nullptr;
325 // We want to eliminate dllimported symbols if no one actually refers them.
326 // These "Live" bits are used to keep track of which import library members
327 // are actually in use.
329 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
330 // symbols provided by this import library member. We also track whether the
331 // imported symbol is used separately from whether the thunk is used in order
332 // to avoid creating unnecessary thunks.
333 bool live
= !config
->doGC
;
334 bool thunkLive
= !config
->doGC
;
338 class BitcodeFile
: public InputFile
{
340 BitcodeFile(MemoryBufferRef mb
, StringRef archiveName
,
341 uint64_t offsetInArchive
);
342 explicit BitcodeFile(MemoryBufferRef m
, StringRef archiveName
,
343 uint64_t offsetInArchive
,
344 std::vector
<Symbol
*> &&symbols
);
346 static bool classof(const InputFile
*f
) { return f
->kind() == BitcodeKind
; }
347 ArrayRef
<Symbol
*> getSymbols() { return symbols
; }
348 MachineTypes
getMachineType() override
;
349 static std::vector
<BitcodeFile
*> instances
;
350 std::unique_ptr
<llvm::lto::InputFile
> obj
;
353 void parse() override
;
355 std::vector
<Symbol
*> symbols
;
358 inline bool isBitcode(MemoryBufferRef mb
) {
359 return identify_magic(mb
.getBuffer()) == llvm::file_magic::bitcode
;
362 std::string
replaceThinLTOSuffix(StringRef path
);
365 std::string
toString(const coff::InputFile
*file
);