Problem with function malloc() (undefined reference to _sbr)

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

krikus
Posts: 6
Joined: Fri Dec 19, 2014 5:25 pm

Problem with function malloc() (undefined reference to _sbr)

Sat Dec 20, 2014 1:57 pm

Hi!
I use my custom board with ATSAM4C08 and Atmel Studio 6. If I compile program with functions like malloc() or new[], I will get an error: "Error 3 undefined reference to `_sbrk' /home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/sbrkr.c 60 1 BlinkApp"
How can I fix this?
krikus
Posts: 6
Joined: Fri Dec 19, 2014 5:25 pm

Re: Problem with function malloc() (undefined reference to _

Sat Dec 20, 2014 2:01 pm

The code:

Code: Select all

typedef struct {	float x;	float y;	float z;} flake;
flake *flakes[size];

/*******************************/

flakes[0] = (flake*)  malloc(sizeof(flake)); //I get an error here
Sam++
Posts: 16
Joined: Tue Jul 01, 2014 9:05 pm

Re: Problem with function malloc() (undefined reference to _

Wed Dec 24, 2014 12:57 pm

Hi Krikus,

sbrk() is a function called by the c libraries to manage heap memory. It is very dependant on the actual implementation you use. It is up to you to specify the location in memory of the stack, the heap and so on.

I have implemented it for Atmel's SAM4S Xplained Pro and Atmel studio as follows:

Code: Select all

extern unsigned char* _sheap;
extern unsigned char* _eheap;
extern unsigned char* _ram_end_;

caddr_t _sbrk(ptrdiff_t __incr)
{
	static unsigned char* heap = NULL;
	unsigned char* prev_heap;
		
	if(heap == NULL)
	{
		heap = (unsigned char*)&_sheap;
	}
	prev_heap = heap;
		
	if((heap + __incr) > (unsigned char*)&_eheap)
	//if((heap + __incr) > (unsigned char*)&_ram_end_)
	{
		errno = ENOMEM;
		assert(strerror(errno));
		__BKPT(0);
		return (caddr_t) 0;
	}
		
	heap += __incr;
	return(caddr_t) prev_heap;
}
Hope this helps.

Paul
krikus
Posts: 6
Joined: Fri Dec 19, 2014 5:25 pm

Re: Problem with function malloc() (undefined reference to _

Mon Jan 05, 2015 1:44 pm

Thanks, Paul. But I still get errors. :(
_sheap and _eheap variables are undefined for my compiler. I found only these pointers in "sam.h" file:

Code: Select all

extern uint32_t _sfixed;
extern uint32_t _efixed;
extern uint32_t _etext;
extern uint32_t _srelocate;
extern uint32_t _erelocate;
extern uint32_t _szero;
extern uint32_t _ezero;
extern uint32_t _sstack;
extern uint32_t _estack;
I have no idea where those pointers are?
donjo9
Posts: 8
Joined: Wed Dec 10, 2014 12:36 pm

Re: Problem with function malloc() (undefined reference to _

Thu Jan 22, 2015 3:00 pm

krikus wrote:Thanks, Paul. But I still get errors. :(
_sheap and _eheap variables are undefined for my compiler. I found only these pointers in "sam.h" file:

Code: Select all

extern uint32_t _sfixed;
extern uint32_t _efixed;
extern uint32_t _etext;
extern uint32_t _srelocate;
extern uint32_t _erelocate;
extern uint32_t _szero;
extern uint32_t _ezero;
extern uint32_t _sstack;
extern uint32_t _estack;
I have no idea where those pointers are?
They are located in the linkerscript.

IN the Solution Explorer you have a CMSIS folder in your project, in there you should have a folder called linkerScripts, with a/some .ld 

Im working with the SAM4 series, so mine is called sam4s_flash.ld

look for a section that looks like this:

Code: Select all

  /* heap section */
    .heap (NOLOAD):
    {
        . = ALIGN(8);
         _sheap = .;
        . = . + HEAP_SIZE;
        . = ALIGN(8);
        _eheap = .;
    } > ram
you should have other notations than _sheap and _eheap, replace those in the code Paul posted.
krikus
Posts: 6
Joined: Fri Dec 19, 2014 5:25 pm

Re: Problem with function malloc() (undefined reference to _

Fri Mar 06, 2015 10:07 pm

donjo9 wrote:
krikus wrote:Thanks, Paul. But I still get errors. :(
_sheap and _eheap variables are undefined for my compiler. I found only these pointers in "sam.h" file:

Code: Select all

extern uint32_t _sfixed;
extern uint32_t _efixed;
extern uint32_t _etext;
extern uint32_t _srelocate;
extern uint32_t _erelocate;
extern uint32_t _szero;
extern uint32_t _ezero;
extern uint32_t _sstack;
extern uint32_t _estack;
I have no idea where those pointers are?
They are located in the linkerscript.

IN the Solution Explorer you have a CMSIS folder in your project, in there you should have a folder called linkerScripts, with a/some .ld 

Im working with the SAM4 series, so mine is called sam4s_flash.ld

look for a section that looks like this:

Code: Select all

  /* heap section */
    .heap (NOLOAD):
    {
        . = ALIGN(8);
         _sheap = .;
        . = . + HEAP_SIZE;
        . = ALIGN(8);
        _eheap = .;
    } > ram
you should have other notations than _sheap and _eheap, replace those in the code Paul posted.
Oops. I can't see any heap sections. :(

*code* (mirror)
Sam++
Posts: 16
Joined: Tue Jul 01, 2014 9:05 pm

Re: Problem with function malloc() (undefined reference to _

Wed Apr 29, 2015 8:12 pm

Hi,
Sorry for the late reaction. What compiler are you using ? If its the gcc thet comes with Atmel Studio, it should work since it's compiling (and running) on my installation of AS6.2 :
Installed Packages: Atmel ARM GNU Toolchain - 4.8.4.1443
ARM Toolchain
Version: ARM_Toolchain_Version:4.8.4.371 GCC_VERSION:4.8.4
Package GUID: D83C9208-1D2D-4665-9760-EB9EE264CF8F
Company: Atmel
HelpUrl:
Release Description: ARM Toolchain

CMSIS
Version: 4.2.0
Package GUID: D83C9208-1D2D-4665-9760-EB9EE264CF8F
Company: Atmel
HelpUrl:
Release Description: ARM Support File Version
Make sure you define the variables as extern...

Code: Select all

extern unsigned char* _sheap;
extern unsigned char* _eheap;
extern unsigned char* _ram_end_;

caddr_t _sbrk(ptrdiff_t __incr)
{
	static unsigned char* heap = NULL;
	unsigned char* prev_heap;
		
	if(heap == NULL)
	{
		heap = (unsigned char*)&_sheap;
	}
	prev_heap = heap;
		
	if((heap + __incr) > (unsigned char*)&_eheap)
	//if((heap + __incr) > (unsigned char*)&_ram_end_)
	{
		errno = ENOMEM;
		assert(strerror(errno));
		__BKPT(0);
		return (caddr_t) 0;
	}
		
	heap += __incr;
	return(caddr_t) prev_heap;
}
Hope this helps....

Paul

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests