Skip to content

Commit 1cba4ff

Browse files
committed
[lld][WebAssembly] Add RUNTIME_PATH support to wasm-ld
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
1 parent c11e3da commit 1cba4ff

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

lld/test/wasm/rpath.s

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
2+
# RUN: wasm-ld -shared -o %t1.wasm %t.o -rpath /a/b/c -rpath /x/y/z
3+
# RUN: obj2yaml %t1.wasm | FileCheck %s
4+
5+
# CHECK: - Type: CUSTOM
6+
# CHECK-NEXT: Name: dylink.0
7+
# CHECK-NEXT: MemorySize: 0
8+
# CHECK-NEXT: MemoryAlignment: 0
9+
# CHECK-NEXT: TableSize: 0
10+
# CHECK-NEXT: TableAlignment: 0
11+
# CHECK-NEXT: Needed: []
12+
# CHECK-NEXT: RuntimePath:
13+
# CHECK-NEXT: - '/a/b/c'
14+
# CHECK-NEXT: - '/x/y/z'

lld/wasm/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct Config {
7070
bool pie;
7171
bool printGcSections;
7272
bool relocatable;
73+
llvm::SmallVector<llvm::StringRef, 0> rpath;
7374
bool saveTemps;
7475
bool shared;
7576
bool shlibSigCheck;

lld/wasm/Driver.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ static void readConfigs(opt::InputArgList &args) {
583583
ctx.arg.optimize = args::getInteger(args, OPT_O, 1);
584584
ctx.arg.outputFile = args.getLastArgValue(OPT_o);
585585
ctx.arg.relocatable = args.hasArg(OPT_relocatable);
586+
ctx.arg.rpath = args::getStrings(args, OPT_rpath);
586587
ctx.arg.gcSections =
587588
args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !ctx.arg.relocatable);
588589
for (auto *arg : args.filtered(OPT_keep_section))

lld/wasm/Options.td

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ def relocatable: F<"relocatable">, HelpText<"Create relocatable object file">;
132132

133133
defm reproduce: EEq<"reproduce", "Dump linker invocation and input files for debugging">;
134134

135+
defm rpath: Eq<"rpath", "Add a DT_RUNPATH to the output">;
136+
135137
defm rsp_quoting: Eq<"rsp-quoting", "Quoting style for response files">,
136138
MetaVarName<"[posix,windows]">;
137139

@@ -289,6 +291,7 @@ def: Flag<["-"], "t">, Alias<trace>, HelpText<"Alias for --trace">;
289291
def: JoinedOrSeparate<["-"], "y">, Alias<trace_symbol>, HelpText<"Alias for --trace-symbol">;
290292
def: JoinedOrSeparate<["-"], "u">, Alias<undefined>;
291293
def: Flag<["-"], "V">, Alias<v>, HelpText<"Alias for -v">;
294+
def: JoinedOrSeparate<["-"], "R">, Alias<rpath>, HelpText<"Alias for --rpath">;
292295

293296
// LTO-related options.
294297
def lto_O: JJ<"lto-O">, MetaVarName<"<opt-level>">,

lld/wasm/SyntheticSections.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "InputElement.h"
1717
#include "OutputSegment.h"
1818
#include "SymbolTable.h"
19+
#include "llvm/BinaryFormat/Wasm.h"
1920
#include "llvm/Support/Path.h"
2021
#include <optional>
2122

@@ -133,6 +134,14 @@ void DylinkSection::writeBody() {
133134

134135
sub.writeTo(os);
135136
}
137+
if (!ctx.arg.rpath.empty()) {
138+
SubSection sub(WASM_DYLINK_RUNTIME_PATH);
139+
writeUleb128(sub.os, ctx.arg.rpath.size(), "num rpath entries");
140+
for (const auto ref: ctx.arg.rpath) {
141+
writeStr(sub.os, ref, "rpath entry");
142+
}
143+
sub.writeTo(os);
144+
}
136145
}
137146

138147
uint32_t TypeSection::registerType(const WasmSignature &sig) {

0 commit comments

Comments
 (0)