diff --git a/lld/test/wasm/alias.ll b/lld/test/wasm/alias.ll index f88452e538c4..d91f4b10d7ed 100644 --- a/lld/test/wasm/alias.ll +++ b/lld/test/wasm/alias.ll @@ -1,5 +1,5 @@ ; RUN: llc -filetype=obj -o %t.o %s -; RUN: wasm-ld %t.o -o %t.wasm +; RUN: wasm-ld --export=start_alias %t.o -o %t.wasm ; RUN: obj2yaml %t.wasm | FileCheck %s target triple = "wasm32-unknown-unknown" diff --git a/lld/test/wasm/archive-export.ll b/lld/test/wasm/archive-export.ll index fc70ec708c70..37256ce37244 100644 --- a/lld/test/wasm/archive-export.ll +++ b/lld/test/wasm/archive-export.ll @@ -5,9 +5,9 @@ RUN: llc -filetype=obj %S/Inputs/archive1.ll -o %t.a1.o RUN: llc -filetype=obj %S/Inputs/archive2.ll -o %t.a2.o RUN: rm -f %t.a RUN: llvm-ar rcs %t.a %t.a1.o %t.a2.o -RUN: wasm-ld --export=archive2_symbol -o %t.wasm %t.a %t.o +RUN: wasm-ld --export-dynamic --export=archive2_symbol -o %t.wasm %t.a %t.o RUN: obj2yaml %t.wasm | FileCheck %s -RUN: wasm-ld -o %t.wasm %t.a %t.o +RUN: wasm-ld --export-dynamic -o %t.wasm %t.a %t.o RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=NOEXPORT CHECK: Exports: diff --git a/lld/test/wasm/call-indirect.ll b/lld/test/wasm/call-indirect.ll index f5dc2208af46..1b9e1723f6bf 100644 --- a/lld/test/wasm/call-indirect.ll +++ b/lld/test/wasm/call-indirect.ll @@ -1,6 +1,6 @@ ; RUN: llc -filetype=obj %p/Inputs/call-indirect.ll -o %t2.o ; RUN: llc -filetype=obj %s -o %t.o -; RUN: wasm-ld -o %t.wasm %t2.o %t.o +; RUN: wasm-ld --export-dynamic -o %t.wasm %t2.o %t.o ; RUN: obj2yaml %t.wasm | FileCheck %s ; bitcode generated from the following C code: diff --git a/lld/test/wasm/comdats.ll b/lld/test/wasm/comdats.ll index d0bec4cda146..9baea22050ef 100644 --- a/lld/test/wasm/comdats.ll +++ b/lld/test/wasm/comdats.ll @@ -1,7 +1,7 @@ ; RUN: llc -filetype=obj -mtriple=wasm32-unknown-uknown-wasm %p/Inputs/comdat1.ll -o %t1.o ; RUN: llc -filetype=obj -mtriple=wasm32-unknown-uknown-wasm %p/Inputs/comdat2.ll -o %t2.o ; RUN: llc -filetype=obj -mtriple=wasm32-unknown-uknown-wasm %s -o %t.o -; RUN: wasm-ld -o %t.wasm %t.o %t1.o %t2.o +; RUN: wasm-ld --export-dynamic -o %t.wasm %t.o %t1.o %t2.o ; RUN: obj2yaml %t.wasm | FileCheck %s target triple = "wasm32-unknown-unknown" diff --git a/lld/test/wasm/compress-relocs.ll b/lld/test/wasm/compress-relocs.ll index e5b93e43b60c..d14ea26a4c33 100644 --- a/lld/test/wasm/compress-relocs.ll +++ b/lld/test/wasm/compress-relocs.ll @@ -1,11 +1,11 @@ ; RUN: llc -filetype=obj %p/Inputs/call-indirect.ll -o %t2.o ; RUN: llc -filetype=obj %s -o %t.o -; RUN: wasm-ld -o %t.wasm %t2.o %t.o +; RUN: wasm-ld --export-dynamic -o %t.wasm %t2.o %t.o ; RUN: obj2yaml %t.wasm | FileCheck %s -; RUN: wasm-ld -O2 -o %t-opt.wasm %t2.o %t.o +; RUN: wasm-ld --export-dynamic -O2 -o %t-opt.wasm %t2.o %t.o ; RUN: obj2yaml %t-opt.wasm | FileCheck %s ; RUN: not wasm-ld --compress-relocations -o %t-compressed.wasm %t2.o %t.o 2>&1 | FileCheck %s -check-prefix=ERROR -; RUN: wasm-ld --strip-debug --compress-relocations -o %t-compressed.wasm %t2.o %t.o +; RUN: wasm-ld --export-dynamic --strip-debug --compress-relocations -o %t-compressed.wasm %t2.o %t.o ; RUN: obj2yaml %t-compressed.wasm | FileCheck %s -check-prefix=COMPRESS target triple = "wasm32-unknown-unknown-wasm" diff --git a/lld/test/wasm/cxx-mangling.ll b/lld/test/wasm/cxx-mangling.ll index c7f15af4f90b..0ab48748aca2 100644 --- a/lld/test/wasm/cxx-mangling.ll +++ b/lld/test/wasm/cxx-mangling.ll @@ -1,7 +1,7 @@ ; RUN: llc -filetype=obj %s -o %t.o -; RUN: wasm-ld --demangle -o %t_demangle.wasm %t.o +; RUN: wasm-ld --export=_Z3fooi --demangle -o %t_demangle.wasm %t.o ; RUN: obj2yaml %t_demangle.wasm | FileCheck %s -; RUN: wasm-ld --no-demangle -o %t_nodemangle.wasm %t.o +; RUN: wasm-ld --export=_Z3fooi --no-demangle -o %t_nodemangle.wasm %t.o ; RUN: obj2yaml %t_nodemangle.wasm | FileCheck %s target triple = "wasm32-unknown-unknown" diff --git a/lld/test/wasm/local-symbols.ll b/lld/test/wasm/local-symbols.ll index 5471466eb076..6d1a324b1c10 100644 --- a/lld/test/wasm/local-symbols.ll +++ b/lld/test/wasm/local-symbols.ll @@ -1,5 +1,6 @@ +; Test that internal symbols can still be GC'd when with --export-dynamic. ; RUN: llc -filetype=obj %s -o %t.o -; RUN: wasm-ld -o %t.wasm %t.o +; RUN: wasm-ld --export-dynamic -o %t.wasm %t.o ; RUN: obj2yaml %t.wasm | FileCheck %s target triple = "wasm32-unknown-unknown" diff --git a/lld/test/wasm/locals-duplicate.test b/lld/test/wasm/locals-duplicate.test index 3c67cdd8cfa4..8da0b22d1418 100644 --- a/lld/test/wasm/locals-duplicate.test +++ b/lld/test/wasm/locals-duplicate.test @@ -1,6 +1,6 @@ ; RUN: llc -filetype=obj %p/Inputs/locals-duplicate1.ll -o %t1.o ; RUN: llc -filetype=obj %p/Inputs/locals-duplicate2.ll -o %t2.o -; RUN: wasm-ld --no-entry -o %t.wasm %t1.o %t2.o +; RUN: wasm-ld --export-dynamic --no-entry -o %t.wasm %t1.o %t2.o ; RUN: obj2yaml %t.wasm | FileCheck %s ; CHECK: --- !WASM diff --git a/lld/test/wasm/lto/archive.ll b/lld/test/wasm/lto/archive.ll index 89fa840cdec5..ab067d8cdce4 100644 --- a/lld/test/wasm/lto/archive.ll +++ b/lld/test/wasm/lto/archive.ll @@ -2,7 +2,7 @@ ; RUN: rm -f %t.a ; RUN: llvm-ar rcs %t.a %t1.o ; RUN: llvm-as %s -o %t2.o -; RUN: wasm-ld %t2.o %t.a -o %t3 +; RUN: wasm-ld --export-dynamic %t2.o %t.a -o %t3 ; RUN: obj2yaml %t3 | FileCheck %s target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" diff --git a/lld/test/wasm/undefined-weak-call.ll b/lld/test/wasm/undefined-weak-call.ll index c13f5c1ae3f1..920379e8d9b9 100644 --- a/lld/test/wasm/undefined-weak-call.ll +++ b/lld/test/wasm/undefined-weak-call.ll @@ -1,5 +1,5 @@ ; RUN: llc -filetype=obj %s -o %t.o -; RUN: wasm-ld --no-entry --print-gc-sections %t.o \ +; RUN: wasm-ld --entry=callWeakFuncs --print-gc-sections %t.o \ ; RUN: -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-GC %s ; RUN: obj2yaml %t.wasm | FileCheck %s diff --git a/lld/test/wasm/visibility-hidden.ll b/lld/test/wasm/visibility-hidden.ll index d1a83cdf407c..b4fe0b53ba21 100644 --- a/lld/test/wasm/visibility-hidden.ll +++ b/lld/test/wasm/visibility-hidden.ll @@ -3,14 +3,14 @@ ; RUN: rm -f %t2.a ; RUN: llvm-ar rcs %t2.a %t2.o -; Test that hidden symbols are not exported, whether pulled in from an archive -; or directly. -; RUN: wasm-ld %t.o %t2.a -o %t.wasm +; Test that symbols with hidden visitiblity are not export, even with +; --export-dynamic +; RUN: wasm-ld --export-dynamic %t.o %t2.a -o %t.wasm ; RUN: obj2yaml %t.wasm | FileCheck %s -; Test that symbols with default visitiblity are not exported when -; --no-export-default is passed. -; RUN: wasm-ld --no-export-default %t.o %t2.a -o %t.nodef.wasm +; Test that symbols with default visitiblity are not exported without +; --export-dynamic +; RUN: wasm-ld %t.o %t2.a -o %t.nodef.wasm ; RUN: obj2yaml %t.nodef.wasm | FileCheck %s -check-prefix=NO-DEFAULT @@ -66,4 +66,13 @@ entry: ; NO-DEFAULT-NEXT: - Name: memory ; NO-DEFAULT-NEXT: Kind: MEMORY ; NO-DEFAULT-NEXT: Index: 0 +; NO-DEFAULT-NEXT: - Name: __heap_base +; NO-DEFAULT-NEXT: Kind: GLOBAL +; NO-DEFAULT-NEXT: Index: 1 +; NO-DEFAULT-NEXT: - Name: __data_end +; NO-DEFAULT-NEXT: Kind: GLOBAL +; NO-DEFAULT-NEXT: Index: 2 +; NO-DEFAULT-NEXT: - Name: _start +; NO-DEFAULT-NEXT: Kind: FUNCTION +; NO-DEFAULT-NEXT: Index: 3 ; NO-DEFAULT-NEXT: - Type: diff --git a/lld/test/wasm/weak-alias-overide.ll b/lld/test/wasm/weak-alias-overide.ll index a6a37f25c9dc..496900b2379a 100644 --- a/lld/test/wasm/weak-alias-overide.ll +++ b/lld/test/wasm/weak-alias-overide.ll @@ -1,6 +1,6 @@ ; RUN: llc -filetype=obj -o %t.o %s ; RUN: llc -filetype=obj %S/Inputs/weak-alias.ll -o %t2.o -; RUN: wasm-ld %t.o %t2.o -o %t.wasm +; RUN: wasm-ld --export-dynamic %t.o %t2.o -o %t.wasm ; RUN: obj2yaml %t.wasm | FileCheck %s ; Test that the strongly defined alias_fn from this file is used both here diff --git a/lld/test/wasm/weak-alias.ll b/lld/test/wasm/weak-alias.ll index 227906a55504..49f288503948 100644 --- a/lld/test/wasm/weak-alias.ll +++ b/lld/test/wasm/weak-alias.ll @@ -1,6 +1,6 @@ ; RUN: llc -filetype=obj -o %t.o %s ; RUN: llc -filetype=obj %S/Inputs/weak-alias.ll -o %t2.o -; RUN: wasm-ld %t.o %t2.o -o %t.wasm +; RUN: wasm-ld --export-dynamic %t.o %t2.o -o %t.wasm ; RUN: obj2yaml %t.wasm | FileCheck %s ; Test that weak aliases (alias_fn is a weak alias of direct_fn) are linked correctly diff --git a/lld/test/wasm/weak-symbols.ll b/lld/test/wasm/weak-symbols.ll index bd45de39ca35..7af432347505 100644 --- a/lld/test/wasm/weak-symbols.ll +++ b/lld/test/wasm/weak-symbols.ll @@ -1,7 +1,7 @@ ; RUN: llc -filetype=obj %p/Inputs/weak-symbol1.ll -o %t1.o ; RUN: llc -filetype=obj %p/Inputs/weak-symbol2.ll -o %t2.o ; RUN: llc -filetype=obj %s -o %t.o -; RUN: wasm-ld -no-gc-sections -o %t.wasm %t.o %t1.o %t2.o +; RUN: wasm-ld --export-dynamic -o %t.wasm %t.o %t1.o %t2.o ; RUN: obj2yaml %t.wasm | FileCheck %s target triple = "wasm32-unknown-unknown" @@ -29,7 +29,7 @@ entry: ; CHECK-NEXT: ReturnType: I32 ; CHECK-NEXT: ParamTypes: ; CHECK-NEXT: - Type: FUNCTION -; CHECK-NEXT: FunctionTypes: [ 0, 0, 1, 1, 1, 1 ] +; CHECK-NEXT: FunctionTypes: [ 0, 0, 1, 1, 1 ] ; CHECK-NEXT: - Type: TABLE ; CHECK-NEXT: Tables: ; CHECK-NEXT: - ElemType: ANYFUNC @@ -59,7 +59,7 @@ entry: ; CHECK-NEXT: Mutable: false ; CHECK-NEXT: InitExpr: ; CHECK-NEXT: Opcode: I32_CONST -; CHECK-NEXT: Value: 1032 +; CHECK-NEXT: Value: 1028 ; CHECK-NEXT: - Index: 3 ; CHECK-NEXT: Type: I32 ; CHECK-NEXT: Mutable: false @@ -91,7 +91,7 @@ entry: ; CHECK-NEXT: Index: 3 ; CHECK-NEXT: - Name: exportWeak2 ; CHECK-NEXT: Kind: FUNCTION -; CHECK-NEXT: Index: 5 +; CHECK-NEXT: Index: 4 ; CHECK-NEXT: - Type: ELEM ; CHECK-NEXT: Segments: ; CHECK-NEXT: - Offset: @@ -114,9 +114,6 @@ entry: ; CHECK-NEXT: Body: 4181808080000B ; CHECK-NEXT: - Index: 4 ; CHECK-NEXT: Locals: -; CHECK-NEXT: Body: 41020B -; CHECK-NEXT: - Index: 5 -; CHECK-NEXT: Locals: ; CHECK-NEXT: Body: 4181808080000B ; CHECK-NEXT: - Type: DATA ; CHECK-NEXT: Segments: @@ -125,7 +122,7 @@ entry: ; CHECK-NEXT: Offset: ; CHECK-NEXT: Opcode: I32_CONST ; CHECK-NEXT: Value: 1024 -; CHECK-NEXT: Content: '0100000002000000' +; CHECK-NEXT: Content: '01000000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: name ; CHECK-NEXT: FunctionNames: @@ -138,7 +135,5 @@ entry: ; CHECK-NEXT: - Index: 3 ; CHECK-NEXT: Name: exportWeak1 ; CHECK-NEXT: - Index: 4 -; CHECK-NEXT: Name: weakFn -; CHECK-NEXT: - Index: 5 ; CHECK-NEXT: Name: exportWeak2 ; CHECK-NEXT: ... diff --git a/lld/test/wasm/weak-undefined.ll b/lld/test/wasm/weak-undefined.ll index 048b91b133e6..2f2451add1e1 100644 --- a/lld/test/wasm/weak-undefined.ll +++ b/lld/test/wasm/weak-undefined.ll @@ -22,7 +22,8 @@ define i32* @get_address_of_global_var() #0 { define void @_start() #0 { entry: - %call = call i32* @get_address_of_global_var() + %call1 = call i32* @get_address_of_global_var() + %call2 = call i8* @get_address_of_foo() ret void } @@ -81,12 +82,6 @@ entry: ; CHECK-NEXT: - Name: __data_end ; CHECK-NEXT: Kind: GLOBAL ; CHECK-NEXT: Index: 2 -; CHECK-NEXT: - Name: get_address_of_foo -; CHECK-NEXT: Kind: FUNCTION -; CHECK-NEXT: Index: 1 -; CHECK-NEXT: - Name: get_address_of_global_var -; CHECK-NEXT: Kind: FUNCTION -; CHECK-NEXT: Index: 2 ; CHECK-NEXT: - Name: _start ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: Index: 3 @@ -103,5 +98,5 @@ entry: ; CHECK-NEXT: Body: 4180808080000B ; CHECK-NEXT: - Index: 3 ; CHECK-NEXT: Locals: -; CHECK-NEXT: Body: 1082808080001A0B +; CHECK-NEXT: Body: 1082808080001A1081808080001A0B ; CHECK-NEXT: ... diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h index e100ab82a590..233ac9f4abfc 100644 --- a/lld/wasm/Config.h +++ b/lld/wasm/Config.h @@ -24,7 +24,7 @@ struct Configuration { bool Demangle; bool DisableVerify; bool ExportAll; - bool ExportDefault; + bool ExportDynamic; bool ExportTable; bool GcSections; bool ImportMemory; diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index 897855873046..7165f75227e3 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -373,8 +373,8 @@ void LinkerDriver::link(ArrayRef ArgsArr) { Config->DisableVerify = Args.hasArg(OPT_disable_verify); Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start"); Config->ExportAll = Args.hasArg(OPT_export_all); - Config->ExportDefault = Args.hasFlag(OPT_export_default, - OPT_no_export_default, true); + Config->ExportDynamic = Args.hasFlag(OPT_export_dynamic, + OPT_no_export_dynamic, false); Config->ExportTable = Args.hasArg(OPT_export_table); errorHandler().FatalWarnings = Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); @@ -473,6 +473,11 @@ void LinkerDriver::link(ArrayRef ArgsArr) { WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol( "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN); WasmSym::DataEnd = Symtab->addSyntheticDataSymbol("__data_end", 0); + + // These two synthetic symbols exist purely for the embedder so we always + // want to export them. + WasmSym::HeapBase->ForceExport = true; + WasmSym::DataEnd->ForceExport = true; } createFiles(Args); @@ -507,7 +512,9 @@ void LinkerDriver::link(ArrayRef ArgsArr) { if (!Config->Entry.empty()) { EntrySym = handleUndefined(Config->Entry); - if (!EntrySym) + if (EntrySym && EntrySym->isDefined()) + EntrySym->ForceExport = true; + else error("entry symbol not defined (pass --no-entry to supress): " + Config->Entry); } diff --git a/lld/wasm/Options.td b/lld/wasm/Options.td index e98b1b9f89a1..c9636ca83ad5 100644 --- a/lld/wasm/Options.td +++ b/lld/wasm/Options.td @@ -30,9 +30,9 @@ defm demangle: B<"demangle", "Demangle symbol names", "Do not demangle symbol names">; -defm export_default: B<"export-default", - "Export symbols marked as 'default' visibility (default)", - "Do not export symbols marked as 'default' visibility">; +defm export_dynamic: B<"export-dynamic", + "Put symbols in the dynamic symbol table", + "Do not put symbols in the dynamic symbol table (default)">; def entry: S<"entry">, MetaVarName<"">, HelpText<"Name of entry point symbol">; @@ -144,12 +144,13 @@ defm whole_archive: B<"whole-archive", "Do not force load of all members in a static library (default)">; // Aliases -def alias_entry_e: JoinedOrSeparate<["-"], "e">, Alias; -def alias_entry_entry: J<"entry=">, Alias; -def alias_initial_memory_i: Flag<["-"], "i">, Alias; -def alias_max_memory_m: Flag<["-"], "m">, Alias; -def alias_relocatable_r: Flag<["-"], "r">, Alias; -def alias_undefined_u: JoinedOrSeparate<["-"], "u">, Alias; +def: JoinedOrSeparate<["-"], "e">, Alias; +def: J<"entry=">, Alias; +def: Flag<["-"], "E">, Alias, HelpText<"Alias for --export-dynamic">; +def: Flag<["-"], "i">, Alias; +def: Flag<["-"], "m">, Alias; +def: Flag<["-"], "r">, Alias; +def: JoinedOrSeparate<["-"], "u">, Alias; // LTO-related options. def lto_O: J<"lto-O">, MetaVarName<"">, diff --git a/lld/wasm/Symbols.cpp b/lld/wasm/Symbols.cpp index 68bcadbb9af4..035c7f2d725c 100644 --- a/lld/wasm/Symbols.cpp +++ b/lld/wasm/Symbols.cpp @@ -105,10 +105,10 @@ bool Symbol::isExported() const { if (ForceExport || Config->ExportAll) return true; - if (!Config->ExportDefault) - return false; + if (Config->ExportDynamic && !isHidden()) + return true; - return !isHidden(); + return false; } uint32_t FunctionSymbol::getFunctionIndex() const {