views:

30

answers:

2

I know when it's a matter of accessing memory of a stack frame it'll be through using stack frame pointer but I wonder about how the access to data, BSS segments containing global/static data will be, through using a pointer like stack frame pointer indicating starting point of those segments or instructions address pieces of those segments directly so that each time application starts the system will have to write address parts of instructions in text segment ?

+1  A: 

Virtual memory means that these segments always appear in the same location in virtual-address space, so their addresses can be hardcoded into the executable code.

(Note, this is not true for ASLR).

Oli Charlesworth
@Oli Charlesworth_"these segments always appear in the same location in virtual-address space" what do you mean by same location, you mean they're in a piece of memory in a fixed position from starting point of application life-time to the end of it?
Pooria
@Pooria: I mean that it is not necessary to access them through a pointer. Their addresses are known at link-time.
Oli Charlesworth
@Oli Charlesworth_yeah I was thinking the same way, every time application is executed, those addresses are hardcoded into instructions.
Pooria
+1  A: 

You could declare a global variable with __attribute__ ((section ("BSS"))) and get the address of the variable. Take a look at the Gcc documentation

You can also declare a non-initialized static variable and get its address.

Thomas
Pooria
@Pooria If you have an int that you know it is in a specific section, you can get its address and iterate over the content of the section. After like Oli Charlesworth said, you can take a look at the ELF specification and your OS specification to see where the sections are mapped in memory.
Thomas
After re-read your comment I'm not sure to understand... int a __attribute__ ((section ("BSS"))); int *bss_section_address = of course you'll have to check where in the section you are. I don't know if there is a way to ensure the fact that you are at the beginning or not.
Thomas
A friend of mine has to do a similar thing in his kernel implementation (in objC) http://pmbsd.googlecode.com/hg/kernel/kern/kern_objc.c
Thomas