EmbDev.net

Forum: ARM programming with GCC/GNU tools Linker error, undefined reference to `_sbrk'


von jrmymllr j. (jrmymllr)


Rate this post
useful
not useful
I'm trying to compile an MP3 decoder (Helix) with CodeSourcery for ARM
Cortex-M3, and can't shake this error:

arm-none-eabi/lib/thumb2\libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0xc): undefined reference to `_sbrk'
collect2: ld returned 1 exit status


I've spent hours searching the Internet for info on the error, and hours
fiddling with things trying to fix it.  I can compile a simple LED blink
program.  I believe this error is related to "syscall", but I don't
understand what I need to do to fix it.  Wouldn't this be handled by the
compiler?  If not, would it be possible that the micro vendor (Luminary)
provides a library with the missing piece?  I'd gladly read a small book
to resolve this problem, but don't know what or where to look for it.

von Martin T. (mthomas) (Moderator)


Rate this post
useful
not useful
Jerry Milner wrote:
> I'm trying to compile an MP3 decoder (Helix) with CodeSourcery for ARM
> Cortex-M3, and can't shake this error:
>
> arm-none-eabi/lib/thumb2\libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
> sbrkr.c:(.text+0xc): undefined reference to `_sbrk'
> collect2: ld returned 1 exit status
>
>
> I've spent hours searching the Internet for info on the error, and hours
> fiddling with things trying to fix it.  I can compile a simple LED blink
> program.  I believe this error is related to "syscall", but I don't
> understand what I need to do to fix it.

The fix is to provide the requested function _sbrk.

> Wouldn't this be handled by the compiler?

No, it's not related to the compiler but to the libc (=newlib). I expect
the MP3-decoder source from the Helix community uses malloc. It seem
that in the CS newlib configuration malloc calls the reentrant version
of _sbrk_r which is already in libc.a and _sbrk_r calls _sbrk which has
to be provided.

> If not, would it be possible that the micro vendor (Luminary)
> provides a library with the missing piece?

Sure, they could add this to the driver-library. But it's not that
difficult to add it yourself.

> I'd gladly read a small book to resolve this problem, but don't know
> what or where to look for it.

(1) I expect you are using the linker-script form the Luminary
Driver-Library (standalone.ld). Modify this script a little bit:

[...]
.bss :
{
  _bss = .;
  *(.bss*)
  *(COMMON)
  _ebss = .;
  . = ALIGN (8);
  _end = .;
} > SRAM

PROVIDE(__HEAP_START = _end );
[...]

(2) create a file syscalls.c and add the following code:

/* based on a example-code from Keil for CS G++ */

/* for caddr_t (typedef char * caddr_t;) */
#include <sys/types.h>

extern int  __HEAP_START;

caddr_t _sbrk ( int incr )
{
  static unsigned char *heap = NULL;
  unsigned char *prev_heap;

  if (heap == NULL) {
    heap = (unsigned char *)&__HEAP_START;
  }
  prev_heap = heap;
  /* check removed to show basic approach */

  heap += incr;

  return (caddr_t) prev_heap;
}

(3) make sure the file is compiled and the object-code from it gets
linked

This should get you started (not tested here). You can add additional
"out of heap-space" checks later if needed.

Hope this helps.

EDIT: added include

von jrmymllr j. (jrmymllr)


Rate this post
useful
not useful
Martin Thomas wrote:

> This should get you started (not tested here). You can add additional
> "out of heap-space" checks later if needed.
>
> Hope this helps.
>
> EDIT: added include


Thank you!  I initially tried to get it to compile, but that was without
the include.  I didn't spend much extra time on it until now.  It's
compiling....now I have a place to start.

von Guo J. (Company: fsl) (guojia1979)


Rate this post
useful
not useful
Martin Thomas wrote:


Thank you very much, I really appreciate it! It help me save much time.

von Edi G. (yo3hcv)


Rate this post
useful
not useful
Hmmm.. after 10 years still a excellent reply!


I had the same problem on Tensillica LX6 MCU and you save my day.
Creating syscalls.c solved everything.

THANK YOU!

von Abdelrahman Ahmed (Guest)


Rate this post
useful
not useful
11 years later it still works. Thank you very much.

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.