共享库如何由不同进程共享? [英] How the share library be shared by different processes?

查看:142
本文介绍了共享库如何由不同进程共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些共享带有-fPIC参数的库的文档, .so的.text序列将在流程派生的动态链接阶段共享 (例如,该过程会将.so映射到相同的物理地址)

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)

我对谁(内核或ld.so)以及如何实现此目标感兴趣? 也许我应该跟踪代码,但是我不知道从哪里开始.

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.

尽管如此,我还是尝试验证该声明.
我决定检查libc.print中的函数地址,例如printf,以便所有c程序都可以链接. 我获得了进程的printf虚拟地址,并且需要获得物理地址.尝试编写内核模块并将地址值传递给内核,然后调用virt_to_phys.但这不起作用,因为virt_to_phys仅适用于kmalloc地址.

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?

提前谢谢!

推荐答案

动态加载程序使用mmap(2)具有MAP_PRIVATE和适当的权限.您可以通过运行strace -e file,mmap中的命令来查看其确切功能.例如:

The dynamic loader uses mmap(2) with MAP_PRIVATE and appropriate permissions. You can see what it does exactly by running a command from strace -e file,mmap. For instance:

strace -e file,mmap ls 

所有魔力都来自mmap(2). mmap(2)在调用过程中创建映射,它们通常由文件或交换支持(匿名映射).在文件支持的映射中,MAP_PRIVATE表示写入内存不会更新文件,并导致从该点开始通过交换(写时复制)来支持该页面.

All the magic comes from mmap(2). mmap(2) creates mappings in the calling process, they are usually backed either by a file or by swap (anonymous mappings). In a file-backed mapping, MAP_PRIVATE means that writes to the memory don't update the file, and cause that page to be backed by swap from that point on (copy-on-write).

动态加载程序从ELF的程序标头中获取所需的信息,您可以使用以下命令查看该信息:

The dynamic loader gets the info it needs from ELF's program headers, which you can view with:

readelf -l libfoo.so

根据这些信息,动态加载程序确定将什么映射为代码,只读数据,数据和bss(零填充段,文件中大小为零,内存中大小为非零,并且名称仅通过Lisp的汽车和cdr).

From these, the dynamic loader determines what to map as code, read-only data, data and bss (zero-filled segment with zero size in file, non-zero size in memory, and a name only matched in crypticness by Lisp's car and cdr).

因此,实际上,代码以及数据都是共享的,直到写操作导致写时复制.这就是为什么将常量数据标记为常量是潜在的重要空间优化方法(请参见 DSO howto )

So, in fact, code and also data is shared, until a write causes copy-on-write. That is why marking constant data as constant is a potentially important space optimization (see DSO howto).

您可以在mmap(2)联机帮助页以及 Documentation/中获取更多信息. nommu-mmap.txt (在MMU中,no-MMU用于嵌入式设备,例如ADSL路由器和Nintendo DS).

You can get more info on the mmap(2) manpage, and in Documentation/nommu-mmap.txt (the MMU case, no-MMU is for embedded devices, like ADSL routers and the Nintendo DS).

这篇关于共享库如何由不同进程共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