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 Optional<Symbol *> ObjFile::createDefined(
388     COFFSymbolRef Sym,
389     std::vector<const coff_aux_section_definition *> &ComdatDefs,
390     bool &Prevailing) {
391   Prevailing = false;
392   auto GetName = [&]() {
393     StringRef S;
394     COFFObj->getSymbolName(Sym, S);
395     return S;
396   };
397 
398   if (Sym.isCommon()) {
399     auto *C = make<CommonChunk>(Sym);
400     Chunks.push_back(C);
401     return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
402                              C);
403   }
404 
405   if (Sym.isAbsolute()) {
406     StringRef Name = GetName();
407 
408     // Skip special symbols.
409     if (Name == "@comp.id")
410       return nullptr;
411     if (Name == "@feat.00") {
412       Feat00Flags = Sym.getValue();
413       return nullptr;
414     }
415 
416     if (Sym.isExternal())
417       return Symtab->addAbsolute(Name, Sym);
418     return make<DefinedAbsolute>(Name, Sym);
419   }
420 
421   int32_t SectionNumber = Sym.getSectionNumber();
422   if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
423     return nullptr;
424 
425   if (llvm::COFF::isReservedSectionNumber(SectionNumber))
426     fatal(toString(this) + ": " + GetName() +
427           " should not refer to special section " + Twine(SectionNumber));
428 
429   if ((uint32_t)SectionNumber >= SparseChunks.size())
430     fatal(toString(this) + ": " + GetName() +
431           " should not refer to non-existent section " + Twine(SectionNumber));
432 
433   // Comdat handling.
434   // A comdat symbol consists of two symbol table entries.
435   // The first symbol entry has the name of the section (e.g. .text), fixed
436   // values for the other fields, and one auxilliary record.
437   // The second symbol entry has the name of the comdat symbol, called the
438   // "comdat leader".
439   // When this function is called for the first symbol entry of a comdat,
440   // it sets ComdatDefs and returns None, and when it's called for the second
441   // symbol entry it reads ComdatDefs and then sets it back to nullptr.
442 
443   // Handle comdat leader.
444   if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
445     ComdatDefs[SectionNumber] = nullptr;
446     DefinedRegular *Leader;
447 
448     if (Sym.isExternal()) {
449       std::tie(Leader, Prevailing) =
450           Symtab->addComdat(this, GetName(), Sym.getGeneric());
451     } else {
452       Leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
453                                     /*IsExternal*/ false, Sym.getGeneric());
454       Prevailing = true;
455     }
456 
457     if (Def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
458         // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
459         // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
460         Def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
461       fatal("unknown comdat type " + std::to_string((int)Def->Selection) +
462             " for " + GetName() + " in " + toString(this));
463     }
464     COMDATType Selection = (COMDATType)Def->Selection;
465 
466     if (!Prevailing && Leader->isCOMDAT()) {
467       // There's already an existing comdat for this symbol: `Leader`.
468       // Use the comdats's selection field to determine if the new
469       // symbol in `Sym` should be discarded, produce a duplicate symbol
470       // error, etc.
471 
472       SectionChunk *LeaderChunk = nullptr;
473       COMDATType LeaderSelection = IMAGE_COMDAT_SELECT_ANY;
474 
475       if (Leader->Data) {
476         LeaderChunk = Leader->getChunk();
477         LeaderSelection = LeaderChunk->Selection;
478       } else {
479         // FIXME: comdats from LTO files don't know their selection; treat them
480         // as "any".
481         Selection = LeaderSelection;
482       }
483 
484       if ((Selection == IMAGE_COMDAT_SELECT_ANY &&
485            LeaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
486           (Selection == IMAGE_COMDAT_SELECT_LARGEST &&
487            LeaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
488         // cl.exe picks "any" for vftables when building with /GR- and
489         // "largest" when building with /GR. To be able to link object files
490         // compiled with each flag, "any" and "largest" are merged as "largest".
491         LeaderSelection = Selection = IMAGE_COMDAT_SELECT_LARGEST;
492       }
493 
494       // Other than that, comdat selections must match.  This is a bit more
495       // strict than link.exe which allows merging "any" and "largest" if "any"
496       // is the first symbol the linker sees, and it allows merging "largest"
497       // with everything (!) if "largest" is the first symbol the linker sees.
498       // Making this symmetric independent of which selection is seen first
499       // seems better though.
500       // (This behavior matches ModuleLinker::getComdatResult().)
501       if (Selection != LeaderSelection) {
502         std::string Msg = ("conflicting comdat type for " + toString(*Leader) +
503                            ": " + Twine((int)LeaderSelection) + " in " +
504                            toString(Leader->getFile()) + " and " +
505                            Twine((int)Selection) + " in " + toString(this))
506                               .str();
507         if (Config->ForceMultiple)
508           warn(Msg);
509         else
510           error(Msg);
511       }
512 
513       switch (Selection) {
514       case IMAGE_COMDAT_SELECT_NODUPLICATES:
515         Symtab->reportDuplicate(Leader, this);
516         break;
517 
518       case IMAGE_COMDAT_SELECT_ANY:
519         // Nothing to do.
520         break;
521 
522       case IMAGE_COMDAT_SELECT_SAME_SIZE:
523         if (LeaderChunk->getSize() != getSection(SectionNumber)->SizeOfRawData)
524           Symtab->reportDuplicate(Leader, this);
525         break;
526 
527       case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
528         SectionChunk NewChunk(this, getSection(SectionNumber));
529         // link.exe only compares section contents here and doesn't complain
530         // if the two comdat sections have e.g. different alignment.
531         // Match that.
532         if (LeaderChunk->getContents() != NewChunk.getContents())
533           Symtab->reportDuplicate(Leader, this);
534         break;
535       }
536 
537       case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
538         // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
539         // (This means lld-link doesn't produce duplicate symbol errors for
540         // associative comdats while link.exe does, but associate comdats
541         // are never extern in practice.)
542         llvm_unreachable("createDefined not called for associative comdats");
543 
544       case IMAGE_COMDAT_SELECT_LARGEST:
545         if (LeaderChunk->getSize() < getSection(SectionNumber)->SizeOfRawData) {
546           // Replace the existing comdat symbol with the new one.
547           // FIXME: This is incorrect: With /opt:noref, the previous sections
548           // make it into the final executable as well. Correct handling would
549           // be to undo reading of the whole old section that's being replaced,
550           // or doing one pass that determines what the final largest comdat
551           // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
552           // only the largest one.
553           replaceSymbol<DefinedRegular>(
554               Leader, this, GetName(), /*IsCOMDAT*/ true,
555               /*IsExternal*/ true, Sym.getGeneric(), nullptr);
556           Prevailing = true;
557         }
558         break;
559 
560       case IMAGE_COMDAT_SELECT_NEWEST:
561         llvm_unreachable("should have been rejected earlier");
562       }
563     }
564 
565     if (Prevailing) {
566       SectionChunk *C = readSection(SectionNumber, Def, GetName());
567       SparseChunks[SectionNumber] = C;
568       C->Sym = cast<DefinedRegular>(Leader);
569       C->Selection = Selection;
570       cast<DefinedRegular>(Leader)->Data = &C->Repl;
571     } else {
572       SparseChunks[SectionNumber] = nullptr;
573     }
574     return Leader;
575   }
576 
577   // Prepare to handle the comdat leader symbol by setting the section's
578   // ComdatDefs pointer if we encounter a non-associative comdat.
579   if (SparseChunks[SectionNumber] == PendingComdat) {
580     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
581       if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
582         ComdatDefs[SectionNumber] = Def;
583     }
584     return None;
585   }
586 
587   return createRegular(Sym);
588 }
589 
590 MachineTypes ObjFile::getMachineType() {
591   if (COFFObj)
592     return static_cast<MachineTypes>(COFFObj->getMachine());
593   return IMAGE_FILE_MACHINE_UNKNOWN;
594 }
595 
596 StringRef ltrim1(StringRef S, const char *Chars) {
597   if (!S.empty() && strchr(Chars, S[0]))
598     return S.substr(1);
599   return S;
600 }
601 
602 void ImportFile::parse() {
603   const char *Buf = MB.getBufferStart();
604   const char *End = MB.getBufferEnd();
605   const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
606 
607   // Check if the total size is valid.
608   if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
609     fatal("broken import library");
610 
611   // Read names and create an __imp_ symbol.
612   StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
613   StringRef ImpName = Saver.save("__imp_" + Name);
614   const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
615   DLLName = StringRef(NameStart);
616   StringRef ExtName;
617   switch (Hdr->getNameType()) {
618   case IMPORT_ORDINAL:
619     ExtName = "";
620     break;
621   case IMPORT_NAME:
622     ExtName = Name;
623     break;
624   case IMPORT_NAME_NOPREFIX:
625     ExtName = ltrim1(Name, "?@_");
626     break;
627   case IMPORT_NAME_UNDECORATE:
628     ExtName = ltrim1(Name, "?@_");
629     ExtName = ExtName.substr(0, ExtName.find('@'));
630     break;
631   }
632 
633   this->Hdr = Hdr;
634   ExternalName = ExtName;
635 
636   ImpSym = Symtab->addImportData(ImpName, this);
637   // If this was a duplicate, we logged an error but may continue;
638   // in this case, ImpSym is nullptr.
639   if (!ImpSym)
640     return;
641 
642   if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
643     static_cast<void>(Symtab->addImportData(Name, this));
644 
645   // If type is function, we need to create a thunk which jump to an
646   // address pointed by the __imp_ symbol. (This allows you to call
647   // DLL functions just like regular non-DLL functions.)
648   if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
649     ThunkSym = Symtab->addImportThunk(
650         Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
651 }
652 
653 void BitcodeFile::parse() {
654   Obj = check(lto::InputFile::create(MemoryBufferRef(
655       MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
656   std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
657   for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
658     // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
659     // selection handling.
660     Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
661   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
662     StringRef SymName = Saver.save(ObjSym.getName());
663     int ComdatIndex = ObjSym.getComdatIndex();
664     Symbol *Sym;
665     if (ObjSym.isUndefined()) {
666       Sym = Symtab->addUndefined(SymName, this, false);
667     } else if (ObjSym.isCommon()) {
668       Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
669     } else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
670       // Weak external.
671       Sym = Symtab->addUndefined(SymName, this, true);
672       std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
673       Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
674       checkAndSetWeakAlias(Symtab, this, Sym, Alias);
675     } else if (ComdatIndex != -1) {
676       if (SymName == Obj->getComdatTable()[ComdatIndex])
677         Sym = Comdat[ComdatIndex].first;
678       else if (Comdat[ComdatIndex].second)
679         Sym = Symtab->addRegular(this, SymName);
680       else
681         Sym = Symtab->addUndefined(SymName, this, false);
682     } else {
683       Sym = Symtab->addRegular(this, SymName);
684     }
685     Symbols.push_back(Sym);
686   }
687   Directives = Obj->getCOFFLinkerOpts();
688 }
689 
690 MachineTypes BitcodeFile::getMachineType() {
691   switch (Triple(Obj->getTargetTriple()).getArch()) {
692   case Triple::x86_64:
693     return AMD64;
694   case Triple::x86:
695     return I386;
696   case Triple::arm:
697     return ARMNT;
698   case Triple::aarch64:
699     return ARM64;
700   default:
701     return IMAGE_FILE_MACHINE_UNKNOWN;
702   }
703 }
704 } // namespace coff
705 } // namespace lld
706 
707 // Returns the last element of a path, which is supposed to be a filename.
708 static StringRef getBasename(StringRef Path) {
709   return sys::path::filename(Path, sys::path::Style::windows);
710 }
711 
712 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
713 std::string lld::toString(const coff::InputFile *File) {
714   if (!File)
715     return "<internal>";
716   if (File->ParentName.empty())
717     return File->getName();
718 
719   return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
720           ")")
721       .str();
722 }
723