Skip to content

Conversation

@mzukovec
Copy link
Contributor

@mzukovec mzukovec commented Jul 4, 2024

Add allow-multiple-definition flag to wasm-ld. This follows the ELF linker logic. In case of duplication, the first symbol met is used.

This PR resolves the #97543

@github-actions
Copy link

github-actions bot commented Jul 4, 2024

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 Jul 4, 2024

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-wasm

Author: None (mzukovec)

Changes

Add allow-multiple-definition flag to wasm-ld. This follows the ELF linker logic. In case of duplication, the first symbol met is used.

This PR resolves the #97543


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

6 Files Affected:

  • (added) lld/test/wasm/Inputs/allow-multiple-definition.s (+6)
  • (added) lld/test/wasm/allow-multiple-definition.s (+39)
  • (modified) lld/wasm/Config.h (+1)
  • (modified) lld/wasm/Driver.cpp (+3)
  • (modified) lld/wasm/Options.td (+4)
  • (modified) lld/wasm/SymbolTable.cpp (+8-3)
diff --git a/lld/test/wasm/Inputs/allow-multiple-definition.s b/lld/test/wasm/Inputs/allow-multiple-definition.s
new file mode 100644
index 0000000000000..7a5577cb12791
--- /dev/null
+++ b/lld/test/wasm/Inputs/allow-multiple-definition.s
@@ -0,0 +1,6 @@
+  .hidden foo
+  .globl  foo
+foo:
+  .functype foo () -> (i32)
+  i32.const 1
+  end_function
diff --git a/lld/test/wasm/allow-multiple-definition.s b/lld/test/wasm/allow-multiple-definition.s
new file mode 100644
index 0000000000000..605ed4965b15b
--- /dev/null
+++ b/lld/test/wasm/allow-multiple-definition.s
@@ -0,0 +1,39 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t1
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/allow-multiple-definition.s -o %t2
+# RUN: llvm-objdump --no-print-imm-hex -d %t1 | FileCheck --check-prefix=DEF1 %s
+# RUN: llvm-objdump --no-print-imm-hex -d %t2 | FileCheck --check-prefix=DEF2 %s
+# RUN: not wasm-ld -o %t.wasm %t1 %t2 2>&1 | FileCheck --check-prefix=DUP1 %s
+# RUN: not wasm-ld -o %t.wasm %t2 %t1 2>&1 | FileCheck --check-prefix=DUP2 %s
+# RUN: wasm-ld --allow-multiple-definition %t1 %t2 -o %t3
+# RUN: llvm-objdump --no-print-imm-hex -d %t3 | FileCheck --check-prefix=RES12 %s
+# RUN: wasm-ld --allow-multiple-definition %t2 %t1 -o %t4
+# RUN: llvm-objdump --no-print-imm-hex -d %t4 | FileCheck --check-prefix=RES21 %s
+
+
+# DUP1: duplicate symbol: foo
+# DUP2: duplicate symbol: foo
+
+# DEF1: i32.const 0
+# DEF2: i32.const 1
+
+# RES12: i32.const 0
+# RES21: i32.const 1
+
+# inputs contain different constants for function foo return.
+# Tests below checks that order of files in command line
+# affects on what symbol will be used.
+# If flag allow-multiple-definition is enabled the first
+# meet symbol should be used.
+
+  .hidden foo
+  .globl  foo
+foo:
+  .functype foo () -> (i32)
+  i32.const 0
+  end_function
+
+	.globl _start
+_start:
+	.functype	_start () -> (i32)
+	call foo
+	end_function
\ No newline at end of file
diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index d0ffa83d111e0..f36300cf21e32 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -43,6 +43,7 @@ enum class BuildIdKind { None, Fast, Sha1, Hexstring, Uuid };
 // and such fields have the same name as the corresponding options.
 // Most fields are initialized by the driver.
 struct Configuration {
+  bool allowMultipleDefinition;
   bool bsymbolic;
   bool checkFeatures;
   bool compressRelocations;
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index d099689911fc6..3bc64b1ae387d 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -467,6 +467,9 @@ getBuildId(opt::InputArgList &args) {
 
 // Initializes Config members by the command line options.
 static void readConfigs(opt::InputArgList &args) {
+  config->allowMultipleDefinition =
+      args.hasFlag(OPT_allow_multiple_definition,
+                   OPT_no_allow_multiple_definition, false);
   config->bsymbolic = args.hasArg(OPT_Bsymbolic);
   config->checkFeatures =
       args.hasFlag(OPT_check_features, OPT_no_check_features, true);
diff --git a/lld/wasm/Options.td b/lld/wasm/Options.td
index 7e954822ef642..dc08ae6f81310 100644
--- a/lld/wasm/Options.td
+++ b/lld/wasm/Options.td
@@ -42,6 +42,10 @@ def Bdynamic: F<"Bdynamic">, HelpText<"Link against shared libraries">;
 
 def Bstatic: F<"Bstatic">, HelpText<"Do not link against shared libraries (default)">;
 
+defm allow_multiple_definition: B<"allow-multiple-definition",
+    "Allow multiple definitions",
+    "Do not allow multiple definitions (default)">;
+
 def build_id: F<"build-id">, HelpText<"Alias for --build-id=fast">;
 
 def build_id_eq: J<"build-id=">, HelpText<"Generate build ID note">,
diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp
index 081f811cd139d..e50706d7c8a19 100644
--- a/lld/wasm/SymbolTable.cpp
+++ b/lld/wasm/SymbolTable.cpp
@@ -310,9 +310,14 @@ static bool shouldReplace(const Symbol *existing, InputFile *newFile,
   }
 
   // Neither symbol is week. They conflict.
-  error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " +
-        toString(existing->getFile()) + "\n>>> defined in " +
-        toString(newFile));
+  std::string msg = "duplicate symbol: " + toString(*existing) +
+                    "\n>>> defined in " + toString(existing->getFile()) +
+                    "\n>>> defined in " + toString(newFile);
+  if (config->allowMultipleDefinition) {
+    warn(msg);
+    return false;
+  }
+  error(msg);
   return true;
 }
 

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Thanks for this!

LGTM with a few comments

@github-actions
Copy link

github-actions bot commented Jul 5, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 843117050d63db6de983d4840ab3786f91c118b3 7e67621bbe9e126762233838fcf4e6b218ae5a50 -- lld/wasm/Config.h lld/wasm/Driver.cpp lld/wasm/SymbolTable.cpp
View the diff from clang-format here.
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 3bc64b1ae3..06b0a71cf2 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -467,9 +467,8 @@ getBuildId(opt::InputArgList &args) {
 
 // Initializes Config members by the command line options.
 static void readConfigs(opt::InputArgList &args) {
-  config->allowMultipleDefinition =
-      args.hasFlag(OPT_allow_multiple_definition,
-                   OPT_no_allow_multiple_definition, false);
+  config->allowMultipleDefinition = args.hasFlag(
+      OPT_allow_multiple_definition, OPT_no_allow_multiple_definition, false);
   config->bsymbolic = args.hasArg(OPT_Bsymbolic);
   config->checkFeatures =
       args.hasFlag(OPT_check_features, OPT_no_check_features, true);

@mzukovec
Copy link
Contributor Author

mzukovec commented Jul 7, 2024

I think all of the above comments should be fixed now.

@mzukovec mzukovec force-pushed the wasm_ld_allow_multiple branch from a0992b0 to 8874104 Compare July 8, 2024 18:44
@mzukovec
Copy link
Contributor Author

mzukovec commented Jul 8, 2024

Should be correctly formatted now

@mzukovec mzukovec force-pushed the wasm_ld_allow_multiple branch from 8874104 to 0c5cd70 Compare July 8, 2024 19:44
@mzukovec
Copy link
Contributor Author

This should be ready now

@mzukovec
Copy link
Contributor Author

@sbc100

@mzukovec mzukovec force-pushed the wasm_ld_allow_multiple branch from 0c5cd70 to 4765387 Compare July 18, 2024 06:40
@mzukovec
Copy link
Contributor Author

@sbc100 I had to rebase to main due to conflict in Options.td

@mzukovec
Copy link
Contributor Author

@sbc100 Can we merge this?

@@ -0,0 +1,6 @@
.hidden foo
Copy link
Member

Choose a reason for hiding this comment

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

FYI In ELF, we now migrate used-once extra files from Inputs/ to split-file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should that be included in this PR? It seems to me that it's better to leave it same as ELF test for easier tracking.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think matching ELF for now seem fine. We can try to follow the migration there as it happens.

@mzukovec
Copy link
Contributor Author

Ping

@mzukovec
Copy link
Contributor Author

mzukovec commented Sep 4, 2024

@sbc100 Can we merge this?

@sbc100 sbc100 changed the title wasm-ld: Add allow-multiple-definition flag [lld][WebAssembly] Add allow-multiple-definition flag Sep 4, 2024
@sbc100 sbc100 merged commit 0367305 into llvm:main Sep 4, 2024
@github-actions
Copy link

github-actions bot commented Sep 4, 2024

@mzukovec 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!

@mzukovec mzukovec deleted the wasm_ld_allow_multiple branch September 4, 2024 18:05
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.

4 participants