Page 1 of 1

Undefined reference to _sbrk_r for variadic arguments

Posted: Mon Oct 24, 2016 10:07 am
by raphaelchang
I'm using ChibiOS 16.1.4 on an STM32F303. I am attempting to use va_list, va_args, etc. in a function, but whenever I include <stdargs.h> and use those functions, I get the following linker errors:

/opt/homebrew-cask/Caskroom/gcc-arm-embedded/5_2-2015q4,20151219/gcc-arm-none-eabi-5_2-2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-mallocr.o): In function `_malloc_r':
mallocr.c:(.text._malloc_r+0x232): undefined reference to `_sbrk_r'
mallocr.c:(.text._malloc_r+0x292): undefined reference to `_sbrk_r'
/opt/homebrew-cask/Caskroom/gcc-arm-embedded/5_2-2015q4,20151219/gcc-arm-none-eabi-5_2-2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-freer.o): In function `_malloc_trim_r':
mallocr.c:(.text._malloc_trim_r+0x30): undefined reference to `_sbrk_r'
mallocr.c:(.text._malloc_trim_r+0x4a): undefined reference to `_sbrk_r'
mallocr.c:(.text._malloc_trim_r+0x72): undefined reference to `_sbrk_r'

I have included ChibiOS/os/various/syscalls.c in the Makefile. Without it, the error is much simpler:
/opt/homebrew-cask/Caskroom/gcc-arm-embedded/5_2-2015q4,20151219/gcc-arm-none-eabi-5_2-2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'

I see _sbrk_r defined in syscalls.c, so why can it still not find it after I include it? If I delete all references to va_list, va_args, etc., the compiler is successful again. How can I use variadic arguments in ChibiOS? I know chprintf uses it, so it must be possible.

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Mon Oct 24, 2016 10:39 am
by Giovanni
Hi,

Note that chprintf () does not require syscalls.c, so it could be something else requiring _sbrk_r() not va_list.

Is syscall.c actually compiled? it could be an error in the makefile. Anyway, I am unable to duplicate the problem, chprintf does it. Best way is if you could post a small minimal project.

Giovanni

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Mon Oct 24, 2016 7:33 pm
by raphaelchang
Thank you for your reply. I found that the problem is actually in calling vsnprintf, not va_list. If I just comment out that one line, the project compiles. I think syscalls.c is being compiled, because the error changes when I add it to the makefile.

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Mon Oct 24, 2016 7:35 pm
by Giovanni
Is it printed in console while compiling? all files are listed there.

Giovanni

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Mon Oct 24, 2016 7:42 pm
by raphaelchang
Yes, it is printed. I have resolved the issue by adding --specs=nosys.specs to the compiler options, and not including syscalls.c. If I include syscalls.c the error remains.

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Mon Oct 24, 2016 7:48 pm
by Giovanni
It must be a conflict with the default implementation of syscalls.c.

Note that the error disappearing does not mean it works correctly, you need to use the syscalls.c for ChibiOS.

I'll give it a try tomorrow.

Giovanni

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Thu Nov 10, 2016 4:28 pm
by dalbert
Did you solve this?

I am trying to build an old project; I installed the latest GNU ARM Embedded Tools (5.4 2016q3) and upgraded from ChibiOS 2.6.7 to 2.6.8 and without adding --specs=nosys.specs to the compiler options, it fails with the same undefined reference.

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Thu Nov 10, 2016 4:33 pm
by Giovanni
I haven't tried this yet, moving this thread to bug reports so it will not be lost.

Giovanni

Re: Undefined reference to _sbrk_r for variadic arguments

Posted: Tue Nov 15, 2016 11:20 am
by Giovanni
Hi,

Fixed as bug #789.

The GCC garbage collector does not see as used the functions in syscalls.c because there is nothing in the code calling them, the call happens from within the library and apparently the linker does not consider those. It is a GCC bug I think but I worked around it by declaring functions as "used".

Giovanni