编译时的加载地址是在RAM中复制可执行文件的地方吗? [英] Is the load address at compile time the place to copy the executable in RAM?

查看:88
本文介绍了编译时的加载地址是在RAM中复制可执行文件的地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写自己的加载器.在此程序中,我将程序复制到RAM中的特定地址,并通过读取elf的入口点跳到入口点地址.但是,我无法理解加载程序的地址是什么?这是否意味着二进制文件仅需要在编译时指定的加载程序地址处复制.我不能在编译时指定的加载地址之外的其他位置加载程序吗?我的基本问题是:我不能将程序加载到其他位置,而不是在编译时指定的加载地址吗?"

I am trying to write my own loader. In this program, I will copy the program to the specific address in RAM, and will jump on to the entry point address by reading the entry point of elf. But, I am not able to understand what is the loader address? Does it mean that the binary needs to be copied only at the loader address specified at compile time. can't I load the program in some other location rather than the load address specified during compile time? My basic question is the following, "can't I load the program in some other location rather than the load address specified during compile time?"

推荐答案

您是否有机会瞄准ARM?

Are you targetting ARM, by any chance?

解决问题的方法是PIC/PIE(位置独立代码/可执行文件).这样,程序将编译如下:

The solution to your problem is PIC/PIE (Position Independent Code/Executable). This way, the program is compiled as follows:

  • 任何功能上的跳转都是相对的(诸如if,case,loop等之类的东西)
  • 使用GOT(全局偏移表),可通过两步间接地进行任何截面内跳转
  • 节间跳转是相对跳转还是间接跳转,具体取决于链接器参数"long-jumps"
  • 使用GOT间接进行任何全局数据访问

这样,程序在代码中的任何地方都不包含绝对地址.它在代码中也没有重定位.唯一需要的重定位是GOT本身的位置.

This way, the program contains no absolute addresses anywhere in the code. It also has no relocations in the code. The only relocation needed is the placement of the GOT itself.

GOT是一个表,其中包含所有全局对象,函数和数据的地址.在非PIC代码中,数据的绝对地址直接嵌入在代码内部.该功能需要知道GOT表的地址和GOT表中与特定对象相对应的条目号.对象编号是PIC代码中唯一嵌入的内容.通过我遇到的两种方法之一将GOT地址呈现给函数:在函数体末尾或预定义的CPU寄存器中附加的重定位.我发现后一种选择更好.

GOT is a table that contains addresses of all global objects, functions and data. In a non-PIC code, the absolute addresses of data are embedded directly inside the code. The function needs to know the GOT table address and the entry number in the GOT table corresponding to a particular object. The object number is the only thing embedded in a PIC code. The GOT address is presented to a function by one of the 2 ways I've encountered: a relocation appended at the end of the function body or in a predefined CPU register. I find the latter option better.

如果在最初加载程序的内存映射ROM中,并且想要将其复制到RAM中以加快处理速度并允许修改数据,则PIC非常有用.

PICs are useful if you have memory-mapped ROM where your program is initially loaded and you want to copy it to the RAM to make things faster and to allow modification of data.

请注意,您可以在没有PIC的情况下完成所有上述操作,这更加麻烦.您将在代码内进行很多重定位(如果对PIC进行功能代码的移动只是一个简单的memcpy问题),并且您必须一次复制所有数据块(正确编写的PIC可执行文件允许将各个对象移动到不同的位置).内存区域).

Note, that you can do all of the above without PIC, it's just much more hassle. You will have a lot of relocations inside the code (in case of PIC moving of the functions code is a matter of a simple memcpy) and you have to copy all data blocks at once (correctly written PIC executables allow moving of individual objects in different regions of memory).

如果您感到好奇,那么我可以向您发送一些用于ARM体系结构的链接器脚本.

If you are curious, then I can send you some of my linker scripts for ARM architecture.

这篇关于编译时的加载地址是在RAM中复制可执行文件的地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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