__curbrk (and &_end)
Robert Millan
zeratul2@wanadoo.es
Mon Feb 16 12:24:00 GMT 2004
[ Please CC, I'm not subscribed ]
Hi!
Unlike Linux, kFreeBSD doesn't return the current break address through the
stack after running the SYS_break syscall.
Therefore, the brk() implementation cannot initialise __curbrk on that
platform (the expected behaviour would be that calling brk(0) when
__curbrk == 0 would initialise __curbrk to a sane value.)
I have attempted a solution in the lines of:
/* initialise the break */
if (__curbrk == 0)
__curbrk = &_end;
This solution works perfectly on staticaly-linked programs. However, it seems
that when &_end is used from libc.so, it contains the break address of libc
itself, not the running program.
I don't understand what makes it different, or how can I obtain the program's
break address from libc without changing the API. But I have done this simple
test:
$ cat getend.c
extern void _end;
void *
getend ()
{ return &_end; }
$ cat test.c
#include <stdio.h>
extern void _end;
int
main ()
{ printf ("%p\n%p\n", &_end, getend ()); }
$ gcc -shared getend.c -o libgetend.so
$ gcc test.c -o test -L. -lgetend
$ LD_LIBRARY_PATH=. ./test
0x8049710
0x8049710
And my test shared object returns the correct address. So my guess is it
must be possible to do the same in libc. Any hints?
Thanks.
--
Robert Millan
"[..] but the delight and pride of Aule is in the deed of making, and in the
thing made, and neither in possession nor in his own mastery; wherefore he
gives and hoards not, and is free from care, passing ever on to some new work."
-- J.R.R.T., Ainulindale (Silmarillion)
More information about the Libc-alpha
mailing list