title: Using newlib's malloc in an ARM Cortex-M3
body: <p>I'm creating code for an ARM Cortex-M3 (NXP's LCP17xx). I've been using static memory up to now and everything worked well.  I tried to add dynamic memory support, but once I call malloc, the system gets stuck.</p>
  
  <p>I'm compiling with gcc for arm bare metal, and using newlib. Version: gcc-arm-none-eabi-4_6-2012q1</p>
  
  <p>To add malloc support, I implemented a simple _sbrk function and modified my linker script to make some space for the heap (I've read many different tutorials about this part, but none cover the problem that I encountered next).</p>
  
  <p>With the help of some leds, I can be certain that the code runs up until the point that it calls <code>malloc</code>, then it doesn't go on. It doesn't even reach my <code>_sbrk</code> function. Also, it will get stuck in a call to <code>sizeof</code>, if I include a call to <code>malloc</code> later on in the code.</p>
  
  <p>So, what can I be doing wrong that when calling <code>malloc</code> the code gets stuck without ever reaching <code>_sbrk</code> or returning?</p>
  
  <p>After staring for quite a while to the memory map generated when the <code>malloc</code> call is included and when it's not, I suspect that it's related to the structures that are used by <code>malloc</code>.</p>
  
  <p>This is the part of the ld script that defines the ram memory:</p>
  
  <pre><code>.bss :
  {
      _start_bss = .;
      *(.bss)
      *(COMMON)
      _ebss = .;
      . = ALIGN (8);
      _end = .;
  } &gt;sram
  . = ALIGN(4);
  _end_bss = .;
  . = ALIGN(256);
  _start_heap = .;
  PROVIDE( __cs3_heap_start = _start_heap)
  
  _end_stack = 0x10008000;
  </code></pre>
  
  <p>_end_stack is then set in the interrupt vector table.</p>
  
  <p>And now a comparison of the different maps.  Without using malloc in the code:</p>
  
  <pre><code> *(COMMON)
              0x1000000c                _ebss = .
              0x10000010                . = ALIGN (0x8)
   *fill*     0x1000000c        0x4 00
              0x10000010                _end = .
              0x10000010                . = ALIGN (0x4)
              0x10000010                _end_bss = .
              0x10000100                . = ALIGN (0x100)
              0x10000100                _start_heap = .
  </code></pre>
  
  <p>Memory map using malloc in the code:</p>
  
  <pre><code>*(COMMON)
  COMMON      0x10000848        0x4 ...arm-none-eabi/lib/armv7-m/libc.a(lib_a-reent.o)
              0x10000848                errno
              0x1000084c                _ebss = .
              0x10000850                . = ALIGN (0x8)
  *fill*      0x1000084c        0x4 00
              0x10000850                _end = .
  
  .bss.__malloc_max_total_mem
              0x10000850        0x4
  .bss.__malloc_max_total_mem
              0x10000850        0x4 ...arm-none-eabi/lib/armv7-m/libc.a(lib_a-mallocr.o)
              0x10000850                __malloc_max_total_mem
  
  (...) It goes on (...)
              0x1000085c                __malloc_current_mallinfo
              0x10000884                . = ALIGN (0x4)
              0x10000884                _end_bss = .
              0x10000900                . = ALIGN (0x100)
              0x10000900                _start_heap = .
  </code></pre>