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 // We set SectionChunk pointers in the SparseChunks vector to this value
130 // temporarily to mark comdat sections as having an unknown resolution. As we
131 // walk the object file's symbol table, once we visit either a leader symbol or
132 // an associative section definition together with the parent comdat's leader,
133 // we set the pointer to either nullptr (to mark the section as discarded) or a
134 // valid SectionChunk for that section.
135 static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1);
136 
137 void ObjFile::initializeChunks() {
138   uint32_t NumSections = COFFObj->getNumberOfSections();
139   Chunks.reserve(NumSections);
140   SparseChunks.resize(NumSections + 1);
141   for (uint32_t I = 1; I < NumSections + 1; ++I) {
142     const coff_section *Sec;
143     if (auto EC = COFFObj->getSection(I, Sec))
144       fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
145 
146     if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
147       SparseChunks[I] = PendingComdat;
148     else
149       SparseChunks[I] = readSection(I, nullptr, "");
150   }
151 }
152 
153 SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
154                                    const coff_aux_section_definition *Def,
155                                    StringRef LeaderName) {
156   const coff_section *Sec;
157   if (auto EC = COFFObj->getSection(SectionNumber, Sec))
158     fatal("getSection failed: #" + Twine(SectionNumber) + ": " + EC.message());
159 
160   StringRef Name;
161   if (auto EC = COFFObj->getSectionName(Sec, Name))
162     fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
163           EC.message());
164 
165   if (Name == ".drectve") {
166     ArrayRef<uint8_t> Data;
167     COFFObj->getSectionContents(Sec, Data);
168     Directives = std::string((const char *)Data.data(), Data.size());
169     return nullptr;
170   }
171 
172   if (Name == ".llvm_addrsig") {
173     AddrsigSec = Sec;
174     return nullptr;
175   }
176 
177   // Object files may have DWARF debug info or MS CodeView debug info
178   // (or both).
179   //
180   // DWARF sections don't need any special handling from the perspective
181   // of the linker; they are just a data section containing relocations.
182   // We can just link them to complete debug info.
183   //
184   // CodeView needs linker support. We need to interpret debug info,
185   // and then write it to a separate .pdb file.
186 
187   // Ignore DWARF debug info unless /debug is given.
188   if (!Config->Debug && Name.startswith(".debug_"))
189     return nullptr;
190 
191   if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
192     return nullptr;
193   auto *C = make<SectionChunk>(this, Sec);
194   if (Def)
195     C->Checksum = Def->CheckSum;
196 
197   // CodeView sections are stored to a different vector because they are not
198   // linked in the regular manner.
199   if (C->isCodeView())
200     DebugChunks.push_back(C);
201   else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gfids$y")
202     GuardFidChunks.push_back(C);
203   else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gljmp$y")
204     GuardLJmpChunks.push_back(C);
205   else if (Name == ".sxdata")
206     SXDataChunks.push_back(C);
207   else if (Config->TailMerge && Sec->NumberOfRelocations == 0 &&
208            Name == ".rdata" && LeaderName.startswith("??_C@"))
209     // COFF sections that look like string literal sections (i.e. no
210     // relocations, in .rdata, leader symbol name matches the MSVC name mangling
211     // for string literals) are subject to string tail merging.
212     MergeChunk::addSection(C);
213   else
214     Chunks.push_back(C);
215 
216   return C;
217 }
218 
219 void ObjFile::readAssociativeDefinition(
220     COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
221   readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj()));
222 }
223 
224 void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
225                                         const coff_aux_section_definition *Def,
226                                         uint32_t ParentIndex) {
227   SectionChunk *Parent = SparseChunks[ParentIndex];
228 
229   auto Diag = [&]() {
230     StringRef Name, ParentName;
231     COFFObj->getSymbolName(Sym, Name);
232 
233     const coff_section *ParentSec;
234     COFFObj->getSection(ParentIndex, ParentSec);
235     COFFObj->getSectionName(ParentSec, ParentName);
236     error(toString(this) + ": associative comdat " + Name +
237           " has invalid reference to section " + ParentName);
238   };
239 
240   if (Parent == PendingComdat) {
241     // This can happen if an associative comdat refers to another associative
242     // comdat that appears after it (invalid per COFF spec) or to a section
243     // without any symbols.
244     Diag();
245     return;
246   }
247 
248   // Check whether the parent is prevailing. If it is, so are we, and we read
249   // the section; otherwise mark it as discarded.
250   int32_t SectionNumber = Sym.getSectionNumber();
251   if (Parent) {
252     if (Parent->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
253       Diag();
254       return;
255     }
256 
257     SectionChunk *C = readSection(SectionNumber, Def, "");
258     C->Selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
259     SparseChunks[SectionNumber] = C;
260     if (SparseChunks[SectionNumber])
261       Parent->addAssociative(SparseChunks[SectionNumber]);
262   } else {
263     SparseChunks[SectionNumber] = nullptr;
264   }
265 }
266 
267 void ObjFile::recordPrevailingSymbolForMingw(
268     COFFSymbolRef Sym, DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
269   // For comdat symbols in executable sections, where this is the copy
270   // of the section chunk we actually include instead of discarding it,
271   // add the symbol to a map to allow using it for implicitly
272   // associating .[px]data$<func> sections to it.
273   int32_t SectionNumber = Sym.getSectionNumber();
274   SectionChunk *SC = SparseChunks[SectionNumber];
275   if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
276     StringRef Name;
277     COFFObj->getSymbolName(Sym, Name);
278     PrevailingSectionMap[Name] = SectionNumber;
279   }
280 }
281 
282 void ObjFile::maybeAssociateSEHForMingw(
283     COFFSymbolRef Sym, const coff_aux_section_definition *Def,
284     const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
285   StringRef Name;
286   COFFObj->getSymbolName(Sym, Name);
287   if (Name.consume_front(".pdata$") || Name.consume_front(".xdata$")) {
288     // For MinGW, treat .[px]data$<func> as implicitly associative to
289     // the symbol <func>.
290     auto ParentSym = PrevailingSectionMap.find(Name);
291     if (ParentSym != PrevailingSectionMap.end())
292       readAssociativeDefinition(Sym, Def, ParentSym->second);
293   }
294 }
295 
296 Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
297   SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
298   if (Sym.isExternal()) {
299     StringRef Name;
300     COFFObj->getSymbolName(Sym, Name);
301     if (SC)
302       return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
303     // For MinGW symbols named .weak.* that point to a discarded section,
304     // don't create an Undefined symbol. If nothing ever refers to the symbol,
305     // everything should be fine. If something actually refers to the symbol
306     // (e.g. the undefined weak alias), linking will fail due to undefined
307     // references at the end.
308     if (Config->MinGW && Name.startswith(".weak."))
309       return nullptr;
310     return Symtab->addUndefined(Name, this, false);
311   }
312   if (SC)
313     return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
314                                 /*IsExternal*/ false, Sym.getGeneric(), SC);
315   return nullptr;
316 }
317 
318 void ObjFile::initializeSymbols() {
319   uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
320   Symbols.resize(NumSymbols);
321 
322   SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
323   std::vector<uint32_t> PendingIndexes;
324   PendingIndexes.reserve(NumSymbols);
325 
326   DenseMap<StringRef, uint32_t> PrevailingSectionMap;
327   std::vector<const coff_aux_section_definition *> ComdatDefs(
328       COFFObj->getNumberOfSections() + 1);
329 
330   for (uint32_t I = 0; I < NumSymbols; ++I) {
331     COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
332     bool PrevailingComdat;
333     if (COFFSym.isUndefined()) {
334       Symbols[I] = createUndefined(COFFSym);
335     } else if (COFFSym.isWeakExternal()) {
336       Symbols[I] = createUndefined(COFFSym);
337       uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
338       WeakAliases.emplace_back(Symbols[I], TagIndex);
339     } else if (Optional<Symbol *> OptSym =
340                    createDefined(COFFSym, ComdatDefs, PrevailingComdat)) {
341       Symbols[I] = *OptSym;
342       if (Config->MinGW && PrevailingComdat)
343         recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap);
344     } else {
345       // createDefined() returns None if a symbol belongs to a section that
346       // was pending at the point when the symbol was read. This can happen in
347       // two cases:
348       // 1) section definition symbol for a comdat leader;
349       // 2) symbol belongs to a comdat section associated with another section.
350       // In both of these cases, we can expect the section to be resolved by
351       // the time we finish visiting the remaining symbols in the symbol
352       // table. So we postpone the handling of this symbol until that time.
353       PendingIndexes.push_back(I);
354     }
355     I += COFFSym.getNumberOfAuxSymbols();
356   }
357 
358   for (uint32_t I : PendingIndexes) {
359     COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
360     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
361       if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
362         readAssociativeDefinition(Sym, Def);
363       else if (Config->MinGW)
364         maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap);
365     }
366     if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) {
367       StringRef Name;
368       COFFObj->getSymbolName(Sym, Name);
369       log("comdat section " + Name +
370           " without leader and unassociated, discarding");
371       continue;
372     }
373     Symbols[I] = createRegular(Sym);
374   }
375 
376   for (auto &KV : WeakAliases) {
377     Symbol *Sym = KV.first;
378     uint32_t Idx = KV.second;
379     checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]);
380   }
381 }
382 
383 Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
384   StringRef Name;
385   COFFObj->getSymbolName(Sym, Name);
386   return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
387 }
388 
389 Optional<Symbol *> ObjFile::createDefined(
390     COFFSymbolRef Sym,
391     std::vector<const coff_aux_section_definition *> &ComdatDefs,
392     bool &Prevailing) {
393   Prevailing = false;
394   auto GetName = [&]() {
395     StringRef S;
396     COFFObj->getSymbolName(Sym, S);
397     return S;
398   };
399 
400   if (Sym.isCommon()) {
401     auto *C = make<CommonChunk>(Sym);
402     Chunks.push_back(C);
403     return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
404                              C);
405   }
406 
407   if (Sym.isAbsolute()) {
408     StringRef Name = GetName();
409 
410     // Skip special symbols.
411     if (Name == "@comp.id")
412       return nullptr;
413     if (Name == "@feat.00") {
414       Feat00Flags = Sym.getValue();
415       return nullptr;
416     }
417 
418     if (Sym.isExternal())
419       return Symtab->addAbsolute(Name, Sym);
420     return make<DefinedAbsolute>(Name, Sym);
421   }
422 
423   int32_t SectionNumber = Sym.getSectionNumber();
424   if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
425     return nullptr;
426 
427   if (llvm::COFF::isReservedSectionNumber(SectionNumber))
428     fatal(toString(this) + ": " + GetName() +
429           " should not refer to special section " + Twine(SectionNumber));
430 
431   if ((uint32_t)SectionNumber >= SparseChunks.size())
432     fatal(toString(this) + ": " + GetName() +
433           " should not refer to non-existent section " + Twine(SectionNumber));
434 
435   // Handle comdat leader symbols.
436   if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
437     ComdatDefs[SectionNumber] = nullptr;
438     Symbol *Leader;
439     if (Sym.isExternal()) {
440       std::tie(Leader, Prevailing) =
441           Symtab->addComdat(this, GetName(), Sym.getGeneric());
442     } else {
443       Leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
444                                     /*IsExternal*/ false, Sym.getGeneric());
445       Prevailing = true;
446     }
447 
448     if (Prevailing) {
449       SectionChunk *C = readSection(SectionNumber, Def, GetName());
450       SparseChunks[SectionNumber] = C;
451       C->Sym = cast<DefinedRegular>(Leader);
452       cast<DefinedRegular>(Leader)->Data = &C->Repl;
453     } else {
454       SparseChunks[SectionNumber] = nullptr;
455     }
456     return Leader;
457   }
458 
459   // Prepare to handle the comdat leader symbol by setting the section's
460   // ComdatDefs pointer if we encounter a non-associative comdat.
461   if (SparseChunks[SectionNumber] == PendingComdat) {
462     if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
463       if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
464         ComdatDefs[SectionNumber] = Def;
465     }
466     return None;
467   }
468 
469   return createRegular(Sym);
470 }
471 
472 MachineTypes ObjFile::getMachineType() {
473   if (COFFObj)
474     return static_cast<MachineTypes>(COFFObj->getMachine());
475   return IMAGE_FILE_MACHINE_UNKNOWN;
476 }
477 
478 StringRef ltrim1(StringRef S, const char *Chars) {
479   if (!S.empty() && strchr(Chars, S[0]))
480     return S.substr(1);
481   return S;
482 }
483 
484 void ImportFile::parse() {
485   const char *Buf = MB.getBufferStart();
486   const char *End = MB.getBufferEnd();
487   const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
488 
489   // Check if the total size is valid.
490   if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
491     fatal("broken import library");
492 
493   // Read names and create an __imp_ symbol.
494   StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
495   StringRef ImpName = Saver.save("__imp_" + Name);
496   const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
497   DLLName = StringRef(NameStart);
498   StringRef ExtName;
499   switch (Hdr->getNameType()) {
500   case IMPORT_ORDINAL:
501     ExtName = "";
502     break;
503   case IMPORT_NAME:
504     ExtName = Name;
505     break;
506   case IMPORT_NAME_NOPREFIX:
507     ExtName = ltrim1(Name, "?@_");
508     break;
509   case IMPORT_NAME_UNDECORATE:
510     ExtName = ltrim1(Name, "?@_");
511     ExtName = ExtName.substr(0, ExtName.find('@'));
512     break;
513   }
514 
515   this->Hdr = Hdr;
516   ExternalName = ExtName;
517 
518   ImpSym = Symtab->addImportData(ImpName, this);
519   // If this was a duplicate, we logged an error but may continue;
520   // in this case, ImpSym is nullptr.
521   if (!ImpSym)
522     return;
523 
524   if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
525     static_cast<void>(Symtab->addImportData(Name, this));
526 
527   // If type is function, we need to create a thunk which jump to an
528   // address pointed by the __imp_ symbol. (This allows you to call
529   // DLL functions just like regular non-DLL functions.)
530   if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
531     ThunkSym = Symtab->addImportThunk(
532         Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
533 }
534 
535 void BitcodeFile::parse() {
536   Obj = check(lto::InputFile::create(MemoryBufferRef(
537       MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
538   std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
539   for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
540     Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
541   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
542     StringRef SymName = Saver.save(ObjSym.getName());
543     int ComdatIndex = ObjSym.getComdatIndex();
544     Symbol *Sym;
545     if (ObjSym.isUndefined()) {
546       Sym = Symtab->addUndefined(SymName, this, false);
547     } else if (ObjSym.isCommon()) {
548       Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
549     } else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
550       // Weak external.
551       Sym = Symtab->addUndefined(SymName, this, true);
552       std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
553       Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
554       checkAndSetWeakAlias(Symtab, this, Sym, Alias);
555     } else if (ComdatIndex != -1) {
556       if (SymName == Obj->getComdatTable()[ComdatIndex])
557         Sym = Comdat[ComdatIndex].first;
558       else if (Comdat[ComdatIndex].second)
559         Sym = Symtab->addRegular(this, SymName);
560       else
561         Sym = Symtab->addUndefined(SymName, this, false);
562     } else {
563       Sym = Symtab->addRegular(this, SymName);
564     }
565     Symbols.push_back(Sym);
566   }
567   Directives = Obj->getCOFFLinkerOpts();
568 }
569 
570 MachineTypes BitcodeFile::getMachineType() {
571   switch (Triple(Obj->getTargetTriple()).getArch()) {
572   case Triple::x86_64:
573     return AMD64;
574   case Triple::x86:
575     return I386;
576   case Triple::arm:
577     return ARMNT;
578   case Triple::aarch64:
579     return ARM64;
580   default:
581     return IMAGE_FILE_MACHINE_UNKNOWN;
582   }
583 }
584 } // namespace coff
585 } // namespace lld
586 
587 // Returns the last element of a path, which is supposed to be a filename.
588 static StringRef getBasename(StringRef Path) {
589   return sys::path::filename(Path, sys::path::Style::windows);
590 }
591 
592 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
593 std::string lld::toString(const coff::InputFile *File) {
594   if (!File)
595     return "<internal>";
596   if (File->ParentName.empty())
597     return File->getName();
598 
599   return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
600           ")")
601       .str();
602 }
603