Undefined reference to _sbrk_r for variadic arguments Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
Post Reply
raphaelchang
Posts: 6
Joined: Sat Jul 23, 2016 7:50 am
Been thanked: 1 time

Undefined reference to _sbrk_r for variadic arguments

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Undefined reference to _sbrk_r for variadic arguments

Post 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
raphaelchang
Posts: 6
Joined: Sat Jul 23, 2016 7:50 am
Been thanked: 1 time

Re: Undefined reference to _sbrk_r for variadic arguments

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Undefined reference to _sbrk_r for variadic arguments

Post by Giovanni »

Is it printed in console while compiling? all files are listed there.

Giovanni
raphaelchang
Posts: 6
Joined: Sat Jul 23, 2016 7:50 am
Been thanked: 1 time

Re: Undefined reference to _sbrk_r for variadic arguments

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Undefined reference to _sbrk_r for variadic arguments

Post 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
dalbert
Posts: 16
Joined: Mon Mar 02, 2015 8:02 pm

Re: Undefined reference to _sbrk_r for variadic arguments

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Undefined reference to _sbrk_r for variadic arguments

Post by Giovanni »

I haven't tried this yet, moving this thread to bug reports so it will not be lost.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Undefined reference to _sbrk_r for variadic arguments

Post 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
Post Reply