ijuio
ijuio

Reputation: 91

Memory Addresses: Linker Vs. Loader

I am trying to understand how memory allocation works at the different stages of compilation and loading of a program.

1) Compilers and assemblers generate code and data sections that start at address 0.

2) The linker relocates these sections by associating a memory location with each symbol definition, and then modifying all of the references to those symbols so that they point to this memory location.

3) The loader loads the program to main memory, in the context of a process, and
hence it is at this step that paging and all memory management related operations are done.

My question is about two things:

1)How are the addresses assigned by the linker related to the ones assigned by the loader. can we call linker addresses virtual addresses?

2)Do all programs have the same virtual addresses(that are eventually mapped to different physical addresses?)

Upvotes: 1

Views: 1354

Answers (1)

user3344003
user3344003

Reputation: 21627

Generally compilers generate relocatable code that does not start at any particular address. There are cases where that is no entirely possible. E.g.

int x ;
int *y = &x ;

These need special handling.

The linker merges program sections referenced by the compiler. The output of the linked is a program that directs the loader how to place the program in memory. Those instructions will handle the case above.

The loader follow the instructions given by the linker.

1)How are the addresses assigned by the linker related to the ones assigned by the loader. can we call linker addresses virtual addresses?

The linker generally produces relocatable code, unless the compiler or assembly produced something that cannot be relocated. The linker does not produce virtual addresses.

2)Do all programs have the same virtual addresses(that are eventually mapped to different physical addresses?)

In most systems, each run of a program produces the same logical address layout. It is becoming more common for this not to be the case as a security measure. Each time the program gets loaded, it gets loaded differently.

Upvotes: 1

Related Questions