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