Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
742a3e6
Fix nested dependencies handling in rpath
ryanking13 May 1, 2025
f1f85ff
fallback
ryanking13 May 1, 2025
65f39d4
syntax
ryanking13 May 1, 2025
8bc8fff
Automatic rebaseline of codesize expectations. NFC
ryanking13 May 1, 2025
3fb1ddd
Merge remote-tracking branch 'upstream/main' into rpath-nested-deps
ryanking13 May 2, 2025
62dc4a4
wasm ==> so
ryanking13 May 2, 2025
81b1dbd
Always pass absolute path in loadDynamicLibrary
ryanking13 May 2, 2025
f4e3b25
Remove unnecessary changes
ryanking13 May 2, 2025
8d92565
Automatic rebaseline of codesize expectations. NFC
ryanking13 May 2, 2025
d9289c2
Merge branch 'main' into rpath-nested-deps
ryanking13 May 7, 2025
3396b43
Merge remote-tracking branch 'upstream/main' into rpath-nested-deps
ryanking13 May 31, 2025
af36541
Store filename only
ryanking13 May 31, 2025
f4fecbb
Revert "Store filename only"
ryanking13 Jun 1, 2025
b86e1f0
Fix codesize
ryanking13 Jun 1, 2025
805749f
Merge remote-tracking branch 'upstream/main' into rpath-nested-deps
ryanking13 Nov 8, 2025
12f094a
rebaseline
ryanking13 Nov 8, 2025
ee7070d
Automatic rebaseline of codesize expectations. NFC
ryanking13 Nov 8, 2025
2813537
Merge remote-tracking branch 'upstream/main' into rpath-nested-deps
ryanking13 Nov 20, 2025
9d7550f
Automatic rebaseline of codesize expectations. NFC
ryanking13 Nov 20, 2025
bd87116
Merge remote-tracking branch 'upstream/main' into rpath-nested-deps
ryanking13 Nov 21, 2025
e0fda6d
Automatic rebaseline of codesize expectations. NFC
ryanking13 Nov 21, 2025
1680a0e
Automatic rebaseline of codesize expectations. NFC
ryanking13 Nov 21, 2025
7fc0ca7
Remove codesize files that are not used anymore
ryanking13 Nov 21, 2025
df8125e
Automatic rebaseline of codesize expectations. NFC
ryanking13 Nov 21, 2025
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