LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2014, 02:48 PM   #1
Nilesh Tekale
LQ Newbie
 
Registered: Mar 2014
Location: India
Posts: 4

Rep: Reputation: Disabled
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?
 
Old 03-22-2014, 10:45 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
Old 03-22-2014, 11:49 PM   #3
Nilesh Tekale
LQ Newbie
 
Registered: Mar 2014
Location: India
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-23-2014, 12:12 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by Nilesh Tekale View Post
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.
You did call the function fun5 in fun5.so. And that required it to be loaded - and in turn that required fun2.so.

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.
 
1 members found this post helpful.
Old 03-23-2014, 12:52 AM   #5
Nilesh Tekale
LQ Newbie
 
Registered: Mar 2014
Location: India
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Nilesh Tekale; 03-23-2014 at 12:59 AM.
 
Old 03-23-2014, 06:44 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.

Last edited by jpollard; 03-23-2014 at 06:45 AM.
 
Old 03-23-2014, 06:56 AM   #7
Nilesh Tekale
LQ Newbie
 
Registered: Mar 2014
Location: India
Posts: 4

Original Poster
Rep: Reputation: Disabled
yes I had removed those declarations
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linking static library INTO dynamic library miguelg Programming 6 12-10-2012 09:56 AM
linking library in kernel - what might be the calls library should not be using lakshmi00786 Linux - Kernel 2 07-25-2009 01:30 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
linking own library bobby2k3 Programming 2 10-20-2003 10:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration