Skip to content

Conversation

@Lukasdoe
Copy link
Contributor

@Lukasdoe Lukasdoe commented Nov 26, 2025

When wrapping a symbol foo via -wrap=foo, we create the symbol __wrap_foo that replaces all mentions of foo. This feature was implemented for wasm-ld in commit a5ca34e.

So far, no valid signature has been attached to the undefined symbol, leading to a nullptr dereference in the logic for creating the import section. This change adds the correct signature to the wrapped symbol, enabling the generation of an import for it.

When wrapping a symbol `foo`, we create the symbol `__wrap_foo` that replaces all mentions of `foo`. This change adds the correct signature to the wrapped symbol to enable generating an import for it.
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Nov 26, 2025

@llvm/pr-subscribers-lld-wasm

Author: Lukas Döllerer (Lukasdoe)

Changes

When wrapping a symbol foo via -wrap=foo, we create the symbol __wrap_foo that replaces all mentions of foo.

So far, no valid signature has been attached to the undefined symbol, leading to a nullptr dereference in the logic for creating the import section. This change adds the correct signature to the wrapped symbol, enabling the generation of an import for it.


Full diff: https://github.com/llvm/llvm-project/pull/169656.diff

3 Files Affected:

  • (added) lld/test/wasm/wrap_import.s (+36)
  • (modified) lld/wasm/Driver.cpp (+5-3)
  • (modified) lld/wasm/SyntheticSections.cpp (+3-1)
diff --git a/lld/test/wasm/wrap_import.s b/lld/test/wasm/wrap_import.s
new file mode 100644
index 0000000000000..526c1f539a4fd
--- /dev/null
+++ b/lld/test/wasm/wrap_import.s
@@ -0,0 +1,36 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
+# RUN: wasm-ld -emit-relocs -wrap nosuchsym -wrap foo -allow-undefined -o %t.wasm %t.o
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+.globl foo
+.globl _start
+
+foo:
+  .functype foo () -> (i32)
+  i32.const 1
+  end_function
+
+_start:
+  .functype _start () -> ()
+  call  foo
+  drop
+  end_function
+
+# CHECK:      - Type:            IMPORT
+# CHECK-NEXT:   Imports:
+# CHECK-NEXT:     - Module:          env
+# CHECK-NEXT:       Field:           __wrap_foo
+# CHECK-NEXT:       Kind:            FUNCTION
+# CHECK-NEXT        SigIndex:        0
+
+# CHECK:      - Type:            CODE
+# CHECK-NEXT:   Relocations:
+# CHECK-NEXT:     - Type:            R_WASM_FUNCTION_INDEX_LEB
+# CHECK-NEXT:       Index:           1
+# CHECK-NEXT:       Offset:          0x4
+
+# CHECK:        FunctionNames:
+# CHECK-NEXT:      - Index:           0
+# CHECK-NEXT:        Name:            __wrap_foo
+# CHECK-NEXT:      - Index:           1
+# CHECK-NEXT:        Name:            _start
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index fac166587cb9b..97e50783985a8 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -1173,9 +1173,10 @@ struct WrappedSymbol {
   Symbol *wrap;
 };
 
