Page 1 of 1

Linking errors with C++

Posted: Fri Sep 18, 2015 2:48 am
by charlesrwest
Hello,

I experienced an odd intermittent problem where calling certain functions would suddenly cause the following errors to appear:

Code: Select all

/usr/lib/gcc/arm-none-eabi/4.8/../../../arm-none-eabi/lib/armv6-m/libg.a(lib_a-abort.o): In function `abort':
/build/buildd/newlib-2.1.0+git20140818.1a8323b/build/arm-none-eabi/armv6-m/newlib/libc/stdlib/../../../../../../newlib/libc/stdlib/abort.c:63: undefined reference to `_exit'
/usr/lib/gcc/arm-none-eabi/4.8/../../../arm-none-eabi/lib/armv6-m/libg.a(lib_a-signalr.o): In function `_kill_r':
/build/buildd/newlib-2.1.0+git20140818.1a8323b/build/arm-none-eabi/armv6-m/newlib/libc/reent/../../../../../../newlib/libc/reent/signalr.c:61: undefined reference to `_kill'
/usr/lib/gcc/arm-none-eabi/4.8/../../../arm-none-eabi/lib/armv6-m/libg.a(lib_a-signalr.o): In function `_getpid_r':
/build/buildd/newlib-2.1.0+git20140818.1a8323b/build/arm-none-eabi/armv6-m/newlib/libc/reent/../../../../../../newlib/libc/reent/signalr.c:96: undefined reference to `_getpid'


Defining stubs for those functions in my main file or adding syscalls_cpp.cpp to the project just changed the errors to the following:

Code: Select all

`_exit' referenced in section `.text.abort' of /usr/lib/gcc/arm-none-eabi/4.8/../../../arm-none-eabi/lib/armv6-m/libg.a(lib_a-abort.o): defined in discarded section `.text' of build/obj/syscalls_cpp.o (symbol from plugin)
`_kill' referenced in section `.text._kill_r' of /usr/lib/gcc/arm-none-eabi/4.8/../../../arm-none-eabi/lib/armv6-m/libg.a(lib_a-signalr.o): defined in discarded section `.text' of build/obj/syscalls_cpp.o (symbol from plugin)
`_getpid' referenced in section `.text._getpid_r' of /usr/lib/gcc/arm-none-eabi/4.8/../../../arm-none-eabi/lib/armv6-m/libg.a(lib_a-signalr.o): defined in discarded section `.text' of build/obj/syscalls_cpp.o (symbol from plugin)


It appears that the -02 optimization level (default in the makefile, right?) is cutting out the stubs from the wrapper before they can be linked to. This can be avoided (for GCC at least) by adding the __attribute__((used)) tag to each of the 3 function definitions in syscalls_cpp.cpp.

If I may ask, would it be possible to incorporate that change into the ChibiOS code? If so, what is the best way to post any future bug fixes (Github is read-only)?

Thanks!
Charlie West

Re: Linking errors with C++

Posted: Fri Sep 18, 2015 7:48 am
by barthess
It is not -O2 but LTO problem. And it looks fixed in latest GCC.
Any way your fix looks reasonable because syscalls_cpp.cpp is for GCC only.

Yes, Github mirror is read-only. You may contribute patch via
this forum or via Sourceforge bug tracker.

Re: Linking errors with C++

Posted: Wed Jul 13, 2016 4:01 pm
by piers
Thanks for this fix! Is there a reason it is not incorporated in ChibiOS yet?

You say it's fixed, in which GCC release it should have been fixed? At least I still get this behaviour in gcc 5.4.

Re: Linking errors with C++

Posted: Wed Feb 08, 2017 6:09 am
by charlesrwest
Hello.

I'm doing another project and noticed that I seem to be getting the same issue. I guess the next step is to submit to the source forge bug tracker?

Thanks,
Charlie

Re: Linking errors with C++

Posted: Wed Feb 08, 2017 9:03 am
by Giovanni
Hi,

There is a new C++ wrapper in development, are you using this?

viewtopic.php?f=3&t=3287

Please report any problem in that thread so involved people can address it.

Giovanni