1 //===- InputFiles.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "InputFiles.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"
25 #define DEBUG_TYPE "lld"
28 using namespace llvm::object
;
29 using namespace llvm::wasm
;
30 using namespace llvm::sys
;
34 // Returns a string in the format of "foo.o" or "foo.a(bar.o)".
35 std::string
toString(const wasm::InputFile
*file
) {
39 if (file
->archiveName
.empty())
40 return std::string(file
->getName());
42 return (file
->archiveName
+ "(" + file
->getName() + ")").str();
47 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");
58 std::unique_ptr
<llvm::TarWriter
> tar
;
60 std::optional
<MemoryBufferRef
> readFile(StringRef path
) {
61 log("Loading: " + path
);
63 auto mbOrErr
= MemoryBuffer::getFile(path
);
64 if (auto ec
= mbOrErr
.getError()) {
65 error("cannot open " + path
+ ": " + ec
.message());
68 std::unique_ptr
<MemoryBuffer
> &mb
= *mbOrErr
;
69 MemoryBufferRef mbref
= mb
->getMemBufferRef();
70 make
<std::unique_ptr
<MemoryBuffer
>>(std::move(mb
)); // take MB ownership
73 tar
->append(relativeToRoot(path
), mbref
.getBuffer());
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
);
89 if (magic
== file_magic::bitcode
)
90 return make
<BitcodeFile
>(mb
, archiveName
, offsetInArchive
);
92 std::string name
= mb
.getBufferIdentifier().str();
93 if (!archiveName
.empty()) {
94 name
= archiveName
.str() + "(" + name
+ ")";
97 fatal("unknown file type: " + name
);
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 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
:
132 case R_WASM_SECTION_OFFSET_I32
:
133 return getSectionSymbol(reloc
.Index
)->section
->getOffset(reloc
.Addend
);
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())
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
;
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())
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();
193 case R_WASM_TYPE_INDEX_LEB
:
194 return typeMap
[reloc
.Index
];
195 case R_WASM_FUNCTION_INDEX_LEB
:
196 case R_WASM_FUNCTION_INDEX_I32
:
197 return getFunctionSymbol(reloc
.Index
)->getFunctionIndex();
198 case R_WASM_GLOBAL_INDEX_LEB
:
199 case R_WASM_GLOBAL_INDEX_I32
:
200 if (auto gs
= dyn_cast
<GlobalSymbol
>(sym
))
201 return gs
->getGlobalIndex();
202 return sym
->getGOTIndex();
203 case R_WASM_TAG_INDEX_LEB
:
204 return getTagSymbol(reloc
.Index
)->getTagIndex();
205 case R_WASM_FUNCTION_OFFSET_I32
:
206 case R_WASM_FUNCTION_OFFSET_I64
: {
207 if (isa
<UndefinedFunction
>(sym
)) {
208 return tombstone
? tombstone
: reloc
.Addend
;
210 auto *f
= cast
<DefinedFunction
>(sym
);
211 return f
->function
->getOffset(f
->function
->getFunctionCodeOffset() +
214 case R_WASM_SECTION_OFFSET_I32
:
215 return getSectionSymbol(reloc
.Index
)->section
->getOffset(reloc
.Addend
);
216 case R_WASM_TABLE_NUMBER_LEB
:
217 return getTableSymbol(reloc
.Index
)->getTableNumber();
219 llvm_unreachable("unknown relocation type");
224 static void setRelocs(const std::vector
<T
*> &chunks
,
225 const WasmSection
*section
) {
229 ArrayRef
<WasmRelocation
> relocs
= section
->Relocations
;
230 assert(llvm::is_sorted(
231 relocs
, [](const WasmRelocation
&r1
, const WasmRelocation
&r2
) {
232 return r1
.Offset
< r2
.Offset
;
234 assert(llvm::is_sorted(chunks
, [](InputChunk
*c1
, InputChunk
*c2
) {
235 return c1
->getInputSectionOffset() < c2
->getInputSectionOffset();
238 auto relocsNext
= relocs
.begin();
239 auto relocsEnd
= relocs
.end();
240 auto relocLess
= [](const WasmRelocation
&r
, uint32_t val
) {
241 return r
.Offset
< val
;
243 for (InputChunk
*c
: chunks
) {
244 auto relocsStart
= std::lower_bound(relocsNext
, relocsEnd
,
245 c
->getInputSectionOffset(), relocLess
);
246 relocsNext
= std::lower_bound(
247 relocsStart
, relocsEnd
, c
->getInputSectionOffset() + c
->getInputSize(),
249 c
->setRelocations(ArrayRef
<WasmRelocation
>(relocsStart
, relocsNext
));
253 // An object file can have two approaches to tables. With the reference-types
254 // feature enabled, input files that define or use tables declare the tables
255 // using symbols, and record each use with a relocation. This way when the
256 // linker combines inputs, it can collate the tables used by the inputs,
257 // assigning them distinct table numbers, and renumber all the uses as
258 // appropriate. At the same time, the linker has special logic to build the
259 // indirect function table if it is needed.
261 // However, MVP object files (those that target WebAssembly 1.0, the "minimum
262 // viable product" version of WebAssembly) neither write table symbols nor
263 // record relocations. These files can have at most one table, the indirect
264 // function table used by call_indirect and which is the address space for
265 // function pointers. If this table is present, it is always an import. If we
266 // have a file with a table import but no table symbols, it is an MVP object
267 // file. synthesizeMVPIndirectFunctionTableSymbolIfNeeded serves as a shim when
268 // loading these input files, defining the missing symbol to allow the indirect
269 // function table to be built.
271 // As indirect function table table usage in MVP objects cannot be relocated,
272 // the linker must ensure that this table gets assigned index zero.
273 void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
274 uint32_t tableSymbolCount
) {
275 uint32_t tableCount
= wasmObj
->getNumImportedTables() + tables
.size();
277 // If there are symbols for all tables, then all is good.
278 if (tableCount
== tableSymbolCount
)
281 // It's possible for an input to define tables and also use the indirect
282 // function table, but forget to compile with -mattr=+reference-types.
283 // For these newer files, we require symbols for all tables, and
284 // relocations for all of their uses.
285 if (tableSymbolCount
!= 0) {
286 error(toString(this) +
287 ": expected one symbol table entry for each of the " +
288 Twine(tableCount
) + " table(s) present, but got " +
289 Twine(tableSymbolCount
) + " symbol(s) instead.");
293 // An MVP object file can have up to one table import, for the indirect
294 // function table, but will have no table definitions.
296 error(toString(this) +
297 ": unexpected table definition(s) without corresponding "
298 "symbol-table entries.");
302 // An MVP object file can have only one table import.
303 if (tableCount
!= 1) {
304 error(toString(this) +
305 ": multiple table imports, but no corresponding symbol-table "
310 const WasmImport
*tableImport
= nullptr;
311 for (const auto &import
: wasmObj
->imports()) {
312 if (import
.Kind
== WASM_EXTERNAL_TABLE
) {
313 assert(!tableImport
);
314 tableImport
= &import
;
319 // We can only synthesize a symtab entry for the indirect function table; if
320 // it has an unexpected name or type, assume that it's not actually the
321 // indirect function table.
322 if (tableImport
->Field
!= functionTableName
||
323 tableImport
->Table
.ElemType
!= uint8_t(ValType::FUNCREF
)) {
324 error(toString(this) + ": table import " + Twine(tableImport
->Field
) +
325 " is missing a symbol table entry.");
329 auto *info
= make
<WasmSymbolInfo
>();
330 info
->Name
= tableImport
->Field
;
331 info
->Kind
= WASM_SYMBOL_TYPE_TABLE
;
332 info
->ImportModule
= tableImport
->Module
;
333 info
->ImportName
= tableImport
->Field
;
334 info
->Flags
= WASM_SYMBOL_UNDEFINED
;
335 info
->Flags
|= WASM_SYMBOL_NO_STRIP
;
336 info
->ElementIndex
= 0;
337 LLVM_DEBUG(dbgs() << "Synthesizing symbol for table import: " << info
->Name
339 const WasmGlobalType
*globalType
= nullptr;
340 const WasmSignature
*signature
= nullptr;
342 make
<WasmSymbol
>(*info
, globalType
, &tableImport
->Table
, signature
);
343 Symbol
*sym
= createUndefined(*wasmSym
, false);
344 // We're only sure it's a TableSymbol if the createUndefined succeeded.
347 symbols
.push_back(sym
);
348 // Because there are no TABLE_NUMBER relocs, we can't compute accurate
349 // liveness info; instead, just mark the symbol as always live.
352 // We assume that this compilation unit has unrelocatable references to
354 config
->legacyFunctionTable
= true;
357 static bool shouldMerge(const WasmSection
&sec
) {
358 if (config
->optimize
== 0)
360 // Sadly we don't have section attributes yet for custom sections, so we
361 // currently go by the name alone.
362 // TODO(sbc): Add ability for wasm sections to carry flags so we don't
363 // need to use names here.
364 // For now, keep in sync with uses of wasm::WASM_SEG_FLAG_STRINGS in
365 // MCObjectFileInfo::initWasmMCObjectFileInfo which creates these custom
367 return sec
.Name
== ".debug_str" || sec
.Name
== ".debug_str.dwo" ||
368 sec
.Name
== ".debug_line_str";
371 static bool shouldMerge(const WasmSegment
&seg
) {
372 // As of now we only support merging strings, and only with single byte
374 if (!(seg
.Data
.LinkingFlags
& WASM_SEG_FLAG_STRINGS
) ||
375 (seg
.Data
.Alignment
!= 0))
378 // On a regular link we don't merge sections if -O0 (default is -O1). This
379 // sometimes makes the linker significantly faster, although the output will
381 if (config
->optimize
== 0)
384 // A mergeable section with size 0 is useless because they don't have
385 // any data to merge. A mergeable string section with size 0 can be
386 // argued as invalid because it doesn't end with a null character.
387 // We'll avoid a mess by handling them as if they were non-mergeable.
388 if (seg
.Data
.Content
.size() == 0)
394 void ObjFile::parse(bool ignoreComdats
) {
395 // Parse a memory buffer as a wasm file.
396 LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
397 std::unique_ptr
<Binary
> bin
= CHECK(createBinary(mb
), toString(this));
399 auto *obj
= dyn_cast
<WasmObjectFile
>(bin
.get());
401 fatal(toString(this) + ": not a wasm file");
402 if (!obj
->isRelocatableObject())
403 fatal(toString(this) + ": not a relocatable wasm file");
408 checkArch(obj
->getArch());
410 // Build up a map of function indices to table indices for use when
411 // verifying the existing table index relocations
412 uint32_t totalFunctions
=
413 wasmObj
->getNumImportedFunctions() + wasmObj
->functions().size();
414 tableEntriesRel
.resize(totalFunctions
);
415 tableEntries
.resize(totalFunctions
);
416 for (const WasmElemSegment
&seg
: wasmObj
->elements()) {
418 if (seg
.Offset
.Extended
)
419 fatal(toString(this) + ": extended init exprs not supported");
420 else if (seg
.Offset
.Inst
.Opcode
== WASM_OPCODE_I32_CONST
)
421 offset
= seg
.Offset
.Inst
.Value
.Int32
;
422 else if (seg
.Offset
.Inst
.Opcode
== WASM_OPCODE_I64_CONST
)
423 offset
= seg
.Offset
.Inst
.Value
.Int64
;
425 fatal(toString(this) + ": invalid table elements");
426 for (size_t index
= 0; index
< seg
.Functions
.size(); index
++) {
427 auto functionIndex
= seg
.Functions
[index
];
428 tableEntriesRel
[functionIndex
] = index
;
429 tableEntries
[functionIndex
] = offset
+ index
;
433 ArrayRef
<StringRef
> comdats
= wasmObj
->linkingData().Comdats
;
434 for (StringRef comdat
: comdats
) {
435 bool isNew
= ignoreComdats
|| symtab
->addComdat(comdat
);
436 keptComdats
.push_back(isNew
);
439 uint32_t sectionIndex
= 0;
441 // Bool for each symbol, true if called directly. This allows us to implement
442 // a weaker form of signature checking where undefined functions that are not
443 // called directly (i.e. only address taken) don't have to match the defined
444 // function's signature. We cannot do this for directly called functions
445 // because those signatures are checked at validation times.
446 // See https://github.com/llvm/llvm-project/issues/39758
447 std::vector
<bool> isCalledDirectly(wasmObj
->getNumberOfSymbols(), false);
448 for (const SectionRef
&sec
: wasmObj
->sections()) {
449 const WasmSection
§ion
= wasmObj
->getWasmSection(sec
);
450 // Wasm objects can have at most one code and one data section.
451 if (section
.Type
== WASM_SEC_CODE
) {
452 assert(!codeSection
);
453 codeSection
= §ion
;
454 } else if (section
.Type
== WASM_SEC_DATA
) {
455 assert(!dataSection
);
456 dataSection
= §ion
;
457 } else if (section
.Type
== WASM_SEC_CUSTOM
) {
458 InputChunk
*customSec
;
459 if (shouldMerge(section
))
460 customSec
= make
<MergeInputChunk
>(section
, this);
462 customSec
= make
<InputSection
>(section
, this);
463 customSec
->discarded
= isExcludedByComdat(customSec
);
464 customSections
.emplace_back(customSec
);
465 customSections
.back()->setRelocations(section
.Relocations
);
466 customSectionsByIndex
[sectionIndex
] = customSections
.back();
469 // Scans relocations to determine if a function symbol is called directly.
470 for (const WasmRelocation
&reloc
: section
.Relocations
)
471 if (reloc
.Type
== R_WASM_FUNCTION_INDEX_LEB
)
472 isCalledDirectly
[reloc
.Index
] = true;
475 typeMap
.resize(getWasmObj()->types().size());
476 typeIsUsed
.resize(getWasmObj()->types().size(), false);
479 // Populate `Segments`.
480 for (const WasmSegment
&s
: wasmObj
->dataSegments()) {
483 seg
= make
<MergeInputChunk
>(s
, this);
485 seg
= make
<InputSegment
>(s
, this);
486 seg
->discarded
= isExcludedByComdat(seg
);
487 // Older object files did not include WASM_SEG_FLAG_TLS and instead
488 // relied on the naming convention. To maintain compat with such objects
489 // we still imply the TLS flag based on the name of the segment.
491 (seg
->name
.starts_with(".tdata") || seg
->name
.starts_with(".tbss")))
492 seg
->flags
|= WASM_SEG_FLAG_TLS
;
493 segments
.emplace_back(seg
);
495 setRelocs(segments
, dataSection
);
497 // Populate `Functions`.
498 ArrayRef
<WasmFunction
> funcs
= wasmObj
->functions();
499 ArrayRef
<WasmSignature
> types
= wasmObj
->types();
500 functions
.reserve(funcs
.size());
502 for (auto &f
: funcs
) {
503 auto *func
= make
<InputFunction
>(types
[f
.SigIndex
], &f
, this);
504 func
->discarded
= isExcludedByComdat(func
);
505 functions
.emplace_back(func
);
507 setRelocs(functions
, codeSection
);
509 // Populate `Tables`.
510 for (const WasmTable
&t
: wasmObj
->tables())
511 tables
.emplace_back(make
<InputTable
>(t
, this));
513 // Populate `Globals`.
514 for (const WasmGlobal
&g
: wasmObj
->globals())
515 globals
.emplace_back(make
<InputGlobal
>(g
, this));
518 for (const WasmTag
&t
: wasmObj
->tags())
519 tags
.emplace_back(make
<InputTag
>(types
[t
.SigIndex
], t
, this));
521 // Populate `Symbols` based on the symbols in the object.
522 symbols
.reserve(wasmObj
->getNumberOfSymbols());
523 uint32_t tableSymbolCount
= 0;
524 for (const SymbolRef
&sym
: wasmObj
->symbols()) {
525 const WasmSymbol
&wasmSym
= wasmObj
->getWasmSymbol(sym
.getRawDataRefImpl());
526 if (wasmSym
.isTypeTable())
528 if (wasmSym
.isDefined()) {
529 // createDefined may fail if the symbol is comdat excluded in which case
530 // we fall back to creating an undefined symbol
531 if (Symbol
*d
= createDefined(wasmSym
)) {
532 symbols
.push_back(d
);
536 size_t idx
= symbols
.size();
537 symbols
.push_back(createUndefined(wasmSym
, isCalledDirectly
[idx
]));
540 addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount
);
543 bool ObjFile::isExcludedByComdat(const InputChunk
*chunk
) const {
544 uint32_t c
= chunk
->getComdat();
547 return !keptComdats
[c
];
550 FunctionSymbol
*ObjFile::getFunctionSymbol(uint32_t index
) const {
551 return cast
<FunctionSymbol
>(symbols
[index
]);
554 GlobalSymbol
*ObjFile::getGlobalSymbol(uint32_t index
) const {
555 return cast
<GlobalSymbol
>(symbols
[index
]);
558 TagSymbol
*ObjFile::getTagSymbol(uint32_t index
) const {
559 return cast
<TagSymbol
>(symbols
[index
]);
562 TableSymbol
*ObjFile::getTableSymbol(uint32_t index
) const {
563 return cast
<TableSymbol
>(symbols
[index
]);
566 SectionSymbol
*ObjFile::getSectionSymbol(uint32_t index
) const {
567 return cast
<SectionSymbol
>(symbols
[index
]);
570 DataSymbol
*ObjFile::getDataSymbol(uint32_t index
) const {
571 return cast
<DataSymbol
>(symbols
[index
]);
574 Symbol
*ObjFile::createDefined(const WasmSymbol
&sym
) {
575 StringRef name
= sym
.Info
.Name
;
576 uint32_t flags
= sym
.Info
.Flags
;
578 switch (sym
.Info
.Kind
) {
579 case WASM_SYMBOL_TYPE_FUNCTION
: {
580 InputFunction
*func
=
581 functions
[sym
.Info
.ElementIndex
- wasmObj
->getNumImportedFunctions()];
582 if (sym
.isBindingLocal())
583 return make
<DefinedFunction
>(name
, flags
, this, func
);
586 return symtab
->addDefinedFunction(name
, flags
, this, func
);
588 case WASM_SYMBOL_TYPE_DATA
: {
589 InputChunk
*seg
= segments
[sym
.Info
.DataRef
.Segment
];
590 auto offset
= sym
.Info
.DataRef
.Offset
;
591 auto size
= sym
.Info
.DataRef
.Size
;
592 // Support older (e.g. llvm 13) object files that pre-date the per-symbol
593 // TLS flag, and symbols were assumed to be TLS by being defined in a TLS
595 if (!(flags
& WASM_SYMBOL_TLS
) && seg
->isTLS())
596 flags
|= WASM_SYMBOL_TLS
;
597 if (sym
.isBindingLocal())
598 return make
<DefinedData
>(name
, flags
, this, seg
, offset
, size
);
601 return symtab
->addDefinedData(name
, flags
, this, seg
, offset
, size
);
603 case WASM_SYMBOL_TYPE_GLOBAL
: {
604 InputGlobal
*global
=
605 globals
[sym
.Info
.ElementIndex
- wasmObj
->getNumImportedGlobals()];
606 if (sym
.isBindingLocal())
607 return make
<DefinedGlobal
>(name
, flags
, this, global
);
608 return symtab
->addDefinedGlobal(name
, flags
, this, global
);
610 case WASM_SYMBOL_TYPE_SECTION
: {
611 InputChunk
*section
= customSectionsByIndex
[sym
.Info
.ElementIndex
];
612 assert(sym
.isBindingLocal());
613 // Need to return null if discarded here? data and func only do that when
614 // binding is not local.
615 if (section
->discarded
)
617 return make
<SectionSymbol
>(flags
, section
, this);
619 case WASM_SYMBOL_TYPE_TAG
: {
620 InputTag
*tag
= tags
[sym
.Info
.ElementIndex
- wasmObj
->getNumImportedTags()];
621 if (sym
.isBindingLocal())
622 return make
<DefinedTag
>(name
, flags
, this, tag
);
623 return symtab
->addDefinedTag(name
, flags
, this, tag
);
625 case WASM_SYMBOL_TYPE_TABLE
: {
627 tables
[sym
.Info
.ElementIndex
- wasmObj
->getNumImportedTables()];
628 if (sym
.isBindingLocal())
629 return make
<DefinedTable
>(name
, flags
, this, table
);
630 return symtab
->addDefinedTable(name
, flags
, this, table
);
633 llvm_unreachable("unknown symbol kind");
636 Symbol
*ObjFile::createUndefined(const WasmSymbol
&sym
, bool isCalledDirectly
) {
637 StringRef name
= sym
.Info
.Name
;
638 uint32_t flags
= sym
.Info
.Flags
| WASM_SYMBOL_UNDEFINED
;
640 switch (sym
.Info
.Kind
) {
641 case WASM_SYMBOL_TYPE_FUNCTION
:
642 if (sym
.isBindingLocal())
643 return make
<UndefinedFunction
>(name
, sym
.Info
.ImportName
,
644 sym
.Info
.ImportModule
, flags
, this,
645 sym
.Signature
, isCalledDirectly
);
646 return symtab
->addUndefinedFunction(name
, sym
.Info
.ImportName
,
647 sym
.Info
.ImportModule
, flags
, this,
648 sym
.Signature
, isCalledDirectly
);
649 case WASM_SYMBOL_TYPE_DATA
:
650 if (sym
.isBindingLocal())
651 return make
<UndefinedData
>(name
, flags
, this);
652 return symtab
->addUndefinedData(name
, flags
, this);
653 case WASM_SYMBOL_TYPE_GLOBAL
:
654 if (sym
.isBindingLocal())
655 return make
<UndefinedGlobal
>(name
, sym
.Info
.ImportName
,
656 sym
.Info
.ImportModule
, flags
, this,
658 return symtab
->addUndefinedGlobal(name
, sym
.Info
.ImportName
,
659 sym
.Info
.ImportModule
, flags
, this,
661 case WASM_SYMBOL_TYPE_TABLE
:
662 if (sym
.isBindingLocal())
663 return make
<UndefinedTable
>(name
, sym
.Info
.ImportName
,
664 sym
.Info
.ImportModule
, flags
, this,
666 return symtab
->addUndefinedTable(name
, sym
.Info
.ImportName
,
667 sym
.Info
.ImportModule
, flags
, this,
669 case WASM_SYMBOL_TYPE_TAG
:
670 if (sym
.isBindingLocal())
671 return make
<UndefinedTag
>(name
, sym
.Info
.ImportName
,
672 sym
.Info
.ImportModule
, flags
, this,
674 return symtab
->addUndefinedTag(name
, sym
.Info
.ImportName
,
675 sym
.Info
.ImportModule
, flags
, this,
677 case WASM_SYMBOL_TYPE_SECTION
:
678 llvm_unreachable("section symbols cannot be undefined");
680 llvm_unreachable("unknown symbol kind");
684 StringRef
strip(StringRef s
) {
685 while (s
.starts_with(" ")) {
688 while (s
.ends_with(" ")) {
694 void StubFile::parse() {
697 SmallVector
<StringRef
> lines
;
698 mb
.getBuffer().split(lines
, '\n');
699 for (StringRef line
: lines
) {
702 // File must begin with #STUB
704 assert(line
== "#STUB");
708 // Lines starting with # are considered comments
709 if (line
.starts_with("#"))
714 std::tie(sym
, rest
) = line
.split(':');
718 symbolDependencies
[sym
] = {};
720 while (rest
.size()) {
722 std::tie(dep
, rest
) = rest
.split(',');
724 symbolDependencies
[sym
].push_back(dep
);
729 void ArchiveFile::parse() {
730 // Parse a MemoryBufferRef as an archive file.
731 LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
732 file
= CHECK(Archive::create(mb
), toString(this));
734 // Read the symbol table to construct Lazy symbols.
736 for (const Archive::Symbol
&sym
: file
->symbols()) {
737 symtab
->addLazy(this, &sym
);
740 LLVM_DEBUG(dbgs() << "Read " << count
<< " symbols\n");
744 void ArchiveFile::addMember(const Archive::Symbol
*sym
) {
745 const Archive::Child
&c
=
746 CHECK(sym
->getMember(),
747 "could not get the member for symbol " + sym
->getName());
749 // Don't try to load the same member twice (this can happen when members
750 // mutually reference each other).
751 if (!seen
.insert(c
.getChildOffset()).second
)
754 LLVM_DEBUG(dbgs() << "loading lazy: " << sym
->getName() << "\n");
755 LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
758 CHECK(c
.getMemoryBufferRef(),
759 "could not get the buffer for the member defining symbol " +
762 InputFile
*obj
= createObjectFile(mb
, getName(), c
.getChildOffset());
763 symtab
->addFile(obj
, sym
->getName());
766 static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility
) {
767 switch (gvVisibility
) {
768 case GlobalValue::DefaultVisibility
:
769 return WASM_SYMBOL_VISIBILITY_DEFAULT
;
770 case GlobalValue::HiddenVisibility
:
771 case GlobalValue::ProtectedVisibility
:
772 return WASM_SYMBOL_VISIBILITY_HIDDEN
;
774 llvm_unreachable("unknown visibility");
777 static Symbol
*createBitcodeSymbol(const std::vector
<bool> &keptComdats
,
778 const lto::InputFile::Symbol
&objSym
,
780 StringRef name
= saver().save(objSym
.getName());
782 uint32_t flags
= objSym
.isWeak() ? WASM_SYMBOL_BINDING_WEAK
: 0;
783 flags
|= mapVisibility(objSym
.getVisibility());
785 int c
= objSym
.getComdatIndex();
786 bool excludedByComdat
= c
!= -1 && !keptComdats
[c
];
788 if (objSym
.isUndefined() || excludedByComdat
) {
789 flags
|= WASM_SYMBOL_UNDEFINED
;
790 if (objSym
.isExecutable())
791 return symtab
->addUndefinedFunction(name
, std::nullopt
, std::nullopt
,
792 flags
, &f
, nullptr, true);
793 return symtab
->addUndefinedData(name
, flags
, &f
);
796 if (objSym
.isExecutable())
797 return symtab
->addDefinedFunction(name
, flags
, &f
, nullptr);
798 return symtab
->addDefinedData(name
, flags
, &f
, nullptr, 0, 0);
801 BitcodeFile::BitcodeFile(MemoryBufferRef m
, StringRef archiveName
,
802 uint64_t offsetInArchive
)
803 : InputFile(BitcodeKind
, m
) {
804 this->archiveName
= std::string(archiveName
);
806 std::string path
= mb
.getBufferIdentifier().str();
808 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
809 // name. If two archives define two members with the same name, this
810 // causes a collision which result in only one of the objects being taken
811 // into consideration at LTO time (which very likely causes undefined
812 // symbols later in the link stage). So we append file offset to make
814 StringRef name
= archiveName
.empty()
816 : saver().save(archiveName
+ "(" + path::filename(path
) +
817 " at " + utostr(offsetInArchive
) + ")");
818 MemoryBufferRef
mbref(mb
.getBuffer(), name
);
820 obj
= check(lto::InputFile::create(mbref
));
822 // If this isn't part of an archive, it's eagerly linked, so mark it live.
823 if (archiveName
.empty())
827 bool BitcodeFile::doneLTO
= false;
829 void BitcodeFile::parse(StringRef symName
) {
831 error(toString(this) + ": attempt to add bitcode file after LTO (" + symName
+ ")");
835 Triple
t(obj
->getTargetTriple());
837 error(toString(this) + ": machine type must be wasm32 or wasm64");
840 checkArch(t
.getArch());
841 std::vector
<bool> keptComdats
;
842 // TODO Support nodeduplicate
843 // https://github.com/llvm/llvm-project/issues/49875
844 for (std::pair
<StringRef
, Comdat::SelectionKind
> s
: obj
->getComdatTable())
845 keptComdats
.push_back(symtab
->addComdat(s
.first
));
847 for (const lto::InputFile::Symbol
&objSym
: obj
->symbols())
848 symbols
.push_back(createBitcodeSymbol(keptComdats
, objSym
, *this));