diff --git a/lld/test/wasm/rpath.s b/lld/test/wasm/rpath.s new file mode 100644 index 0000000000000..53372f490e9ad --- /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 --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' diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h index 1fa6c42d9cd86..0edb1fe6523b6 100644 --- a/lld/wasm/Config.h +++ b/lld/wasm/Config.h @@ -120,6 +120,7 @@ struct Config { llvm::StringSet<> exportedSymbols; std::vector requiredExports; llvm::SmallVector searchPaths; + llvm::SmallVector rpath; llvm::CachePruningPolicy thinLTOCachePolicy; std::optional> features; std::optional> extraFeatures; 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..19af82335d7b3 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]">; diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp index 7fb44b9f0c009..f5ec55946a591 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 @@ -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) {