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 "Config.h"
11 #include "InputChunks.h"
12 #include "InputEvent.h"
13 #include "InputGlobal.h"
14 #include "SymbolTable.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "lld/Common/Reproduce.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/Wasm.h"
20 #include "llvm/Support/TarWriter.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 #define DEBUG_TYPE "lld"
24 
25 using namespace lld;
26 using namespace lld::wasm;
27 
28 using namespace llvm;
29 using namespace llvm::object;
30 using namespace llvm::wasm;
31 
32 std::unique_ptr<llvm::TarWriter> lld::wasm::Tar;
33 
34 Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) {
35   log("Loading: " + Path);
36 
37   auto MBOrErr = MemoryBuffer::getFile(Path);
38   if (auto EC = MBOrErr.getError()) {
39     error("cannot open " + Path + ": " + EC.message());
40     return None;
41   }
42   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
43   MemoryBufferRef MBRef = MB->getMemBufferRef();
44   make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
45 
46   if (Tar)
47     Tar->append(relativeToRoot(Path), MBRef.getBuffer());
48   return MBRef;
49 }
50 
51 InputFile *lld::wasm::createObjectFile(MemoryBufferRef MB,
52                                        StringRef ArchiveName) {
53   file_magic Magic = identify_magic(MB.getBuffer());
54   if (Magic == file_magic::wasm_object) {
55     std::unique_ptr<Binary> Bin = check(createBinary(MB));
56     auto *Obj = cast<WasmObjectFile>(Bin.get());
57     if (Obj->isSharedObject())
58       return make<SharedFile>(MB);
59     return make<ObjFile>(MB, ArchiveName);
60   }
61 
62   if (Magic == file_magic::bitcode)
63     return make<BitcodeFile>(MB, ArchiveName);
64 
65   fatal("unknown file type: " + MB.getBufferIdentifier());
66 }
67 
68 void ObjFile::dumpInfo() const {
69   log("info for: " + toString(this) +
70       "\n              Symbols : " + Twine(Symbols.size()) +
71       "\n     Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) +
72       "\n       Global Imports : " + Twine(WasmObj->getNumImportedGlobals()) +
73       "\n        Event Imports : " + Twine(WasmObj->getNumImportedEvents()));
74 }
75 
76 // Relocations contain either symbol or type indices.  This function takes a
77 // relocation and returns relocated index (i.e. translates from the input
78 // symbol/type space to the output symbol/type space).
79 uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const {
80   if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) {
81     assert(TypeIsUsed[Reloc.Index]);
82     return TypeMap[Reloc.Index];
83   }
84   const Symbol *Sym = Symbols[Reloc.Index];
85   if (auto *SS = dyn_cast<SectionSymbol>(Sym))
86     Sym = SS->getOutputSectionSymbol();
87   return Sym->getOutputSymbolIndex();
88 }
89 
90 // Relocations can contain addend for combined sections. This function takes a
91 // relocation and returns updated addend by offset in the output section.
92 uint32_t ObjFile::calcNewAddend(const WasmRelocation &Reloc) const {
93   switch (Reloc.Type) {
94   case R_WASM_MEMORY_ADDR_LEB:
95   case R_WASM_MEMORY_ADDR_SLEB:
96   case R_WASM_MEMORY_ADDR_I32:
97   case R_WASM_FUNCTION_OFFSET_I32:
98     return Reloc.Addend;
99   case R_WASM_SECTION_OFFSET_I32:
100     return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend;
101   default:
102     llvm_unreachable("unexpected relocation type");
103   }
104 }
105 
106 // Calculate the value we expect to find at the relocation location.
107 // This is used as a sanity check before applying a relocation to a given
108 // location.  It is useful for catching bugs in the compiler and linker.
109 uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const {
110   switch (Reloc.Type) {
111   case R_WASM_TABLE_INDEX_I32:
112   case R_WASM_TABLE_INDEX_SLEB:
113   case R_WASM_TABLE_INDEX_REL_SLEB: {
114     const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
115     return TableEntries[Sym.Info.ElementIndex];
116   }
117   case R_WASM_MEMORY_ADDR_SLEB:
118   case R_WASM_MEMORY_ADDR_I32:
119   case R_WASM_MEMORY_ADDR_LEB:
120   case R_WASM_MEMORY_ADDR_REL_SLEB: {
121     const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
122     if (Sym.isUndefined())
123       return 0;
124     const WasmSegment &Segment =
125         WasmObj->dataSegments()[Sym.Info.DataRef.Segment];
126     return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset +
127            Reloc.Addend;
128   }
129   case R_WASM_FUNCTION_OFFSET_I32: {
130     const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
131     InputFunction *F =
132         Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()];
133     return F->getFunctionInputOffset() + F->getFunctionCodeOffset() +
134            Reloc.Addend;
135   }
136   case R_WASM_SECTION_OFFSET_I32:
137     return Reloc.Addend;
138   case R_WASM_TYPE_INDEX_LEB:
139     return Reloc.Index;
140   case R_WASM_FUNCTION_INDEX_LEB:
141   case R_WASM_GLOBAL_INDEX_LEB:
142   case R_WASM_EVENT_INDEX_LEB: {
143     const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
144     return Sym.Info.ElementIndex;
145   }
146   default:
147     llvm_unreachable("unknown relocation type");
148   }
149 }
150 
151 // Translate from the relocation's index into the final linked output value.
152 uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const {
153   const Symbol* Sym = nullptr;
154   if (Reloc.Type != R_WASM_TYPE_INDEX_LEB) {
155     Sym = Symbols[Reloc.Index];
156 
157     // We can end up with relocations against non-live symbols.  For example
158     // in debug sections.
159     if ((isa<FunctionSymbol>(Sym) || isa<DataSymbol>(Sym)) && !Sym->isLive())
160       return 0;
161 
162     // Special handling for undefined data symbols.  Most relocations against
163     // such symbols cannot be resolved.
164     if (isa<DataSymbol>(Sym) && Sym->isUndefined()) {
165       if (Sym->isWeak() || Config->Relocatable)
166         return 0;
167       // R_WASM_MEMORY_ADDR_I32 relocations in PIC code are turned into runtime
168       // fixups in __wasm_apply_relocs
169       if (Config->Pic && Reloc.Type == R_WASM_MEMORY_ADDR_I32)
170         return 0;
171       if (Reloc.Type != R_WASM_GLOBAL_INDEX_LEB) {
172         llvm_unreachable(
173           ("invalid relocation against undefined data symbol: " + toString(*Sym))
174               .c_str());
175       }
176     }
177   }
178 
179   switch (Reloc.Type) {
180   case R_WASM_TABLE_INDEX_I32:
181   case R_WASM_TABLE_INDEX_SLEB:
182   case R_WASM_TABLE_INDEX_REL_SLEB:
183     if (Config->Pic && !getFunctionSymbol(Reloc.Index)->hasTableIndex())
184       return 0;
185     return getFunctionSymbol(Reloc.Index)->getTableIndex();
186   case R_WASM_MEMORY_ADDR_SLEB:
187   case R_WASM_MEMORY_ADDR_I32:
188   case R_WASM_MEMORY_ADDR_LEB:
189   case R_WASM_MEMORY_ADDR_REL_SLEB:
190     return cast<DefinedData>(Sym)->getVirtualAddress() + Reloc.Addend;
191   case R_WASM_TYPE_INDEX_LEB:
192     return TypeMap[Reloc.Index];
193   case R_WASM_FUNCTION_INDEX_LEB:
194     return getFunctionSymbol(Reloc.Index)->getFunctionIndex();
195   case R_WASM_GLOBAL_INDEX_LEB:
196     if (auto GS = dyn_cast<GlobalSymbol>(Sym))
197       return GS->getGlobalIndex();
198     return Sym->getGOTIndex();
199   case R_WASM_EVENT_INDEX_LEB:
200     return getEventSymbol(Reloc.Index)->getEventIndex();
201   case R_WASM_FUNCTION_OFFSET_I32: {
202     auto *F = cast<DefinedFunction>(Sym);
203     return F->Function->OutputOffset + F->Function->getFunctionCodeOffset() +
204            Reloc.Addend;
205   }
206   case R_WASM_SECTION_OFFSET_I32:
207     return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend;
208   default:
209     llvm_unreachable("unknown relocation type");
210   }
211 }
212 
213 template <class T>
214 static void setRelocs(const std::vector<T *> &Chunks,
215                       const WasmSection *Section) {
216   if (!Section)
217     return;
218 
219   ArrayRef<WasmRelocation> Relocs = Section->Relocations;
220   assert(std::is_sorted(Relocs.begin(), Relocs.end(),
221                         [](const WasmRelocation &R1, const WasmRelocation &R2) {
222                           return R1.Offset < R2.Offset;
223                         }));
224   assert(std::is_sorted(
225       Chunks.begin(), Chunks.end(), [](InputChunk *C1, InputChunk *C2) {
226         return C1->getInputSectionOffset() < C2->getInputSectionOffset();
227       }));
228 
229   auto RelocsNext = Relocs.begin();
230   auto RelocsEnd = Relocs.end();
231   auto RelocLess = [](const WasmRelocation &R, uint32_t Val) {
232     return R.Offset < Val;
233   };
234   for (InputChunk *C : Chunks) {
235     auto RelocsStart = std::lower_bound(RelocsNext, RelocsEnd,
236                                         C->getInputSectionOffset(), RelocLess);
237     RelocsNext = std::lower_bound(
238         RelocsStart, RelocsEnd, C->getInputSectionOffset() + C->getInputSize(),
239         RelocLess);
240     C->setRelocations(ArrayRef<WasmRelocation>(RelocsStart, RelocsNext));
241   }
242 }
243 
244 void ObjFile::parse(bool IgnoreComdats) {
245   // Parse a memory buffer as a wasm file.
246   LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
247   std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this));
248 
249   auto *Obj = dyn_cast<WasmObjectFile>(Bin.get());
250   if (!Obj)
251     fatal(toString(this) + ": not a wasm file");
252   if (!Obj->isRelocatableObject())
253     fatal(toString(this) + ": not a relocatable wasm file");
254 
255   Bin.release();
256   WasmObj.reset(Obj);
257 
258   // Build up a map of function indices to table indices for use when
259   // verifying the existing table index relocations
260   uint32_t TotalFunctions =
261       WasmObj->getNumImportedFunctions() + WasmObj->functions().size();
262   TableEntries.resize(TotalFunctions);
263   for (const WasmElemSegment &Seg : WasmObj->elements()) {
264     if (Seg.Offset.Opcode != WASM_OPCODE_I32_CONST)
265       fatal(toString(this) + ": invalid table elements");
266     uint32_t Offset = Seg.Offset.Value.Int32;
267     for (uint32_t Index = 0; Index < Seg.Functions.size(); Index++) {
268 
269       uint32_t FunctionIndex = Seg.Functions[Index];
270       TableEntries[FunctionIndex] = Offset + Index;
271     }
272   }
273 
274   uint32_t SectionIndex = 0;
275 
276   // Bool for each symbol, true if called directly.  This allows us to implement
277   // a weaker form of signature checking where undefined functions that are not
278   // called directly (i.e. only address taken) don't have to match the defined
279   // function's signature.  We cannot do this for directly called functions
280   // because those signatures are checked at validation times.
281   // See https://bugs.llvm.org/show_bug.cgi?id=40412
282   std::vector<bool> IsCalledDirectly(WasmObj->getNumberOfSymbols(), false);
283   for (const SectionRef &Sec : WasmObj->sections()) {
284     const WasmSection &Section = WasmObj->getWasmSection(Sec);
285     // Wasm objects can have at most one code and one data section.
286     if (Section.Type == WASM_SEC_CODE) {
287       assert(!CodeSection);
288       CodeSection = &Section;
289     } else if (Section.Type == WASM_SEC_DATA) {
290       assert(!DataSection);
291       DataSection = &Section;
292     } else if (Section.Type == WASM_SEC_CUSTOM) {
293       CustomSections.emplace_back(make<InputSection>(Section, this));
294       CustomSections.back()->setRelocations(Section.Relocations);
295       CustomSectionsByIndex[SectionIndex] = CustomSections.back();
296     }
297     SectionIndex++;
298     // Scans relocations to dermine determine if a function symbol is called
299     // directly
300     for (const WasmRelocation &Reloc : Section.Relocations)
301       if (Reloc.Type == R_WASM_FUNCTION_INDEX_LEB)
302         IsCalledDirectly[Reloc.Index] = true;
303   }
304 
305   TypeMap.resize(getWasmObj()->types().size());
306   TypeIsUsed.resize(getWasmObj()->types().size(), false);
307 
308   ArrayRef<StringRef> Comdats = WasmObj->linkingData().Comdats;
309   for (unsigned I = 0; I < Comdats.size(); ++I)
310     if (IgnoreComdats)
311       KeptComdats.push_back(true);
312     else
313       KeptComdats.push_back(Symtab->addComdat(Comdats[I]));
314 
315   // Populate `Segments`.
316   for (const WasmSegment &S : WasmObj->dataSegments())
317     Segments.emplace_back(make<InputSegment>(S, this));
318   setRelocs(Segments, DataSection);
319 
320   // Populate `Functions`.
321   ArrayRef<WasmFunction> Funcs = WasmObj->functions();
322   ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
323   ArrayRef<WasmSignature> Types = WasmObj->types();
324   Functions.reserve(Funcs.size());
325 
326   for (size_t I = 0, E = Funcs.size(); I != E; ++I)
327     Functions.emplace_back(
328         make<InputFunction>(Types[FuncTypes[I]], &Funcs[I], this));
329   setRelocs(Functions, CodeSection);
330 
331   // Populate `Globals`.
332   for (const WasmGlobal &G : WasmObj->globals())
333     Globals.emplace_back(make<InputGlobal>(G, this));
334 
335   // Populate `Events`.
336   for (const WasmEvent &E : WasmObj->events())
337     Events.emplace_back(make<InputEvent>(Types[E.Type.SigIndex], E, this));
338 
339   // Populate `Symbols` based on the WasmSymbols in the object.
340   Symbols.reserve(WasmObj->getNumberOfSymbols());
341   for (const SymbolRef &Sym : WasmObj->symbols()) {
342     const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
343     if (WasmSym.isDefined()) {
344       // createDefined may fail if the symbol is comdat excluded in which case
345       // we fall back to creating an undefined symbol
346       if (Symbol *D = createDefined(WasmSym)) {
347         Symbols.push_back(D);
348         continue;
349       }
350     }
351     size_t Idx = Symbols.size();
352     Symbols.push_back(createUndefined(WasmSym, IsCalledDirectly[Idx]));
353   }
354 }
355 
356 bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const {
357   uint32_t C = Chunk->getComdat();
358   if (C == UINT32_MAX)
359     return false;
360   return !KeptComdats[C];
361 }
362 
363 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
364   return cast<FunctionSymbol>(Symbols[Index]);
365 }
366 
367 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
368   return cast<GlobalSymbol>(Symbols[Index]);
369 }
370 
371 EventSymbol *ObjFile::getEventSymbol(uint32_t Index) const {
372   return cast<EventSymbol>(Symbols[Index]);
373 }
374 
375 SectionSymbol *ObjFile::getSectionSymbol(uint32_t Index) const {
376   return cast<SectionSymbol>(Symbols[Index]);
377 }
378 
379 DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
380   return cast<DataSymbol>(Symbols[Index]);
381 }
382 
383 Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
384   StringRef Name = Sym.Info.Name;
385   uint32_t Flags = Sym.Info.Flags;
386 
387   switch (Sym.Info.Kind) {
388   case WASM_SYMBOL_TYPE_FUNCTION: {
389     InputFunction *Func =
390         Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()];
391     if (isExcludedByComdat(Func)) {
392       Func->Live = false;
393       return nullptr;
394     }
395 
396     if (Sym.isBindingLocal())
397       return make<DefinedFunction>(Name, Flags, this, Func);
398     return Symtab->addDefinedFunction(Name, Flags, this, Func);
399   }
400   case WASM_SYMBOL_TYPE_DATA: {
401     InputSegment *Seg = Segments[Sym.Info.DataRef.Segment];
402     if (isExcludedByComdat(Seg)) {
403       Seg->Live = false;
404       return nullptr;
405     }
406 
407     uint32_t Offset = Sym.Info.DataRef.Offset;
408     uint32_t Size = Sym.Info.DataRef.Size;
409 
410     if (Sym.isBindingLocal())
411       return make<DefinedData>(Name, Flags, this, Seg, Offset, Size);
412     return Symtab->addDefinedData(Name, Flags, this, Seg, Offset, Size);
413   }
414   case WASM_SYMBOL_TYPE_GLOBAL: {
415     InputGlobal *Global =
416         Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()];
417     if (Sym.isBindingLocal())
418       return make<DefinedGlobal>(Name, Flags, this, Global);
419     return Symtab->addDefinedGlobal(Name, Flags, this, Global);
420   }
421   case WASM_SYMBOL_TYPE_SECTION: {
422     InputSection *Section = CustomSectionsByIndex[Sym.Info.ElementIndex];
423     assert(Sym.isBindingLocal());
424     return make<SectionSymbol>(Flags, Section, this);
425   }
426   case WASM_SYMBOL_TYPE_EVENT: {
427     InputEvent *Event =
428         Events[Sym.Info.ElementIndex - WasmObj->getNumImportedEvents()];
429     if (Sym.isBindingLocal())
430       return make<DefinedEvent>(Name, Flags, this, Event);
431     return Symtab->addDefinedEvent(Name, Flags, this, Event);
432   }
433   }
434   llvm_unreachable("unknown symbol kind");
435 }
436 
437 Symbol *ObjFile::createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly) {
438   StringRef Name = Sym.Info.Name;
439   uint32_t Flags = Sym.Info.Flags;
440 
441   switch (Sym.Info.Kind) {
442   case WASM_SYMBOL_TYPE_FUNCTION:
443     return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName,
444                                         Sym.Info.ImportModule, Flags, this,
445                                         Sym.Signature, IsCalledDirectly);
446   case WASM_SYMBOL_TYPE_DATA:
447     return Symtab->addUndefinedData(Name, Flags, this);
448   case WASM_SYMBOL_TYPE_GLOBAL:
449     return Symtab->addUndefinedGlobal(Name, Sym.Info.ImportName,
450                                       Sym.Info.ImportModule, Flags, this,
451                                       Sym.GlobalType);
452   case WASM_SYMBOL_TYPE_SECTION:
453     llvm_unreachable("section symbols cannot be undefined");
454   }
455   llvm_unreachable("unknown symbol kind");
456 }
457 
458 void ArchiveFile::parse(bool IgnoreComdats) {
459   // Parse a MemoryBufferRef as an archive file.
460   LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
461   File = CHECK(Archive::create(MB), toString(this));
462 
463   // Read the symbol table to construct Lazy symbols.
464   int Count = 0;
465   for (const Archive::Symbol &Sym : File->symbols()) {
466     Symtab->addLazy(this, &Sym);
467     ++Count;
468   }
469   LLVM_DEBUG(dbgs() << "Read " << Count << " symbols\n");
470 }
471 
472 void ArchiveFile::addMember(const Archive::Symbol *Sym) {
473   const Archive::Child &C =
474       CHECK(Sym->getMember(),
475             "could not get the member for symbol " + Sym->getName());
476 
477   // Don't try to load the same member twice (this can happen when members
478   // mutually reference each other).
479   if (!Seen.insert(C.getChildOffset()).second)
480     return;
481 
482   LLVM_DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n");
483   LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
484 
485   MemoryBufferRef MB =
486       CHECK(C.getMemoryBufferRef(),
487             "could not get the buffer for the member defining symbol " +
488                 Sym->getName());
489 
490   InputFile *Obj = createObjectFile(MB, getName());
491   Symtab->addFile(Obj);
492 }
493 
494 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
495   switch (GvVisibility) {
496   case GlobalValue::DefaultVisibility:
497     return WASM_SYMBOL_VISIBILITY_DEFAULT;
498   case GlobalValue::HiddenVisibility:
499   case GlobalValue::ProtectedVisibility:
500     return WASM_SYMBOL_VISIBILITY_HIDDEN;
501   }
502   llvm_unreachable("unknown visibility");
503 }
504 
505 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
506                                    const lto::InputFile::Symbol &ObjSym,
507                                    BitcodeFile &F) {
508   StringRef Name = Saver.save(ObjSym.getName());
509 
510   uint32_t Flags = ObjSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
511   Flags |= mapVisibility(ObjSym.getVisibility());
512 
513   int C = ObjSym.getComdatIndex();
514   bool ExcludedByComdat = C != -1 && !KeptComdats[C];
515 
516   if (ObjSym.isUndefined() || ExcludedByComdat) {
517     if (ObjSym.isExecutable())
518       return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F,
519                                           nullptr, true);
520     return Symtab->addUndefinedData(Name, Flags, &F);
521   }
522 
523   if (ObjSym.isExecutable())
524     return Symtab->addDefinedFunction(Name, Flags, &F, nullptr);
525   return Symtab->addDefinedData(Name, Flags, &F, nullptr, 0, 0);
526 }
527 
528 void BitcodeFile::parse(bool IgnoreComdats) {
529   Obj = check(lto::InputFile::create(MemoryBufferRef(
530       MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier()))));
531   Triple T(Obj->getTargetTriple());
532   if (T.getArch() != Triple::wasm32) {
533     error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32");
534     return;
535   }
536   std::vector<bool> KeptComdats;
537   for (StringRef S : Obj->getComdatTable())
538     if (IgnoreComdats)
539       KeptComdats.push_back(true);
540     else
541       KeptComdats.push_back(Symtab->addComdat(S));
542 
543   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
544     Symbols.push_back(createBitcodeSymbol(KeptComdats, ObjSym, *this));
545 }
546 
547 // Returns a string in the format of "foo.o" or "foo.a(bar.o)".
548 std::string lld::toString(const wasm::InputFile *File) {
549   if (!File)
550     return "<internal>";
551 
552   if (File->ArchiveName.empty())
553     return File->getName();
554 
555   return (File->ArchiveName + "(" + File->getName() + ")").str();
556 }
557