EmbDev.net

Forum: ARM programming with GCC/GNU tools 'calloc 'returns NULL pointer


von Thejus K. (thejus)


Rate this post
useful
not useful
I am giving brief introduction about my project environment.
I have an elf image, created using WinARM tool chain. I have used gcc
4.1.1
I have to run the code(debug the elf image) on ARM9 platform.Our board
we have 64MB sdram.I have used the start-up code and Linker script for
initial configuaration. Iam using RealView Debugger for debugging the
target.

In the tool we have to mention a 'Top Memory' from which address the SP
is configuerd, they are recomending it should at highest possible
address.So i placed the 'Top Memory' at 0x04000000 (64MB).

According to the sbrk() implimention , that we are using with WinARM
(systemcalls.c)
HeaP comes after the Stack .So heap is allocated at rest of the memory
after stack  Am I right? If so how can we overcome these conflicing
situations.

Actual problem is that while trying to allocate memory, it is returning
a NULL pointer allways.


------------------------------------------------------------------------ 
-------
This is the part of Linker script i used.

ENTRY(_start)
STACK_SIZE = 10M ;

/* Memory Definitions */
MEMORY
{

     RAM (rw) : ORIGIN = 0x00008000, LENGTH = 0x03ff8000

}

/* Section Definitions */
SECTIONS
{
  /* first section is .text which is used for code */
 . = 0x8000 ;
  .text :
  {
    *crt0.o (.text)            /* Startup code */
    *(.text)                   /* remaining code */
    *(.rodata)                 /* read-only data (constants) */

  } > RAM

  . = ALIGN(4);
  _etext = . ;
  PROVIDE (etext = .);

  /* .data section which is used for initialized data */
  .data :
  {
    _data = .;
    *(.data)
  } > RAM

  . = ALIGN(4);
  _edata = . ;
  PROVIDE (edata = .);

  /* .bss section which is used for uninitialized data */
  .bss (NOLOAD) :
  {
    __bss_start = . ;
    _bss_start_ = . ;
    *(.bss)
    *(COMMON)
    . = ALIGN(4);
  } > RAM

  . = ALIGN(4);

   _bss_end_ = . ;

   PROVIDE (__bss_end = .);


  .stack ALIGN(256) :
  {
    . += STACK_SIZE;
    PROVIDE (_stack = .);
  } > RAM

   _end = . ;
   PROVIDE (end = .);

------------------------------------------------------------------------ 
-----

this the sbrk() implimentaion i have used


caddr_t
_sbrk (int incr)
{
  extern char   end asm ("end");  /* Defined by the linker.  */
  static char * heap_end;
  char *        prev_heap_end;

  if (heap_end == NULL)
    heap_end = & end;

  prev_heap_end = heap_end;

  if (heap_end + incr > stack_ptr)
    {
      /* Some of the libstdc++-v3 tests rely upon detecting
   out of memory errors, so do not abort here.  */
#if 0
      extern void abort (void);

      _write (1, "_sbrk: Heap and stack collision\n", 32);

      abort ();
#else
      errno = ENOMEM;
      return (caddr_t) -1;
#endif
    }

  heap_end += incr;

  return (caddr_t) prev_heap_end;
}


Thaks in advace for your great answers

regards
Thejus

von Martin Thomas (Guest)


Rate this post
useful
not useful
I hope that I understand your problem correctly...

Thejus Kuttian wrote:

> In the tool we have to mention a 'Top Memory' from which address the SP
> is configuerd, they are recomending it should at highest possible
> address.So i placed the 'Top Memory' at 0x04000000 (64MB).

Which "tool"? Which SP (IRQ, SVC, ...)?

> According to the sbrk() implimention , that we are using with WinARM
> (systemcalls.c)
> HeaP comes after the Stack .So heap is allocated at rest of the memory
> after stack  Am I right? If so how can we overcome these conflicing
> situations.

Your configuration looks like this. But it's difficult to understand
your setup completely without the initialistion-code for the
stack-pointers. Please show your startup-code or at least the parts
where the stack-pointers are initialized.

> Actual problem is that while trying to allocate memory, it is returning
> a NULL pointer allways.

Verify that the "end"-address is assigned correclty: see map-file,
sym-file etc. If you have a Hardware-Debugger or Simulator: set a
breakpoint in sbrk and see what's going on.

> This is the part of Linker script i used.
>
> ENTRY(_start)
> STACK_SIZE = 10M ;
>
> /* Memory Definitions */
> MEMORY
> {
>
>      RAM (rw) : ORIGIN = 0x00008000, LENGTH = 0x03ff8000
>
> }
...
>    . = ALIGN(4);
>
>    _bss_end_ = . ;
>
>    PROVIDE (__bss_end = .);
>
>
>   .stack ALIGN(256) :
>   {
>     . += STACK_SIZE;
>     PROVIDE (_stack = .);
>   } > RAM

I wrote that the tools wants "the stack" at the top of RAM. Here you
create a section stack right after the bss. Is "_stack" used in the
startup-code?

>    _end = . ;
>    PROVIDE (end = .);

So the head starts after the memory-area reserved for the stack. At
least it should when sbrk "sees this end".

You may:

(1) remove the section .stack from the linker-script (STACK_SIZE is no
longer needed)
(2) add a "variable" like i.e. __TOP_STACK into the linker script. i.e.
like
__TOP_STACK=0x04000000
of with newer versions of the binutils
__TOP_STACK=ORIGIN(RAM)+LENGTH(RAM);
(3) use __TOP_STACK for the stack-pointer initialisation in the
startup-code. Roughly like this:
...
.RAM_TOP:
  .word  __TOP_STACK

        ldr     r0,.RAM_TOP
        msr     CPSR_c, #ARM_MODE_IRQ | I_BIT | F_BIT
        mov     r13, r0
        sub     r0, r0, #IRQ_STACK_SIZE
        ... init for the other modes ...

This should create a "layout" like this:
TEXT|DATA|BSS|HEAP--> .....<--STACK|

The basic method is demonstrated in my "AT91SAM7S gamma" example at
http://www.siwawi.arubi.uni-kl.de/avr_projects/arm_projects/index_at91.html#at91_gamma
At least the parts about the stack-location and setup should be
portable.

Martin Thomas

von Thejus K. (thejus)


Rate this post
useful
not useful
Hi ,

    Thanks and Thanks alot Martin , i just tried your opinion ie, i
applied modifications in both startup code and linkerscript..

regards
Thejus

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.