Skip to content

dlopen does not make symbols of dependencies visible #18264

Closed

Description

@ryanking13

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.26 (8eaf19f)
clang version 16.0.0 (https://github.com/llvm/llvm-project f81f0cb75a2808a67d2662f044ad07628fc9d900)


loadDynamicLibrary, which is internally used in dlopen, loads a dynamic library and its dependencies.

When loading dependencies, loadDynamicLibrary propagates flags parameter as-is. So if a target library is loaded locally (e.g. loadDynamicLibrary(..., {global: false}) or dlopen(..., RTLD_NOW | RTLD_LOCAL)) it also loads its dependencies locally.

This is problematic because normally a shared library needs to use symbols from its dependencies.

Edited

The problem is that currently, a side module cannot see symbols of its dependencies if they are loaded locally. For example, when a.so depends on b.so, if we load a.so locally via dlopen("a.so", RTLD_LOCAL), a.so can't use symbols from b.so.

How to reproduce

main.c depends on dep.so and dep.so depends on dep2.so. In native (linux), this works fine.

#!/bin/bash

set -e

cat > dep2.h << EOF
int _add(int a);
EOF

cat > dep2.c << EOF
int _add(int a) { return a; }
EOF

cat > dep.h << EOF
int add(int a);
EOF

cat > dep.c << EOF
#include "dep2.h"
int add(int a) { return _add(a) + 1; }
EOF

cat > main.c << EOF
#include<dlfcn.h>
#include<stdio.h>
#include "dep.h"

int main() {
    void *lib_handle = dlopen("./dep.so", RTLD_NOW | RTLD_LOCAL);
    int (*add)(int) = (int (*)(int))dlsym(lib_handle, "add");

    printf("3 + 1 = %d\n", add(3));
}
EOF

rm -rf dep.o dep2.o dep.so dep2.so main
gcc dep2.c -shared -o dep2.so
gcc dep.c dep2.so -shared -o dep.so
gcc main.c -ldl -o main

LD_LIBRARY_PATH=$(pwd) ./main

While in emscripten, the same example raises error,

#!/bin/bash

set -e

cat > dep2.h << EOF
int _add(int a);
EOF

cat > dep2.c << EOF
int _add(int a) { return a; }
EOF

cat > dep.h << EOF
int add(int a);
EOF

cat > dep.c << EOF
#include "dep2.h"
int add(int a) { return _add(a) + 1; }
EOF

cat > main.c << EOF
#include<dlfcn.h>
#include<stdio.h>
#include "dep.h"

int main() {
    void *lib_handle = dlopen("./dep.so", RTLD_NOW | RTLD_LOCAL);
    int (*add)(int) = (int (*)(int))dlsym(lib_handle, "add");

    printf("3 + 1 = %d\n", add(3));
}
EOF

rm -rf dep.o dep2.o dep.so dep2.so main
emcc -c dep.c
emcc -c dep2.c
emcc dep2.o -shared -o dep2.so -sSIDE_MODULE
emcc dep.o dep2.so -shared -o dep.so -sSIDE_MODULE

emcc main.c -sMAIN_MODULE -o main

node main
Aborted(Assertion failed: undefined symbol `_add`. perhaps a side module was not linked in?
if this global was expected to arrive from a system library, try to build the MAIN_MODULE with EMCC_FORCE_STDLIBS=1 in the environment)

Activity

  1. self-assigned this
    on Nov 28, 2022
  2. allsey87 commented on Aug 4, 2023

    @allsey87

    @ryanking13 just to confirm my own understanding, the situation should be as follows right?

    image

    Actually, I think that perhaps 2 for liba.so is wrong, it can access the symbols in the main executable...

  3. sbc100 commented on Aug 4, 2023

    @sbc100
    Collaborator

    @ryanking13 you are correct, in emscripten all symbols from the main module are accessible to all modules that get loaded. Indeed we ship the whole of libc, libc++ (and all other core libraries) embedded in the main module so pretty much all modules that get loaded end up using some of those symbols.

  4. ryanking13 commented on Aug 6, 2023

    @ryanking13
    CollaboratorAuthor

    @allsey87 Right, and the issue I raised in this post is that if the library is loaded with RTLD_LOCAL, liba.so will not see the symbols in libb.so.

    By the way, I just noticed that the title and the content of this issue is a bit wrong. Dependencies don't have to be loaded globally, they just have to be visible to the libraries that use them, even if they are loaded locally.

  5. changed the title [-]`dlopen` should load dependencies globally[/-] [+]`dlopen` does not make symbols of dependencies visible[/+] on Aug 6, 2023
  6. sbc100 commented on Sep 8, 2023

    @sbc100
    Collaborator

    I confirmed that this was fixed in #18924 which was released in 3.1.34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions