-   Programming (
-   -   Dynamically linked libraries.. (

vishalbutte 04-16-2006 03:42 PM

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!!

futhark 04-16-2006 03:46 PM

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.

vishalbutte 04-16-2006 03:51 PM

thanks for the reply..
I want the function in the kernel code which loads the dynamically linked libraries..?

Mara 04-16-2006 04:40 PM

The kernel doesn't load libraries (exception: kernel modules). What you probably mean is dlopen or its internals. Is it correct?

vishalbutte 04-17-2006 08:26 AM

yes you got the point..

If kernel comes to know that the currently executing executable requires some dynamically linked library the what it does?

aluser 04-17-2006 09:40 PM

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/ 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: , but I haven't pinned down where yet.

The reason I know that the interpreter is /lib/ is this:

$ 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

(0x114 is 276 and 0x13 is 19) You can see load_elf_binary() reading this string from the file analogously to my dd command:

                if (elf_ppnt->p_type == PT_INTERP) {
/* skip a bit */
                        retval = kernel_read(bprm->file, elf_ppnt->p_offset,

If you strace the executable (mine is just 'hello world'), you'll see the loader mmapping shared libraries:

$ strace ./hw
execve("./hw", ["./hw"], [/* 40 vars */]) = 0
/* skip a bit... */
open("/lib/tls/", 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...

aluser 04-17-2006 09:54 PM

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

All times are GMT -5. The time now is 04:32 PM.