donet8 发表于 2012-05-18 15:20

为什么链接器本身是个ld.so文件? 那又由谁去把这个ld.so加载到进程空间呢?

今天在看<<C专家编程>>,里面P126显示一个调用了共享库的进程,内存映像的一个图。里面在高地址(堆栈的下方),是链接器。

我上网搜了一下,发现ld确实是一个动态库ld.so.

问题是,人不能提着头发把自己抓起来,对吧,那么ld.so怎么把自己加载到进程地址空间呢?
这个ld又是链接的工具,又是程序装载的工具,如何被调用的呢?

EricFisher 发表于 2012-05-18 15:20

以ELF格式的文件为例,动态可执行程序的程序头(program header)里有一个域PT_INTERP,其指定了解析器即动态链接器的路径和名字。

$ readelf -l a.out

Program Headers:
Type         Offset   VirtAddr   PhysAddr   FileSiz MemSizFlg Align
PHDR         0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4
INTERP         0x000134 0x08048134 0x08048134 0x00013 0x00013 R   0x1
      

在程序执行时,操作系统会先将解析器加载进来,创建映像,然后将控制转交给解析器,解析器再来加载可执行程序和动态库。

folklore 发表于 2012-05-20 19:09

this ld is not that one.

ld command: run at the compiling time. it is an application just as other one (you can call it linker yet), it link the parts of program up to a executable image. ( pick parts to one)

program loader/linker: it is a kernel component to load program, it "load" the program from disk image and "link" (relocating a disk image) to specified memory.
(the function of this "program" is mapping (*note mapping ,not linking*) a disk image to memory.

unfortunately, in chinese, we calling the both of above the same name "链接器", but they are actually not the same one.

janetliu9 发表于 2012-06-09 19:41

二楼解释的基本上是对的,不过可执行程序是由内核加载的,解释器只负责库的加载。对于静态链接的程序是没有加载解释器一说的,内核加载完后,会直接调到程序入口处执行。动态链接的,内核加载完后调到解释器的入口!
页: [1]
查看完整版本: 为什么链接器本身是个ld.so文件? 那又由谁去把这个ld.so加载到进程空间呢?