-static Symbol *addUndefined(StringRef name) {
+static Symbol *addUndefined(StringRef name,
+                            const WasmSignature *signature = nullptr) {
   return symtab->addUndefinedFunction(name, std::nullopt, std::nullopt,
-                                      WASM_SYMBOL_UNDEFINED, nullptr, nullptr,
+                                      WASM_SYMBOL_UNDEFINED, nullptr, signature,
                                       false);
 }
 
@@ -1198,7 +1199,8 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
       continue;
 
     Symbol *real = addUndefined(saver().save("__real_" + name));
-    Symbol *wrap = addUndefined(saver().save("__wrap_" + name));
+    Symbol *wrap =
+        addUndefined(saver().save("__wrap_" + name), sym->getSignature());
     v.push_back({sym, real, wrap});
 
     // We want to tell LTO not to inline symbols to be overwritten
diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index 399a5084e6595..5e7b9c229f3ed 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -196,7 +196,9 @@ void ImportSection::addImport(Symbol *sym) {
   StringRef module = sym->importModule.value_or(defaultModule);
   StringRef name = sym->importName.value_or(sym->getName());
   if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
-    ImportKey<WasmSignature> key(*(f->getSignature()), module, name);
+    const WasmSignature *sig = f->getSignature();
+    assert(sig && "imported functions must have a signature");
+    ImportKey<WasmSignature> key(*sig, module, name);
     auto entry = importedFunctions.try_emplace(key, numImportedFunctions);
     if (entry.second) {
       importedSymbols.emplace_back(sym);

@llvmbot
Copy link
Member

llvmbot commented Nov 26, 2025

@llvm/pr-subscribers-lld

Author: Lukas Döllerer (Lukasdoe)

Changes

When wrapping a symbol foo via -wrap=foo, we create the symbol __wrap_foo that replaces all mentions of foo.

So far, no valid signature has been attached to the undefined symbol, leading to a nullptr dereference in the logic for creating the import section. This change adds the correct signature to the wrapped symbol, enabling the generation of an import for it.


Full diff: https://github.com/llvm/llvm-project/pull/169656.diff

3 Files Affected:

  • (added) lld/test/wasm/wrap_import.s (+36)
  • (modified) lld/wasm/Driver.cpp (+5-3)
  • (modified) lld/wasm/SyntheticSections.cpp (+3-1)
diff --git a/lld/test/wasm/wrap_import.s b/lld/test/wasm/wrap_import.s
new file mode 100644
index 0000000000000..526c1f539a4fd
--- /dev/null
+++ b/lld/test/wasm/wrap_import.s
@@ -0,0 +1,36 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
+# RUN: wasm-ld -emit-relocs -wrap nosuchsym -wrap foo -allow-undefined -o %t.wasm %t.o
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+.globl foo
+.globl _start
+
+foo:
+  .functype foo () -> (i32)
+  i32.const 1
+  end_function
+
+_start:
+  .functype _start () -> ()
+  call  foo
+  drop
+  end_function
+
+# CHECK:      - Type:            IMPORT
+# CHECK-NEXT:   Imports:
+# CHECK-NEXT:     - Module:          env
+# CHECK-NEXT:       Field:           __wrap_foo
+# CHECK-NEXT:       Kind:            FUNCTION
+# CHECK-NEXT        SigIndex:        0
+
+# CHECK:      - Type:            CODE
+# CHECK-NEXT:   Relocations:
+# CHECK-NEXT:     - Type:            R_WASM_FUNCTION_INDEX_LEB
+# CHECK-NEXT:       Index:           1
+# CHECK-NEXT:       Offset:          0x4
+
+# CHECK:        FunctionNames:
+# CHECK-NEXT:      - Index:           0
+# CHECK-NEXT:        Name:            __wrap_foo
+# CHECK-NEXT:      - Index:           1
+# CHECK-NEXT:        Name:            _start
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index fac166587cb9b..97e50783985a8 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -1173,9 +1173,10 @@ struct WrappedSymbol {
   Symbol *wrap;
 };
 
-static Symbol *addUndefined(StringRef name) {
+static Symbol *addUndefined(StringRef name,
+                            const WasmSignature *signature = nullptr) {
   return symtab->addUndefinedFunction(name, std::nullopt, std::nullopt,
-                                      WASM_SYMBOL_UNDEFINED, nullptr, nullptr,
+                                      WASM_SYMBOL_UNDEFINED, nullptr, signature,
                                       false);
 }
 
@@ -1198,7 +1199,8 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
       continue;
 
     Symbol *real = addUndefined(saver().save("__real_" + name));
-    Symbol *wrap = addUndefined(saver().save("__wrap_" + name));
+    Symbol *wrap =
+        addUndefined(saver().save("__wrap_" + name), sym->getSignature());
     v.push_back({sym, real, wrap});
 
     // We want to tell LTO not to inline symbols to be overwritten
diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index 399a5084e6595..5e7b9c229f3ed 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -196,7 +196,9 @@ void ImportSection::addImport(Symbol *sym) {
   StringRef module = sym->importModule.value_or(defaultModule);
   StringRef name = sym->importName.value_or(sym->getName());
   if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
-    ImportKey<WasmSignature> key(*(f->getSignature()), module, name);
+    const WasmSignature *sig = f->getSignature();
+    assert(sig && "imported functions must have a signature");
+    ImportKey<WasmSignature> key(*sig, module, name);
     auto entry = importedFunctions.try_emplace(key, numImportedFunctions);
     if (entry.second) {
       importedSymbols.emplace_back(sym);

# CHECK: - Type: CODE
# CHECK-NEXT: Relocations:
# CHECK-NEXT: - Type: R_WASM_FUNCTION_INDEX_LEB
# CHECK-NEXT: Index: 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right value here for the relocation? Shouldn't it be function zero (i.e. the __wrap_foo import) that is called here? It is this index 1 in the symbol table? If so you we should probably show the symbol table here too. (or just drop the Relocations and emit-relocs flag completely?)

Could be a bug in --emit-relocs maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just stepped through the code a bit and I think this is intended behavior.

  - Type:            CODE
    Relocations:
      - Type:            R_WASM_FUNCTION_INDEX_LEB
        Index:           1
        Offset:          0x4
    Functions:
      - Index:           1
        Locals:          []
        Body:            1080808080000B
  - Type:            CUSTOM
    Name:            linking
    Version:         2
    SymbolTable:
      - Index:           0
        Kind:            FUNCTION
        Name:            _start
        Flags:           [  ]
        Function:        1
      - Index:           1
        Kind:            FUNCTION
        Name:            __wrap_foo
        Flags:           [ UNDEFINED ]
        Function:        0
  - Type:            CUSTOM
    Name:            name
    FunctionNames:
      - Index:           0
        Name:            __wrap_foo
      - Index:           1
        Name:            _start

The symbol index is a different one than the function index. __wrap_foo has symbol index 1, which is why the relocation is emitted for index 1, while _start has function index 1, explaining why this function is shown.

@sbc100 sbc100 changed the title Fix SEGFAULT in wasm-ld when importing wrapped symbol [lld][WebAssembly] Fix SEGFAULT when importing wrapped symbol Nov 27, 2025
@sbc100 sbc100 enabled auto-merge (squash) November 27, 2025 19:13
@sbc100 sbc100 merged commit 965c3d7 into llvm:main Nov 27, 2025
10 checks passed
@github-actions
Copy link

@Lukasdoe Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants