Skip to content

[dylink] Fix rpath calculation in nested dependencies #24234

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/lib/libdylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,21 @@ var LibraryDylink = {
// now load needed libraries and the module itself.
if (flags.loadAsync) {
return metadata.neededDynlibs
.reduce((chain, dynNeeded) => chain.then(() =>
loadDynamicLibrary(dynNeeded, flags, localScope)
), Promise.resolve())
.reduce((chain, needed) => chain.then(() => {
#if FILESYSTEM
needed = findLibraryFS(needed, flags.rpath) ?? needed;
#endif
return loadDynamicLibrary(needed, flags, localScope);
}), Promise.resolve())
.then(loadModule);
}

metadata.neededDynlibs.forEach((needed) => loadDynamicLibrary(needed, flags, localScope));
metadata.neededDynlibs.forEach((needed) => {
#if FILESYSTEM
needed = findLibraryFS(needed, flags.rpath) ?? needed;
#endif
return loadDynamicLibrary(needed, flags, localScope);
});
return loadModule();
},

Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11735
11757
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27774
27823
27 changes: 20 additions & 7 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7710,11 +7710,22 @@ def test_ld_library_path(self, args):

@also_with_wasmfs
def test_dlopen_rpath(self):
create_file('hello_nested_dep.c', r'''
#include <stdio.h>

void hello_nested_dep() {
printf("Hello_nested_dep\n");
return;
}
''')
create_file('hello_dep.c', r'''
#include <stdio.h>

void hello_nested_dep();

void hello_dep() {
printf("Hello_dep\n");
hello_nested_dep();
return;
}
''')
Expand All @@ -7741,7 +7752,7 @@ def test_dlopen_rpath(self):
void (*f)();
double (*f2)(double);

h = dlopen("/usr/lib/libhello.wasm", RTLD_NOW);
h = dlopen("/usr/lib/libhello.so", RTLD_NOW);
assert(h);
f = dlsym(h, "hello");
assert(f);
Expand All @@ -7756,20 +7767,22 @@ def test_dlopen_rpath(self):
os.mkdir('subdir')

def _build(rpath_flag, expected, **kwds):
self.run_process([EMCC, '-o', 'subdir/libhello_dep.so', 'hello_dep.c', '-sSIDE_MODULE'])
self.run_process([EMCC, '-o', 'hello.wasm', 'hello.c', '-sSIDE_MODULE', 'subdir/libhello_dep.so'] + rpath_flag)
self.run_process([EMCC, '-o', 'subdir/libhello_nested_dep.so', 'hello_nested_dep.c', '-sSIDE_MODULE'])
self.run_process([EMCC, '-o', 'subdir/libhello_dep.so', 'hello_dep.c', '-sSIDE_MODULE', 'subdir/libhello_nested_dep.so'] + rpath_flag)
self.run_process([EMCC, '-o', 'hello.so', 'hello.c', '-sSIDE_MODULE', 'subdir/libhello_dep.so'] + rpath_flag)
args = ['--profiling-funcs', '-sMAIN_MODULE=2', '-sINITIAL_MEMORY=32Mb',
'--embed-file', 'hello.wasm@/usr/lib/libhello.wasm',
'--embed-file', 'hello.so@/usr/lib/libhello.so',
'--embed-file', 'subdir/libhello_dep.so@/usr/lib/subdir/libhello_dep.so',
'hello.wasm', '-sNO_AUTOLOAD_DYLIBS',
'-L./subdir', '-lhello_dep']
'--embed-file', 'subdir/libhello_nested_dep.so@/usr/lib/subdir/libhello_nested_dep.so',
'hello.so', '-sNO_AUTOLOAD_DYLIBS',
'-L./subdir', '-lhello_dep', '-lhello_nested_dep']
self.do_runf('main.c', expected, emcc_args=args, **kwds)

# case 1) without rpath: fail to locate the library
_build([], r"no such file or directory, open '.*libhello_dep\.so'", regex=True, assert_returncode=NON_ZERO)

# case 2) with rpath: success
_build(['-Wl,-rpath,$ORIGIN/subdir'], "Hello\nHello_dep\nOk\n")
_build(['-Wl,-rpath,$ORIGIN/subdir,-rpath,$ORIGIN'], "Hello\nHello_dep\nHello_nested_dep\nOk\n")

def test_dlopen_bad_flags(self):
create_file('main.c', r'''
Expand Down