Where in memory is shared library loaded?
i guess it should be in the process virtual address space only but if
that is the case how another process which wants to use the same library
comes to know that the library has already been loaded and how is it
able to use that?
Read the following link:
http://www.tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
But, to help you out a little, check this out:
$ cat x.c
#include <stdio.h>
main()
{
printf("hit any key...");
getchar();
}
$ gcc x.c -ox
$ ./x
$ readelf -a x | grep getchar
0804960c 00000107 R_386_JUMP_SLOT 00000000 getchar
1: 00000000 221 FUNC GLOBAL DEFAULT UND getchar@xxxxxxxxx (2)
82: 00000000 221 FUNC GLOBAL DEFAULT UND getchar@@GLIBC_2.0
$ readelf -a x | grep printf
08049614 00000307 R_386_JUMP_SLOT 00000000 printf
3: 00000000 57 FUNC GLOBAL DEFAULT UND printf@xxxxxxxxx (2)
97: 00000000 57 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.0
The addresses of printf and getchar are undefined and will be resolved at run time.
$readelf -a ./x
[snip]
0x00000001 (NEEDED) Shared library: [libc.so.6]
[snip]
With this hint, ld figures out it needs to load the shared library. Let us run program ./x in one terminal and check its process map from another.
hareesh@salty:~$ pmap `pgrep ./x`
11680: ./x
08048000 4K r-x-- /x
08049000 4K rw--- /x
40000000 88K r-x-- /ld-2.3.2.so <--- Loader
40016000 4K rw--- /ld-2.3.2.so
40017000 12K rw--- [ anon ]
40022000 1192K r-x-- /libc-2.3.2.so <--- glibc (code section; perhaps)
4014c000 36K rw--- /libc-2.3.2.so <--- glibc (data section; perhaps)
40155000 12K rw--- [ anon ]
bfffe000 8K rw--- [ stack ]
ffffe000 4K ----- [ anon ]
total 1364K
c u
./hareesh