[JITLink] Add support of R_X86_64_32S relocation
[llvm-project.git] / lld / wasm / InputFiles.cpp
blob35d8dd1ed7be702aab943d9b0a8da1445fda79ed
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 //===----------------------------------------------------------------------===//
9 #include "InputFiles.h"
10 #include "Config.h"
11 #include "InputChunks.h"
12 #include "InputElement.h"
13 #include "OutputSegment.h"
14 #include "SymbolTable.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "lld/Common/Reproduce.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/Wasm.h"
20 #include "llvm/Support/TarWriter.h"
21 #include "llvm/Support/raw_ostream.h"
23 #define DEBUG_TYPE "lld"
25 using namespace llvm;
26 using namespace llvm::object;
27 using namespace llvm::wasm;
29 namespace lld {
31 // Returns a string in the format of "foo.o" or "foo.a(bar.o)".
32 std::string toString(const wasm::InputFile *file) {
33 if (!file)
34 return "<internal>";
36 if (file->archiveName.empty())
37 return std::string(file->getName());
39 return (file->archiveName + "(" + file->getName() + ")").str();
42 namespace wasm {
44 void InputFile::checkArch(Triple::ArchType arch) const {
45 bool is64 = arch == Triple::wasm64;
46 if (is64 && !config->is64.hasValue()) {
47 fatal(toString(this) +
48 ": must specify -mwasm64 to process wasm64 object files");
49 } else if (config->is64.getValueOr(false) != is64) {
50 fatal(toString(this) +
51 ": wasm32 object file can't be linked in wasm64 mode");
55 std::unique_ptr<llvm::TarWriter> tar;
57 Optional<MemoryBufferRef> readFile(StringRef path) {
58 log("Loading: " + path);
60 auto mbOrErr = MemoryBuffer::getFile(path);
61 if (auto ec = mbOrErr.getError()) {
62 error("cannot open " + path + ": " + ec.message());
63 return None;
65 std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
66 MemoryBufferRef mbref = mb->getMemBufferRef();
67 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership
69 if (tar)
70 tar->append(relativeToRoot(path), mbref.getBuffer());
71 return mbref;
74 InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName) {
75 file_magic magic = identify_magic(mb.getBuffer());
76 if (magic == file_magic::wasm_object) {
77 std::unique_ptr<Binary> bin =
78 CHECK(createBinary(mb), mb.getBufferIdentifier());
79 auto *obj = cast<WasmObjectFile>(bin.get());
80 if (obj->isSharedObject())
81 return make<SharedFile>(mb);
82 return make<ObjFile>(mb, archiveName);
85 if (magic == file_magic::bitcode)
86 return make<BitcodeFile>(mb, archiveName);
88 fatal("unknown file type: " + mb.getBufferIdentifier());
91 void ObjFile::dumpInfo() const {
92 log("info for: " + toString(this) +
93 "\n Symbols : " + Twine(symbols.size()) +
94 "\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) +
95 "\n Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) +
96 "\n Tag Imports : " + Twine(wasmObj->getNumImportedTags()) +
97 "\n Table Imports : " + Twine(wasmObj->getNumImportedTables()));
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).
103 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];
108 const Symbol *sym = symbols[reloc.Index];
109 if (auto *ss = dyn_cast<SectionSymbol>(sym))
110 sym = ss->getOutputSectionSymbol();
111 return sym->getOutputSymbolIndex();
114 // Relocations can contain addend for combined sections. This function takes a
115 // relocation and returns updated addend by offset in the output section.
116 uint64_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");
139 // Translate from the relocation's index into the final linked output value.
140 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];
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.
151 if (!isa<SectionSymbol>(sym) && !sym->isLive())
152 return tombstone ? tombstone : reloc.Addend;
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;
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_LOCREL_I32: {
179 if (isa<UndefinedData>(sym) || sym->isUndefWeak())
180 return 0;
181 auto D = cast<DefinedData>(sym);
182 // Treat non-TLS relocation against symbols that live in the TLS segment
183 // like TLS relocations. This beaviour exists to support older object
184 // files created before we introduced TLS relocations.
185 // TODO(sbc): Remove this legacy behaviour one day. This will break
186 // backward compat with old object files built with `-fPIC`.
187 if (D->segment && D->segment->outputSeg->isTLS())
188 return D->getOutputSegmentOffset() + reloc.Addend;
190 uint64_t value = D->getVA() + reloc.Addend;
191 if (reloc.Type == R_WASM_MEMORY_ADDR_LOCREL_I32) {
192 const auto *segment = cast<InputSegment>(chunk);
193 uint64_t p = segment->outputSeg->startVA + segment->outputSegmentOffset +
194 reloc.Offset - segment->getInputSectionOffset();
195 value -= p;
197 return value;
199 case R_WASM_MEMORY_ADDR_TLS_SLEB:
200 case R_WASM_MEMORY_ADDR_TLS_SLEB64:
201 if (isa<UndefinedData>(sym) || sym->isUndefWeak())
202 return 0;
203 // TLS relocations are relative to the start of the TLS output segment
204 return cast<DefinedData>(sym)->getOutputSegmentOffset() + reloc.Addend;
205 case R_WASM_TYPE_INDEX_LEB:
206 return typeMap[reloc.Index];
207 case R_WASM_FUNCTION_INDEX_LEB:
208 return getFunctionSymbol(reloc.Index)->getFunctionIndex();
209 case R_WASM_GLOBAL_INDEX_LEB:
210 case R_WASM_GLOBAL_INDEX_I32:
211 if (auto gs = dyn_cast<GlobalSymbol>(sym))
212 return gs->getGlobalIndex();
213 return sym->getGOTIndex();
214 case R_WASM_TAG_INDEX_LEB:
215 return getTagSymbol(reloc.Index)->getTagIndex();
216 case R_WASM_FUNCTION_OFFSET_I32:
217 case R_WASM_FUNCTION_OFFSET_I64: {
218 auto *f = cast<DefinedFunction>(sym);
219 return f->function->getOffset(f->function->getFunctionCodeOffset() +
220 reloc.Addend);
222 case R_WASM_SECTION_OFFSET_I32:
223 return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
224 case R_WASM_TABLE_NUMBER_LEB:
225 return getTableSymbol(reloc.Index)->getTableNumber();
226 default:
227 llvm_unreachable("unknown relocation type");
231 template <class T>
232 static void setRelocs(const std::vector<T *> &chunks,
233 const WasmSection *section) {
234 if (!section)
235 return;
237 ArrayRef<WasmRelocation> relocs = section->Relocations;
238 assert(llvm::is_sorted(
239 relocs, [](const WasmRelocation &r1, const WasmRelocation &r2) {
240 return r1.Offset < r2.Offset;
241 }));
242 assert(llvm::is_sorted(chunks, [](InputChunk *c1, InputChunk *c2) {
243 return c1->getInputSectionOffset() < c2->getInputSectionOffset();
244 }));
246 auto relocsNext = relocs.begin();
247 auto relocsEnd = relocs.end();
248 auto relocLess = [](const WasmRelocation &r, uint32_t val) {
249 return r.Offset < val;
251 for (InputChunk *c : chunks) {
252 auto relocsStart = std::lower_bound(relocsNext, relocsEnd,
253 c->getInputSectionOffset(), relocLess);
254 relocsNext = std::lower_bound(
255 relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(),
256 relocLess);
257 c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext));
261 // An object file can have two approaches to tables. With the reference-types
262 // feature enabled, input files that define or use tables declare the tables
263 // using symbols, and record each use with a relocation. This way when the
264 // linker combines inputs, it can collate the tables used by the inputs,
265 // assigning them distinct table numbers, and renumber all the uses as
266 // appropriate. At the same time, the linker has special logic to build the
267 // indirect function table if it is needed.
269 // However, MVP object files (those that target WebAssembly 1.0, the "minimum
270 // viable product" version of WebAssembly) neither write table symbols nor
271 // record relocations. These files can have at most one table, the indirect
272 // function table used by call_indirect and which is the address space for
273 // function pointers. If this table is present, it is always an import. If we
274 // have a file with a table import but no table symbols, it is an MVP object
275 // file. synthesizeMVPIndirectFunctionTableSymbolIfNeeded serves as a shim when
276 // loading these input files, defining the missing symbol to allow the indirect
277 // function table to be built.
279 // As indirect function table table usage in MVP objects cannot be relocated,
280 // the linker must ensure that this table gets assigned index zero.
281 void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
282 uint32_t tableSymbolCount) {
283 uint32_t tableCount = wasmObj->getNumImportedTables() + tables.size();
285 // If there are symbols for all tables, then all is good.
286 if (tableCount == tableSymbolCount)
287 return;
289 // It's possible for an input to define tables and also use the indirect
290 // function table, but forget to compile with -mattr=+reference-types.
291 // For these newer files, we require symbols for all tables, and
292 // relocations for all of their uses.
293 if (tableSymbolCount != 0) {
294 error(toString(this) +
295 ": expected one symbol table entry for each of the " +
296 Twine(tableCount) + " table(s) present, but got " +
297 Twine(tableSymbolCount) + " symbol(s) instead.");
298 return;
301 // An MVP object file can have up to one table import, for the indirect
302 // function table, but will have no table definitions.
303 if (tables.size()) {
304 error(toString(this) +
305 ": unexpected table definition(s) without corresponding "
306 "symbol-table entries.");
307 return;
310 // An MVP object file can have only one table import.
311 if (tableCount != 1) {
312 error(toString(this) +
313 ": multiple table imports, but no corresponding symbol-table "
314 "entries.");
315 return;
318 const WasmImport *tableImport = nullptr;
319 for (const auto &import : wasmObj->imports()) {
320 if (import.Kind == WASM_EXTERNAL_TABLE) {
321 assert(!tableImport);
322 tableImport = &import;
325 assert(tableImport);
327 // We can only synthesize a symtab entry for the indirect function table; if
328 // it has an unexpected name or type, assume that it's not actually the
329 // indirect function table.
330 if (tableImport->Field != functionTableName ||
331 tableImport->Table.ElemType != uint8_t(ValType::FUNCREF)) {
332 error(toString(this) + ": table import " + Twine(tableImport->Field) +
333 " is missing a symbol table entry.");
334 return;
337 auto *info = make<WasmSymbolInfo>();
338 info->Name = tableImport->Field;
339 info->Kind = WASM_SYMBOL_TYPE_TABLE;
340 info->ImportModule = tableImport->Module;
341 info->ImportName = tableImport->Field;
342 info->Flags = WASM_SYMBOL_UNDEFINED;
343 info->Flags |= WASM_SYMBOL_NO_STRIP;
344 info->ElementIndex = 0;
345 LLVM_DEBUG(dbgs() << "Synthesizing symbol for table import: " << info->Name
346 << "\n");
347 const WasmGlobalType *globalType = nullptr;
348 const WasmTagType *tagType = nullptr;
349 const WasmSignature *signature = nullptr;
350 auto *wasmSym = make<WasmSymbol>(*info, globalType, &tableImport->Table,
351 tagType, signature);
352 Symbol *sym = createUndefined(*wasmSym, false);
353 // We're only sure it's a TableSymbol if the createUndefined succeeded.
354 if (errorCount())
355 return;
356 symbols.push_back(sym);
357 // Because there are no TABLE_NUMBER relocs, we can't compute accurate
358 // liveness info; instead, just mark the symbol as always live.
359 sym->markLive();
361 // We assume that this compilation unit has unrelocatable references to
362 // this table.
363 config->legacyFunctionTable = true;
366 static bool shouldMerge(const WasmSection &sec) {
367 if (config->optimize == 0)
368 return false;
369 // Sadly we don't have section attributes yet for custom sections, so we
370 // currently go by the name alone.
371 // TODO(sbc): Add ability for wasm sections to carry flags so we don't
372 // need to use names here.
373 // For now, keep in sync with uses of wasm::WASM_SEG_FLAG_STRINGS in
374 // MCObjectFileInfo::initWasmMCObjectFileInfo which creates these custom
375 // sections.
376 return sec.Name == ".debug_str" || sec.Name == ".debug_str.dwo" ||
377 sec.Name == ".debug_line_str";
380 static bool shouldMerge(const WasmSegment &seg) {
381 // As of now we only support merging strings, and only with single byte
382 // alignment (2^0).
383 if (!(seg.Data.LinkingFlags & WASM_SEG_FLAG_STRINGS) ||
384 (seg.Data.Alignment != 0))
385 return false;
387 // On a regular link we don't merge sections if -O0 (default is -O1). This
388 // sometimes makes the linker significantly faster, although the output will
389 // be bigger.
390 if (config->optimize == 0)
391 return false;
393 // A mergeable section with size 0 is useless because they don't have
394 // any data to merge. A mergeable string section with size 0 can be
395 // argued as invalid because it doesn't end with a null character.
396 // We'll avoid a mess by handling them as if they were non-mergeable.
397 if (seg.Data.Content.size() == 0)
398 return false;
400 return true;
403 void ObjFile::parse(bool ignoreComdats) {
404 // Parse a memory buffer as a wasm file.
405 LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
406 std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this));
408 auto *obj = dyn_cast<WasmObjectFile>(bin.get());
409 if (!obj)
410 fatal(toString(this) + ": not a wasm file");
411 if (!obj->isRelocatableObject())
412 fatal(toString(this) + ": not a relocatable wasm file");
414 bin.release();
415 wasmObj.reset(obj);
417 checkArch(obj->getArch());
419 // Build up a map of function indices to table indices for use when
420 // verifying the existing table index relocations
421 uint32_t totalFunctions =
422 wasmObj->getNumImportedFunctions() + wasmObj->functions().size();
423 tableEntriesRel.resize(totalFunctions);
424 tableEntries.resize(totalFunctions);
425 for (const WasmElemSegment &seg : wasmObj->elements()) {
426 int64_t offset;
427 if (seg.Offset.Opcode == WASM_OPCODE_I32_CONST)
428 offset = seg.Offset.Value.Int32;
429 else if (seg.Offset.Opcode == WASM_OPCODE_I64_CONST)
430 offset = seg.Offset.Value.Int64;
431 else
432 fatal(toString(this) + ": invalid table elements");
433 for (size_t index = 0; index < seg.Functions.size(); index++) {
434 auto functionIndex = seg.Functions[index];
435 tableEntriesRel[functionIndex] = index;
436 tableEntries[functionIndex] = offset + index;
440 ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats;
441 for (StringRef comdat : comdats) {
442 bool isNew = ignoreComdats || symtab->addComdat(comdat);
443 keptComdats.push_back(isNew);
446 uint32_t sectionIndex = 0;
448 // Bool for each symbol, true if called directly. This allows us to implement
449 // a weaker form of signature checking where undefined functions that are not
450 // called directly (i.e. only address taken) don't have to match the defined
451 // function's signature. We cannot do this for directly called functions
452 // because those signatures are checked at validation times.
453 // See https://bugs.llvm.org/show_bug.cgi?id=40412
454 std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false);
455 for (const SectionRef &sec : wasmObj->sections()) {
456 const WasmSection &section = wasmObj->getWasmSection(sec);
457 // Wasm objects can have at most one code and one data section.
458 if (section.Type == WASM_SEC_CODE) {
459 assert(!codeSection);
460 codeSection = &section;
461 } else if (section.Type == WASM_SEC_DATA) {
462 assert(!dataSection);
463 dataSection = &section;
464 } else if (section.Type == WASM_SEC_CUSTOM) {
465 InputChunk *customSec;
466 if (shouldMerge(section))
467 customSec = make<MergeInputChunk>(section, this);
468 else
469 customSec = make<InputSection>(section, this);
470 customSec->discarded = isExcludedByComdat(customSec);
471 customSections.emplace_back(customSec);
472 customSections.back()->setRelocations(section.Relocations);
473 customSectionsByIndex[sectionIndex] = customSections.back();
475 sectionIndex++;
476 // Scans relocations to determine if a function symbol is called directly.
477 for (const WasmRelocation &reloc : section.Relocations)
478 if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB)
479 isCalledDirectly[reloc.Index] = true;
482 typeMap.resize(getWasmObj()->types().size());
483 typeIsUsed.resize(getWasmObj()->types().size(), false);
486 // Populate `Segments`.
487 for (const WasmSegment &s : wasmObj->dataSegments()) {
488 InputChunk *seg;
489 if (shouldMerge(s)) {
490 seg = make<MergeInputChunk>(s, this);
491 } else
492 seg = make<InputSegment>(s, this);
493 seg->discarded = isExcludedByComdat(seg);
495 segments.emplace_back(seg);
497 setRelocs(segments, dataSection);
499 // Populate `Functions`.
500 ArrayRef<WasmFunction> funcs = wasmObj->functions();
501 ArrayRef<uint32_t> funcTypes = wasmObj->functionTypes();
502 ArrayRef<WasmSignature> types = wasmObj->types();
503 functions.reserve(funcs.size());
505 for (size_t i = 0, e = funcs.size(); i != e; ++i) {
506 auto* func = make<InputFunction>(types[funcTypes[i]], &funcs[i], this);
507 func->discarded = isExcludedByComdat(func);
508 functions.emplace_back(func);
510 setRelocs(functions, codeSection);
512 // Populate `Tables`.
513 for (const WasmTable &t : wasmObj->tables())
514 tables.emplace_back(make<InputTable>(t, this));
516 // Populate `Globals`.
517 for (const WasmGlobal &g : wasmObj->globals())
518 globals.emplace_back(make<InputGlobal>(g, this));
520 // Populate `Tags`.
521 for (const WasmTag &t : wasmObj->tags())
522 tags.emplace_back(make<InputTag>(types[t.Type.SigIndex], t, this));
524 // Populate `Symbols` based on the symbols in the object.
525 symbols.reserve(wasmObj->getNumberOfSymbols());
526 uint32_t tableSymbolCount = 0;
527 for (const SymbolRef &sym : wasmObj->symbols()) {
528 const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl());
529 if (wasmSym.isTypeTable())
530 tableSymbolCount++;
531 if (wasmSym.isDefined()) {
532 // createDefined may fail if the symbol is comdat excluded in which case
533 // we fall back to creating an undefined symbol
534 if (Symbol *d = createDefined(wasmSym)) {
535 symbols.push_back(d);
536 continue;
539 size_t idx = symbols.size();
540 symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx]));
543 addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount);
546 bool ObjFile::isExcludedByComdat(InputChunk *chunk) const {
547 uint32_t c = chunk->getComdat();
548 if (c == UINT32_MAX)
549 return false;
550 return !keptComdats[c];
553 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const {
554 return cast<FunctionSymbol>(symbols[index]);
557 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const {
558 return cast<GlobalSymbol>(symbols[index]);
561 TagSymbol *ObjFile::getTagSymbol(uint32_t index) const {
562 return cast<TagSymbol>(symbols[index]);
565 TableSymbol *ObjFile::getTableSymbol(uint32_t index) const {
566 return cast<TableSymbol>(symbols[index]);
569 SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const {
570 return cast<SectionSymbol>(symbols[index]);
573 DataSymbol *ObjFile::getDataSymbol(uint32_t index) const {
574 return cast<DataSymbol>(symbols[index]);
577 Symbol *ObjFile::createDefined(const WasmSymbol &sym) {
578 StringRef name = sym.Info.Name;
579 uint32_t flags = sym.Info.Flags;
581 switch (sym.Info.Kind) {
582 case WASM_SYMBOL_TYPE_FUNCTION: {
583 InputFunction *func =
584 functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
585 if (sym.isBindingLocal())
586 return make<DefinedFunction>(name, flags, this, func);
587 if (func->discarded)
588 return nullptr;
589 return symtab->addDefinedFunction(name, flags, this, func);
591 case WASM_SYMBOL_TYPE_DATA: {
592 InputChunk *seg = segments[sym.Info.DataRef.Segment];
593 auto offset = sym.Info.DataRef.Offset;
594 auto size = sym.Info.DataRef.Size;
595 if (sym.isBindingLocal())
596 return make<DefinedData>(name, flags, this, seg, offset, size);
597 if (seg->discarded)
598 return nullptr;
599 return symtab->addDefinedData(name, flags, this, seg, offset, size);
601 case WASM_SYMBOL_TYPE_GLOBAL: {
602 InputGlobal *global =
603 globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()];
604 if (sym.isBindingLocal())
605 return make<DefinedGlobal>(name, flags, this, global);
606 return symtab->addDefinedGlobal(name, flags, this, global);
608 case WASM_SYMBOL_TYPE_SECTION: {
609 InputChunk *section = customSectionsByIndex[sym.Info.ElementIndex];
610 assert(sym.isBindingLocal());
611 // Need to return null if discarded here? data and func only do that when
612 // binding is not local.
613 if (section->discarded)
614 return nullptr;
615 return make<SectionSymbol>(flags, section, this);
617 case WASM_SYMBOL_TYPE_TAG: {
618 InputTag *tag = tags[sym.Info.ElementIndex - wasmObj->getNumImportedTags()];
619 if (sym.isBindingLocal())
620 return make<DefinedTag>(name, flags, this, tag);
621 return symtab->addDefinedTag(name, flags, this, tag);
623 case WASM_SYMBOL_TYPE_TABLE: {
624 InputTable *table =
625 tables[sym.Info.ElementIndex - wasmObj->getNumImportedTables()];
626 if (sym.isBindingLocal())
627 return make<DefinedTable>(name, flags, this, table);
628 return symtab->addDefinedTable(name, flags, this, table);
631 llvm_unreachable("unknown symbol kind");
634 Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
635 StringRef name = sym.Info.Name;
636 uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED;
638 switch (sym.Info.Kind) {
639 case WASM_SYMBOL_TYPE_FUNCTION:
640 if (sym.isBindingLocal())
641 return make<UndefinedFunction>(name, sym.Info.ImportName,
642 sym.Info.ImportModule, flags, this,
643 sym.Signature, isCalledDirectly);
644 return symtab->addUndefinedFunction(name, sym.Info.ImportName,
645 sym.Info.ImportModule, flags, this,
646 sym.Signature, isCalledDirectly);
647 case WASM_SYMBOL_TYPE_DATA:
648 if (sym.isBindingLocal())
649 return make<UndefinedData>(name, flags, this);
650 return symtab->addUndefinedData(name, flags, this);
651 case WASM_SYMBOL_TYPE_GLOBAL:
652 if (sym.isBindingLocal())
653 return make<UndefinedGlobal>(name, sym.Info.ImportName,
654 sym.Info.ImportModule, flags, this,
655 sym.GlobalType);
656 return symtab->addUndefinedGlobal(name, sym.Info.ImportName,
657 sym.Info.ImportModule, flags, this,
658 sym.GlobalType);
659 case WASM_SYMBOL_TYPE_TABLE:
660 if (sym.isBindingLocal())
661 return make<UndefinedTable>(name, sym.Info.ImportName,
662 sym.Info.ImportModule, flags, this,
663 sym.TableType);
664 return symtab->addUndefinedTable(name, sym.Info.ImportName,
665 sym.Info.ImportModule, flags, this,
666 sym.TableType);
667 case WASM_SYMBOL_TYPE_SECTION:
668 llvm_unreachable("section symbols cannot be undefined");
670 llvm_unreachable("unknown symbol kind");
673 void ArchiveFile::parse() {
674 // Parse a MemoryBufferRef as an archive file.
675 LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
676 file = CHECK(Archive::create(mb), toString(this));
678 // Read the symbol table to construct Lazy symbols.
679 int count = 0;
680 for (const Archive::Symbol &sym : file->symbols()) {
681 symtab->addLazy(this, &sym);
682 ++count;
684 LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n");
687 void ArchiveFile::addMember(const Archive::Symbol *sym) {
688 const Archive::Child &c =
689 CHECK(sym->getMember(),
690 "could not get the member for symbol " + sym->getName());
692 // Don't try to load the same member twice (this can happen when members
693 // mutually reference each other).
694 if (!seen.insert(c.getChildOffset()).second)
695 return;
697 LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n");
698 LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
700 MemoryBufferRef mb =
701 CHECK(c.getMemoryBufferRef(),
702 "could not get the buffer for the member defining symbol " +
703 sym->getName());
705 InputFile *obj = createObjectFile(mb, getName());
706 symtab->addFile(obj);
709 static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
710 switch (gvVisibility) {
711 case GlobalValue::DefaultVisibility:
712 return WASM_SYMBOL_VISIBILITY_DEFAULT;
713 case GlobalValue::HiddenVisibility:
714 case GlobalValue::ProtectedVisibility:
715 return WASM_SYMBOL_VISIBILITY_HIDDEN;
717 llvm_unreachable("unknown visibility");
720 static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
721 const lto::InputFile::Symbol &objSym,
722 BitcodeFile &f) {
723 StringRef name = saver.save(objSym.getName());
725 uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
726 flags |= mapVisibility(objSym.getVisibility());
728 int c = objSym.getComdatIndex();
729 bool excludedByComdat = c != -1 && !keptComdats[c];
731 if (objSym.isUndefined() || excludedByComdat) {
732 flags |= WASM_SYMBOL_UNDEFINED;
733 if (objSym.isExecutable())
734 return symtab->addUndefinedFunction(name, None, None, flags, &f, nullptr,
735 true);
736 return symtab->addUndefinedData(name, flags, &f);
739 if (objSym.isExecutable())
740 return symtab->addDefinedFunction(name, flags, &f, nullptr);
741 return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0);
744 bool BitcodeFile::doneLTO = false;
746 void BitcodeFile::parse() {
747 if (doneLTO) {
748 error(toString(this) + ": attempt to add bitcode file after LTO.");
749 return;
752 obj = check(lto::InputFile::create(MemoryBufferRef(
753 mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier()))));
754 Triple t(obj->getTargetTriple());
755 if (!t.isWasm()) {
756 error(toString(this) + ": machine type must be wasm32 or wasm64");
757 return;
759 checkArch(t.getArch());
760 std::vector<bool> keptComdats;
761 // TODO Support nodeduplicate https://bugs.llvm.org/show_bug.cgi?id=50531
762 for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable())
763 keptComdats.push_back(symtab->addComdat(s.first));
765 for (const lto::InputFile::Symbol &objSym : obj->symbols())
766 symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this));
769 } // namespace wasm
770 } // namespace lld