Skip to content
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

Creating a shared library from init.c and tramp.S #24

Closed
lastlaugher opened this issue Feb 12, 2023 · 5 comments
Closed

Creating a shared library from init.c and tramp.S #24

lastlaugher opened this issue Feb 12, 2023 · 5 comments
Assignees

Comments

@lastlaugher
Copy link

At first, thanks yugr for creating this nice proejct.

In the example case, it works fine but there is an issue in my case.
I applied Implib.so to libA.so which I want for delay-loading.
I created a shared library, libB.so from libA.so.init.c and libA.tramp.S
And then I built an executable with libB.
The problem is the executable tries to load libA at strarup even though a function in libA is called after some processing is finished. Following is an error message.

$ ./executable
implib-gen: libA.so.4: failed to load library 'libA.so.4' via dlopen: libA.so.4: cannot open shared object file: No such file or directory
executable: /home/xyz/Implib.so/libA.so.4.init.c:61: void *load_library(): Assertion `0 && "Assertion in generated code"' failed.
Aborted

Is this expected result? Or could you help me delay loading works fine in my case?

@yugr yugr self-assigned this Feb 12, 2023
@yugr
Copy link
Owner

yugr commented Feb 12, 2023

Thank you for trying Implib.so.

First of all, there are a few caveats with your usecase (putting wrappers in a dedicated shared library, rather than into executable):

As for the crash, is there a chance that there is a C++ constructor in executable which calls function from libA.so? This would cause libA.so to be loaded at startup. If that's not the case I suggest to run executable under gdb and attach a stacktrace. This would help to figure out what's going on.

@lastlaugher
Copy link
Author

Hi yugr,

Thanks for your quick answer. First I tried the -DIMPLIB_EXPORT_SHIMS option but same thing. And there is no C++ constructor in the executable to call a function in libA. When I tried gdb, the libA is loaded in the _dl_init step even before starting main function. I've attached the call stack from gdb. Please let me know if you can find any clue from this info.

(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007fffef5a37f1 in __GI_abort () at abort.c:79
#2  0x00007fffef5933fa in __assert_fail_base (fmt=0x7fffef71a6c0 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n",
    assertion=assertion@entry=0x7fffe1153730 "0 && \"Assertion in generated code\"", file=file@entry=0x7fffe1153714 "libA.so.4.init.c",
    line=line@entry=61, function=function@entry=0x7fffe11eab40 <__PRETTY_FUNCTION__.3281> "load_library") at assert.c:92
#3  0x00007fffef593472 in __GI___assert_fail (assertion=0x7fffe1153730 "0 && \"Assertion in generated code\"",
    file=0x7fffe1153714 "libA.so.4.init.c", line=61, function=0x7fffe11eab40 <__PRETTY_FUNCTION__.3281> "load_library") at assert.c:101
#4  0x00007fffe1076fb6 in load_library () from ../../../../core/library/lib/libB.so
#5  0x00007fffe107700d in load_lib () from ../../../../core/library/lib/libB.so
#6  0x00007ffff7de38d3 in call_init (env=0x7fffffffe298, argv=0x7fffffffe288, argc=1, l=<optimized out>) at dl-init.c:72
#7  _dl_init (BFD: reopening /tmp/shared.ya8vsp: No such file or directory

BFD: reopening /tmp/shared.ya8vsp: No such file or directory

BFD: reopening /tmp/shared.ya8vsp: No such file or directory

main_map=0x7ffff7ffe170, main_map@entry=<error reading variable: Can't read data for section '.eh_frame' in file '/tmp/shared.ya8vsp'>,
    argc=1, argv=0x7fffffffe288, env=0x7fffffffe298) at dl-init.c:119

@yugr
Copy link
Owner

yugr commented Feb 13, 2023

Did you run implib-gen.py with --no-lazy-load? In that case generated code will try to load libA.so at program startup. Could you provide the

#define HAS_DLOPEN_CALLBACK ...
#define HAS_DLSYM_CALLBACK ...
#define NO_DLOPEN ...
#define LAZY_LOAD ...

snippet from your libA.so.init.c ?

@lastlaugher
Copy link
Author

OMG you're correct. While I was trying several options, I forgot to set the lazy_load option back. Now it works like a charm in my nested shared library case. Many thanks, yugr. You saved my life :)

@yugr
Copy link
Owner

yugr commented Feb 14, 2023

Closing for now, let me know if you have other issues.

@yugr yugr closed this as completed Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants