LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2006, 02:42 PM   #1
vishalbutte
Member
 
Registered: Dec 2005
Posts: 80

Rep: Reputation: 15
Question Dynamically linked libraries..


Hey anyone knows which function loads the dynamically linked libraries required by executables?
Or is there really such function in linux kernel?
I want to k @ runtime which shared library is required by the executable..?

your replies are always welcome!!
 
Old 04-16-2006, 02:46 PM   #2
futhark
Member
 
Registered: Nov 2003
Location: Montréal (Can)
Distribution: FC4
Posts: 110

Rep: Reputation: 15
If you want to know what shared libraries a program uses, try "ldd".

If you wonder what C functions do that, have a look at "man dlopen". Can't help you with those though.
 
Old 04-16-2006, 02:51 PM   #3
vishalbutte
Member
 
Registered: Dec 2005
Posts: 80

Original Poster
Rep: Reputation: 15
thanks for the reply..
I want the function in the kernel code which loads the dynamically linked libraries..?
 
Old 04-16-2006, 03:40 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The kernel doesn't load libraries (exception: kernel modules). What you probably mean is dlopen or its internals. Is it correct?
 
Old 04-17-2006, 07:26 AM   #5
vishalbutte
Member
 
Registered: Dec 2005
Posts: 80

Original Poster
Rep: Reputation: 15
yes you got the point..

If kernel comes to know that the currently executing executable requires some dynamically linked library the what it does?
 
Old 04-17-2006, 08:40 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I did a fair amount of grepping and munging around to get a partial answer to this question. It seems part of the answer is in fs/binfmt_elf.c; load_elf_binary() calls load_elf_interp(), which in turn sets up /lib/ld-linux.so.2 to do the actual work. I suspect that the code for ld-linux is in glibc-2.4.tar.gz which you can get here: http://directory.fsf.org/GNU/glibc.html , but I haven't pinned down where yet.

The reason I know that the interpreter is /lib/ld-linux.so.2 is this:
Code:
$ objdump -p hw | grep -A1 INTERP
  INTERP off    0x00000114 vaddr 0x08048114 paddr 0x08048114 align 2**0
         filesz 0x00000013 memsz 0x00000013 flags r--
$ dd if=hw bs=1 count=19 skip=276 2>/dev/null; echo
/lib/ld-linux.so.2
(0x114 is 276 and 0x13 is 19) You can see load_elf_binary() reading this string from the file analogously to my dd command:
Code:
		if (elf_ppnt->p_type == PT_INTERP) {
/* skip a bit */
			retval = kernel_read(bprm->file, elf_ppnt->p_offset,
					   elf_interpreter,
					   elf_ppnt->p_filesz);
If you strace the executable (mine is just 'hello world'), you'll see the loader mmapping shared libraries:
Code:
$ strace ./hw
execve("./hw", ["./hw"], [/* 40 vars */]) = 0
/* skip a bit... */
open("/lib/tls/libc.so.6", O_RDONLY)    = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320O\1"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1266800, ...}) = 0
old_mmap(NULL, 1272764, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e6a000
/* more... */
So ultimately, the code to load a shared library is just an mmap(), after read()ing the elf header from the library. See 'man 5 elf' for the elf format.

I would feel a lot more confident of everything I just wrote if I had disected ld-linux. I haven't, so take it all with several grains of salt...
 
Old 04-17-2006, 08:54 PM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
of course all that assumes that you're concerned with normal shared libraries which are loaded by the loader at runtime but before your program starts executing. If you mean to load a library later, use dlopen() or such
 
  


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
Dynamically linking or not? Mben Programming 3 04-14-2006 03:30 PM
dynamically phoenix_fei Programming 5 01-17-2005 07:51 PM
How to see which shared libraries are linked to a program zsjoska Linux - Newbie 2 11-19-2004 01:45 PM
Linux Dynamicly Linked Libraries sikil_nuru Programming 3 03-16-2004 07:51 AM
Winxp linked to Linux Linked to home network OverboardKiller Linux - Networking 2 06-09-2003 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 AM.

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