Dyanamic library linking by RTLD_LAZY
Hello everyone
I am trying to find the differance between RTLD_NOW and RTLD_LAZY flags. My query is why RTLD_LAZY loading the library whose function I've never referanced. I have created a dlrun.c file #include "stdio.h" #include "dlfcn.h" main() { void * ptr; void (*fptr)(void); printf("\nMy ID is- %d \n",getpid()); getchar(); ptr = dlopen("./fun5.so", RTLD_NOW); if(ptr==NULL) printf("failed to open fun5.so"); else { printf("I got fun5.so"); fptr= dlsym(ptr,"fun5"); getchar(); fptr(); printf("end of fun5"); dlclose(ptr); } } the next file fun5.c as #include "stdio.h" void fun2(void); void fun1(void); fun5() { printf("I am in fun5"); getchar(); fun1(); } fun() { getchar(); fun2(); } other file fun1.c is #include "stdio.h" fun1() { printf("I am in fun1"); getchar(); } and file fun2.c is #include "stdio.h" fun2() { printf("I am in fun2"); getchar(); } then I use commands gcc -c -fPIC -o fun1.o fun1.c gcc -c -fPIC -o fun2.o fun2.c gcc -c -fPIC -o fun5.o fun5.c gcc -shared -o fun1.so fun1.o gcc -shared -o fun2.so fun2.o gcc -shared -o fun5.so fun5.o ./fun1.so ./fun2.so gcc dlrun.c -o run -ldl ./run now I want to check which libraries are loaded by cd /proc/<pid> and vi maps Here it looks that before fun5.so calling no nonstansard libray is loaded and after calling fun5 function all fun5.so,fun1.so and fun.2.so are loaded as per expectation.Now if I replace RTLD_NOW by RTLD_LAZY then only fun5.so and fun1.so should be loaded because I have never called fun() and fun2() , but actually fun2.so is also loading.so where am I wrong? Am I wrong at creating fun5.so? if so then how should I create it? |
Your shared library fun5.so refers to fun2 - which is in fun2.so.
For dlopen to resolve the reference when loading fun5.so it must also load fun2.so. |
Thanks for your reply.
but as per the man page RTLD_LAZY Only resolve symbols as the code that references them is executed and I've never called fun() or fun2() to execute.In fun5.c I've only defined those function and never called. |
Quote:
Had you NOT called fun5, then neither library would have been loaded. Both get loaded because it is an explicit dependency between fun5.so and fun2.so. If the application is going to call fun5, then both libraries MUST be loaded. dlopen has no way to know ahead of time if fun2 is/is not going to be needed - and doesn't have any way to determine that. Now if fun5 used dlopen to load the fun2.so with a lazy open then it would only be loaded IF something fun5 did called a function in fun2.so. |
Thank you again as I am new in programing please guide me if I am wrong somewhere.
Now I've changed fun5 as fun5() { void * ptr = NULL; void (*fptr)(void); printf("I am in fun5"); getchar(); ptr=dlopen("./fun2.so",RTLD_LAZY); printf("fun2.so called to open"); getchar(); ptr=dlopen("./fun1.so",RTLD_LAZY); fptr=dlsym(ptr,"fun1"); fptr(); } and I created shared object by gcc -c -fPIC -o fun5.o fun5.c gcc -shared -o fun5.so fun5.o (In main function also I was using RTLD_LAZY flag) here I've not called function in fun2.so but still it is loading fun1.so, fun2.so and fun5.so. If I am wrong somewhere then please suggest how should I check differance between use of RTLD_NOW and RTLD_LAZY. |
Did you remove the external declarations of void fun2(void);
void fun1(void); from fun5.so? If so then I think that would have separated the loads, and broken the explicit binding, allowing them to loaded separately. At least, that is how http://tldp.org/HOWTO/Program-Librar...libraries.html appears to direct it. |
yes I had removed those declarations
|
All times are GMT -5. The time now is 04:31 PM. |