Are variables put in RAM?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
CocaCola
Member
Member
Posts: 36
Joined: Fri Sep 07, 2012 9:11 am

Are variables put in RAM?

Post by CocaCola »

Hello everybody.

I know about the "address space" and how not every address belongs to RAM.
I also know that GRUB can prepare a memory map to show what is RAM, and what isn't.

My question is: all the global variables, and the local variables, and the constants, will they be stored in RAM?
And if so, will following GRUB's memory map make me overwrite them?
Then how can I discern which area is safe to use and which isn't?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Are variables put in RAM?

Post by gravaera »

Yo:

Your kernel executable image is "built" by a series of programs which assemble it into a file of organized data and code. A bootloader loads this file into memory and passes execution control to it. The file has information on your "variables" and their offsets within the kernel image.

For more information, consider writing some basic programs and analysing them using programs like objdump. You might also want to consider other hobbies, depending on how much time you really have to spend in this field :)

--Peace out,
gravaera
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
CocaCola
Member
Member
Posts: 36
Joined: Fri Sep 07, 2012 9:11 am

Re: Are variables put in RAM?

Post by CocaCola »

gravaera wrote:The file has information on your "variables" and their offsets within the kernel image.
Could you elaborate, please? (Or link to a relevant Wiki article.)
My main concern is that I can overwrite RAM that is used by variables.
gravaera wrote:For more information, consider writing some basic programs and analysing them using programs like objdump.
I'll try that, thanks.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Are variables put in RAM?

Post by JamesM »

CocaCola wrote:
gravaera wrote:The file has information on your "variables" and their offsets within the kernel image.
Could you elaborate, please? (Or link to a relevant Wiki article.)
My main concern is that I can overwrite RAM that is used by variables.
gravaera wrote:For more information, consider writing some basic programs and analysing them using programs like objdump.
I'll try that, thanks.
Hi,

You've exposed a gap in your knowledge at an alarmingly basic level. I suggest you try something more basic to get the hang of how a computer program actually works, before trying operating system development.

Cheers,

James
User avatar
CocaCola
Member
Member
Posts: 36
Joined: Fri Sep 07, 2012 9:11 am

Re: Are variables put in RAM?

Post by CocaCola »

I've seen that objdump can show the sections of a program and their contents.
And that's useful. But it still doesn't answer my question: how can I be sure I don't overwrite memory in use?

When the kernel image is loaded onto memory, does the GRUB memory map mark it as "reserved"?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Are variables put in RAM?

Post by Brendan »

Hi,
CocaCola wrote:When the kernel image is loaded onto memory, does the GRUB memory map mark it as "reserved"?
No. Sadly the memory map provided by GRUB only says what the firmware told GRUB, and doesn't include areas that GRUB has used for your kernel or extra modules or the multiboot information; and because of this it's a pain in the neck to avoid trashing important things during early initialisation code (especially if you comply with the multi-boot specification and don't make "GRUB specific" assumptions about where things like the multiboot information or extra modules may have been left).

The normal work-around for this is to reserve space in your ".bss" for anything you may need during early initialisation (space you can use safely before you've got your own physical memory management working).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
CocaCola
Member
Member
Posts: 36
Joined: Fri Sep 07, 2012 9:11 am

Re: Are variables put in RAM?

Post by CocaCola »

Thanks to all who posted.

For anyone interested, I found these:
http://duartes.org/gustavo/blog/post/mo ... memory-map
http://duartes.org/gustavo/blog/post/ho ... rs-boot-up
http://duartes.org/gustavo/blog/post/ke ... ot-process
http://www.intel.com/content/www/us/en/ ... nuals.html

If anyone has more useful links to contribute, please do so.
Otherwise, see you in a bit: I've some reading to do.
Post Reply