What does the concept of relocation mean? [closed]

2020-02-10 03:04发布

I'm new to assembly and hence want to understand more about the concept of relocation.

  1. Why exactly do we need to relocate programs, data?
  2. How exactly is it done?

It would be great if someone could explain with a beginner level example.

1条回答
仙女界的扛把子
2楼-- · 2020-02-10 03:55

The assembler has one source file to look at. It has to build a memory image based on that one source file. So it starts with 0 for the code, and also for 0 with the data. The results go into an object file.

Now, the linker comes along and needs to combine many object files into an executable (or kernel image, or whatever). It can't leave everyone at 0, they would overlap. So it must relocate the addresses to assign each object file's worth of data to a unique range.

Then, then the image is loaded into virtual memory, the operating system may have good reasons to want to use an address other than '0' as the base virtual address. So, at runtime, it wants to relocate everything again.

Finally, if there are shared libraries involved, neither the assembler nor the linker knows where they will be in the running environment, and so the references to them must be relocated.

查看更多
登录 后发表回答