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 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion src/lib/libdylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,9 @@ var LibraryDylink = {
// We need to set rpath in flags based on the current library's rpath.
// We can't mutate flags or else if a depends on b and c and b depends on d,
// then c will be loaded with b's rpath instead of a's.
flags = {...flags, rpath: { parentLibPath: libName, paths: metadata.runtimePaths }}
var dso = LDSO.loadedLibsByName[libName];
var libPath = dso?.path ?? libName;
flags = {...flags, rpath: { parentLibPath: libPath, paths: metadata.runtimePaths }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should parentLibPath be just the dirname part of the parent? i.e. is there any point in passing a value if its just a basename?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible to calculate the dirname here.

For now, the dirname is calculated inside replaceORIGIN function, and the reason I did that was to isolate the code that depends on FS or PATH as much as possible.

If we calculate the dirname here, I think it can be something like:

    var dso = LDSO.loadedLibsByName[libName];
#if FILESYSTEM
    var libPath = PATH.dirname(dso?.path ?? libName);
#endif
    flags = {...flags, rpath: { parentLibPath: libPath, paths: metadata.runtimePaths }}

// now load needed libraries and the module itself.
if (flags.loadAsync) {
return metadata.neededDynlibs
Expand Down Expand Up @@ -937,6 +939,7 @@ var LibraryDylink = {
var dso = {
refcount: Infinity,
name,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does that mean that name is always just the basename / so_name? Is is name sometimes absolute too here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, only the dependencies of the libraries will have only the so_name in name. The parent library called from dlopen would have absolute path in the name.

Copy link
Contributor Author

@ryanking13 ryanking13 May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, only the dependencies of the libraries will have only the so_name in name.

So, I think the alternative approach would be ensuring loadDynamicLibrary always take absolute path. Let me try that approach instead.

path: name, // full path to the library, updated when the library is resolved in the filesystem.
exports: syms,
global: true,
};
Expand Down Expand Up @@ -1105,6 +1108,7 @@ var LibraryDylink = {
#endif
if (f) {
var libData = FS.readFile(f, {encoding: 'binary'});
dso.path = f;
return flags.loadAsync ? Promise.resolve(libData) : libData;
}
#endif
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
11758
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
27799
19 changes: 16 additions & 3 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7701,11 +7701,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 Down Expand Up @@ -7747,20 +7758,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', '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.wasm', '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', 'subdir/libhello_dep.so@/usr/lib/subdir/libhello_dep.so',
'--embed-file', 'subdir/libhello_nested_dep.so@/usr/lib/subdir/libhello_nested_dep.so',
'hello.wasm', '-sNO_AUTOLOAD_DYLIBS',
'-L./subdir', '-lhello_dep']
'-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