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