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/LTO/LTO.h"
29 #include "llvm/Object/Binary.h"
30 #include "llvm/Object/COFF.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/Endian.h"
33 #include "llvm/Support/Error.h"
34 #include "llvm/Support/ErrorOr.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Target/TargetOptions.h"
39 #include <system_error>
43 using namespace llvm::COFF
;
44 using namespace llvm::codeview
;
45 using namespace llvm::object
;
46 using namespace llvm::support::endian
;
48 using namespace lld::coff
;
51 using llvm::support::ulittle32_t
;
53 // Returns the last element of a path, which is supposed to be a filename.
54 static StringRef
getBasename(StringRef path
) {
55 return sys::path::filename(path
, sys::path::Style::windows
);
58 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
59 std::string
lld::toString(const coff::InputFile
*file
) {
62 if (file
->parentName
.empty() || file
->kind() == coff::InputFile::ImportKind
)
63 return std::string(file
->getName());
65 return (getBasename(file
->parentName
) + "(" + getBasename(file
->getName()) +
70 std::vector
<ObjFile
*> ObjFile::instances
;
71 std::vector
<ImportFile
*> ImportFile::instances
;
72 std::vector
<BitcodeFile
*> BitcodeFile::instances
;
74 /// Checks that Source is compatible with being a weak alias to Target.
75 /// If Source is Undefined and has no weak alias set, makes it a weak
77 static void checkAndSetWeakAlias(SymbolTable
*symtab
, InputFile
*f
,
78 Symbol
*source
, Symbol
*target
) {
79 if (auto *u
= dyn_cast
<Undefined
>(source
)) {
80 if (u
->weakAlias
&& u
->weakAlias
!= target
) {
81 // Weak aliases as produced by GCC are named in the form
82 // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
83 // of another symbol emitted near the weak symbol.
84 // Just use the definition from the first object file that defined
88 symtab
->reportDuplicate(source
, f
);
90 u
->weakAlias
= target
;
94 static bool ignoredSymbolName(StringRef name
) {
95 return name
== "@feat.00" || name
== "@comp.id";
98 ArchiveFile::ArchiveFile(MemoryBufferRef m
) : InputFile(ArchiveKind
, m
) {}
100 void ArchiveFile::parse() {
101 // Parse a MemoryBufferRef as an archive file.
102 file
= CHECK(Archive::create(mb
), this);
104 // Read the symbol table to construct Lazy objects.
105 for (const Archive::Symbol
&sym
: file
->symbols())
106 symtab
->addLazyArchive(this, sym
);
109 // Returns a buffer pointing to a member file containing a given symbol.
110 void ArchiveFile::addMember(const Archive::Symbol
&sym
) {
111 const Archive::Child
&c
=
112 CHECK(sym
.getMember(),
113 "could not get the member for symbol " + toCOFFString(sym
));
115 // Return an empty buffer if we have already returned the same buffer.
116 if (!seen
.insert(c
.getChildOffset()).second
)
119 driver
->enqueueArchiveMember(c
, sym
, getName());
122 std::vector
<MemoryBufferRef
> lld::coff::getArchiveMembers(Archive
*file
) {
123 std::vector
<MemoryBufferRef
> v
;
124 Error err
= Error::success();
125 for (const Archive::Child
&c
: file
->children(err
)) {
126 MemoryBufferRef mbref
=
127 CHECK(c
.getMemoryBufferRef(),
128 file
->getFileName() +
129 ": could not get the buffer for a child of the archive");
133 fatal(file
->getFileName() +
134 ": Archive::children failed: " + toString(std::move(err
)));
138 void LazyObjFile::fetch() {
139 if (mb
.getBuffer().empty())
144 file
= make
<BitcodeFile
>(mb
, "", 0, std::move(symbols
));
146 file
= make
<ObjFile
>(mb
, std::move(symbols
));
148 symtab
->addFile(file
);
151 void LazyObjFile::parse() {
152 if (isBitcode(this->mb
)) {
154 std::unique_ptr
<lto::InputFile
> obj
=
155 CHECK(lto::InputFile::create(this->mb
), this);
156 for (const lto::InputFile::Symbol
&sym
: obj
->symbols()) {
157 if (!sym
.isUndefined())
158 symtab
->addLazyObject(this, sym
.getName());
163 // Native object file.
164 std::unique_ptr
<Binary
> coffObjPtr
= CHECK(createBinary(mb
), this);
165 COFFObjectFile
*coffObj
= cast
<COFFObjectFile
>(coffObjPtr
.get());
166 uint32_t numSymbols
= coffObj
->getNumberOfSymbols();
167 for (uint32_t i
= 0; i
< numSymbols
; ++i
) {
168 COFFSymbolRef coffSym
= check(coffObj
->getSymbol(i
));
169 if (coffSym
.isUndefined() || !coffSym
.isExternal() ||
170 coffSym
.isWeakExternal())
173 coffObj
->getSymbolName(coffSym
, name
);
174 if (coffSym
.isAbsolute() && ignoredSymbolName(name
))
176 symtab
->addLazyObject(this, name
);
177 i
+= coffSym
.getNumberOfAuxSymbols();
181 void ObjFile::parse() {
182 // Parse a memory buffer as a COFF file.
183 std::unique_ptr
<Binary
> bin
= CHECK(createBinary(mb
), this);
185 if (auto *obj
= dyn_cast
<COFFObjectFile
>(bin
.get())) {
189 fatal(toString(this) + " is not a COFF file");
192 // Read section and symbol tables.
196 initializeDependencies();
199 const coff_section
* ObjFile::getSection(uint32_t i
) {
200 const coff_section
*sec
;
201 if (auto ec
= coffObj
->getSection(i
, sec
))
202 fatal("getSection failed: #" + Twine(i
) + ": " + ec
.message());
206 // We set SectionChunk pointers in the SparseChunks vector to this value
207 // temporarily to mark comdat sections as having an unknown resolution. As we
208 // walk the object file's symbol table, once we visit either a leader symbol or
209 // an associative section definition together with the parent comdat's leader,
210 // we set the pointer to either nullptr (to mark the section as discarded) or a
211 // valid SectionChunk for that section.
212 static SectionChunk
*const pendingComdat
= reinterpret_cast<SectionChunk
*>(1);
214 void ObjFile::initializeChunks() {
215 uint32_t numSections
= coffObj
->getNumberOfSections();
216 chunks
.reserve(numSections
);
217 sparseChunks
.resize(numSections
+ 1);
218 for (uint32_t i
= 1; i
< numSections
+ 1; ++i
) {
219 const coff_section
*sec
= getSection(i
);
220 if (sec
->Characteristics
& IMAGE_SCN_LNK_COMDAT
)
221 sparseChunks
[i
] = pendingComdat
;
223 sparseChunks
[i
] = readSection(i
, nullptr, "");
227 SectionChunk
*ObjFile::readSection(uint32_t sectionNumber
,
228 const coff_aux_section_definition
*def
,
229 StringRef leaderName
) {
230 const coff_section
*sec
= getSection(sectionNumber
);
233 if (Expected
<StringRef
> e
= coffObj
->getSectionName(sec
))
236 fatal("getSectionName failed: #" + Twine(sectionNumber
) + ": " +
237 toString(e
.takeError()));
239 if (name
== ".drectve") {
240 ArrayRef
<uint8_t> data
;
241 cantFail(coffObj
->getSectionContents(sec
, data
));
242 directives
= StringRef((const char *)data
.data(), data
.size());
246 if (name
== ".llvm_addrsig") {
251 // Object files may have DWARF debug info or MS CodeView debug info
254 // DWARF sections don't need any special handling from the perspective
255 // of the linker; they are just a data section containing relocations.
256 // We can just link them to complete debug info.
258 // CodeView needs linker support. We need to interpret debug info,
259 // and then write it to a separate .pdb file.
261 // Ignore DWARF debug info unless /debug is given.
262 if (!config
->debug
&& name
.startswith(".debug_"))
265 if (sec
->Characteristics
& llvm::COFF::IMAGE_SCN_LNK_REMOVE
)
267 auto *c
= make
<SectionChunk
>(this, sec
);
269 c
->checksum
= def
->CheckSum
;
271 // CodeView sections are stored to a different vector because they are not
272 // linked in the regular manner.
274 debugChunks
.push_back(c
);
275 else if (name
== ".gfids$y")
276 guardFidChunks
.push_back(c
);
277 else if (name
== ".gljmp$y")
278 guardLJmpChunks
.push_back(c
);
279 else if (name
== ".sxdata")
280 sXDataChunks
.push_back(c
);
281 else if (config
->tailMerge
&& sec
->NumberOfRelocations
== 0 &&
282 name
== ".rdata" && leaderName
.startswith("??_C@"))
283 // COFF sections that look like string literal sections (i.e. no
284 // relocations, in .rdata, leader symbol name matches the MSVC name mangling
285 // for string literals) are subject to string tail merging.
286 MergeChunk::addSection(c
);
287 else if (name
== ".rsrc" || name
.startswith(".rsrc$"))
288 resourceChunks
.push_back(c
);
295 void ObjFile::includeResourceChunks() {
296 chunks
.insert(chunks
.end(), resourceChunks
.begin(), resourceChunks
.end());
299 void ObjFile::readAssociativeDefinition(
300 COFFSymbolRef sym
, const coff_aux_section_definition
*def
) {
301 readAssociativeDefinition(sym
, def
, def
->getNumber(sym
.isBigObj()));
304 void ObjFile::readAssociativeDefinition(COFFSymbolRef sym
,
305 const coff_aux_section_definition
*def
,
306 uint32_t parentIndex
) {
307 SectionChunk
*parent
= sparseChunks
[parentIndex
];
308 int32_t sectionNumber
= sym
.getSectionNumber();
311 StringRef name
, parentName
;
312 coffObj
->getSymbolName(sym
, name
);
314 const coff_section
*parentSec
= getSection(parentIndex
);
315 if (Expected
<StringRef
> e
= coffObj
->getSectionName(parentSec
))
317 error(toString(this) + ": associative comdat " + name
+ " (sec " +
318 Twine(sectionNumber
) + ") has invalid reference to section " +
319 parentName
+ " (sec " + Twine(parentIndex
) + ")");
322 if (parent
== pendingComdat
) {
323 // This can happen if an associative comdat refers to another associative
324 // comdat that appears after it (invalid per COFF spec) or to a section
325 // without any symbols.
330 // Check whether the parent is prevailing. If it is, so are we, and we read
331 // the section; otherwise mark it as discarded.
333 SectionChunk
*c
= readSection(sectionNumber
, def
, "");
334 sparseChunks
[sectionNumber
] = c
;
336 c
->selection
= IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
337 parent
->addAssociative(c
);
340 sparseChunks
[sectionNumber
] = nullptr;
344 void ObjFile::recordPrevailingSymbolForMingw(
345 COFFSymbolRef sym
, DenseMap
<StringRef
, uint32_t> &prevailingSectionMap
) {
346 // For comdat symbols in executable sections, where this is the copy
347 // of the section chunk we actually include instead of discarding it,
348 // add the symbol to a map to allow using it for implicitly
349 // associating .[px]data$<func> sections to it.
350 int32_t sectionNumber
= sym
.getSectionNumber();
351 SectionChunk
*sc
= sparseChunks
[sectionNumber
];
352 if (sc
&& sc
->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE
) {
354 coffObj
->getSymbolName(sym
, name
);
355 if (getMachineType() == I386
)
356 name
.consume_front("_");
357 prevailingSectionMap
[name
] = sectionNumber
;
361 void ObjFile::maybeAssociateSEHForMingw(
362 COFFSymbolRef sym
, const coff_aux_section_definition
*def
,
363 const DenseMap
<StringRef
, uint32_t> &prevailingSectionMap
) {
365 coffObj
->getSymbolName(sym
, name
);
366 if (name
.consume_front(".pdata$") || name
.consume_front(".xdata$") ||
367 name
.consume_front(".eh_frame$")) {
368 // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
369 // associative to the symbol <func>.
370 auto parentSym
= prevailingSectionMap
.find(name
);
371 if (parentSym
!= prevailingSectionMap
.end())
372 readAssociativeDefinition(sym
, def
, parentSym
->second
);
376 Symbol
*ObjFile::createRegular(COFFSymbolRef sym
) {
377 SectionChunk
*sc
= sparseChunks
[sym
.getSectionNumber()];
378 if (sym
.isExternal()) {
380 coffObj
->getSymbolName(sym
, name
);
382 return symtab
->addRegular(this, name
, sym
.getGeneric(), sc
,
384 // For MinGW symbols named .weak.* that point to a discarded section,
385 // don't create an Undefined symbol. If nothing ever refers to the symbol,
386 // everything should be fine. If something actually refers to the symbol
387 // (e.g. the undefined weak alias), linking will fail due to undefined
388 // references at the end.
389 if (config
->mingw
&& name
.startswith(".weak."))
391 return symtab
->addUndefined(name
, this, false);
394 return make
<DefinedRegular
>(this, /*Name*/ "", /*IsCOMDAT*/ false,
395 /*IsExternal*/ false, sym
.getGeneric(), sc
);
399 void ObjFile::initializeSymbols() {
400 uint32_t numSymbols
= coffObj
->getNumberOfSymbols();
401 symbols
.resize(numSymbols
);
403 SmallVector
<std::pair
<Symbol
*, uint32_t>, 8> weakAliases
;
404 std::vector
<uint32_t> pendingIndexes
;
405 pendingIndexes
.reserve(numSymbols
);
407 DenseMap
<StringRef
, uint32_t> prevailingSectionMap
;
408 std::vector
<const coff_aux_section_definition
*> comdatDefs(
409 coffObj
->getNumberOfSections() + 1);
411 for (uint32_t i
= 0; i
< numSymbols
; ++i
) {
412 COFFSymbolRef coffSym
= check(coffObj
->getSymbol(i
));
413 bool prevailingComdat
;
414 if (coffSym
.isUndefined()) {
415 symbols
[i
] = createUndefined(coffSym
);
416 } else if (coffSym
.isWeakExternal()) {
417 symbols
[i
] = createUndefined(coffSym
);
418 uint32_t tagIndex
= coffSym
.getAux
<coff_aux_weak_external
>()->TagIndex
;
419 weakAliases
.emplace_back(symbols
[i
], tagIndex
);
420 } else if (Optional
<Symbol
*> optSym
=
421 createDefined(coffSym
, comdatDefs
, prevailingComdat
)) {
422 symbols
[i
] = *optSym
;
423 if (config
->mingw
&& prevailingComdat
)
424 recordPrevailingSymbolForMingw(coffSym
, prevailingSectionMap
);
426 // createDefined() returns None if a symbol belongs to a section that
427 // was pending at the point when the symbol was read. This can happen in
429 // 1) section definition symbol for a comdat leader;
430 // 2) symbol belongs to a comdat section associated with another section.
431 // In both of these cases, we can expect the section to be resolved by
432 // the time we finish visiting the remaining symbols in the symbol
433 // table. So we postpone the handling of this symbol until that time.
434 pendingIndexes
.push_back(i
);
436 i
+= coffSym
.getNumberOfAuxSymbols();
439 for (uint32_t i
: pendingIndexes
) {
440 COFFSymbolRef sym
= check(coffObj
->getSymbol(i
));
441 if (const coff_aux_section_definition
*def
= sym
.getSectionDefinition()) {
442 if (def
->Selection
== IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
443 readAssociativeDefinition(sym
, def
);
444 else if (config
->mingw
)
445 maybeAssociateSEHForMingw(sym
, def
, prevailingSectionMap
);
447 if (sparseChunks
[sym
.getSectionNumber()] == pendingComdat
) {
449 coffObj
->getSymbolName(sym
, name
);
450 log("comdat section " + name
+
451 " without leader and unassociated, discarding");
454 symbols
[i
] = createRegular(sym
);
457 for (auto &kv
: weakAliases
) {
458 Symbol
*sym
= kv
.first
;
459 uint32_t idx
= kv
.second
;
460 checkAndSetWeakAlias(symtab
, this, sym
, symbols
[idx
]);
464 Symbol
*ObjFile::createUndefined(COFFSymbolRef sym
) {
466 coffObj
->getSymbolName(sym
, name
);
467 return symtab
->addUndefined(name
, this, sym
.isWeakExternal());
470 void ObjFile::handleComdatSelection(COFFSymbolRef sym
, COMDATType
&selection
,
471 bool &prevailing
, DefinedRegular
*leader
) {
474 // There's already an existing comdat for this symbol: `Leader`.
475 // Use the comdats's selection field to determine if the new
476 // symbol in `Sym` should be discarded, produce a duplicate symbol
479 SectionChunk
*leaderChunk
= nullptr;
480 COMDATType leaderSelection
= IMAGE_COMDAT_SELECT_ANY
;
483 leaderChunk
= leader
->getChunk();
484 leaderSelection
= leaderChunk
->selection
;
486 // FIXME: comdats from LTO files don't know their selection; treat them
488 selection
= leaderSelection
;
491 if ((selection
== IMAGE_COMDAT_SELECT_ANY
&&
492 leaderSelection
== IMAGE_COMDAT_SELECT_LARGEST
) ||
493 (selection
== IMAGE_COMDAT_SELECT_LARGEST
&&
494 leaderSelection
== IMAGE_COMDAT_SELECT_ANY
)) {
495 // cl.exe picks "any" for vftables when building with /GR- and
496 // "largest" when building with /GR. To be able to link object files
497 // compiled with each flag, "any" and "largest" are merged as "largest".
498 leaderSelection
= selection
= IMAGE_COMDAT_SELECT_LARGEST
;
501 // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
502 // Clang on the other hand picks "any". To be able to link two object files
503 // with a __declspec(selectany) declaration, one compiled with gcc and the
504 // other with clang, we merge them as proper "same size as"
505 if (config
->mingw
&& ((selection
== IMAGE_COMDAT_SELECT_ANY
&&
506 leaderSelection
== IMAGE_COMDAT_SELECT_SAME_SIZE
) ||
507 (selection
== IMAGE_COMDAT_SELECT_SAME_SIZE
&&
508 leaderSelection
== IMAGE_COMDAT_SELECT_ANY
))) {
509 leaderSelection
= selection
= IMAGE_COMDAT_SELECT_SAME_SIZE
;
512 // Other than that, comdat selections must match. This is a bit more
513 // strict than link.exe which allows merging "any" and "largest" if "any"
514 // is the first symbol the linker sees, and it allows merging "largest"
515 // with everything (!) if "largest" is the first symbol the linker sees.
516 // Making this symmetric independent of which selection is seen first
517 // seems better though.
518 // (This behavior matches ModuleLinker::getComdatResult().)
519 if (selection
!= leaderSelection
) {
520 log(("conflicting comdat type for " + toString(*leader
) + ": " +
521 Twine((int)leaderSelection
) + " in " + toString(leader
->getFile()) +
522 " and " + Twine((int)selection
) + " in " + toString(this))
524 symtab
->reportDuplicate(leader
, this);
529 case IMAGE_COMDAT_SELECT_NODUPLICATES
:
530 symtab
->reportDuplicate(leader
, this);
533 case IMAGE_COMDAT_SELECT_ANY
:
537 case IMAGE_COMDAT_SELECT_SAME_SIZE
:
538 if (leaderChunk
->getSize() != getSection(sym
)->SizeOfRawData
)
539 symtab
->reportDuplicate(leader
, this);
542 case IMAGE_COMDAT_SELECT_EXACT_MATCH
: {
543 SectionChunk
newChunk(this, getSection(sym
));
544 // link.exe only compares section contents here and doesn't complain
545 // if the two comdat sections have e.g. different alignment.
547 if (leaderChunk
->getContents() != newChunk
.getContents())
548 symtab
->reportDuplicate(leader
, this, &newChunk
, sym
.getValue());
552 case IMAGE_COMDAT_SELECT_ASSOCIATIVE
:
553 // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
554 // (This means lld-link doesn't produce duplicate symbol errors for
555 // associative comdats while link.exe does, but associate comdats
556 // are never extern in practice.)
557 llvm_unreachable("createDefined not called for associative comdats");
559 case IMAGE_COMDAT_SELECT_LARGEST
:
560 if (leaderChunk
->getSize() < getSection(sym
)->SizeOfRawData
) {
561 // Replace the existing comdat symbol with the new one.
563 coffObj
->getSymbolName(sym
, name
);
564 // FIXME: This is incorrect: With /opt:noref, the previous sections
565 // make it into the final executable as well. Correct handling would
566 // be to undo reading of the whole old section that's being replaced,
567 // or doing one pass that determines what the final largest comdat
568 // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
569 // only the largest one.
570 replaceSymbol
<DefinedRegular
>(leader
, this, name
, /*IsCOMDAT*/ true,
571 /*IsExternal*/ true, sym
.getGeneric(),
577 case IMAGE_COMDAT_SELECT_NEWEST
:
578 llvm_unreachable("should have been rejected earlier");
582 Optional
<Symbol
*> ObjFile::createDefined(
584 std::vector
<const coff_aux_section_definition
*> &comdatDefs
,
587 auto getName
= [&]() {
589 coffObj
->getSymbolName(sym
, s
);
593 if (sym
.isCommon()) {
594 auto *c
= make
<CommonChunk
>(sym
);
596 return symtab
->addCommon(this, getName(), sym
.getValue(), sym
.getGeneric(),
600 if (sym
.isAbsolute()) {
601 StringRef name
= getName();
603 if (name
== "@feat.00")
604 feat00Flags
= sym
.getValue();
605 // Skip special symbols.
606 if (ignoredSymbolName(name
))
609 if (sym
.isExternal())
610 return symtab
->addAbsolute(name
, sym
);
611 return make
<DefinedAbsolute
>(name
, sym
);
614 int32_t sectionNumber
= sym
.getSectionNumber();
615 if (sectionNumber
== llvm::COFF::IMAGE_SYM_DEBUG
)
618 if (llvm::COFF::isReservedSectionNumber(sectionNumber
))
619 fatal(toString(this) + ": " + getName() +
620 " should not refer to special section " + Twine(sectionNumber
));
622 if ((uint32_t)sectionNumber
>= sparseChunks
.size())
623 fatal(toString(this) + ": " + getName() +
624 " should not refer to non-existent section " + Twine(sectionNumber
));
627 // A comdat symbol consists of two symbol table entries.
628 // The first symbol entry has the name of the section (e.g. .text), fixed
629 // values for the other fields, and one auxiliary record.
630 // The second symbol entry has the name of the comdat symbol, called the
632 // When this function is called for the first symbol entry of a comdat,
633 // it sets comdatDefs and returns None, and when it's called for the second
634 // symbol entry it reads comdatDefs and then sets it back to nullptr.
636 // Handle comdat leader.
637 if (const coff_aux_section_definition
*def
= comdatDefs
[sectionNumber
]) {
638 comdatDefs
[sectionNumber
] = nullptr;
639 DefinedRegular
*leader
;
641 if (sym
.isExternal()) {
642 std::tie(leader
, prevailing
) =
643 symtab
->addComdat(this, getName(), sym
.getGeneric());
645 leader
= make
<DefinedRegular
>(this, /*Name*/ "", /*IsCOMDAT*/ false,
646 /*IsExternal*/ false, sym
.getGeneric());
650 if (def
->Selection
< (int)IMAGE_COMDAT_SELECT_NODUPLICATES
||
651 // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
652 // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
653 def
->Selection
> (int)IMAGE_COMDAT_SELECT_LARGEST
) {
654 fatal("unknown comdat type " + std::to_string((int)def
->Selection
) +
655 " for " + getName() + " in " + toString(this));
657 COMDATType selection
= (COMDATType
)def
->Selection
;
659 if (leader
->isCOMDAT
)
660 handleComdatSelection(sym
, selection
, prevailing
, leader
);
663 SectionChunk
*c
= readSection(sectionNumber
, def
, getName());
664 sparseChunks
[sectionNumber
] = c
;
665 c
->sym
= cast
<DefinedRegular
>(leader
);
666 c
->selection
= selection
;
667 cast
<DefinedRegular
>(leader
)->data
= &c
->repl
;
669 sparseChunks
[sectionNumber
] = nullptr;
674 // Prepare to handle the comdat leader symbol by setting the section's
675 // ComdatDefs pointer if we encounter a non-associative comdat.
676 if (sparseChunks
[sectionNumber
] == pendingComdat
) {
677 if (const coff_aux_section_definition
*def
= sym
.getSectionDefinition()) {
678 if (def
->Selection
!= IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
679 comdatDefs
[sectionNumber
] = def
;
684 return createRegular(sym
);
687 MachineTypes
ObjFile::getMachineType() {
689 return static_cast<MachineTypes
>(coffObj
->getMachine());
690 return IMAGE_FILE_MACHINE_UNKNOWN
;
693 ArrayRef
<uint8_t> ObjFile::getDebugSection(StringRef secName
) {
694 if (SectionChunk
*sec
= SectionChunk::findByName(debugChunks
, secName
))
695 return sec
->consumeDebugMagic();
699 // OBJ files systematically store critical information in a .debug$S stream,
700 // even if the TU was compiled with no debug info. At least two records are
701 // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
702 // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
703 // currently used to initialize the hotPatchable member.
704 void ObjFile::initializeFlags() {
705 ArrayRef
<uint8_t> data
= getDebugSection(".debug$S");
709 DebugSubsectionArray subsections
;
711 BinaryStreamReader
reader(data
, support::little
);
712 ExitOnError exitOnErr
;
713 exitOnErr(reader
.readArray(subsections
, data
.size()));
715 for (const DebugSubsectionRecord
&ss
: subsections
) {
716 if (ss
.kind() != DebugSubsectionKind::Symbols
)
721 // Only parse the first two records. We are only looking for S_OBJNAME
722 // and S_COMPILE3, and they usually appear at the beginning of the
724 for (unsigned i
= 0; i
< 2; ++i
) {
725 Expected
<CVSymbol
> sym
= readSymbolFromStream(ss
.getRecordData(), offset
);
727 consumeError(sym
.takeError());
730 if (sym
->kind() == SymbolKind::S_COMPILE3
) {
732 cantFail(SymbolDeserializer::deserializeAs
<Compile3Sym
>(sym
.get()));
734 (cs
.Flags
& CompileSym3Flags::HotPatch
) != CompileSym3Flags::None
;
736 if (sym
->kind() == SymbolKind::S_OBJNAME
) {
737 auto objName
= cantFail(SymbolDeserializer::deserializeAs
<ObjNameSym
>(
739 pchSignature
= objName
.Signature
;
741 offset
+= sym
->length();
746 // Depending on the compilation flags, OBJs can refer to external files,
747 // necessary to merge this OBJ into the final PDB. We currently support two
748 // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
749 // And PDB type servers, when compiling with /Zi. This function extracts these
750 // dependencies and makes them available as a TpiSource interface (see
751 // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
752 // output even with /Yc and /Yu and with /Zi.
753 void ObjFile::initializeDependencies() {
759 ArrayRef
<uint8_t> data
= getDebugSection(".debug$P");
763 data
= getDebugSection(".debug$T");
769 BinaryStreamReader
reader(data
, support::little
);
770 cantFail(reader
.readArray(types
, reader
.getLength()));
772 CVTypeArray::Iterator firstType
= types
.begin();
773 if (firstType
== types
.end())
776 // Remember the .debug$T or .debug$P section.
780 debugTypesObj
= makePrecompSource(this);
784 if (firstType
->kind() == LF_TYPESERVER2
) {
785 TypeServer2Record ts
= cantFail(
786 TypeDeserializer::deserializeAs
<TypeServer2Record
>(firstType
->data()));
787 debugTypesObj
= makeUseTypeServerSource(this, &ts
);
791 if (firstType
->kind() == LF_PRECOMP
) {
792 PrecompRecord precomp
= cantFail(
793 TypeDeserializer::deserializeAs
<PrecompRecord
>(firstType
->data()));
794 debugTypesObj
= makeUsePrecompSource(this, &precomp
);
798 debugTypesObj
= makeTpiSource(this);
801 // Used only for DWARF debug info, which is not common (except in MinGW
802 // environments). This returns an optional pair of file name and line
803 // number for where the variable was defined.
804 Optional
<std::pair
<StringRef
, uint32_t>>
805 ObjFile::getVariableLocation(StringRef var
) {
807 dwarf
= make
<DWARFCache
>(DWARFContext::create(*getCOFFObj()));
811 if (config
->machine
== I386
)
812 var
.consume_front("_");
813 Optional
<std::pair
<std::string
, unsigned>> ret
= dwarf
->getVariableLoc(var
);
816 return std::make_pair(saver
.save(ret
->first
), ret
->second
);
819 // Used only for DWARF debug info, which is not common (except in MinGW
821 Optional
<DILineInfo
> ObjFile::getDILineInfo(uint32_t offset
,
822 uint32_t sectionIndex
) {
824 dwarf
= make
<DWARFCache
>(DWARFContext::create(*getCOFFObj()));
829 return dwarf
->getDILineInfo(offset
, sectionIndex
);
832 static StringRef
ltrim1(StringRef s
, const char *chars
) {
833 if (!s
.empty() && strchr(chars
, s
[0]))
838 void ImportFile::parse() {
839 const char *buf
= mb
.getBufferStart();
840 const auto *hdr
= reinterpret_cast<const coff_import_header
*>(buf
);
842 // Check if the total size is valid.
843 if (mb
.getBufferSize() != sizeof(*hdr
) + hdr
->SizeOfData
)
844 fatal("broken import library");
846 // Read names and create an __imp_ symbol.
847 StringRef name
= saver
.save(StringRef(buf
+ sizeof(*hdr
)));
848 StringRef impName
= saver
.save("__imp_" + name
);
849 const char *nameStart
= buf
+ sizeof(coff_import_header
) + name
.size() + 1;
850 dllName
= std::string(StringRef(nameStart
));
852 switch (hdr
->getNameType()) {
859 case IMPORT_NAME_NOPREFIX
:
860 extName
= ltrim1(name
, "?@_");
862 case IMPORT_NAME_UNDECORATE
:
863 extName
= ltrim1(name
, "?@_");
864 extName
= extName
.substr(0, extName
.find('@'));
869 externalName
= extName
;
871 impSym
= symtab
->addImportData(impName
, this);
872 // If this was a duplicate, we logged an error but may continue;
873 // in this case, impSym is nullptr.
877 if (hdr
->getType() == llvm::COFF::IMPORT_CONST
)
878 static_cast<void>(symtab
->addImportData(name
, this));
880 // If type is function, we need to create a thunk which jump to an
881 // address pointed by the __imp_ symbol. (This allows you to call
882 // DLL functions just like regular non-DLL functions.)
883 if (hdr
->getType() == llvm::COFF::IMPORT_CODE
)
884 thunkSym
= symtab
->addImportThunk(
885 name
, cast_or_null
<DefinedImportData
>(impSym
), hdr
->Machine
);
888 BitcodeFile::BitcodeFile(MemoryBufferRef mb
, StringRef archiveName
,
889 uint64_t offsetInArchive
)
890 : BitcodeFile(mb
, archiveName
, offsetInArchive
, {}) {}
892 BitcodeFile::BitcodeFile(MemoryBufferRef mb
, StringRef archiveName
,
893 uint64_t offsetInArchive
,
894 std::vector
<Symbol
*> &&symbols
)
895 : InputFile(BitcodeKind
, mb
), symbols(std::move(symbols
)) {
896 std::string path
= mb
.getBufferIdentifier().str();
897 if (config
->thinLTOIndexOnly
)
898 path
= replaceThinLTOSuffix(mb
.getBufferIdentifier());
900 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
901 // name. If two archives define two members with the same name, this
902 // causes a collision which result in only one of the objects being taken
903 // into consideration at LTO time (which very likely causes undefined
904 // symbols later in the link stage). So we append file offset to make
906 MemoryBufferRef
mbref(
908 saver
.save(archiveName
.empty() ? path
909 : archiveName
+ sys::path::filename(path
) +
910 utostr(offsetInArchive
)));
912 obj
= check(lto::InputFile::create(mbref
));
915 BitcodeFile::~BitcodeFile() = default;
917 void BitcodeFile::parse() {
918 std::vector
<std::pair
<Symbol
*, bool>> comdat(obj
->getComdatTable().size());
919 for (size_t i
= 0; i
!= obj
->getComdatTable().size(); ++i
)
920 // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
921 // selection handling.
922 comdat
[i
] = symtab
->addComdat(this, saver
.save(obj
->getComdatTable()[i
]));
923 for (const lto::InputFile::Symbol
&objSym
: obj
->symbols()) {
924 StringRef symName
= saver
.save(objSym
.getName());
925 int comdatIndex
= objSym
.getComdatIndex();
927 if (objSym
.isUndefined()) {
928 sym
= symtab
->addUndefined(symName
, this, false);
929 } else if (objSym
.isCommon()) {
930 sym
= symtab
->addCommon(this, symName
, objSym
.getCommonSize());
931 } else if (objSym
.isWeak() && objSym
.isIndirect()) {
933 sym
= symtab
->addUndefined(symName
, this, true);
934 std::string fallback
= std::string(objSym
.getCOFFWeakExternalFallback());
935 Symbol
*alias
= symtab
->addUndefined(saver
.save(fallback
));
936 checkAndSetWeakAlias(symtab
, this, sym
, alias
);
937 } else if (comdatIndex
!= -1) {
938 if (symName
== obj
->getComdatTable()[comdatIndex
])
939 sym
= comdat
[comdatIndex
].first
;
940 else if (comdat
[comdatIndex
].second
)
941 sym
= symtab
->addRegular(this, symName
);
943 sym
= symtab
->addUndefined(symName
, this, false);
945 sym
= symtab
->addRegular(this, symName
);
947 symbols
.push_back(sym
);
949 config
->gcroot
.push_back(sym
);
951 directives
= obj
->getCOFFLinkerOpts();
954 MachineTypes
BitcodeFile::getMachineType() {
955 switch (Triple(obj
->getTargetTriple()).getArch()) {
962 case Triple::aarch64
:
965 return IMAGE_FILE_MACHINE_UNKNOWN
;
969 std::string
lld::coff::replaceThinLTOSuffix(StringRef path
) {
970 StringRef suffix
= config
->thinLTOObjectSuffixReplace
.first
;
971 StringRef repl
= config
->thinLTOObjectSuffixReplace
.second
;
973 if (path
.consume_back(suffix
))
974 return (path
+ repl
).str();
975 return std::string(path
);