Finding the start and end of memory areas
Shaun Jackman
sjackman@gmail.com
Thu Dec 23 19:39:00 GMT 2004
Thanks for pointing me towards the linker script. From 'ld --verbose'
I found ld was using /usr/lib/ldscripts/elf_i386.xc. From this I found
the following information...
text ? to &_etext
data &_etext to &_edata
bss - &__bss_start to &_end
heap - &_end to sbrk(0)
(gap)
stack - stack_ptr to ?
>From a snippet of code in newlib (sys/arm/syscalls.c), I found that
stack_ptr can be defined as such:
register void *stack_ptr asm ("sp");
On a Linux system, the beginning of the text and the top of the stack
are constants, 0x08048000 and 0xbffff7a0 respectively. I haven't yet
found a way to find these values at run-time in a cross-platform
manner.
I found the above values give somewhat different results than 'size'.
For exampled, on my sample program...
_etext = 0x8057baf
_edata = 0x805ce18
_end = 0x80b1cd8
_etext - 0x08048000 = 64431 // size of text
_edata - _etext = 21097 // size of data
_end - _edata = 347840 // size of bss
_end - 0x08048000 = 433368 // total
$ size a.out
text data bss dec hex filename
74477 7704 347832 430013 68fbd a.out
The results are similar, but not identical.
Cheers,
Shaun
> The simplest way is to have the cooperation of the linker script used to
> create your executable. If the script places symbols at the start and
> end of each of these sections then you can easily access them at run-time.
>
> If you cannot modify the linker script then you are going to have to
> locate the executable on disk (presumably via argv[0]) and then load and
> process the ELF headers and section tables themselves.
>
> Cheers
> Nick
More information about the Binutils
mailing list