public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Finding the start and end of memory areas
@ 2004-12-23  0:22 Shaun Jackman
  2004-12-23 11:15 ` Nick Clifton
  0 siblings, 1 reply; 3+ messages in thread
From: Shaun Jackman @ 2004-12-23  0:22 UTC (permalink / raw)
  To: binutils

First off, this posting is somewhat off-topic. My apologies in
advance. If anyone knows of a better forum for this topic, I'd
appreciate being pointed in the right direction. Now, on to my
question...

I'd like to find the start and end of the following memory areas
during run-time...

text
data
bss - &__bss_start to &__bss_end
heap - &__bss_end to sbrk(0)
(gap)
stack - stack pointer to ?

Presumably, they appear in that order in memory, so then end of one
should be the start of the next. Note that I'm doing this at run-time
by the program itself (introspection, as it were) and not with tools
like readelf and objdump. I've filled in the few values I know. If you
can provide any others I'd appreciate it.

If it affects the results, the target architectures are
i686-pc-linux-gnu and arm-elf.

Many thanks!
Shaun

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

* Re: Finding the start and end of memory areas
  2004-12-23  0:22 Finding the start and end of memory areas Shaun Jackman
@ 2004-12-23 11:15 ` Nick Clifton
  2004-12-23 19:39   ` Shaun Jackman
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Clifton @ 2004-12-23 11:15 UTC (permalink / raw)
  To: Shaun Jackman; +Cc: binutils

Hi Shaun,

> First off, this posting is somewhat off-topic. My apologies in
> advance. If anyone knows of a better forum for this topic, I'd
> appreciate being pointed in the right direction. 

Actually this is the right forum for this kind of question.

> I'd like to find the start and end of the following memory areas
> during run-time...
> 
> text
> data
> bss - &__bss_start to &__bss_end
> heap - &__bss_end to sbrk(0)
> (gap)
> stack - stack pointer to ?

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

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

* Re: Finding the start and end of memory areas
  2004-12-23 11:15 ` Nick Clifton
@ 2004-12-23 19:39   ` Shaun Jackman
  0 siblings, 0 replies; 3+ messages in thread
From: Shaun Jackman @ 2004-12-23 19:39 UTC (permalink / raw)
  To: binutils

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

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

end of thread, other threads:[~2004-12-23 19:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-23  0:22 Finding the start and end of memory areas Shaun Jackman
2004-12-23 11:15 ` Nick Clifton
2004-12-23 19:39   ` Shaun Jackman

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 read-only IMAP folder(s) and NNTP newsgroup(s).