1 //===- InputFiles.cpp -----------------------------------------------------===//
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 #include "InputFiles.h"
12 #include "DebugTypes.h"
14 #include "SymbolTable.h"
16 #include "lld/Common/DWARF.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Memory.h"
19 #include "llvm-c/lto.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/BinaryFormat/COFF.h"
24 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
25 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
26 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
27 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
28 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
29 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
30 #include "llvm/LTO/LTO.h"
31 #include "llvm/Object/Binary.h"
32 #include "llvm/Object/COFF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/ErrorOr.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Target/TargetOptions.h"
41 #include <system_error>
45 using namespace llvm::COFF
;
46 using namespace llvm::codeview
;
47 using namespace llvm::object
;
48 using namespace llvm::support::endian
;
50 using namespace lld::coff
;
53 using llvm::support::ulittle32_t
;
55 // Returns the last element of a path, which is supposed to be a filename.
56 static StringRef
getBasename(StringRef path
) {
57 return sys::path::filename(path
, sys::path::Style::windows
);
60 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
61 std::string
lld::toString(const coff::InputFile
*file
) {
64 if (file
->parentName
.empty() || file
->kind() == coff::InputFile::ImportKind
)
65 return std::string(file
->getName());
67 return (getBasename(file
->parentName
) + "(" + getBasename(file
->getName()) +
72 std::vector
<ObjFile
*> ObjFile::instances
;
73 std::map
<std::string
, PDBInputFile
*> PDBInputFile::instances
;
74 std::vector
<ImportFile
*> ImportFile::instances
;
75 std::vector
<BitcodeFile
*> BitcodeFile::instances
;
77 /// Checks that Source is compatible with being a weak alias to Target.
78 /// If Source is Undefined and has no weak alias set, makes it a weak
80 static void checkAndSetWeakAlias(SymbolTable
*symtab
, InputFile
*f
,
81 Symbol
*source
, Symbol
*target
) {
82 if (auto *u
= dyn_cast
<Undefined
>(source
)) {
83 if (u
->weakAlias
&& u
->weakAlias
!= target
) {
84 // Weak aliases as produced by GCC are named in the form
85 // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
86 // of another symbol emitted near the weak symbol.
87 // Just use the definition from the first object file that defined
91 symtab
->reportDuplicate(source
, f
);
93 u
->weakAlias
= target
;
97 static bool ignoredSymbolName(StringRef name
) {
98 return name
== "@feat.00" || name
== "@comp.id";
101 ArchiveFile::ArchiveFile(MemoryBufferRef m
) : InputFile(ArchiveKind
, m
) {}
103 void ArchiveFile::parse() {
104 // Parse a MemoryBufferRef as an archive file.
105 file
= CHECK(Archive::create(mb
), this);
107 // Read the symbol table to construct Lazy objects.
108 for (const Archive::Symbol
&sym
: file
->symbols())
109 symtab
->addLazyArchive(this, sym
);
112 // Returns a buffer pointing to a member file containing a given symbol.
113 void ArchiveFile::addMember(const Archive::Symbol
&sym
) {
114 const Archive::Child
&c
=
115 CHECK(sym
.getMember(),
116 "could not get the member for symbol " + toCOFFString(sym
));
118 // Return an empty buffer if we have already returned the same buffer.
119 if (!seen
.insert(c
.getChildOffset()).second
)
122 driver
->enqueueArchiveMember(c
, sym
, getName());
125 std::vector
<MemoryBufferRef
> lld::coff::getArchiveMembers(Archive
*file
) {
126 std::vector
<MemoryBufferRef
> v
;
127 Error err
= Error::success();
128 for (const Archive::Child
&c
: file
->children(err
)) {
129 MemoryBufferRef mbref
=
130 CHECK(c
.getMemoryBufferRef(),
131 file
->getFileName() +
132 ": could not get the buffer for a child of the archive");
136 fatal(file
->getFileName() +
137 ": Archive::children failed: " + toString(std::move(err
)));
141 void LazyObjFile::fetch() {
142 if (mb
.getBuffer().empty())
147 file
= make
<BitcodeFile
>(mb
, "", 0, std::move(symbols
));
149 file
= make
<ObjFile
>(mb
, std::move(symbols
));
151 symtab
->addFile(file
);
154 void LazyObjFile::parse() {
155 if (isBitcode(this->mb
)) {
157 std::unique_ptr
<lto::InputFile
> obj
=
158 CHECK(lto::InputFile::create(this->mb
), this);
159 for (const lto::InputFile::Symbol
&sym
: obj
->symbols()) {
160 if (!sym
.isUndefined())
161 symtab
->addLazyObject(this, sym
.getName());
166 // Native object file.
167 std::unique_ptr
<Binary
> coffObjPtr
= CHECK(createBinary(mb
), this);
168 COFFObjectFile
*coffObj
= cast
<COFFObjectFile
>(coffObjPtr
.get());
169 uint32_t numSymbols
= coffObj
->getNumberOfSymbols();
170 for (uint32_t i
= 0; i
< numSymbols
; ++i
) {
171 COFFSymbolRef coffSym
= check(coffObj
->getSymbol(i
));
172 if (coffSym
.isUndefined() || !coffSym
.isExternal() ||
173 coffSym
.isWeakExternal())
175 StringRef name
= check(coffObj
->getSymbolName(coffSym
));
176 if (coffSym
.isAbsolute() && ignoredSymbolName(name
))
178 symtab
->addLazyObject(this, name
);
179 i
+= coffSym
.getNumberOfAuxSymbols();
183 void ObjFile::parse() {
184 // Parse a memory buffer as a COFF file.
185 std::unique_ptr
<Binary
> bin
= CHECK(createBinary(mb
), this);
187 if (auto *obj
= dyn_cast
<COFFObjectFile
>(bin
.get())) {
191 fatal(toString(this) + " is not a COFF file");
194 // Read section and symbol tables.
198 initializeDependencies();
201 const coff_section
*ObjFile::getSection(uint32_t i
) {
202 auto sec
= coffObj
->getSection(i
);
204 fatal("getSection failed: #" + Twine(i
) + ": " + toString(sec
.takeError()));
208 // We set SectionChunk pointers in the SparseChunks vector to this value
209 // temporarily to mark comdat sections as having an unknown resolution. As we
210 // walk the object file's symbol table, once we visit either a leader symbol or
211 // an associative section definition together with the parent comdat's leader,
212 // we set the pointer to either nullptr (to mark the section as discarded) or a
213 // valid SectionChunk for that section.
214 static SectionChunk
*const pendingComdat
= reinterpret_cast<SectionChunk
*>(1);
216 void ObjFile::initializeChunks() {
217 uint32_t numSections
= coffObj
->getNumberOfSections();
218 sparseChunks
.resize(numSections
+ 1);
219 for (uint32_t i
= 1; i
< numSections
+ 1; ++i
) {
220 const coff_section
*sec
= getSection(i
);
221 if (sec
->Characteristics
& IMAGE_SCN_LNK_COMDAT
)
222 sparseChunks
[i
] = pendingComdat
;
224 sparseChunks
[i
] = readSection(i
, nullptr, "");
228 SectionChunk
*ObjFile::readSection(uint32_t sectionNumber
,
229 const coff_aux_section_definition
*def
,
230 StringRef leaderName
) {
231 const coff_section
*sec
= getSection(sectionNumber
);
234 if (Expected
<StringRef
> e
= coffObj
->getSectionName(sec
))
237 fatal("getSectionName failed: #" + Twine(sectionNumber
) + ": " +
238 toString(e
.takeError()));
240 if (name
== ".drectve") {
241 ArrayRef
<uint8_t> data
;
242 cantFail(coffObj
->getSectionContents(sec
, data
));
243 directives
= StringRef((const char *)data
.data(), data
.size());
247 if (name
== ".llvm_addrsig") {
252 if (name
== ".llvm.call-graph-profile") {
257 // Object files may have DWARF debug info or MS CodeView debug info
260 // DWARF sections don't need any special handling from the perspective
261 // of the linker; they are just a data section containing relocations.
262 // We can just link them to complete debug info.
264 // CodeView needs linker support. We need to interpret debug info,
265 // and then write it to a separate .pdb file.
267 // Ignore DWARF debug info unless /debug is given.
268 if (!config
->debug
&& name
.startswith(".debug_"))
271 if (sec
->Characteristics
& llvm::COFF::IMAGE_SCN_LNK_REMOVE
)
273 auto *c
= make
<SectionChunk
>(this, sec
);
275 c
->checksum
= def
->CheckSum
;
277 // CodeView sections are stored to a different vector because they are not
278 // linked in the regular manner.
280 debugChunks
.push_back(c
);
281 else if (name
== ".gfids$y")
282 guardFidChunks
.push_back(c
);
283 else if (name
== ".giats$y")
284 guardIATChunks
.push_back(c
);
285 else if (name
== ".gljmp$y")
286 guardLJmpChunks
.push_back(c
);
287 else if (name
== ".sxdata")
288 sxDataChunks
.push_back(c
);
289 else if (config
->tailMerge
&& sec
->NumberOfRelocations
== 0 &&
290 name
== ".rdata" && leaderName
.startswith("??_C@"))
291 // COFF sections that look like string literal sections (i.e. no
292 // relocations, in .rdata, leader symbol name matches the MSVC name mangling
293 // for string literals) are subject to string tail merging.
294 MergeChunk::addSection(c
);
295 else if (name
== ".rsrc" || name
.startswith(".rsrc$"))
296 resourceChunks
.push_back(c
);
303 void ObjFile::includeResourceChunks() {
304 chunks
.insert(chunks
.end(), resourceChunks
.begin(), resourceChunks
.end());
307 void ObjFile::readAssociativeDefinition(
308 COFFSymbolRef sym
, const coff_aux_section_definition
*def
) {
309 readAssociativeDefinition(sym
, def
, def
->getNumber(sym
.isBigObj()));
312 void ObjFile::readAssociativeDefinition(COFFSymbolRef sym
,
313 const coff_aux_section_definition
*def
,
314 uint32_t parentIndex
) {
315 SectionChunk
*parent
= sparseChunks
[parentIndex
];
316 int32_t sectionNumber
= sym
.getSectionNumber();
319 StringRef name
= check(coffObj
->getSymbolName(sym
));
321 StringRef parentName
;
322 const coff_section
*parentSec
= getSection(parentIndex
);
323 if (Expected
<StringRef
> e
= coffObj
->getSectionName(parentSec
))
325 error(toString(this) + ": associative comdat " + name
+ " (sec " +
326 Twine(sectionNumber
) + ") has invalid reference to section " +
327 parentName
+ " (sec " + Twine(parentIndex
) + ")");
330 if (parent
== pendingComdat
) {
331 // This can happen if an associative comdat refers to another associative
332 // comdat that appears after it (invalid per COFF spec) or to a section
333 // without any symbols.
338 // Check whether the parent is prevailing. If it is, so are we, and we read
339 // the section; otherwise mark it as discarded.
341 SectionChunk
*c
= readSection(sectionNumber
, def
, "");
342 sparseChunks
[sectionNumber
] = c
;
344 c
->selection
= IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
345 parent
->addAssociative(c
);
348 sparseChunks
[sectionNumber
] = nullptr;
352 void ObjFile::recordPrevailingSymbolForMingw(
353 COFFSymbolRef sym
, DenseMap
<StringRef
, uint32_t> &prevailingSectionMap
) {
354 // For comdat symbols in executable sections, where this is the copy
355 // of the section chunk we actually include instead of discarding it,
356 // add the symbol to a map to allow using it for implicitly
357 // associating .[px]data$<func> sections to it.
358 // Use the suffix from the .text$<func> instead of the leader symbol
359 // name, for cases where the names differ (i386 mangling/decorations,
360 // cases where the leader is a weak symbol named .weak.func.default*).
361 int32_t sectionNumber
= sym
.getSectionNumber();
362 SectionChunk
*sc
= sparseChunks
[sectionNumber
];
363 if (sc
&& sc
->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE
) {
364 StringRef name
= sc
->getSectionName().split('$').second
;
365 prevailingSectionMap
[name
] = sectionNumber
;
369 void ObjFile::maybeAssociateSEHForMingw(
370 COFFSymbolRef sym
, const coff_aux_section_definition
*def
,
371 const DenseMap
<StringRef
, uint32_t> &prevailingSectionMap
) {
372 StringRef name
= check(coffObj
->getSymbolName(sym
));
373 if (name
.consume_front(".pdata$") || name
.consume_front(".xdata$") ||
374 name
.consume_front(".eh_frame$")) {
375 // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
376 // associative to the symbol <func>.
377 auto parentSym
= prevailingSectionMap
.find(name
);
378 if (parentSym
!= prevailingSectionMap
.end())
379 readAssociativeDefinition(sym
, def
, parentSym
->second
);
383 Symbol
*ObjFile::createRegular(COFFSymbolRef sym
) {
384 SectionChunk
*sc
= sparseChunks
[sym
.getSectionNumber()];
385 if (sym
.isExternal()) {
386 StringRef name
= check(coffObj
->getSymbolName(sym
));
388 return symtab
->addRegular(this, name
, sym
.getGeneric(), sc
,
390 // For MinGW symbols named .weak.* that point to a discarded section,
391 // don't create an Undefined symbol. If nothing ever refers to the symbol,
392 // everything should be fine. If something actually refers to the symbol
393 // (e.g. the undefined weak alias), linking will fail due to undefined
394 // references at the end.
395 if (config
->mingw
&& name
.startswith(".weak."))
397 return symtab
->addUndefined(name
, this, false);
400 return make
<DefinedRegular
>(this, /*Name*/ "", /*IsCOMDAT*/ false,
401 /*IsExternal*/ false, sym
.getGeneric(), sc
);
405 void ObjFile::initializeSymbols() {
406 uint32_t numSymbols
= coffObj
->getNumberOfSymbols();
407 symbols
.resize(numSymbols
);
409 SmallVector
<std::pair
<Symbol
*, uint32_t>, 8> weakAliases
;
410 std::vector
<uint32_t> pendingIndexes
;
411 pendingIndexes
.reserve(numSymbols
);
413 DenseMap
<StringRef
, uint32_t> prevailingSectionMap
;
414 std::vector
<const coff_aux_section_definition
*> comdatDefs(
415 coffObj
->getNumberOfSections() + 1);
417 for (uint32_t i
= 0; i
< numSymbols
; ++i
) {
418 COFFSymbolRef coffSym
= check(coffObj
->getSymbol(i
));
419 bool prevailingComdat
;
420 if (coffSym
.isUndefined()) {
421 symbols
[i
] = createUndefined(coffSym
);
422 } else if (coffSym
.isWeakExternal()) {
423 symbols
[i
] = createUndefined(coffSym
);
424 uint32_t tagIndex
= coffSym
.getAux
<coff_aux_weak_external
>()->TagIndex
;
425 weakAliases
.emplace_back(symbols
[i
], tagIndex
);
426 } else if (Optional
<Symbol
*> optSym
=
427 createDefined(coffSym
, comdatDefs
, prevailingComdat
)) {
428 symbols
[i
] = *optSym
;
429 if (config
->mingw
&& prevailingComdat
)
430 recordPrevailingSymbolForMingw(coffSym
, prevailingSectionMap
);
432 // createDefined() returns None if a symbol belongs to a section that
433 // was pending at the point when the symbol was read. This can happen in
435 // 1) section definition symbol for a comdat leader;
436 // 2) symbol belongs to a comdat section associated with another section.
437 // In both of these cases, we can expect the section to be resolved by
438 // the time we finish visiting the remaining symbols in the symbol
439 // table. So we postpone the handling of this symbol until that time.
440 pendingIndexes
.push_back(i
);
442 i
+= coffSym
.getNumberOfAuxSymbols();
445 for (uint32_t i
: pendingIndexes
) {
446 COFFSymbolRef sym
= check(coffObj
->getSymbol(i
));
447 if (const coff_aux_section_definition
*def
= sym
.getSectionDefinition()) {
448 if (def
->Selection
== IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
449 readAssociativeDefinition(sym
, def
);
450 else if (config
->mingw
)
451 maybeAssociateSEHForMingw(sym
, def
, prevailingSectionMap
);
453 if (sparseChunks
[sym
.getSectionNumber()] == pendingComdat
) {
454 StringRef name
= check(coffObj
->getSymbolName(sym
));
455 log("comdat section " + name
+
456 " without leader and unassociated, discarding");
459 symbols
[i
] = createRegular(sym
);
462 for (auto &kv
: weakAliases
) {
463 Symbol
*sym
= kv
.first
;
464 uint32_t idx
= kv
.second
;
465 checkAndSetWeakAlias(symtab
, this, sym
, symbols
[idx
]);
468 // Free the memory used by sparseChunks now that symbol loading is finished.
469 decltype(sparseChunks
)().swap(sparseChunks
);
472 Symbol
*ObjFile::createUndefined(COFFSymbolRef sym
) {
473 StringRef name
= check(coffObj
->getSymbolName(sym
));
474 return symtab
->addUndefined(name
, this, sym
.isWeakExternal());
477 static const coff_aux_section_definition
*findSectionDef(COFFObjectFile
*obj
,
479 uint32_t numSymbols
= obj
->getNumberOfSymbols();
480 for (uint32_t i
= 0; i
< numSymbols
; ++i
) {
481 COFFSymbolRef sym
= check(obj
->getSymbol(i
));
482 if (sym
.getSectionNumber() != section
)
484 if (const coff_aux_section_definition
*def
= sym
.getSectionDefinition())
490 void ObjFile::handleComdatSelection(
491 COFFSymbolRef sym
, COMDATType
&selection
, bool &prevailing
,
492 DefinedRegular
*leader
,
493 const llvm::object::coff_aux_section_definition
*def
) {
496 // There's already an existing comdat for this symbol: `Leader`.
497 // Use the comdats's selection field to determine if the new
498 // symbol in `Sym` should be discarded, produce a duplicate symbol
501 SectionChunk
*leaderChunk
= nullptr;
502 COMDATType leaderSelection
= IMAGE_COMDAT_SELECT_ANY
;
505 leaderChunk
= leader
->getChunk();
506 leaderSelection
= leaderChunk
->selection
;
508 // FIXME: comdats from LTO files don't know their selection; treat them
510 selection
= leaderSelection
;
513 if ((selection
== IMAGE_COMDAT_SELECT_ANY
&&
514 leaderSelection
== IMAGE_COMDAT_SELECT_LARGEST
) ||
515 (selection
== IMAGE_COMDAT_SELECT_LARGEST
&&
516 leaderSelection
== IMAGE_COMDAT_SELECT_ANY
)) {
517 // cl.exe picks "any" for vftables when building with /GR- and
518 // "largest" when building with /GR. To be able to link object files
519 // compiled with each flag, "any" and "largest" are merged as "largest".
520 leaderSelection
= selection
= IMAGE_COMDAT_SELECT_LARGEST
;
523 // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
524 // Clang on the other hand picks "any". To be able to link two object files
525 // with a __declspec(selectany) declaration, one compiled with gcc and the
526 // other with clang, we merge them as proper "same size as"
527 if (config
->mingw
&& ((selection
== IMAGE_COMDAT_SELECT_ANY
&&
528 leaderSelection
== IMAGE_COMDAT_SELECT_SAME_SIZE
) ||
529 (selection
== IMAGE_COMDAT_SELECT_SAME_SIZE
&&
530 leaderSelection
== IMAGE_COMDAT_SELECT_ANY
))) {
531 leaderSelection
= selection
= IMAGE_COMDAT_SELECT_SAME_SIZE
;
534 // Other than that, comdat selections must match. This is a bit more
535 // strict than link.exe which allows merging "any" and "largest" if "any"
536 // is the first symbol the linker sees, and it allows merging "largest"
537 // with everything (!) if "largest" is the first symbol the linker sees.
538 // Making this symmetric independent of which selection is seen first
539 // seems better though.
540 // (This behavior matches ModuleLinker::getComdatResult().)
541 if (selection
!= leaderSelection
) {
542 log(("conflicting comdat type for " + toString(*leader
) + ": " +
543 Twine((int)leaderSelection
) + " in " + toString(leader
->getFile()) +
544 " and " + Twine((int)selection
) + " in " + toString(this))
546 symtab
->reportDuplicate(leader
, this);
551 case IMAGE_COMDAT_SELECT_NODUPLICATES
:
552 symtab
->reportDuplicate(leader
, this);
555 case IMAGE_COMDAT_SELECT_ANY
:
559 case IMAGE_COMDAT_SELECT_SAME_SIZE
:
560 if (leaderChunk
->getSize() != getSection(sym
)->SizeOfRawData
) {
561 if (!config
->mingw
) {
562 symtab
->reportDuplicate(leader
, this);
564 const coff_aux_section_definition
*leaderDef
= findSectionDef(
565 leaderChunk
->file
->getCOFFObj(), leaderChunk
->getSectionNumber());
566 if (!leaderDef
|| leaderDef
->Length
!= def
->Length
)
567 symtab
->reportDuplicate(leader
, this);
572 case IMAGE_COMDAT_SELECT_EXACT_MATCH
: {
573 SectionChunk
newChunk(this, getSection(sym
));
574 // link.exe only compares section contents here and doesn't complain
575 // if the two comdat sections have e.g. different alignment.
577 if (leaderChunk
->getContents() != newChunk
.getContents())
578 symtab
->reportDuplicate(leader
, this, &newChunk
, sym
.getValue());
582 case IMAGE_COMDAT_SELECT_ASSOCIATIVE
:
583 // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
584 // (This means lld-link doesn't produce duplicate symbol errors for
585 // associative comdats while link.exe does, but associate comdats
586 // are never extern in practice.)
587 llvm_unreachable("createDefined not called for associative comdats");
589 case IMAGE_COMDAT_SELECT_LARGEST
:
590 if (leaderChunk
->getSize() < getSection(sym
)->SizeOfRawData
) {
591 // Replace the existing comdat symbol with the new one.
592 StringRef name
= check(coffObj
->getSymbolName(sym
));
593 // FIXME: This is incorrect: With /opt:noref, the previous sections
594 // make it into the final executable as well. Correct handling would
595 // be to undo reading of the whole old section that's being replaced,
596 // or doing one pass that determines what the final largest comdat
597 // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
598 // only the largest one.
599 replaceSymbol
<DefinedRegular
>(leader
, this, name
, /*IsCOMDAT*/ true,
600 /*IsExternal*/ true, sym
.getGeneric(),
606 case IMAGE_COMDAT_SELECT_NEWEST
:
607 llvm_unreachable("should have been rejected earlier");
611 Optional
<Symbol
*> ObjFile::createDefined(
613 std::vector
<const coff_aux_section_definition
*> &comdatDefs
,
616 auto getName
= [&]() { return check(coffObj
->getSymbolName(sym
)); };
618 if (sym
.isCommon()) {
619 auto *c
= make
<CommonChunk
>(sym
);
621 return symtab
->addCommon(this, getName(), sym
.getValue(), sym
.getGeneric(),
625 if (sym
.isAbsolute()) {
626 StringRef name
= getName();
628 if (name
== "@feat.00")
629 feat00Flags
= sym
.getValue();
630 // Skip special symbols.
631 if (ignoredSymbolName(name
))
634 if (sym
.isExternal())
635 return symtab
->addAbsolute(name
, sym
);
636 return make
<DefinedAbsolute
>(name
, sym
);
639 int32_t sectionNumber
= sym
.getSectionNumber();
640 if (sectionNumber
== llvm::COFF::IMAGE_SYM_DEBUG
)
643 if (llvm::COFF::isReservedSectionNumber(sectionNumber
))
644 fatal(toString(this) + ": " + getName() +
645 " should not refer to special section " + Twine(sectionNumber
));
647 if ((uint32_t)sectionNumber
>= sparseChunks
.size())
648 fatal(toString(this) + ": " + getName() +
649 " should not refer to non-existent section " + Twine(sectionNumber
));
652 // A comdat symbol consists of two symbol table entries.
653 // The first symbol entry has the name of the section (e.g. .text), fixed
654 // values for the other fields, and one auxiliary record.
655 // The second symbol entry has the name of the comdat symbol, called the
657 // When this function is called for the first symbol entry of a comdat,
658 // it sets comdatDefs and returns None, and when it's called for the second
659 // symbol entry it reads comdatDefs and then sets it back to nullptr.
661 // Handle comdat leader.
662 if (const coff_aux_section_definition
*def
= comdatDefs
[sectionNumber
]) {
663 comdatDefs
[sectionNumber
] = nullptr;
664 DefinedRegular
*leader
;
666 if (sym
.isExternal()) {
667 std::tie(leader
, prevailing
) =
668 symtab
->addComdat(this, getName(), sym
.getGeneric());
670 leader
= make
<DefinedRegular
>(this, /*Name*/ "", /*IsCOMDAT*/ false,
671 /*IsExternal*/ false, sym
.getGeneric());
675 if (def
->Selection
< (int)IMAGE_COMDAT_SELECT_NODUPLICATES
||
676 // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
677 // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
678 def
->Selection
> (int)IMAGE_COMDAT_SELECT_LARGEST
) {
679 fatal("unknown comdat type " + std::to_string((int)def
->Selection
) +
680 " for " + getName() + " in " + toString(this));
682 COMDATType selection
= (COMDATType
)def
->Selection
;
684 if (leader
->isCOMDAT
)
685 handleComdatSelection(sym
, selection
, prevailing
, leader
, def
);
688 SectionChunk
*c
= readSection(sectionNumber
, def
, getName());
689 sparseChunks
[sectionNumber
] = c
;
690 c
->sym
= cast
<DefinedRegular
>(leader
);
691 c
->selection
= selection
;
692 cast
<DefinedRegular
>(leader
)->data
= &c
->repl
;
694 sparseChunks
[sectionNumber
] = nullptr;
699 // Prepare to handle the comdat leader symbol by setting the section's
700 // ComdatDefs pointer if we encounter a non-associative comdat.
701 if (sparseChunks
[sectionNumber
] == pendingComdat
) {
702 if (const coff_aux_section_definition
*def
= sym
.getSectionDefinition()) {
703 if (def
->Selection
!= IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
704 comdatDefs
[sectionNumber
] = def
;
709 return createRegular(sym
);
712 MachineTypes
ObjFile::getMachineType() {
714 return static_cast<MachineTypes
>(coffObj
->getMachine());
715 return IMAGE_FILE_MACHINE_UNKNOWN
;
718 ArrayRef
<uint8_t> ObjFile::getDebugSection(StringRef secName
) {
719 if (SectionChunk
*sec
= SectionChunk::findByName(debugChunks
, secName
))
720 return sec
->consumeDebugMagic();
724 // OBJ files systematically store critical information in a .debug$S stream,
725 // even if the TU was compiled with no debug info. At least two records are
726 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
727 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
728 // currently used to initialize the hotPatchable member.
729 void ObjFile::initializeFlags() {
730 ArrayRef
<uint8_t> data
= getDebugSection(".debug$S");
734 DebugSubsectionArray subsections
;
736 BinaryStreamReader
reader(data
, support::little
);
737 ExitOnError exitOnErr
;
738 exitOnErr(reader
.readArray(subsections
, data
.size()));
740 for (const DebugSubsectionRecord
&ss
: subsections
) {
741 if (ss
.kind() != DebugSubsectionKind::Symbols
)
746 // Only parse the first two records. We are only looking for S_OBJNAME
747 // and S_COMPILE3, and they usually appear at the beginning of the
749 for (unsigned i
= 0; i
< 2; ++i
) {
750 Expected
<CVSymbol
> sym
= readSymbolFromStream(ss
.getRecordData(), offset
);
752 consumeError(sym
.takeError());
755 if (sym
->kind() == SymbolKind::S_COMPILE3
) {
757 cantFail(SymbolDeserializer::deserializeAs
<Compile3Sym
>(sym
.get()));
759 (cs
.Flags
& CompileSym3Flags::HotPatch
) != CompileSym3Flags::None
;
761 if (sym
->kind() == SymbolKind::S_OBJNAME
) {
762 auto objName
= cantFail(SymbolDeserializer::deserializeAs
<ObjNameSym
>(
764 pchSignature
= objName
.Signature
;
766 offset
+= sym
->length();
771 // Depending on the compilation flags, OBJs can refer to external files,
772 // necessary to merge this OBJ into the final PDB. We currently support two
773 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
774 // And PDB type servers, when compiling with /Zi. This function extracts these
775 // dependencies and makes them available as a TpiSource interface (see
776 // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
777 // output even with /Yc and /Yu and with /Zi.
778 void ObjFile::initializeDependencies() {
784 ArrayRef
<uint8_t> data
= getDebugSection(".debug$P");
788 data
= getDebugSection(".debug$T");
790 // Don't make a TpiSource for objects with no debug info. If the object has
791 // symbols but no types, make a plain, empty TpiSource anyway, because it
792 // simplifies adding the symbols later.
794 if (!debugChunks
.empty())
795 debugTypesObj
= makeTpiSource(this);
799 // Get the first type record. It will indicate if this object uses a type
800 // server (/Zi) or a PCH file (/Yu).
802 BinaryStreamReader
reader(data
, support::little
);
803 cantFail(reader
.readArray(types
, reader
.getLength()));
804 CVTypeArray::Iterator firstType
= types
.begin();
805 if (firstType
== types
.end())
808 // Remember the .debug$T or .debug$P section.
811 // This object file is a PCH file that others will depend on.
813 debugTypesObj
= makePrecompSource(this);
817 // This object file was compiled with /Zi. Enqueue the PDB dependency.
818 if (firstType
->kind() == LF_TYPESERVER2
) {
819 TypeServer2Record ts
= cantFail(
820 TypeDeserializer::deserializeAs
<TypeServer2Record
>(firstType
->data()));
821 debugTypesObj
= makeUseTypeServerSource(this, ts
);
822 PDBInputFile::enqueue(ts
.getName(), this);
826 // This object was compiled with /Yu. It uses types from another object file
827 // with a matching signature.
828 if (firstType
->kind() == LF_PRECOMP
) {
829 PrecompRecord precomp
= cantFail(
830 TypeDeserializer::deserializeAs
<PrecompRecord
>(firstType
->data()));
831 debugTypesObj
= makeUsePrecompSource(this, precomp
);
832 // Drop the LF_PRECOMP record from the input stream.
833 debugTypes
= debugTypes
.drop_front(firstType
->RecordData
.size());
837 // This is a plain old object file.
838 debugTypesObj
= makeTpiSource(this);
841 // Make a PDB path assuming the PDB is in the same folder as the OBJ
842 static std::string
getPdbBaseName(ObjFile
*file
, StringRef tSPath
) {
843 StringRef localPath
=
844 !file
->parentName
.empty() ? file
->parentName
: file
->getName();
845 SmallString
<128> path
= sys::path::parent_path(localPath
);
847 // Currently, type server PDBs are only created by MSVC cl, which only runs
848 // on Windows, so we can assume type server paths are Windows style.
849 sys::path::append(path
,
850 sys::path::filename(tSPath
, sys::path::Style::windows
));
851 return std::string(path
.str());
854 // The casing of the PDB path stamped in the OBJ can differ from the actual path
855 // on disk. With this, we ensure to always use lowercase as a key for the
856 // PDBInputFile::instances map, at least on Windows.
857 static std::string
normalizePdbPath(StringRef path
) {
861 return std::string(path
);
865 // If existing, return the actual PDB path on disk.
866 static Optional
<std::string
> findPdbPath(StringRef pdbPath
,
867 ObjFile
*dependentFile
) {
868 // Ensure the file exists before anything else. In some cases, if the path
869 // points to a removable device, Driver::enqueuePath() would fail with an
870 // error (EAGAIN, "resource unavailable try again") which we want to skip
872 if (llvm::sys::fs::exists(pdbPath
))
873 return normalizePdbPath(pdbPath
);
874 std::string ret
= getPdbBaseName(dependentFile
, pdbPath
);
875 if (llvm::sys::fs::exists(ret
))
876 return normalizePdbPath(ret
);
880 PDBInputFile::PDBInputFile(MemoryBufferRef m
) : InputFile(PDBKind
, m
) {}
882 PDBInputFile::~PDBInputFile() = default;
884 PDBInputFile
*PDBInputFile::findFromRecordPath(StringRef path
,
886 auto p
= findPdbPath(path
.str(), fromFile
);
889 auto it
= PDBInputFile::instances
.find(*p
);
890 if (it
!= PDBInputFile::instances
.end())
895 void PDBInputFile::enqueue(StringRef path
, ObjFile
*fromFile
) {
896 auto p
= findPdbPath(path
.str(), fromFile
);
899 auto it
= PDBInputFile::instances
.emplace(*p
, nullptr);
901 return; // already scheduled for load
902 driver
->enqueuePDB(*p
);
905 void PDBInputFile::parse() {
906 PDBInputFile::instances
[mb
.getBufferIdentifier().str()] = this;
908 std::unique_ptr
<pdb::IPDBSession
> thisSession
;
909 loadErr
.emplace(pdb::NativeSession::createFromPdb(
910 MemoryBuffer::getMemBuffer(mb
, false), thisSession
));
912 return; // fail silently at this point - the error will be handled later,
913 // when merging the debug type stream
915 session
.reset(static_cast<pdb::NativeSession
*>(thisSession
.release()));
917 pdb::PDBFile
&pdbFile
= session
->getPDBFile();
918 auto expectedInfo
= pdbFile
.getPDBInfoStream();
919 // All PDB Files should have an Info stream.
921 loadErr
.emplace(expectedInfo
.takeError());
924 debugTypesObj
= makeTypeServerSource(this);
927 // Used only for DWARF debug info, which is not common (except in MinGW
928 // environments). This returns an optional pair of file name and line
929 // number for where the variable was defined.
930 Optional
<std::pair
<StringRef
, uint32_t>>
931 ObjFile::getVariableLocation(StringRef var
) {
933 dwarf
= make
<DWARFCache
>(DWARFContext::create(*getCOFFObj()));
937 if (config
->machine
== I386
)
938 var
.consume_front("_");
939 Optional
<std::pair
<std::string
, unsigned>> ret
= dwarf
->getVariableLoc(var
);
942 return std::make_pair(saver
.save(ret
->first
), ret
->second
);
945 // Used only for DWARF debug info, which is not common (except in MinGW
947 Optional
<DILineInfo
> ObjFile::getDILineInfo(uint32_t offset
,
948 uint32_t sectionIndex
) {
950 dwarf
= make
<DWARFCache
>(DWARFContext::create(*getCOFFObj()));
955 return dwarf
->getDILineInfo(offset
, sectionIndex
);
958 static StringRef
ltrim1(StringRef s
, const char *chars
) {
959 if (!s
.empty() && strchr(chars
, s
[0]))
964 void ImportFile::parse() {
965 const char *buf
= mb
.getBufferStart();
966 const auto *hdr
= reinterpret_cast<const coff_import_header
*>(buf
);
968 // Check if the total size is valid.
969 if (mb
.getBufferSize() != sizeof(*hdr
) + hdr
->SizeOfData
)
970 fatal("broken import library");
972 // Read names and create an __imp_ symbol.
973 StringRef name
= saver
.save(StringRef(buf
+ sizeof(*hdr
)));
974 StringRef impName
= saver
.save("__imp_" + name
);
975 const char *nameStart
= buf
+ sizeof(coff_import_header
) + name
.size() + 1;
976 dllName
= std::string(StringRef(nameStart
));
978 switch (hdr
->getNameType()) {
985 case IMPORT_NAME_NOPREFIX
:
986 extName
= ltrim1(name
, "?@_");
988 case IMPORT_NAME_UNDECORATE
:
989 extName
= ltrim1(name
, "?@_");
990 extName
= extName
.substr(0, extName
.find('@'));
995 externalName
= extName
;
997 impSym
= symtab
->addImportData(impName
, this);
998 // If this was a duplicate, we logged an error but may continue;
999 // in this case, impSym is nullptr.
1003 if (hdr
->getType() == llvm::COFF::IMPORT_CONST
)
1004 static_cast<void>(symtab
->addImportData(name
, this));
1006 // If type is function, we need to create a thunk which jump to an
1007 // address pointed by the __imp_ symbol. (This allows you to call
1008 // DLL functions just like regular non-DLL functions.)
1009 if (hdr
->getType() == llvm::COFF::IMPORT_CODE
)
1010 thunkSym
= symtab
->addImportThunk(
1011 name
, cast_or_null
<DefinedImportData
>(impSym
), hdr
->Machine
);
1014 BitcodeFile::BitcodeFile(MemoryBufferRef mb
, StringRef archiveName
,
1015 uint64_t offsetInArchive
)
1016 : BitcodeFile(mb
, archiveName
, offsetInArchive
, {}) {}
1018 BitcodeFile::BitcodeFile(MemoryBufferRef mb
, StringRef archiveName
,
1019 uint64_t offsetInArchive
,
1020 std::vector
<Symbol
*> &&symbols
)
1021 : InputFile(BitcodeKind
, mb
), symbols(std::move(symbols
)) {
1022 std::string path
= mb
.getBufferIdentifier().str();
1023 if (config
->thinLTOIndexOnly
)
1024 path
= replaceThinLTOSuffix(mb
.getBufferIdentifier());
1026 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1027 // name. If two archives define two members with the same name, this
1028 // causes a collision which result in only one of the objects being taken
1029 // into consideration at LTO time (which very likely causes undefined
1030 // symbols later in the link stage). So we append file offset to make
1032 MemoryBufferRef
mbref(
1034 saver
.save(archiveName
.empty() ? path
1035 : archiveName
+ sys::path::filename(path
) +
1036 utostr(offsetInArchive
)));
1038 obj
= check(lto::InputFile::create(mbref
));
1041 BitcodeFile::~BitcodeFile() = default;
1043 void BitcodeFile::parse() {
1044 std::vector
<std::pair
<Symbol
*, bool>> comdat(obj
->getComdatTable().size());
1045 for (size_t i
= 0; i
!= obj
->getComdatTable().size(); ++i
)
1046 // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
1047 // selection handling.
1048 comdat
[i
] = symtab
->addComdat(this, saver
.save(obj
->getComdatTable()[i
]));
1049 for (const lto::InputFile::Symbol
&objSym
: obj
->symbols()) {
1050 StringRef symName
= saver
.save(objSym
.getName());
1051 int comdatIndex
= objSym
.getComdatIndex();
1053 if (objSym
.isUndefined()) {
1054 sym
= symtab
->addUndefined(symName
, this, false);
1055 } else if (objSym
.isCommon()) {
1056 sym
= symtab
->addCommon(this, symName
, objSym
.getCommonSize());
1057 } else if (objSym
.isWeak() && objSym
.isIndirect()) {
1059 sym
= symtab
->addUndefined(symName
, this, true);
1060 std::string fallback
= std::string(objSym
.getCOFFWeakExternalFallback());
1061 Symbol
*alias
= symtab
->addUndefined(saver
.save(fallback
));
1062 checkAndSetWeakAlias(symtab
, this, sym
, alias
);
1063 } else if (comdatIndex
!= -1) {
1064 if (symName
== obj
->getComdatTable()[comdatIndex
])
1065 sym
= comdat
[comdatIndex
].first
;
1066 else if (comdat
[comdatIndex
].second
)
1067 sym
= symtab
->addRegular(this, symName
);
1069 sym
= symtab
->addUndefined(symName
, this, false);
1071 sym
= symtab
->addRegular(this, symName
);
1073 symbols
.push_back(sym
);
1074 if (objSym
.isUsed())
1075 config
->gcroot
.push_back(sym
);
1077 directives
= obj
->getCOFFLinkerOpts();
1080 MachineTypes
BitcodeFile::getMachineType() {
1081 switch (Triple(obj
->getTargetTriple()).getArch()) {
1082 case Triple::x86_64
:
1088 case Triple::aarch64
:
1091 return IMAGE_FILE_MACHINE_UNKNOWN
;
1095 std::string
lld::coff::replaceThinLTOSuffix(StringRef path
) {
1096 StringRef suffix
= config
->thinLTOObjectSuffixReplace
.first
;
1097 StringRef repl
= config
->thinLTOObjectSuffixReplace
.second
;
1099 if (path
.consume_back(suffix
))
1100 return (path
+ repl
).str();
1101 return std::string(path
);