StaticBug
StaticBug

Reputation: 577

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).

Upvotes: 3

Views: 818

Answers (1)

cowdrool
cowdrool

Reputation: 314

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.

Upvotes: 3

Related Questions