This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
What does ld.so do that dlopen don't do when loading shared libraries
- From: Celelibi <celelibi at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Sun, 28 Apr 2013 23:08:26 +0200
- Subject: What does ld.so do that dlopen don't do when loading shared libraries
Hello there,
This is my first message to this mailing list. ^^
And I have a question that may be quite unusual: What does ld.so do
that dlopen don't do when loading shared libraries?
A bit of explanation may be needed. There is a profiler named TAU
<tau.uoregon.edu> which can instrument files it compiles and then
generate a file profile.0.0.0 when the program is run. Much like
compiling with gcc -pg.
And I can do things like: compile a shared library with tau_cc (which
instrument the shared object) and then link it with a simple program.
tau_cc.sh -shared -fPIC -o foo.so foo.c
gcc -o sep sep.c foo.so
This works fine, running ./sep produce the profile trace. (That
wouldn't work with gprof.)
However, if I dlopen/dlsym the library instead of linking against it,
it doesn't produce any file.
tau_cc.sh -shared -fPIC -o foo.so foo.c
gcc -o dyn dyn.c
Therefore I guess ld.so does something special when it loads the
needed library that dlopen doesn't do. I think about ctor/entrypoint
execution, but I have no clue to find out.
I join foo.c sep.c and dyn.c as an example.
I can also provide the generated binaries if requested.
PS: I really can't link against the library since the whole
application is designed as components. All the main program does is
loading and running .so following a given xml file.
Best regards,
Celelibi
#include <stdio.h>
/* Just dummy computation and printf */
void foo(void) {
char charlist[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int i, j;
int state = 4000;
char str[100];
for (i = 0; i < 99; i++) {
for (j = 0; j < i*100000; j++)
state = state * 65537 + 4001;
str[i] = charlist[state % (sizeof(charlist) - 1)];
}
str[99] = '\0';
printf("%s\n", str);
}
void foo(void);
int main(void) {
foo();
return 0;
}
#include <dlfcn.h>
void foo(void);
int main(void) {
void *lib = dlopen("./foo.so", RTLD_LAZY);
void (*fun)(void);
if (!lib)
return 1;
*(void **)&fun = dlsym(lib, "foo");
if (!fun)
return 2;
fun();
if (dlclose(lib))
return 3;
return 0;
}