1 //===- InputFiles.cpp -----------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
8 
9 #include "InputFiles.h"
10 #include "Chunks.h"
11 #include "Config.h"
12 #include "Driver.h"
13 #include "SymbolTable.h"
14 #include "Symbols.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/ErrorOr.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include <cstring>
32 #include <system_error>
33 #include <utility>
34 
35 using namespace llvm;
36 using namespace llvm::COFF;
37 using namespace llvm::object;
38 using namespace llvm::support::endian;
39 
40 using llvm::Triple;
41 using llvm::support::ulittle32_t;
42 
43 namespace lld {
44 namespace coff {
45 
46 std::vector<ObjFile *> ObjFile::Instances;
47 std::vector<ImportFile *> ImportFile::Instances;
48 std::vector<BitcodeFile *> BitcodeFile::Instances;
49 
50 /// Checks that Source is compatible with being a weak alias to Target.
51 /// If Source is Undefined and has no weak alias set, makes it a weak
52 /// alias to Target.
53 static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F,
54                                  Symbol *Source, Symbol *Target) {
55   if (auto *U = dyn_cast<Undefined>(Source)) {
56     if (U->WeakAlias && U->WeakAlias != Target) {
57       // Weak aliases as produced by GCC are named in the form
58       // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
59       // of another symbol emitted near the weak symbol.
60       // Just use the definition from the first object file that defined
61       // this weak symbol.
62       if (Config->MinGW)
63         return;
64       Symtab->reportDuplicate(Source, F);
65     }
66     U->WeakAlias = Target;
67   }
68 }
69 
70 ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
71 
72 void ArchiveFile::parse() {
73   // Parse a MemoryBufferRef as an archive file.
74   File = CHECK(Archive::create(MB), this);
75 
76   // Read the symbol table to construct Lazy objects.
77   for (const Archive::Symbol &Sym : File->symbols())
78     Symtab->addLazy(this, Sym);
79 }
80 
81 // Returns a buffer pointing to a member file containing a given symbol.
82 void ArchiveFile::addMember(const Archive::Symbol *Sym) {
83   const Archive::Child &C =
84       CHECK(Sym->getMember(),
85             "could not get the member for symbol " + Sym->getName());
86 
87   // Return an empty buffer if we have already returned the same buffer.
88   if (!Seen.insert(C.getChildOffset()).second)
89     return;
90 
91   Driver->enqueueArchiveMember(C, Sym->getName(), getName());
92 }
93 
94 std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) {
95   std::vector<MemoryBufferRef> V;
96   Error Err = Error::success();
97   for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
98     Archive::Child C =
99         CHECK(COrErr,
100               File->getFileName() + ": could not get the child of the archive");
101     MemoryBufferRef MBRef =
102         CHECK(C.getMemoryBufferRef(),
103               File->getFileName() +
104                   ": could not get the buffer for a child of the archive");
105     V.push_back(MBRef);
106   }
107   if (Err)
108     fatal(File->getFileName() +
109           ": Archive::children failed: " + toString(std::move(Err)));
110   return V;
111 }
112 
113 void ObjFile::parse() {
114   // Parse a memory buffer as a COFF file.
115   std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this);
116 
117   if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
118     Bin.release();
119     COFFObj.reset(Obj);
120   } else {
121     fatal(toString(this) + " is not a COFF file");
122   }
123 
124   // Read section and symbol tables.
125   initializeChunks();
126   initializeSymbols();
127 }
128 
129 const coff_section* ObjFile::getSection(uint32_t I) {
130   const coff_section *Sec;
131   if (auto EC = COFFObj->getSection(I, Sec))
132     fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
133   return Sec;
134 }
135 
136 // We set SectionChunk pointers in the SparseChunks vector to this value
137 // temporarily to mark comdat sections as having an unknown resolution. As we
138 // walk the object file's symbol table, once we visit either a leader symbol or
139 // an associative section definition together with the parent comdat's leader,
140 // we set the pointer to either nullptr (to mark the section as discarded) or a
141 // valid SectionChunk for that section.
142 static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1);
143 
144 void ObjFile::initializeChunks() {
145   uint32_t NumSections = COFFObj->getNumberOfSections();
146   Chunks.reserve(NumSections);
147   SparseChunks.resize(NumSections + 1);
148   for (uint32_t I = 1; I < NumSections + 1; ++I) {
149     const coff_section *Sec = getSection(I);
150     if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
151       SparseChunks[I] = PendingComdat;
152     else
153       SparseChunks[I] = readSection(I, nullptr, "");
154   }
155 }
156 
157 SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
158                                    const coff_aux_section_definition *Def,
159                                    StringRef LeaderName) {
160   const coff_section *Sec = getSection(SectionNumber);
161 
162   StringRef Name;
163   if (auto EC = COFFObj->getSectionName(Sec, Name))
164     fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
165           EC.message());
166 
167   if (Name == ".drectve") {
168     ArrayRef<uint8_t> Data;
169     COFFObj->getSectionContents(Sec, Data);
170     Directives = std::string((const char *)Data.data(), Data.size());
171     return nullptr;
172   }
173 
174   if (Name == ".llvm_addrsig") {
175     AddrsigSec = Sec;
176     return nullptr;
177   }
178 
179   // Object files may have DWARF debug info or MS CodeView debug info
180   // (or both).
181   //
182   // DWARF sections don't need any special handling from the perspective
183   // of the linker; they are just a data section containing relocations.
184   // We can just link them to complete debug info.
185   //
186   // CodeView needs linker support. We need to interpret debug info,
187   // and then write it to a separate .pdb file.
188 
189   // Ignore DWARF debug info unless /debug is given.
190   if (!Config->Debug && Name.startswith(".debug_"))
191     return nullptr;
192 
193   if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
194     return nullptr;
195   auto *C = make<SectionChunk>(this, Sec);
196   if (Def)
197     C->Checksum = Def->CheckSum;
198 
199   // CodeView sections are stored to a different vector because they are not
200   // linked in the regular manner.
201   if (C->isCodeView())
202     DebugChunks.push_back(C);
203   else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gfids$y")
204     GuardFidChunks.push_back(C);
205   else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gljmp$y")
206     GuardLJmpChunks.push_back(C);
207   else if (Name == ".sxdata")
208     SXDataChunks.push_back(C);
209   else if (Config->TailMerge && Sec->NumberOfRelocations == 0 &&
210            Name == ".rdata" && LeaderName.startswith("??_C@"))
211     // COFF sections that look like string literal sections (i.e. no
212     // relocations, in .rdata, leader symbol name matches the MSVC name mangling
213     // for string literals) are subject to string tail merging.
214     MergeChunk::addSection(C);
215   else
216     Chunks.push_back(C);
217 
218   return C;
219 }
220 
221 void ObjFile::readAssociativeDefinition(
222     COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
223   readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj()));
224 }
225 
226 void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
227                                         const coff_aux_section_definition *Def,
228                                         uint32_t ParentIndex) {
229   SectionChunk *Parent = SparseChunks[ParentIndex];
230   int32_t SectionNumber = Sym.getSectionNumber();
231 
232   auto Diag = [&]() {
233     StringRef Name, ParentName;
234     COFFObj->getSymbolName(Sym, Name);
235 
236     const coff_section *ParentSec = getSection(ParentIndex);
237     COFFObj->getSectionName(ParentSec, ParentName);
238     error(toString(this) + ": associative comdat " + Name + " (sec " +
239           Twine(SectionNumber) + ") has invalid reference to section " +
240           ParentName + " (sec " + Twine(ParentIndex) + ")");
241   };
242 
243   if (Parent == PendingComdat) {
244     // This can happen if an associative comdat refers to another associative
245     // comdat that appears after it (invalid per COFF spec) or to a section
246     // without any symbols.
247     Diag();
248     return;
249   }
250 
251   // Check whether the parent is prevailing. If it is, so are we, and we read
252   // the section; otherwise mark it as discarded.
253   if (Parent) {
254     SectionChunk *C = readSection(SectionNumber, Def, "");
255     SparseChunks[SectionNumber] = C;
256     if (C) {
257       C->Selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
258       Parent->addAssociative(C);
259     }
260   } else {
261     SparseChunks[SectionNumber] = nullptr;
262   }
263 }
264 
265 void ObjFile::recordPrevailingSymbolForMingw(
266     COFFSymbolRef Sym, DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
267   // For comdat symbols in executable sections, where this is the copy
268   // of the section chunk we actually include instead of discarding it,
269   // add the symbol to a map to allow using it for implicitly
270   // associating .[px]data$<func> sections to it.
271   int32_t SectionNumber = Sym.getSectionNumber();
272   SectionChunk *SC = SparseChunks[SectionNumber];
273   if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
274     StringRef Name;
275     COFFObj->getSymbolName(Sym, Name);
276     PrevailingSectionMap[Name] = SectionNumber;
277   }
278 }
279 
280 void ObjFile::maybeAssociateSEHForMingw(
281     COFFSymbolRef Sym, const coff_aux_section_definition *Def,
282     const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
283   StringRef Name;
284   COFFObj->getSymbolName(Sym, Name);
285   if (Name.consume_front(".pdata$") || Name.consume_front(".xdata$")) {
286     // For MinGW, treat .[px]data$<func> as implicitly associative to
287     // the symbol <func>.
288     auto ParentSym = PrevailingSectionMap.find(Name);
289     if (ParentSym != PrevailingSectionMap.end())
290       readAssociativeDefinition(Sym, Def, ParentSym->second);
291   }
292 }
293 
294 Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
295   SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
296   if (Sym.isExternal()) {
297     StringRef Name;
298     COFFObj->getSymbolName(Sym, Name);
299     if (SC)
300       return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
301     // For MinGW symbols named .weak.* that point to a discarded section,
302     // don't create an Undefined symbol. If nothing ever refers to the symbol,
303     // everything should be fine. If something actually refers to the symbol
304     // (e.g. the undefined weak alias), linking will fail due to undefined
305     // references at the end.
306     if (Config->MinGW && Name.startswith(".weak."))
307       return nullptr;
308     return Symtab->addUndefined(Name, this, false);
309   }
310   if (SC)
311     return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
312                                 /*IsExternal*/ false, Sym.getGeneric(), SC);
313   return nullptr;
314 }
315 
316 void ObjFile::initializeSymbols() {
317   uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
318   Symbols.resize(NumSymbols);
319 
320   SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
321   std::vector<uint32_t> PendingIndexes;
322   PendingIndexes.reserve(NumSymbols);
323 
324   DenseMap<StringRef, uint32_t> PrevailingSectionMap;
325   std::vector<const coff_aux_section_definition *> ComdatDefs(
326       COFFObj->getNumberOfSections() + 1);
327 
328   for (uint32_t I = 0; I < NumSymbols; ++I) {
329     COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
330     bool PrevailingComdat;
331     if (COFFSym.isUndefined()) {
332       Symbols[I] = createUndefined(COFFSym);
333     } else if (COFFSym.isWeakExternal()) {
334       Symbols[I] = createUndefined(COFFSym);
335       uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
336       WeakAliases.emplace_back(Symbols[I], TagIndex);
337     } else if (Optional<Symbol *> OptSym =
338                    createDefined(COFFSym, ComdatDefs, PrevailingComdat)) {
339       Symbols[I] = *OptSym;
340       if (Config->MinGW && PrevailingComdat)
341         recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap);
342     } else {
343       // createDefined() returns None if a symbol belongs to a section that
344       // was pending at the point when the symbol was read. This can happen in
345       // two cases:
346       // 1) section definition symbol for a comdat leader;
347       // 2) symbol belongs to a comdat section associated with another section.
348       // In both of these cases, we can expect the section to be resolved by
349       // the time we finish visiting the remaining symbols in the symbol
350       // table. So we postpone the handling of this symbol until that time.
351       PendingIndexes.push_back(I);
352     }
353     I += COFFSym.getNumberOfAuxSymbols();
354   }
355 
356   for (uint32_t I : PendingIndexes) {
357     COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
358     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
359       if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
360         readAssociativeDefinition(Sym, Def);
361       else if (Config->MinGW)
362         maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap);
363     }
364     if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) {
365       StringRef Name;
366       COFFObj->getSymbolName(Sym, Name);
367       log("comdat section " + Name +
368           " without leader and unassociated, discarding");
369       continue;
370     }
371     Symbols[I] = createRegular(Sym);
372   }
373 
374   for (auto &KV : WeakAliases) {
375     Symbol *Sym = KV.first;
376     uint32_t Idx = KV.second;
377     checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]);
378   }
379 }
380 
381 Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
382   StringRef Name;
383   COFFObj->getSymbolName(Sym, Name);
384   return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
385 }
386 
387 void ObjFile::handleComdatSelection(COFFSymbolRef Sym, COMDATType &Selection,
388                                     bool &Prevailing, DefinedRegular *Leader) {
389   if (Prevailing)
390     return;
391   // There's already an existing comdat for this symbol: `Leader`.
392   // Use the comdats's selection field to determine if the new
393   // symbol in `Sym` should be discarded, produce a duplicate symbol
394   // error, etc.
395 
396   SectionChunk *LeaderChunk = nullptr;
397   COMDATType LeaderSelection = IMAGE_COMDAT_SELECT_ANY;
398 
399   if (Leader->Data) {
400     LeaderChunk = Leader->getChunk();
401     LeaderSelection = LeaderChunk->Selection;
402   } else {
403     // FIXME: comdats from LTO files don't know their selection; treat them
404     // as "any".
405     Selection = LeaderSelection;
406   }
407 
408   if ((Selection == IMAGE_COMDAT_SELECT_ANY &&
409        LeaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
410       (Selection == IMAGE_COMDAT_SELECT_LARGEST &&
411        LeaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
412     // cl.exe picks "any" for vftables when building with /GR- and
413     // "largest" when building with /GR. To be able to link object files
414     // compiled with each flag, "any" and "largest" are merged as "largest".
415     LeaderSelection = Selection = IMAGE_COMDAT_SELECT_LARGEST;
416   }
417 
418   // Other than that, comdat selections must match.  This is a bit more
419   // strict than link.exe which allows merging "any" and "largest" if "any"
420   // is the first symbol the linker sees, and it allows merging "largest"
421   // with everything (!) if "largest" is the first symbol the linker sees.
422   // Making this symmetric independent of which selection is seen first
423   // seems better though.
424   // (This behavior matches ModuleLinker::getComdatResult().)
425   if (Selection != LeaderSelection) {
426     log(("conflicting comdat type for " + toString(*Leader) + ": " +
427          Twine((int)LeaderSelection) + " in " + toString(Leader->getFile()) +
428          " and " + Twine((int)Selection) + " in " + toString(this))
429             .str());
430     Symtab->reportDuplicate(Leader, this);
431     return;
432   }
433 
434   switch (Selection) {
435   case IMAGE_COMDAT_SELECT_NODUPLICATES:
436     Symtab->reportDuplicate(Leader, this);
437     break;
438 
439   case IMAGE_COMDAT_SELECT_ANY:
440     // Nothing to do.
441     break;
442 
443   case IMAGE_COMDAT_SELECT_SAME_SIZE:
444     if (LeaderChunk->getSize() != getSection(Sym)->SizeOfRawData)
445       Symtab->reportDuplicate(Leader, this);
446     break;
447 
448   case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
449     SectionChunk NewChunk(this, getSection(Sym));
450     // link.exe only compares section contents here and doesn't complain
451     // if the two comdat sections have e.g. different alignment.
452     // Match that.
453     if (LeaderChunk->getContents() != NewChunk.getContents())
454       Symtab->reportDuplicate(Leader, this);
455     break;
456   }
457 
458   case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
459     // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
460     // (This means lld-link doesn't produce duplicate symbol errors for
461     // associative comdats while link.exe does, but associate comdats
462     // are never extern in practice.)
463     llvm_unreachable("createDefined not called for associative comdats");
464 
465   case IMAGE_COMDAT_SELECT_LARGEST:
466     if (LeaderChunk->getSize() < getSection(Sym)->SizeOfRawData) {
467       // Replace the existing comdat symbol with the new one.
468       StringRef Name;
469       COFFObj->getSymbolName(Sym, Name);
470       // FIXME: This is incorrect: With /opt:noref, the previous sections
471       // make it into the final executable as well. Correct handling would
472       // be to undo reading of the whole old section that's being replaced,
473       // or doing one pass that determines what the final largest comdat
474       // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
475       // only the largest one.
476       replaceSymbol<DefinedRegular>(Leader, this, Name, /*IsCOMDAT*/ true,
477                                     /*IsExternal*/ true, Sym.getGeneric(),
478                                     nullptr);
479       Prevailing = true;
480     }
481     break;
482 
483   case IMAGE_COMDAT_SELECT_NEWEST:
484     llvm_unreachable("should have been rejected earlier");
485   }
486 }
487 
488 Optional<Symbol *> ObjFile::createDefined(
489     COFFSymbolRef Sym,
490     std::vector<const coff_aux_section_definition *> &ComdatDefs,
491     bool &Prevailing) {
492   Prevailing = false;
493   auto GetName = [&]() {
494     StringRef S;
495     COFFObj->getSymbolName(Sym, S);
496     return S;
497   };
498 
499   if (Sym.isCommon()) {
500     auto *C = make<CommonChunk>(Sym);
501     Chunks.push_back(C);
502     return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
503                              C);
504   }
505 
506   if (Sym.isAbsolute()) {
507     StringRef Name = GetName();
508 
509     // Skip special symbols.
510     if (Name == "@comp.id")
511       return nullptr;
512     if (Name == "@feat.00") {
513       Feat00Flags = Sym.getValue();
514       return nullptr;
515     }
516 
517     if (Sym.isExternal())
518       return Symtab->addAbsolute(Name, Sym);
519     return make<DefinedAbsolute>(Name, Sym);
520   }
521 
522   int32_t SectionNumber = Sym.getSectionNumber();
523   if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
524     return nullptr;
525 
526   if (llvm::COFF::isReservedSectionNumber(SectionNumber))
527     fatal(toString(this) + ": " + GetName() +
528           " should not refer to special section " + Twine(SectionNumber));
529 
530   if ((uint32_t)SectionNumber >= SparseChunks.size())
531     fatal(toString(this) + ": " + GetName() +
532           " should not refer to non-existent section " + Twine(SectionNumber));
533 
534   // Comdat handling.
535   // A comdat symbol consists of two symbol table entries.
536   // The first symbol entry has the name of the section (e.g. .text), fixed
537   // values for the other fields, and one auxilliary record.
538   // The second symbol entry has the name of the comdat symbol, called the
539   // "comdat leader".
540   // When this function is called for the first symbol entry of a comdat,
541   // it sets ComdatDefs and returns None, and when it's called for the second
542   // symbol entry it reads ComdatDefs and then sets it back to nullptr.
543 
544   // Handle comdat leader.
545   if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
546     ComdatDefs[SectionNumber] = nullptr;
547     DefinedRegular *Leader;
548 
549     if (Sym.isExternal()) {
550       std::tie(Leader, Prevailing) =
551           Symtab->addComdat(this, GetName(), Sym.getGeneric());
552     } else {
553       Leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
554                                     /*IsExternal*/ false, Sym.getGeneric());
555       Prevailing = true;
556     }
557 
558     if (Def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
559         // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
560         // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
561         Def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
562       fatal("unknown comdat type " + std::to_string((int)Def->Selection) +
563             " for " + GetName() + " in " + toString(this));
564     }
565     COMDATType Selection = (COMDATType)Def->Selection;
566 
567     if (Leader->isCOMDAT())
568       handleComdatSelection(Sym, Selection, Prevailing, Leader);
569 
570     if (Prevailing) {
571       SectionChunk *C = readSection(SectionNumber, Def, GetName());
572       SparseChunks[SectionNumber] = C;
573       C->Sym = cast<DefinedRegular>(Leader);
574       C->Selection = Selection;
575       cast<DefinedRegular>(Leader)->Data = &C->Repl;
576     } else {
577       SparseChunks[SectionNumber] = nullptr;
578     }
579     return Leader;
580   }
581 
582   // Prepare to handle the comdat leader symbol by setting the section's
583   // ComdatDefs pointer if we encounter a non-associative comdat.
584   if (SparseChunks[SectionNumber] == PendingComdat) {
585     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
586       if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
587         ComdatDefs[SectionNumber] = Def;
588     }
589     return None;
590   }
591 
592   return createRegular(Sym);
593 }
594 
595 MachineTypes ObjFile::getMachineType() {
596   if (COFFObj)
597     return static_cast<MachineTypes>(COFFObj->getMachine());
598   return IMAGE_FILE_MACHINE_UNKNOWN;
599 }
600 
601 StringRef ltrim1(StringRef S, const char *Chars) {
602   if (!S.empty() && strchr(Chars, S[0]))
603     return S.substr(1);
604   return S;
605 }
606 
607 void ImportFile::parse() {
608   const char *Buf = MB.getBufferStart();
609   const char *End = MB.getBufferEnd();
610   const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
611 
612   // Check if the total size is valid.
613   if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
614     fatal("broken import library");
615 
616   // Read names and create an __imp_ symbol.
617   StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
618   StringRef ImpName = Saver.save("__imp_" + Name);
619   const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
620   DLLName = StringRef(NameStart);
621   StringRef ExtName;
622   switch (Hdr->getNameType()) {
623   case IMPORT_ORDINAL:
624     ExtName = "";
625     break;
626   case IMPORT_NAME:
627     ExtName = Name;
628     break;
629   case IMPORT_NAME_NOPREFIX:
630     ExtName = ltrim1(Name, "?@_");
631     break;
632   case IMPORT_NAME_UNDECORATE:
633     ExtName = ltrim1(Name, "?@_");
634     ExtName = ExtName.substr(0, ExtName.find('@'));
635     break;
636   }
637 
638   this->Hdr = Hdr;
639   ExternalName = ExtName;
640 
641   ImpSym = Symtab->addImportData(ImpName, this);
642   // If this was a duplicate, we logged an error but may continue;
643   // in this case, ImpSym is nullptr.
644   if (!ImpSym)
645     return;
646 
647   if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
648     static_cast<void>(Symtab->addImportData(Name, this));
649 
650   // If type is function, we need to create a thunk which jump to an
651   // address pointed by the __imp_ symbol. (This allows you to call
652   // DLL functions just like regular non-DLL functions.)
653   if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
654     ThunkSym = Symtab->addImportThunk(
655         Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
656 }
657 
658 void BitcodeFile::parse() {
659   Obj = check(lto::InputFile::create(MemoryBufferRef(
660       MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
661   std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
662   for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
663     // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
664     // selection handling.
665     Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
666   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
667     StringRef SymName = Saver.save(ObjSym.getName());
668     int ComdatIndex = ObjSym.getComdatIndex();
669     Symbol *Sym;
670     if (ObjSym.isUndefined()) {
671       Sym = Symtab->addUndefined(SymName, this, false);
672     } else if (ObjSym.isCommon()) {
673       Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
674     } else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
675       // Weak external.
676       Sym = Symtab->addUndefined(SymName, this, true);
677       std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
678       Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
679       checkAndSetWeakAlias(Symtab, this, Sym, Alias);
680     } else if (ComdatIndex != -1) {
681       if (SymName == Obj->getComdatTable()[ComdatIndex])
682         Sym = Comdat[ComdatIndex].first;
683       else if (Comdat[ComdatIndex].second)
684         Sym = Symtab->addRegular(this, SymName);
685       else
686         Sym = Symtab->addUndefined(SymName, this, false);
687     } else {
688       Sym = Symtab->addRegular(this, SymName);
689     }
690     Symbols.push_back(Sym);
691   }
692   Directives = Obj->getCOFFLinkerOpts();
693 }
694 
695 MachineTypes BitcodeFile::getMachineType() {
696   switch (Triple(Obj->getTargetTriple()).getArch()) {
697   case Triple::x86_64:
698     return AMD64;
699   case Triple::x86:
700     return I386;
701   case Triple::arm:
702     return ARMNT;
703   case Triple::aarch64:
704     return ARM64;
705   default:
706     return IMAGE_FILE_MACHINE_UNKNOWN;
707   }
708 }
709 } // namespace coff
710 } // namespace lld
711 
712 // Returns the last element of a path, which is supposed to be a filename.
713 static StringRef getBasename(StringRef Path) {
714   return sys::path::filename(Path, sys::path::Style::windows);
715 }
716 
717 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
718 std::string lld::toString(const coff::InputFile *File) {
719   if (!File)
720     return "<internal>";
721   if (File->ParentName.empty())
722     return File->getName();
723 
724   return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
725           ")")
726       .str();
727 }
728