当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Linker replacing symbolic references with memory addresses?

问题描述:

From http://blog.jamesdbloom.com/JVMInternals.html I found this

C/C++ code is typically compiled to an object file then multiple object files are linked together to product a usable artifact such as an executable or dll. During the linking phase symbolic references in each object file are replaced with an actual memory address relative to the final executable.

How is it possible to determine the relative Memory addresses during linking, if the OS running the executable is the one who is responsible for allocating Memory during Runtime and this Memory gets allocated whereever there is free Memory space (My understanding of how things work).

网友答案:

The memory addresses are relative to the final executable -- they are not absolute, that would be impossible to determine during linking.

When an executable is run in Windows, it is granted by the operating system a virtual memory space or virtual address space of fixed size, usually 4GB on 32-bit OSs. Each process has its own virtual space where it can read and write data to/from memory. Then, the executable and any dependencies are written to this space, so their location is now known and within the VAS. Now, upon execution, the operating system will take the relative addresses provided by the linker and translate them to absolute addresses. These are determined by the location of the executable in the virtual memory space.

分享给朋友:
您可能感兴趣的文章:
随机阅读: