How Linkers Relocate Symbol References

Once the linker has completed the symbol resolution step, it has associated each symbol reference in the code with exactly one symbol definition. At this point, the linker knows the exact sizes of the code and data sections in its input object modules. It is now ready to begin the relocation step, where it merges the input modules and assigns run-time addresses to each symbol.

      The pseudo code for the linker's relocation algorithm is presented below:

foreach section s {
    foreach relocation entry r {
        refptr = s + r.offset; // ptr to reference to be relocated

        // Relocate a PC-relative reference
        if (r.type == R_386_PC32) {
            refaddr = ADDR(s) + r.offset; // ref’s runtime address
            *refptr = (unsigned) (ADDR(r.symbol) + *refptr - refaddr);
        }

        // Relocate an absolute reference 
        if (r.type == R_386_32)
            *refptr = (unsigned) (ADDR(r.symbol) + *refptr);
    }
}

Lines 1 and 2 iterate over each sections and each relocation entry r associated with each section. Assume that when the algorithm runs, the linker has already chosen run-time addresses for each section (denoted ADDR(s)) and each symbol (denoted ADDR(r.symbol)). Line 3 computes the address in thes array of the 4-byte reference that needs to be relocated. If this reference uses PC-relative addressing, then it is relocated by lines 5-9. If the reference uses absolute addressing, then it is relocated by lines 11-13.

Source:

Randal E. Bryant, David R. O'Hallaron(2011). COMPUTER SYSTEMS A Programmer's Perspective (Second Edition).Beijing: China Machine Press.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linkers and Loaders是指链接器和装载器,是计算机领域中的一种重要工具,主要用于将程序的各个部分合并在一起并最终加载到内存中运行。 链接器的主要工作是将多个目标文件合并成一个可执行文件。目标文件包含机器语言指令和符号表信息,链接器将各个目标文件的符号表信息进行合并,并解决模块之间的引用问题,使得程序能正确地执行。 装载器的主要工作是将可执行文件加载到计算机的内存中运行。装载器将可执行文件中的各个段按照一定的地址空间进行装载,并进行重定位等操作,确保程序能够在内存中正确执行。 总之,链接器和装载器是构建可执行程序需要用到的重要工具,对于程序的可靠性、效率和安全性都具有重要作用。 ### 回答2: linkers and loaders是计算机科学中用于程序编译的工具。Linkers在编译过程中将多个目标文件合并成一个可执行文件,同时也会解决外部和内部符号的引用问题。Loaders则是将可执行文件加载到内存中,并把符号解析成运行时地址,最终使程序在计算机上运行。 linkers在编译过程中实现了模块化编程,将单独编写的多个目标文件链接在一起。linkers将程序中各个部分之间的符号和引用关系整合在一起,实现一个完整的可执行程序。linkers主要有静态链接和动态链接两种形式,静态链接将库文件直接合并到可执行文件中,而动态链接则是在程序运行时动态加载库文件。 loaders则负责将可执行文件从磁盘加载到内存中去。借助加载器,操作系统可以根据可执行文件中文件头和段表信息将程序映射到内存中的相应地址,方便操作系统进行命令的调用和运行。 总之,linkers和loaders是编译过程中非常重要的组成部分。它们不仅使编译工作更加高效和灵活,同时也是计算机开发中程序正确性和性能的关键。 ### 回答3: linker是编译后的程序中用来处理符号链接的工具。它的作用是将不同程序模块(source file, object file)之间的符号联系起来。在链接过程中,linker会分析程序中使用到的符号,并将这些符号与其对应的地址进行关联,以便程序中的不同模块能够相互调用。同时,linker也会将所有的模块组合成一个可执行文件,方便程序的执行。 loader是将程序加载入内存并实现分页与映射的软件。loader的主要作用是将程序文件从磁盘上读入内存,并根据程序的逻辑要求,将其按照合适的方式映射到内存中的合适位置。同时,loader还负责分配与管理程序所需要的内存空间,维护程序在内存中的状态,以及实现程序的执行。 在计算机科学领域中,linkers和loaders是非常重要的工具。它们帮助编程人员将不同的程序模块链接起来,并在不同的系统中进行适当的加载。对于任何一个开发者而言,了解和掌握linkers和loaders都是非常重要的基本技能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值