Skip to content

[lld][WebAssembly] Add RUNTIME_PATH support to wasm-ld #129050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2025

Conversation

hoodmane
Copy link
Contributor

This finishes adding RPATH support for WebAssembly.

See my previous PR which added RPATH support to yaml2obj and obj2yaml: #126080
See corresponding update to the WebAssembly/tool-conventions repo on dynamic linking: WebAssembly/tool-conventions#246

cc @sbc100 @ryanking13

@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2025

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-wasm

Author: Hood Chatham (hoodmane)

Changes

This finishes adding RPATH support for WebAssembly.

See my previous PR which added RPATH support to yaml2obj and obj2yaml: #126080
See corresponding update to the WebAssembly/tool-conventions repo on dynamic linking: WebAssembly/tool-conventions#246

cc @sbc100 @ryanking13


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

5 Files Affected:

  • (added) lld/test/wasm/rpath.s (+14)
  • (modified) lld/wasm/Config.h (+1)
  • (modified) lld/wasm/Driver.cpp (+1)
  • (modified) lld/wasm/Options.td (+3)
  • (modified) lld/wasm/SyntheticSections.cpp (+9)
diff --git a/lld/test/wasm/rpath.s b/lld/test/wasm/rpath.s
new file mode 100644
index 0000000000000..1b430b40966a9
--- /dev/null
+++ b/lld/test/wasm/rpath.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
+# RUN: wasm-ld -shared -o %t1.wasm %t.o -rpath /a/b/c -rpath /x/y/z
+# RUN: obj2yaml %t1.wasm | FileCheck %s
+
+# CHECK:  - Type:            CUSTOM
+# CHECK-NEXT:    Name:            dylink.0
+# CHECK-NEXT:    MemorySize:      0
+# CHECK-NEXT:    MemoryAlignment: 0
+# CHECK-NEXT:    TableSize:       0
+# CHECK-NEXT:    TableAlignment:  0
+# CHECK-NEXT:    Needed:          []
+# CHECK-NEXT:    RuntimePath:
+# CHECK-NEXT:      - '/a/b/c'
+# CHECK-NEXT:      - '/x/y/z'
diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index 1fa6c42d9cd86..117ddc8650643 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -70,6 +70,7 @@ struct Config {
   bool pie;
   bool printGcSections;
   bool relocatable;
+  llvm::SmallVector<llvm::StringRef, 0> rpath;
   bool saveTemps;
   bool shared;
   bool shlibSigCheck;
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index c3a74dde6480e..85ca1fb782844 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -583,6 +583,7 @@ static void readConfigs(opt::InputArgList &args) {
   ctx.arg.optimize = args::getInteger(args, OPT_O, 1);
   ctx.arg.outputFile = args.getLastArgValue(OPT_o);
   ctx.arg.relocatable = args.hasArg(OPT_relocatable);
+  ctx.arg.rpath = args::getStrings(args, OPT_rpath);
   ctx.arg.gcSections =
       args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !ctx.arg.relocatable);
   for (auto *arg : args.filtered(OPT_keep_section))
diff --git a/lld/wasm/Options.td b/lld/wasm/Options.td
index 1316dc5c70d93..3df46870a2319 100644
--- a/lld/wasm/Options.td
+++ b/lld/wasm/Options.td
@@ -132,6 +132,8 @@ def relocatable: F<"relocatable">, HelpText<"Create relocatable object file">;
 
 defm reproduce: EEq<"reproduce", "Dump linker invocation and input files for debugging">;
 
+defm rpath: Eq<"rpath", "Add a DT_RUNPATH to the output">;
+
 defm rsp_quoting: Eq<"rsp-quoting", "Quoting style for response files">,
   MetaVarName<"[posix,windows]">;
 
@@ -289,6 +291,7 @@ def: Flag<["-"], "t">, Alias<trace>, HelpText<"Alias for --trace">;
 def: JoinedOrSeparate<["-"], "y">, Alias<trace_symbol>, HelpText<"Alias for --trace-symbol">;
 def: JoinedOrSeparate<["-"], "u">, Alias<undefined>;
 def: Flag<["-"], "V">, Alias<v>, HelpText<"Alias for -v">;
+def: JoinedOrSeparate<["-"], "R">, Alias<rpath>, HelpText<"Alias for --rpath">;
 
 // LTO-related options.
 def lto_O: JJ<"lto-O">, MetaVarName<"<opt-level>">,
diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index 7fb44b9f0c009..d009869341e83 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -16,6 +16,7 @@
 #include "InputElement.h"
 #include "OutputSegment.h"
 #include "SymbolTable.h"
+#include "llvm/BinaryFormat/Wasm.h"
 #include "llvm/Support/Path.h"
 #include <optional>
 
@@ -133,6 +134,14 @@ void DylinkSection::writeBody() {
 
     sub.writeTo(os);
   }
+  if (!ctx.arg.rpath.empty()) {
+      SubSection sub(WASM_DYLINK_RUNTIME_PATH);
+      writeUleb128(sub.os, ctx.arg.rpath.size(), "num rpath entries");
+      for (const auto ref: ctx.arg.rpath) {
+        writeStr(sub.os, ref, "rpath entry");
+      }
+      sub.writeTo(os);
+  }
 }
 
 uint32_t TypeSection::registerType(const WasmSignature &sig) {

Copy link

github-actions bot commented Feb 27, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

This finishes adding RPATH support for WebAssembly.

See my previous PR which added RPATH support to yaml2obj and obj2yaml:
llvm#126080
See corresponding update to the WebAssembly/tool-conventions repo on dynamic
linking: WebAssembly/tool-conventions#246
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.

lgtm % comments.

@hoodmane
Copy link
Contributor Author

Thanks for the review @sbc100! I think addressed the comments.

@sbc100 sbc100 merged commit 80ea31c into llvm:main Feb 28, 2025
9 of 10 checks passed
@hoodmane hoodmane deleted the wasm-ld-rpath branch February 28, 2025 19:53
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