GCC mixing C and C++ with -flto

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

GCC mixing C and C++ with -flto

Postby lilvinz » Sat Aug 20, 2016 7:07 pm

Hi Giovanni,

it seems that there is an issue with GCC when using lto and accessing the same union from C as well as C++ code.
The background is that i am running unit tests using google test (C++) against some custom hal drivers and a custom posix port. This means that the public ch structure is being used from compile units in C as well as C++. Now if -flto is enabled, this leads to the following Error:

Code: Select all

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

os/rt/include/chschd.h:535:20: error: type of ‘ch’ does not match original declaration [-Werror]
 extern ch_system_t ch;
                    ^
os/rt/src/chschd.c:42:13: note: previously declared here
 ch_system_t ch;
             ^
lto1: all warnings being treated as errors
lto-wrapper: fatal error: g++ returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status

The issue seems to be that the anonymous union in struct ch_thread is being translated differently for C++ than for C language. If i change that union to be a struct (took quite some time to get to that point), the issue is "fixed". I am not sure if this is a gcc bug.
I assume that this issue can also be hit when using c++ and lto on arm or so.
If found a similar bug record:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44871

Any thoughts on that?

Cheers

Vinz

PS: Seeing this on ChibiOS 16.1.5 Stable

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: GCC mixing C and C++ with -flto

Postby Giovanni » Sat Aug 20, 2016 7:18 pm

Him

Not sure, it looks like a GCC bug because it is triggered by LTO, we could implement a workaround if it does not mess too much with the code, suggestions?

Giovanni

User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Re: GCC mixing C and C++ with -flto

Postby lilvinz » Sat Aug 20, 2016 7:23 pm

Currently i am using this in chschd.h within struct ch_thread:

Code: Select all

#ifdef PORT_ARCHITECTURE_SIMIA32
  /* This is to work around an issue with gcc when accessing a union from both
   * C as well as C++ compile units in combination with -flto. */
  struct {
#else
  union {
#endif

But this really isn't a workaround. Also it will only help for my custom stuff and my unit tests.
Making that union into a struct wastes memory. Currently i have no idea how to fix this without using a union. I have tried all kinds of __attribute__() things to get that C and C++ types match without any success.

Cheers

Vinz

User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Re: GCC mixing C and C++ with -flto

Postby lilvinz » Sat Aug 20, 2016 7:26 pm

Giovanni wrote:Not sure, it looks like a GCC bug because it is triggered by LTO


I am very hesitant with attributing issues to GCC because very often LTO just makes hidden bugs surface.
In this case, the type of the ch structure doesn't match between C and C++ - at least by means of how gcc compares them to each others.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: GCC mixing C and C++ with -flto

Postby Giovanni » Sat Aug 20, 2016 8:43 pm

GCC for ARM also uses LTO and it works.

If there is a bug in the code then I don't know what rule I am violating.

Giovanni


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 57 guests