views:

27

answers:

2

I read some documents that share library comiled with -fPIC argument, the .text seqment of the .so will be shared at process fork's dynamic linking stage (eq. the process will map the .so to the same physical address)

i am interested in who (the kernel or ld.so ) and how to accomplish this? maybe i should trace the code, but i dont know where to start it.

Nevertheless, i try to verify the statement.
I decide to check the function address like printf which is in the libc.so that all c program will link. I get the printf virtual address of the process and need to get the physical address. Tried to write a kernel module and pass the address value to kernel, then call virt_to_phys. But it did not work cause the virt_to_phys only works for kmalloc address.

So, process page table look-at might be the solution to find the virtual address map to physical address. Were there any ways to do page table look-at? Or othere ways can fit the verify experiment?

thanks in advance!

+1  A: 

Shared libraries just a particular use of mapped files.

The address which a file is mapped at in a process's address space has nothing to do with whether it is shared or not.

Pages can be shared even if they are mapped at different addresses.

To find out if pages are being shared, do the following:

  1. Find the address that the file(s) are mapped at by examining /proc/pid/maps
  2. There is a tool which extracts data from /proc/pid/pagemap - find it and use it. This gives you info as to exactly which page(s) of a mapping are present and what physical location they are at

If two processes have a page mapped in at the same physical address, it is of course, shared.

MarkR
Thanks a lot! it's very useful!And, are there any guides or documents to explain how the kernel let the share pages to be shared? Will the kernel map the share libraries page to the same physical address.
The dynamic loader, ld.so decides which physical address to map libraries at, at load-time, so you can look at its source (in gnu C library I think). Usually a given library always gets loaded at the same address, yes, particularly common libraries such as libc.
MarkR