title: TI LM4F120, GNU ARM GCC eabi and malloc: crash when calling malloc
body: <p>I am working with the Stellaris Launchpad, <a href="https://launchpad.net/gcc-arm-embedded/4.7/4.7-2012-q4-major" rel="nofollow">GCC arm embedded</a> and I am trying to use malloc. According to <a href="http://communities.mentor.com/community/cs/archives/arm-gnu/msg00995.html" rel="nofollow">this</a> it should not be a problem at all, but it is a problem at compile time: you need an implementation of _sbrk to make it run. So based on <a href="http://e2e.ti.com/support/microcontrollers/stellaris_arm/f/471/t/44452.aspx#539503" rel="nofollow">this</a> thread I created the following _sbrk implementation:</p>
  
  <pre><code>#include "stdio.h"
  extern int  _HEAP_START;
  extern int  _HEAP_END;
  
  extern void *_sbrk(int incr)
  {
      static unsigned char *heap = NULL;
      unsigned char *prev_heap;
  
      if (heap == NULL) {
          heap = (unsigned char *)&amp;_HEAP_START;
      }
      prev_heap = heap;
  
      if ((heap + incr) &gt;= (unsigned char *)&amp;_HEAP_END) {
          return 0;
      }
      heap += incr;
      return (void *)prev_heap;
  }
  </code></pre>
  
  <p>I also have the following linkerscript to define a stack, a heap and all the rest (also based on the same thread)</p>
  
  <pre><code>MEMORY
  {
      FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 256K
      SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
  }
  
  SECTIONS
  {
      .text :
      {
          KEEP(*(.isr_vector))
          *(.text*)
          *(.rodata*)
          _etext = .;
      } &gt; FLASH
  
      .data : AT (ADDR(.text) + SIZEOF(.text))
      {
          _data = .;
          *(vtable)
          *(.data*)
          _edata = .;
      } &gt; SRAM
  
      .bss :
      {
          _bss = .;
          *(.bss*)
          *(COMMON)
          _ebss = .;
          . = ALIGN (8);
         _end = .;
      } &gt; SRAM
  }
  
  /* end of allocated ram _end */
  PROVIDE( _HEAP_START = _end );
  
  /* end of the heap -&gt; align 8 byte */
  PROVIDE ( _HEAP_END = ALIGN(ORIGIN(SRAM) + LENGTH(SRAM) - 8 ,8) );
  </code></pre>
  
  <p>the last pieces of information: Compiler params:</p>
  
  <pre><code>-g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -ffunction-sections -fdata-sections -MD -std=c99 -Wall -pedantic -DPART_LM4F120H5QR -c -Iinc/  -Ilib/inc -I/usr/arm-none-eabi/include -DTARGET_IS_BLIZZARD_RA1
  </code></pre>
  
  <p>And the Linker params:</p>
  
  <pre><code>-Tlib/linker_script.ld --entry ResetISR --gc-sections -L lib/ -L /usr/arm-none-eabi/lib -o final.elf obj/main.c.o obj/_sbrk.c.o obj/startup_gcc.c.o obj/switch.S.o -ldriver-cm4f -lc -lm
  </code></pre>
  
  <p>The main file consists of one malloc call:</p>
  
  <pre><code>#include "stdio.h"
  #include "stdlib.h"
  
  int main(){
      int *i = malloc(sizeof(int));
  }
  </code></pre>
  
  <p>And it crashes on the malloc call. Breakpoints in the _sbrk do not get hit, so the program crashes before it even reaches _sbrk. What is going on? Why does malloc not, as promised, work on its own? And why does it crash before it reaches its core algorithm (_sbrk)?</p>