kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* How a program could generate the memory addresses for its variables, when it is about to run?
@ 2011-05-27  6:44 sandeep kumar
  2011-05-27  6:56 ` Rajat Sharma
  2011-05-27 17:21 ` Mulyadi Santosa
  0 siblings, 2 replies; 4+ messages in thread
From: sandeep kumar @ 2011-05-27  6:44 UTC (permalink / raw)
  To: kernelnewbies

hi all,
I am new to the linux kernel internals. I know there is a memory management
subsystem which handles all the memory related things.

But Now i want to know a bit deeper how things work.

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?*
**
Can please somebody give pointers how to learn this kind of things like,
"in the early stages (when our program is about to be executed..about to
become a process) what are the things that will be done by the kernel?"

Please help me in this regard,

Thanking you,
Sandeep Kumar A.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110527/4f9f1817/attachment.html 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* How a program could generate the memory addresses for its variables, when it is about to run?
  2011-05-27  6:44 How a program could generate the memory addresses for its variables, when it is about to run? sandeep kumar
@ 2011-05-27  6:56 ` Rajat Sharma
  2011-05-27  7:11   ` Rahul Ramasubramanian
  2011-05-27 17:21 ` Mulyadi Santosa
  1 sibling, 1 reply; 4+ messages in thread
From: Rajat Sharma @ 2011-05-27  6:56 UTC (permalink / raw)
  To: kernelnewbies

Hi Sandeep,

probably you want to look at how your program is loaded in memory. For
example an ELF binary is understood by ELF format handler inside
kernel. Format handler supply their load_binary methods to load a
program image im memory and initial its different virtual memory areas
(stack, heap etc.). exec system call searches for correct format
handler for you based on file header.

Please go thoroghly through Understanding the Linux Kernel, 3rd
Edition, Chapter 20. Program ExZecution.

-Rajat

On Fri, May 27, 2011 at 12:14 PM, sandeep kumar
<coolsandyforyou@gmail.com> wrote:
> hi all,
> I am new to the linux kernel internals. I know there is a memory management
> subsystem which handles all the memory related things.
>
> But Now i want to know a bit deeper how things work.
>
> 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?
>
> Can please somebody give pointers how to learn this kind of things like,
> "in the early stages (when our program is about to be executed..about to
> become a process) what are the things that will be done by the kernel?"
>
> Please help me in this regard,
>
> Thanking you,
> Sandeep Kumar A.
>
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* How a program could generate the memory addresses for its variables, when it is about to run?
  2011-05-27  6:56 ` Rajat Sharma
@ 2011-05-27  7:11   ` Rahul Ramasubramanian
  0 siblings, 0 replies; 4+ messages in thread
From: Rahul Ramasubramanian @ 2011-05-27  7:11 UTC (permalink / raw)
  To: kernelnewbies

hi
this may help
http://www.akae.cn/study/ebook/computerscience/Linkers%20and%20Loaders.pdf

On Fri, May 27, 2011 at 2:56 AM, Rajat Sharma <fs.rajat@gmail.com> wrote:

> Hi Sandeep,
>
> probably you want to look at how your program is loaded in memory. For
> example an ELF binary is understood by ELF format handler inside
> kernel. Format handler supply their load_binary methods to load a
> program image im memory and initial its different virtual memory areas
> (stack, heap etc.). exec system call searches for correct format
> handler for you based on file header.
>
> Please go thoroghly through Understanding the Linux Kernel, 3rd
> Edition, Chapter 20. Program ExZecution.
>
> -Rajat
>
> On Fri, May 27, 2011 at 12:14 PM, sandeep kumar
> <coolsandyforyou@gmail.com> wrote:
> > hi all,
> > I am new to the linux kernel internals. I know there is a memory
> management
> > subsystem which handles all the memory related things.
> >
> > But Now i want to know a bit deeper how things work.
> >
> > 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?
> >
> > Can please somebody give pointers how to learn this kind of things like,
> > "in the early stages (when our program is about to be executed..about to
> > become a process) what are the things that will be done by the kernel?"
> >
> > Please help me in this regard,
> >
> > Thanking you,
> > Sandeep Kumar A.
> >
> >
> >
> > _______________________________________________
> > Kernelnewbies mailing list
> > Kernelnewbies at kernelnewbies.org
> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> >
> >
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110527/b2308d8b/attachment.html 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* How a program could generate the memory addresses for its variables, when it is about to run?
  2011-05-27  6:44 How a program could generate the memory addresses for its variables, when it is about to run? sandeep kumar
  2011-05-27  6:56 ` Rajat Sharma
@ 2011-05-27 17:21 ` Mulyadi Santosa
  1 sibling, 0 replies; 4+ messages in thread
From: Mulyadi Santosa @ 2011-05-27 17:21 UTC (permalink / raw)
  To: kernelnewbies

Hi...

On Fri, May 27, 2011 at 13:44, sandeep kumar <coolsandyforyou@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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-05-27 17:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-27  6:44 How a program could generate the memory addresses for its variables, when it is about to run? sandeep kumar
2011-05-27  6:56 ` Rajat Sharma
2011-05-27  7:11   ` Rahul Ramasubramanian
2011-05-27 17:21 ` Mulyadi Santosa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).