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

This finishes adding RPATH support for WebAssembly.

See my previous PR which added RPATH support to yaml2obj and obj2yaml:
https://github.com/llvm/llvm-project/pull/126080
See corresponding update to the WebAssembly/tool-conventions repo on
dynamic linking:
https://github.com/WebAssembly/tool-conventions/pull/246
This commit is contained in:
Hood Chatham 2025-02-28 20:12:52 +01:00 committed by GitHub
parent 9da67e8c92
commit 80ea31ccd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 0 deletions

14
lld/test/wasm/rpath.s Normal file
View File

@ -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 --experimental-pic
# 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'

View File

@ -120,6 +120,7 @@ struct Config {
llvm::StringSet<> exportedSymbols;
std::vector<llvm::StringRef> requiredExports;
llvm::SmallVector<llvm::StringRef, 0> searchPaths;
llvm::SmallVector<llvm::StringRef, 0> rpath;
llvm::CachePruningPolicy thinLTOCachePolicy;
std::optional<std::vector<std::string>> features;
std::optional<std::vector<std::string>> extraFeatures;

View File

@ -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))

View File

@ -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]">;

View File

@ -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) {