problem with malloc

Asked by Jens Vankeirsbilck

Hi

I'm trying to execute some C++ code on an LPC1768, an ARM Cortex-M3 driven microcontroller.
The C++ code uses malloc and free, so I included <stdlib.h>. While building and compiling the code with arm-none-eabi-gcc gave no errors, the binary does not execute. The Cortex-M3 goes into HardFault.

When compiling the exact same code with the online compiler of mbed (https://developer.mbed.org/compiler/) the binary does execute!

This leads me to believe that my compilation process, when I use arm-none-eabi-gcc, is not correct. Could it be that the wrong <stdlib.h> and thus the wrong implementation of malloc() and free() are included into the binary, causing the hardfault?

here are the contents of the makefile:

--> dijkstra.cpp includes the code that calls malloc() and free()
$(cpp) -I(...) dijkstra.cpp -o algorithms/dijkstra.o

--> linker to create the elf (the binary is extracted from the .elf )
$(cpp) -T"lpc1768.ld" -nostartfiles -Xlinker --gc-sections -WL,-Map,"dijkstra.map" --specs=nosys.specs ..... -o dijkstra.elf

Could you please let me know what I do wrong?

Thanks in advance.

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#1

Hi Jens,

malloc will rely on _sbrk to get memory so you need to implement _sbrk in your application. The mbed compiler must includes some library with such an implementation.

Best regards,

Thomas

Revision history for this message
Jens Vankeirsbilck (jens-vankeirsbilck) said :
#2

Hi Thomas

would a missing _sbrk function not result in a failed compilation process. During my online search to solve my problem, i came across the missing _sbrk. However, in every case that resulted in a failure to compile.

As i wrote before, everything compiles fine. The binary just does not execute...

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#3

Hi Jens,

Linking with nosys.specs will bring a default _sbrk implementation that will return memory just after the symbol end (which points at the end of the .bss segment).

Best regards.

Can you help with this problem?

Provide an answer of your own, or ask Jens Vankeirsbilck for more information if necessary.

To post a message you must log in.