How a program could generate the memory addresses for its variables, when it is about to run?

Mulyadi Santosa mulyadi.santosa at gmail.com
Fri May 27 13:21:36 EDT 2011


Hi...

On Fri, May 27, 2011 at 13:44, sandeep kumar <coolsandyforyou at gmail.com> wrote:
>
> I want to start with the following question,
> How a program could generate the memory addresses for its variables, when it
> is about to run?

first, linker will determine in which section and which offset (from
the start of the program) the variables will be positioned. If it is a
dynamic variable (one that ends up in stack), it will determined
dynamically later according to stack position.

later, loader will reposition those variables based on real memory
address positioning. This is based on the information placed inside
the binary (ELF header). Usually, it is followed strictly, but if it
is PIE (Position Independent Executable), further flexible reposition
is allowed based on certain policy.

those address (for static variables) are then substituted into GOT
(Global Offset Table), some kind of lookup table to determined address
of variables (and function address too actually).

for dynamic variable, usually simply using compiler generated code, it
is sufficient to reference them. This is because they are referenced
using relative offset against base address of frame (which is usually
saved in esp or ebp in x86 arch)

I hope I correctly describe it....

-- 
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com



More information about the Kernelnewbies mailing list