malloc ld script requirements
Jeff Johnston
jjohnstn@redhat.com
Wed Apr 28 20:23:00 GMT 2004
Vitus Jensen wrote:
>>Bryce Schober wrote:
>>
>>
>>>What does the memory heap (malloc, etc) need from an ld script?
>
>
> Nothing...
>
>
>
>>I ask this question here because the anwer I got on the
>>binutils-list was:
>>
>>
>>>That entirely depends on how your implementation of malloc works. It
>>>might require you to define symbols in the linker script that it
>>>can refer
>>>to later to know the amount of memory available. Or your malloc
>>>implementation might depend on something else in your BSP to test,
>>>measure,
>>>deduce or infer the amount of memory. You'll have to find out
>>>what library
>>>code you're using and look at how it works.
>>
>>I really hope that you're not going to ask me to delve into the
>>source to figure this out. Can a newbie get some help?
>
>
> malloc() requires you to supply a sbrk() routine which is called whenever
> malloc() needs more memory. sbrk() is one of those ~17 routines you have to
> implement in something called libgloss.
>
> HOW you implement sbrk() is totally up to you. See the libgloss
> subdirectory for examples. And -- sorry -- you have to delve into that
> code.
>
Just to elaborate on Vitus' answer. Currently, many libgloss implementations
use the external symbol "end" or "_end" to mark the beginning of the heap. This
symbol is used by typical sbrk implementations. The malloc function just asks
sbrk to give it more storage and lets sbrk worry about heap-specifics. As Vitus
has pointed out, the sbrk function could be implemented anyway you like.
Typically, many implementations have the heap grow upwards and the stack grow
down from some preset location; the idea being that you use the memory once all
defined data has been accounted for, up to some maximum address on the platform.
The following are snippets from the mn10300's sim.ld script:
.bss :
{
*(.dynbss)
*(.bss)
*(COMMON)
}
_end = . ;
PROVIDE (end = .);
.stack 0x80000 : { _stack = .; *(.stack) *(._stack) }
In the the sbrk.c file in libgloss/mn10300:
caddr_t
_sbrk (size_t incr)
{
extern char end; /* Defined by the linker */
static char *heap_end;
char *prev_heap_end;
#if 0
char *sp = (char *)stack_ptr;
#else
char *sp = (char *)&sp;
#endif
if (heap_end == 0)
{
heap_end = &end;
}
Note the external reference to "end".
-- Jeff J.
More information about the Newlib
mailing list