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

source code of lld/wasm/InputFiles.cpp