I have written a program using OPenSSL shared library and make another copy (Program2 is duplicate copy of program1) . Just to check whether the shared library function is loaded only once , I print the address from both Program1 and program2. To my surprise I got same address sometimes, but on other time I got different address .
As per my understanding , shared library functions loaded into shared memory and only one copy is loaded. Why I am getting two different address which signifies that shared functions are loaded into two different memory location.
Following code I am using when getting same address
CASE 1: Program 1:
#include <openssl/aes.h>
int main(int argc, char ** argv)
{
printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);
while(1) {}
......
}
Address of AES_cbc_encrypt =0x400600
Program 2:
#include <openssl/aes.h>
int main(int argc, char ** argv)
{
printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);
while(1) {}
......
}
Address of AES_cbc_encrypt =0x400600
CASE2 : When I am getting two different address for shared library function
Program 1:
#include <openssl/aes.h>
int main(int argc, char ** argv)
{
printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);
while(1) {}
......
}
Address of AES_cbc_encrypt =0x400840
Program 2:
#include <openssl/aes.h>
int main(int argc, char ** argv)
{
printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);
// open function makes the difference
fd = open(argv[1], O_RDONLY);
if (fd <= 0)
{
strerror(errno);
exit(0);
}
while(1) {}
......
}
Address of AES_cbc_encrypt =0x4007f0
In case 2 , I am not getting same address for shared library function . What is the reason ? How Can I make sure both shared library function loaded into same memory location.
I am using GCC under Fedora 19.
EDIT 1 :
As per both comment/answer of Paul and John, the above addresses are not Physical address, they are Virtual addresses. So I try to convert the virtual address to physical address ( I use the code of this tool Capturing Process Memory Usage Under Linux) to check whether they are same , but I am getting two different physical addresses . Am I doing something wrong ? Is there some other way to make it forcefully load the shared library into same physical memory.
Thanks in advance.