共享库函数未加载到相同的内存位置

时间:2022-07-01 06:59:58

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 .

我已经使用OPenSSL共享库编写了一个程序并制作了另一个副本(Program2是program1的副本)。只是为了检查共享库函数是否只加载一次,我从Program1和program2打印地址。令我惊讶的是,我有时会得到相同的地址,但在其他时间我得到了不同的地址。

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:

案例1:计划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

AES_cbc_encrypt的地址= 0x400600

Program 2:

计划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

AES_cbc_encrypt的地址= 0x400600

CASE2 : When I am getting two different address for shared library function

CASE2:当我为共享库函数获取两个不同的地址时

Program 1:

计划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

AES_cbc_encrypt的地址= 0x400840

Program 2:

计划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

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.

在案例2中,我没有获得共享库函数的相同地址。是什么原因 ?如何确保将共享库函数加载到相同的内存位置。

I am using GCC under Fedora 19.

我在Fedora 19下使用GCC。

EDIT 1 :

编辑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.

根据Paul和John的评论/回答,上述地址不是物理地址,它们是虚拟地址。所以我尝试将虚拟地址转换为物理地址(我使用此工具的代码捕获Linux下的进程内存使用情况)来检查它们是否相同,但我得到两个不同的物理地址。难道我做错了什么 ?是否有其他方法可以强制将共享库加载到相同的物理内存中。

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

Your operating system takes care of this for you, and you do not need to worry about it. However, if you are still worried about it, consult the "SHR" (Shared) column in top to see how much memory is being shared between processes.

您的操作系统会为您完成此操作,您无需担心。但是,如果您仍然担心它,请查看顶部的“SHR”(共享)列以查看进程之间共享的内存量。

Also consider that "virtual memory" (do a web search) means that just because two different processes are using two different-valued pointers to refer to something, does not mean that the operating system can't just map them to the same backing storage behind the scenes. In fact that will be typically what happens: the OS will load the shared library once, and map it in two different base addresses in two processes, and there is no functional difference to you, so long as you don't actually care about these virtual address values (which you shouldn't).

还要考虑“虚拟内存”(进行网络搜索)意味着仅仅因为两个不同的进程使用两个不同值的指针来引用某些内容,并不意味着操作系统不能仅将它们映射到相同的后备存储在幕后。事实上,通常会发生这样的情况:操作系统将加载共享库一次,并在两个进程中将它映射到两个不同的基址中,并且没有功能上的区别,只要您实际上并不关心这些虚拟地址值(你不应该)。

#1


1  

Your operating system takes care of this for you, and you do not need to worry about it. However, if you are still worried about it, consult the "SHR" (Shared) column in top to see how much memory is being shared between processes.

您的操作系统会为您完成此操作,您无需担心。但是,如果您仍然担心它,请查看顶部的“SHR”(共享)列以查看进程之间共享的内存量。

Also consider that "virtual memory" (do a web search) means that just because two different processes are using two different-valued pointers to refer to something, does not mean that the operating system can't just map them to the same backing storage behind the scenes. In fact that will be typically what happens: the OS will load the shared library once, and map it in two different base addresses in two processes, and there is no functional difference to you, so long as you don't actually care about these virtual address values (which you shouldn't).

还要考虑“虚拟内存”(进行网络搜索)意味着仅仅因为两个不同的进程使用两个不同值的指针来引用某些内容,并不意味着操作系统不能仅将它们映射到相同的后备存储在幕后。事实上,通常会发生这样的情况:操作系统将加载共享库一次,并在两个进程中将它映射到两个不同的基址中,并且没有功能上的区别,只要您实际上并不关心这些虚拟地址值(你不应该)。