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 "InputElement.h"
13 #include "OutputSegment.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/Path.h"
21 #include "llvm/Support/TarWriter.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 #define DEBUG_TYPE "lld"
25 
26 using namespace llvm;
27 using namespace llvm::object;
28 using namespace llvm::wasm;
29 using namespace llvm::sys;
30 
31 namespace lld {
32 
33 // Returns a string in the format of "foo.o" or "foo.a(bar.o)".
34 std::string toString(const wasm::InputFile *file) {
35   if (!file)
36     return "<internal>";
37 
38   if (file->archiveName.empty())
39     return std::string(file->getName());
40 
41   return (file->archiveName + "(" + file->getName() + ")").str();
42 }
43 
44 namespace wasm {
45 
46 void InputFile::checkArch(Triple::ArchType arch) const {
47   bool is64 = arch == Triple::wasm64;
48   if (is64 && !config->is64.hasValue()) {
49     fatal(toString(this) +
50           ": must specify -mwasm64 to process wasm64 object files");
51   } else if (config->is64.getValueOr(false) != is64) {
52     fatal(toString(this) +
53           ": wasm32 object file can't be linked in wasm64 mode");
54   }
55 }
56 
57 std::unique_ptr<llvm::TarWriter> tar;
58 
59 Optional<MemoryBufferRef> readFile(StringRef path) {
60   log("Loading: " + path);
61 
62   auto mbOrErr = MemoryBuffer::getFile(path);
63   if (auto ec = mbOrErr.getError()) {
64     error("cannot open " + path + ": " + ec.message());
65     return None;
66   }
67   std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
68   MemoryBufferRef mbref = mb->getMemBufferRef();
69   make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership
70 
71   if (tar)
72     tar->append(relativeToRoot(path), mbref.getBuffer());
73   return mbref;
74 }
75 
76 InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName,
77                             uint64_t offsetInArchive) {
78   file_magic magic = identify_magic(mb.getBuffer());
79   if (magic == file_magic::wasm_object) {
80     std::unique_ptr<Binary> bin =
81         CHECK(createBinary(mb), mb.getBufferIdentifier());
82     auto *obj = cast<WasmObjectFile>(bin.get());
83     if (obj->isSharedObject())
84       return make<SharedFile>(mb);
85     return make<ObjFile>(mb, archiveName);
86   }
87 
88   if (magic == file_magic::bitcode)
89     return make<BitcodeFile>(mb, archiveName, offsetInArchive);
90 
91   fatal("unknown file type: " + mb.getBufferIdentifier());
92 }
93 
94 // Relocations contain either symbol or type indices.  This function takes a
95 // relocation and returns relocated index (i.e. translates from the input
96 // symbol/type space to the output symbol/type space).
97 uint32_t ObjFile::calcNewIndex(const WasmRelocation &reloc) const {
98   if (reloc.Type == R_WASM_TYPE_INDEX_LEB) {
99     assert(typeIsUsed[reloc.Index]);
100     return typeMap[reloc.Index];
101   }
102   const Symbol *sym = symbols[reloc.Index];
103   if (auto *ss = dyn_cast<SectionSymbol>(sym))
104     sym = ss->getOutputSectionSymbol();
105   return sym->getOutputSymbolIndex();
106 }
107 
108 // Relocations can contain addend for combined sections. This function takes a
109 // relocation and returns updated addend by offset in the output section.
110 uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const {
111   switch (reloc.Type) {
112   case R_WASM_MEMORY_ADDR_LEB:
113   case R_WASM_MEMORY_ADDR_LEB64:
114   case R_WASM_MEMORY_ADDR_SLEB64:
115   case R_WASM_MEMORY_ADDR_SLEB:
116   case R_WASM_MEMORY_ADDR_REL_SLEB:
117   case R_WASM_MEMORY_ADDR_REL_SLEB64:
118   case R_WASM_MEMORY_ADDR_I32:
119   case R_WASM_MEMORY_ADDR_I64:
120   case R_WASM_MEMORY_ADDR_TLS_SLEB:
121   case R_WASM_MEMORY_ADDR_TLS_SLEB64:
122   case R_WASM_FUNCTION_OFFSET_I32:
123   case R_WASM_FUNCTION_OFFSET_I64:
124   case R_WASM_MEMORY_ADDR_LOCREL_I32:
125     return reloc.Addend;
126   case R_WASM_SECTION_OFFSET_I32:
127     return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
128   default:
129     llvm_unreachable("unexpected relocation type");
130   }
131 }
132 
133 // Translate from the relocation's index into the final linked output value.
134 uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone,
135                                const InputChunk *chunk) const {
136   const Symbol* sym = nullptr;
137   if (reloc.Type != R_WASM_TYPE_INDEX_LEB) {
138     sym = symbols[reloc.Index];
139 
140     // We can end up with relocations against non-live symbols.  For example
141     // in debug sections. We return a tombstone value in debug symbol sections
142     // so this will not produce a valid range conflicting with ranges of actual
143     // code. In other sections we return reloc.Addend.
144 
145     if (!isa<SectionSymbol>(sym) && !sym->isLive())
146       return tombstone ? tombstone : reloc.Addend;
147   }
148 
149   switch (reloc.Type) {
150   case R_WASM_TABLE_INDEX_I32:
151   case R_WASM_TABLE_INDEX_I64:
152   case R_WASM_TABLE_INDEX_SLEB:
153   case R_WASM_TABLE_INDEX_SLEB64:
154   case R_WASM_TABLE_INDEX_REL_SLEB:
155   case R_WASM_TABLE_INDEX_REL_SLEB64: {
156     if (!getFunctionSymbol(reloc.Index)->hasTableIndex())
157       return 0;
158     uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex();
159     if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB ||
160         reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB64)
161       index -= config->tableBase;
162     return index;
163   }
164   case R_WASM_MEMORY_ADDR_LEB:
165   case R_WASM_MEMORY_ADDR_LEB64:
166   case R_WASM_MEMORY_ADDR_SLEB:
167   case R_WASM_MEMORY_ADDR_SLEB64:
168   case R_WASM_MEMORY_ADDR_REL_SLEB:
169   case R_WASM_MEMORY_ADDR_REL_SLEB64:
170   case R_WASM_MEMORY_ADDR_I32:
171   case R_WASM_MEMORY_ADDR_I64:
172   case R_WASM_MEMORY_ADDR_TLS_SLEB:
173   case R_WASM_MEMORY_ADDR_TLS_SLEB64:
174   case R_WASM_MEMORY_ADDR_LOCREL_I32: {
175     if (isa<UndefinedData>(sym) || sym->isUndefWeak())
176       return 0;
177     auto D = cast<DefinedData>(sym);
178     uint64_t value = D->getVA() + reloc.Addend;
179     if (reloc.Type == R_WASM_MEMORY_ADDR_LOCREL_I32) {
180       const auto *segment = cast<InputSegment>(chunk);
181       uint64_t p = segment->outputSeg->startVA + segment->outputSegmentOffset +
182                    reloc.Offset - segment->getInputSectionOffset();
183       value -= p;
184     }
185     return value;
186   }
187   case R_WASM_TYPE_INDEX_LEB:
188     return typeMap[reloc.Index];
189   case R_WASM_FUNCTION_INDEX_LEB:
190     return getFunctionSymbol(reloc.Index)->getFunctionIndex();
191   case R_WASM_GLOBAL_INDEX_LEB:
192   case R_WASM_GLOBAL_INDEX_I32:
193     if (auto gs = dyn_cast<GlobalSymbol>(sym))
194       return gs->getGlobalIndex();
195     return sym->getGOTIndex();
196   case R_WASM_TAG_INDEX_LEB:
197     return getTagSymbol(reloc.Index)->getTagIndex();
198   case R_WASM_FUNCTION_OFFSET_I32:
199   case R_WASM_FUNCTION_OFFSET_I64: {
200     if (isa<UndefinedFunction>(sym)) {
201       return tombstone ? tombstone : reloc.Addend;
202     }
203     auto *f = cast<DefinedFunction>(sym);
204     return f->function->getOffset(f->function->getFunctionCodeOffset() +
205                                   reloc.Addend);
206   }
207   case R_WASM_SECTION_OFFSET_I32:
208     return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
209   case R_WASM_TABLE_NUMBER_LEB:
210     return getTableSymbol(reloc.Index)->getTableNumber();
211   default:
212     llvm_unreachable("unknown relocation type");
213   }
214 }
215 
216 template <class T>
217 static void setRelocs(const std::vector<T *> &chunks,
218                       const WasmSection *section) {
219   if (!section)
220     return;
221 
222   ArrayRef<WasmRelocation> relocs = section->Relocations;
223   assert(llvm::is_sorted(
224       relocs, [](const WasmRelocation &r1, const WasmRelocation &r2) {
225         return r1.Offset < r2.Offset;
226       }));
227   assert(llvm::is_sorted(chunks, [](InputChunk *c1, InputChunk *c2) {
228     return c1->getInputSectionOffset() < c2->getInputSectionOffset();
229   }));
230 
231   auto relocsNext = relocs.begin();
232   auto relocsEnd = relocs.end();
233   auto relocLess = [](const WasmRelocation &r, uint32_t val) {
234     return r.Offset < val;
235   };
236   for (InputChunk *c : chunks) {
237     auto relocsStart = std::lower_bound(relocsNext, relocsEnd,
238                                         c->getInputSectionOffset(), relocLess);
239     relocsNext = std::lower_bound(
240         relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(),
241         relocLess);
242     c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext));
243   }
244 }
245 
246 // An object file can have two approaches to tables.  With the reference-types
247 // feature enabled, input files that define or use tables declare the tables
248 // using symbols, and record each use with a relocation.  This way when the
249 // linker combines inputs, it can collate the tables used by the inputs,
250 // assigning them distinct table numbers, and renumber all the uses as
251 // appropriate.  At the same time, the linker has special logic to build the
252 // indirect function table if it is needed.
253 //
254 // However, MVP object files (those that target WebAssembly 1.0, the "minimum
255 // viable product" version of WebAssembly) neither write table symbols nor
256 // record relocations.  These files can have at most one table, the indirect
257 // function table used by call_indirect and which is the address space for
258 // function pointers.  If this table is present, it is always an import.  If we
259 // have a file with a table import but no table symbols, it is an MVP object
260 // file.  synthesizeMVPIndirectFunctionTableSymbolIfNeeded serves as a shim when
261 // loading these input files, defining the missing symbol to allow the indirect
262 // function table to be built.
263 //
264 // As indirect function table table usage in MVP objects cannot be relocated,
265 // the linker must ensure that this table gets assigned index zero.
266 void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
267     uint32_t tableSymbolCount) {
268   uint32_t tableCount = wasmObj->getNumImportedTables() + tables.size();
269 
270   // If there are symbols for all tables, then all is good.
271   if (tableCount == tableSymbolCount)
272     return;
273 
274   // It's possible for an input to define tables and also use the indirect
275   // function table, but forget to compile with -mattr=+reference-types.
276   // For these newer files, we require symbols for all tables, and
277   // relocations for all of their uses.
278   if (tableSymbolCount != 0) {
279     error(toString(this) +
280           ": expected one symbol table entry for each of the " +
281           Twine(tableCount) + " table(s) present, but got " +
282           Twine(tableSymbolCount) + " symbol(s) instead.");
283     return;
284   }
285 
286   // An MVP object file can have up to one table import, for the indirect
287   // function table, but will have no table definitions.
288   if (tables.size()) {
289     error(toString(this) +
290           ": unexpected table definition(s) without corresponding "
291           "symbol-table entries.");
292     return;
293   }
294 
295   // An MVP object file can have only one table import.
296   if (tableCount != 1) {
297     error(toString(this) +
298           ": multiple table imports, but no corresponding symbol-table "
299           "entries.");
300     return;
301   }
302 
303   const WasmImport *tableImport = nullptr;
304   for (const auto &import : wasmObj->imports()) {
305     if (import.Kind == WASM_EXTERNAL_TABLE) {
306       assert(!tableImport);
307       tableImport = &import;
308     }
309   }
310   assert(tableImport);
311 
312   // We can only synthesize a symtab entry for the indirect function table; if
313   // it has an unexpected name or type, assume that it's not actually the
314   // indirect function table.
315   if (tableImport->Field != functionTableName ||
316       tableImport->Table.ElemType != uint8_t(ValType::FUNCREF)) {
317     error(toString(this) + ": table import " + Twine(tableImport->Field) +
318           " is missing a symbol table entry.");
319     return;
320   }
321 
322   auto *info = make<WasmSymbolInfo>();
323   info->Name = tableImport->Field;
324   info->Kind = WASM_SYMBOL_TYPE_TABLE;
325   info->ImportModule = tableImport->Module;
326   info->ImportName = tableImport->Field;
327   info->Flags = WASM_SYMBOL_UNDEFINED;
328   info->Flags |= WASM_SYMBOL_NO_STRIP;
329   info->ElementIndex = 0;
330   LLVM_DEBUG(dbgs() << "Synthesizing symbol for table import: " << info->Name
331                     << "\n");
332   const WasmGlobalType *globalType = nullptr;
333   const WasmSignature *signature = nullptr;
334   auto *wasmSym =
335       make<WasmSymbol>(*info, globalType, &tableImport->Table, signature);
336   Symbol *sym = createUndefined(*wasmSym, false);
337   // We're only sure it's a TableSymbol if the createUndefined succeeded.
338   if (errorCount())
339     return;
340   symbols.push_back(sym);
341   // Because there are no TABLE_NUMBER relocs, we can't compute accurate
342   // liveness info; instead, just mark the symbol as always live.
343   sym->markLive();
344 
345   // We assume that this compilation unit has unrelocatable references to
346   // this table.
347   config->legacyFunctionTable = true;
348 }
349 
350 static bool shouldMerge(const WasmSection &sec) {
351   if (config->optimize == 0)
352     return false;
353   // Sadly we don't have section attributes yet for custom sections, so we
354   // currently go by the name alone.
355   // TODO(sbc): Add ability for wasm sections to carry flags so we don't
356   // need to use names here.
357   // For now, keep in sync with uses of wasm::WASM_SEG_FLAG_STRINGS in
358   // MCObjectFileInfo::initWasmMCObjectFileInfo which creates these custom
359   // sections.
360   return sec.Name == ".debug_str" || sec.Name == ".debug_str.dwo" ||
361          sec.Name == ".debug_line_str";
362 }
363 
364 static bool shouldMerge(const WasmSegment &seg) {
365   // As of now we only support merging strings, and only with single byte
366   // alignment (2^0).
367   if (!(seg.Data.LinkingFlags & WASM_SEG_FLAG_STRINGS) ||
368       (seg.Data.Alignment != 0))
369     return false;
370 
371   // On a regular link we don't merge sections if -O0 (default is -O1). This
372   // sometimes makes the linker significantly faster, although the output will
373   // be bigger.
374   if (config->optimize == 0)
375     return false;
376 
377   // A mergeable section with size 0 is useless because they don't have
378   // any data to merge. A mergeable string section with size 0 can be
379   // argued as invalid because it doesn't end with a null character.
380   // We'll avoid a mess by handling them as if they were non-mergeable.
381   if (seg.Data.Content.size() == 0)
382     return false;
383 
384   return true;
385 }
386 
387 void ObjFile::parse(bool ignoreComdats) {
388   // Parse a memory buffer as a wasm file.
389   LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
390   std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this));
391 
392   auto *obj = dyn_cast<WasmObjectFile>(bin.get());
393   if (!obj)
394     fatal(toString(this) + ": not a wasm file");
395   if (!obj->isRelocatableObject())
396     fatal(toString(this) + ": not a relocatable wasm file");
397 
398   bin.release();
399   wasmObj.reset(obj);
400 
401   checkArch(obj->getArch());
402 
403   // Build up a map of function indices to table indices for use when
404   // verifying the existing table index relocations
405   uint32_t totalFunctions =
406       wasmObj->getNumImportedFunctions() + wasmObj->functions().size();
407   tableEntriesRel.resize(totalFunctions);
408   tableEntries.resize(totalFunctions);
409   for (const WasmElemSegment &seg : wasmObj->elements()) {
410     int64_t offset;
411     if (seg.Offset.Opcode == WASM_OPCODE_I32_CONST)
412       offset = seg.Offset.Value.Int32;
413     else if (seg.Offset.Opcode == WASM_OPCODE_I64_CONST)
414       offset = seg.Offset.Value.Int64;
415     else
416       fatal(toString(this) + ": invalid table elements");
417     for (size_t index = 0; index < seg.Functions.size(); index++) {
418       auto functionIndex = seg.Functions[index];
419       tableEntriesRel[functionIndex] = index;
420       tableEntries[functionIndex] = offset + index;
421     }
422   }
423 
424   ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats;
425   for (StringRef comdat : comdats) {
426     bool isNew = ignoreComdats || symtab->addComdat(comdat);
427     keptComdats.push_back(isNew);
428   }
429 
430   uint32_t sectionIndex = 0;
431 
432   // Bool for each symbol, true if called directly.  This allows us to implement
433   // a weaker form of signature checking where undefined functions that are not
434   // called directly (i.e. only address taken) don't have to match the defined
435   // function's signature.  We cannot do this for directly called functions
436   // because those signatures are checked at validation times.
437   // See https://bugs.llvm.org/show_bug.cgi?id=40412
438   std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false);
439   for (const SectionRef &sec : wasmObj->sections()) {
440     const WasmSection &section = wasmObj->getWasmSection(sec);
441     // Wasm objects can have at most one code and one data section.
442     if (section.Type == WASM_SEC_CODE) {
443       assert(!codeSection);
444       codeSection = &section;
445     } else if (section.Type == WASM_SEC_DATA) {
446       assert(!dataSection);
447       dataSection = &section;
448     } else if (section.Type == WASM_SEC_CUSTOM) {
449       InputChunk *customSec;
450       if (shouldMerge(section))
451         customSec = make<MergeInputChunk>(section, this);
452       else
453         customSec = make<InputSection>(section, this);
454       customSec->discarded = isExcludedByComdat(customSec);
455       customSections.emplace_back(customSec);
456       customSections.back()->setRelocations(section.Relocations);
457       customSectionsByIndex[sectionIndex] = customSections.back();
458     }
459     sectionIndex++;
460     // Scans relocations to determine if a function symbol is called directly.
461     for (const WasmRelocation &reloc : section.Relocations)
462       if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB)
463         isCalledDirectly[reloc.Index] = true;
464   }
465 
466   typeMap.resize(getWasmObj()->types().size());
467   typeIsUsed.resize(getWasmObj()->types().size(), false);
468 
469 
470   // Populate `Segments`.
471   for (const WasmSegment &s : wasmObj->dataSegments()) {
472     InputChunk *seg;
473     if (shouldMerge(s)) {
474       seg = make<MergeInputChunk>(s, this);
475     } else
476       seg = make<InputSegment>(s, this);
477     seg->discarded = isExcludedByComdat(seg);
478     // Older object files did not include WASM_SEG_FLAG_TLS and instead
479     // relied on the naming convention.  To maintain compat with such objects
480     // we still imply the TLS flag based on the name of the segment.
481     if (!seg->isTLS() &&
482         (seg->name.startswith(".tdata") || seg->name.startswith(".tbss"))) {
483       seg->flags |= WASM_SEG_FLAG_TLS;
484       seg->implicitTLS = true;
485     }
486     segments.emplace_back(seg);
487   }
488   setRelocs(segments, dataSection);
489 
490   // Populate `Functions`.
491   ArrayRef<WasmFunction> funcs = wasmObj->functions();
492   ArrayRef<WasmSignature> types = wasmObj->types();
493   functions.reserve(funcs.size());
494 
495   for (auto &f : funcs) {
496     auto *func = make<InputFunction>(types[f.SigIndex], &f, this);
497     func->discarded = isExcludedByComdat(func);
498     functions.emplace_back(func);
499   }
500   setRelocs(functions, codeSection);
501 
502   // Populate `Tables`.
503   for (const WasmTable &t : wasmObj->tables())
504     tables.emplace_back(make<InputTable>(t, this));
505 
506   // Populate `Globals`.
507   for (const WasmGlobal &g : wasmObj->globals())
508     globals.emplace_back(make<InputGlobal>(g, this));
509 
510   // Populate `Tags`.
511   for (const WasmTag &t : wasmObj->tags())
512     tags.emplace_back(make<InputTag>(types[t.SigIndex], t, this));
513 
514   // Populate `Symbols` based on the symbols in the object.
515   symbols.reserve(wasmObj->getNumberOfSymbols());
516   uint32_t tableSymbolCount = 0;
517   for (const SymbolRef &sym : wasmObj->symbols()) {
518     const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl());
519     if (wasmSym.isTypeTable())
520       tableSymbolCount++;
521     if (wasmSym.isDefined()) {
522       // createDefined may fail if the symbol is comdat excluded in which case
523       // we fall back to creating an undefined symbol
524       if (Symbol *d = createDefined(wasmSym)) {
525         symbols.push_back(d);
526         continue;
527       }
528     }
529     size_t idx = symbols.size();
530     symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx]));
531   }
532 
533   addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount);
534 }
535 
536 bool ObjFile::isExcludedByComdat(const InputChunk *chunk) const {
537   uint32_t c = chunk->getComdat();
538   if (c == UINT32_MAX)
539     return false;
540   return !keptComdats[c];
541 }
542 
543 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const {
544   return cast<FunctionSymbol>(symbols[index]);
545 }
546 
547 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const {
548   return cast<GlobalSymbol>(symbols[index]);
549 }
550 
551 TagSymbol *ObjFile::getTagSymbol(uint32_t index) const {
552   return cast<TagSymbol>(symbols[index]);
553 }
554 
555 TableSymbol *ObjFile::getTableSymbol(uint32_t index) const {
556   return cast<TableSymbol>(symbols[index]);
557 }
558 
559 SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const {
560   return cast<SectionSymbol>(symbols[index]);
561 }
562 
563 DataSymbol *ObjFile::getDataSymbol(uint32_t index) const {
564   return cast<DataSymbol>(symbols[index]);
565 }
566 
567 Symbol *ObjFile::createDefined(const WasmSymbol &sym) {
568   StringRef name = sym.Info.Name;
569   uint32_t flags = sym.Info.Flags;
570 
571   switch (sym.Info.Kind) {
572   case WASM_SYMBOL_TYPE_FUNCTION: {
573     InputFunction *func =
574         functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
575     if (sym.isBindingLocal())
576       return make<DefinedFunction>(name, flags, this, func);
577     if (func->discarded)
578       return nullptr;
579     return symtab->addDefinedFunction(name, flags, this, func);
580   }
581   case WASM_SYMBOL_TYPE_DATA: {
582     InputChunk *seg = segments[sym.Info.DataRef.Segment];
583     auto offset = sym.Info.DataRef.Offset;
584     auto size = sym.Info.DataRef.Size;
585     if (seg->implicitTLS) {
586       flags |= WASM_SYMBOL_TLS;
587     }
588     if (sym.isBindingLocal())
589       return make<DefinedData>(name, flags, this, seg, offset, size);
590     if (seg->discarded)
591       return nullptr;
592     return symtab->addDefinedData(name, flags, this, seg, offset, size);
593   }
594   case WASM_SYMBOL_TYPE_GLOBAL: {
595     InputGlobal *global =
596         globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()];
597     if (sym.isBindingLocal())
598       return make<DefinedGlobal>(name, flags, this, global);
599     return symtab->addDefinedGlobal(name, flags, this, global);
600   }
601   case WASM_SYMBOL_TYPE_SECTION: {
602     InputChunk *section = customSectionsByIndex[sym.Info.ElementIndex];
603     assert(sym.isBindingLocal());
604     // Need to return null if discarded here? data and func only do that when
605     // binding is not local.
606     if (section->discarded)
607       return nullptr;
608     return make<SectionSymbol>(flags, section, this);
609   }
610   case WASM_SYMBOL_TYPE_TAG: {
611     InputTag *tag = tags[sym.Info.ElementIndex - wasmObj->getNumImportedTags()];
612     if (sym.isBindingLocal())
613       return make<DefinedTag>(name, flags, this, tag);
614     return symtab->addDefinedTag(name, flags, this, tag);
615   }
616   case WASM_SYMBOL_TYPE_TABLE: {
617     InputTable *table =
618         tables[sym.Info.ElementIndex - wasmObj->getNumImportedTables()];
619     if (sym.isBindingLocal())
620       return make<DefinedTable>(name, flags, this, table);
621     return symtab->addDefinedTable(name, flags, this, table);
622   }
623   }
624   llvm_unreachable("unknown symbol kind");
625 }
626 
627 Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
628   StringRef name = sym.Info.Name;
629   uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED;
630 
631   switch (sym.Info.Kind) {
632   case WASM_SYMBOL_TYPE_FUNCTION:
633     if (sym.isBindingLocal())
634       return make<UndefinedFunction>(name, sym.Info.ImportName,
635                                      sym.Info.ImportModule, flags, this,
636                                      sym.Signature, isCalledDirectly);
637     return symtab->addUndefinedFunction(name, sym.Info.ImportName,
638                                         sym.Info.ImportModule, flags, this,
639                                         sym.Signature, isCalledDirectly);
640   case WASM_SYMBOL_TYPE_DATA:
641     if (sym.isBindingLocal())
642       return make<UndefinedData>(name, flags, this);
643     return symtab->addUndefinedData(name, flags, this);
644   case WASM_SYMBOL_TYPE_GLOBAL:
645     if (sym.isBindingLocal())
646       return make<UndefinedGlobal>(name, sym.Info.ImportName,
647                                    sym.Info.ImportModule, flags, this,
648                                    sym.GlobalType);
649     return symtab->addUndefinedGlobal(name, sym.Info.ImportName,
650                                       sym.Info.ImportModule, flags, this,
651                                       sym.GlobalType);
652   case WASM_SYMBOL_TYPE_TABLE:
653     if (sym.isBindingLocal())
654       return make<UndefinedTable>(name, sym.Info.ImportName,
655                                   sym.Info.ImportModule, flags, this,
656                                   sym.TableType);
657     return symtab->addUndefinedTable(name, sym.Info.ImportName,
658                                      sym.Info.ImportModule, flags, this,
659                                      sym.TableType);
660   case WASM_SYMBOL_TYPE_TAG:
661     if (sym.isBindingLocal())
662       return make<UndefinedTag>(name, sym.Info.ImportName,
663                                 sym.Info.ImportModule, flags, this,
664                                 sym.Signature);
665     return symtab->addUndefinedTag(name, sym.Info.ImportName,
666                                    sym.Info.ImportModule, flags, this,
667                                    sym.Signature);
668   case WASM_SYMBOL_TYPE_SECTION:
669     llvm_unreachable("section symbols cannot be undefined");
670   }
671   llvm_unreachable("unknown symbol kind");
672 }
673 
674 void ArchiveFile::parse() {
675   // Parse a MemoryBufferRef as an archive file.
676   LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
677   file = CHECK(Archive::create(mb), toString(this));
678 
679   // Read the symbol table to construct Lazy symbols.
680   int count = 0;
681   for (const Archive::Symbol &sym : file->symbols()) {
682     symtab->addLazy(this, &sym);
683     ++count;
684   }
685   LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n");
686 }
687 
688 void ArchiveFile::addMember(const Archive::Symbol *sym) {
689   const Archive::Child &c =
690       CHECK(sym->getMember(),
691             "could not get the member for symbol " + sym->getName());
692 
693   // Don't try to load the same member twice (this can happen when members
694   // mutually reference each other).
695   if (!seen.insert(c.getChildOffset()).second)
696     return;
697 
698   LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n");
699   LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
700 
701   MemoryBufferRef mb =
702       CHECK(c.getMemoryBufferRef(),
703             "could not get the buffer for the member defining symbol " +
704                 sym->getName());
705 
706   InputFile *obj = createObjectFile(mb, getName(), c.getChildOffset());
707   symtab->addFile(obj);
708 }
709 
710 static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
711   switch (gvVisibility) {
712   case GlobalValue::DefaultVisibility:
713     return WASM_SYMBOL_VISIBILITY_DEFAULT;
714   case GlobalValue::HiddenVisibility:
715   case GlobalValue::ProtectedVisibility:
716     return WASM_SYMBOL_VISIBILITY_HIDDEN;
717   }
718   llvm_unreachable("unknown visibility");
719 }
720 
721 static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
722                                    const lto::InputFile::Symbol &objSym,
723                                    BitcodeFile &f) {
724   StringRef name = saver.save(objSym.getName());
725 
726   uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
727   flags |= mapVisibility(objSym.getVisibility());
728 
729   int c = objSym.getComdatIndex();
730   bool excludedByComdat = c != -1 && !keptComdats[c];
731 
732   if (objSym.isUndefined() || excludedByComdat) {
733     flags |= WASM_SYMBOL_UNDEFINED;
734     if (objSym.isExecutable())
735       return symtab->addUndefinedFunction(name, None, None, flags, &f, nullptr,
736                                           true);
737     return symtab->addUndefinedData(name, flags, &f);
738   }
739 
740   if (objSym.isExecutable())
741     return symtab->addDefinedFunction(name, flags, &f, nullptr);
742   return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0);
743 }
744 
745 BitcodeFile::BitcodeFile(MemoryBufferRef m, StringRef archiveName,
746                          uint64_t offsetInArchive)
747     : InputFile(BitcodeKind, m) {
748   this->archiveName = std::string(archiveName);
749 
750   std::string path = mb.getBufferIdentifier().str();
751 
752   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
753   // name. If two archives define two members with the same name, this
754   // causes a collision which result in only one of the objects being taken
755   // into consideration at LTO time (which very likely causes undefined
756   // symbols later in the link stage). So we append file offset to make
757   // filename unique.
758   StringRef name = archiveName.empty()
759                        ? saver.save(path)
760                        : saver.save(archiveName + "(" + path::filename(path) +
761                                     " at " + utostr(offsetInArchive) + ")");
762   MemoryBufferRef mbref(mb.getBuffer(), name);
763 
764   obj = check(lto::InputFile::create(mbref));
765 
766   // If this isn't part of an archive, it's eagerly linked, so mark it live.
767   if (archiveName.empty())
768     markLive();
769 }
770 
771 bool BitcodeFile::doneLTO = false;
772 
773 void BitcodeFile::parse() {
774   if (doneLTO) {
775     error(toString(this) + ": attempt to add bitcode file after LTO.");
776     return;
777   }
778 
779   Triple t(obj->getTargetTriple());
780   if (!t.isWasm()) {
781     error(toString(this) + ": machine type must be wasm32 or wasm64");
782     return;
783   }
784   checkArch(t.getArch());
785   std::vector<bool> keptComdats;
786   // TODO Support nodeduplicate https://bugs.llvm.org/show_bug.cgi?id=50531
787   for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable())
788     keptComdats.push_back(symtab->addComdat(s.first));
789 
790   for (const lto::InputFile::Symbol &objSym : obj->symbols())
791     symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this));
792 }
793 
794 } // namespace wasm
795 } // namespace lld
796