Bug 11738

Summary: [gcc] compilation fails when using pre-compiled headers
Product: [Build System, Metadata & Runtime] OE-Core Reporter: Alistair <grolim64>
Component: devtools / tool chainAssignee: Juro Bystricky <juro.bystricky>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: Medium+ CC: meta.mr.watcher, meta.watcher, pgowda.cve, pinskia, randy.macleod, richard.purdie
Version: 2.3.2   
Target Milestone: 2.4 M3   
Hardware: BeagleBone   
OS: arm   
Whiteboard: Backport to 2.2.2, 2.3.2 if needed.
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: No (bug/feature does not impact docs)
Attachments:
Description Flags
This patch fixes the problem none

Description Alistair 2017-06-29 20:59:32 UTC
Hi all,

Trying to update our code from krogoth to pyro, we have run into an issue where gcc/g++ (tested both versions 6.3 and 5.4).
built under poky-pyro will segmentation fault when using pre-compiled headers and defining functions named 'major' or 'minor'.
When using an ubuntu-provided cross-compiler (apt-get install g++-arm-linux-gnueabihf) or the host compiler, I cannot reproduce the behaviour (compiles without warning/error).
To my knowledge the segmentation fault only occurs when using a compiler built via bitbake from the pyro branch.

Environment:
-----------------------
 - ubuntu 16.04 x64
 - pyro branch of poky
 - MACHINE set to beaglebone in local.conf
 - bblayers left at default (no extra layers)

Example source files:
-----------------------
$ cat precompiled.h
#ifndef PRECOMPILED_H
#define PRECOMPILED_H

#include <algorithm>

#endif // PRECOMPILED_H

$ cat main.cpp 
#include "precompiled.h"

class Version {
public:
	void major() {}
};
int main(void){ return 0;}

Compilation:
-----------------------
$ $CXX $CXXFLAGS -x c++-header precompiled.h
$ $CXX $CXXFLAGS -H main.cpp 
! precompiled.h.gch
 main.cpp
main.cpp:5:2: internal compiler error: Segmentation fault
  void major() {}
  ^~~~
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.

$ $CXX -v
Using built-in specs.
COLLECT_GCC=arm-poky-linux-gnueabi-g++
COLLECT_LTO_WRAPPER=/opt/work/poky/build/tmp/work/cortexa8hf-neon-poky-linux-gnueabi/meta-ide-support/1.0-r3/recipe-sysroot-native/usr/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.3.0/lto-wrapper
Target: arm-poky-linux-gnueabi
Configured with: ../../../../../../work-shared/gcc-6.3.0-r0/gcc-6.3.0/configure --build=x86_64-linux --host=x86_64-linux --target=arm-poky-linux-gnueabi --prefix=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr --exec_prefix=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr --bindir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/bin/arm-poky-linux-gnueabi --sbindir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/bin/arm-poky-linux-gnueabi --libexecdir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/libexec/arm-poky-linux-gnueabi --datadir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/share --sysconfdir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/etc --sharedstatedir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/com --localstatedir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/var --libdir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/lib/arm-poky-linux-gnueabi --includedir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/include --oldincludedir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/include --infodir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/share/info --mandir=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr/share/man --disable-silent-rules --disable-dependency-tracking --with-libtool-sysroot=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native --enable-clocale=generic --with-gnu-ld --enable-shared --enable-languages=c,c++ --enable-threads=posix --disable-multilib --enable-c99 --enable-long-long --enable-symvers=gnu --enable-libstdcxx-pch --program-prefix=arm-poky-linux-gnueabi- --without-local-prefix --enable-lto --enable-libssp --enable-libitm --disable-bootstrap --disable-libmudflap --with-system-zlib --with-linker-hash-style=sysv --enable-linker-build-id --with-ppl=no --with-cloog=no --enable-checking=release --enable-cheaders=c_global --without-isl --with-gxx-include-dir=/not/exist/usr/include/c++/6.3.0 --with-sysroot=/not/exist --with-build-sysroot=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot --without-long-double-128 --enable-poison-system-directories --with-mpfr=/opt/work/poky/build/tmp/work/x86_64-linux/gcc-cross-arm/6.3.0-r0/recipe-sysroot-native/usr --with-system-zlib --disable-static --disable-nls --enable-initfini-array --with-arch=armv7-a
Thread model: posix
gcc version 6.3.0 (GCC) 

$ echo $CXXFLAGS
-O2 -pipe -g -feliminate-unused-debug-types
Comment 1 Juro Bystricky 2017-07-06 16:09:08 UTC
I was also able to reproduce the problem with gcc version 7.1.0 (GCC) for i586
with the master (about June 28):

$ $CXX -v
Using built-in specs.
COLLECT_GCC=i586-poky-linux-g++
COLLECT_LTO_WRAPPER=/home/juro/sdk-11587/sysroots/x86_64-pokysdk-linux/usr/libexec/i586-poky-linux/gcc/i586-poky-linux/7.1.0/lto-wrapper
Target: i586-poky-linux
Configured with: ../../../../../../work-shared/gcc-7.1.0-r0/gcc-7.1.0/configure --build=x86_64-linux --host=x86_64-pokysdk-linux --target=i586-poky-linux --prefix=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr --exec_prefix=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr --bindir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/bin/i586-poky-linux --sbindir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/bin/i586-poky-linux --libexecdir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/libexec/i586-poky-linux --datadir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/share --sysconfdir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/etc --sharedstatedir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/com --localstatedir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/var --libdir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/lib/i586-poky-linux --includedir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/include --oldincludedir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/include --infodir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/share/info --mandir=/opt/poky/2.3/sysroots/x86_64-pokysdk-linux/usr/share/man --disable-silent-rules --disable-dependency-tracking --with-libtool-sysroot=/data/master-master/poky/build-11587/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-i586/7.1.0-r0/recipe-sysroot --with-gnu-ld --enable-shared --enable-languages=c,c++ --enable-threads=posix --enable-multilib --enable-c99 --enable-long-long --enable-symvers=gnu --enable-libstdcxx-pch --program-prefix=i586-poky-linux- --without-local-prefix --enable-lto --enable-libssp --enable-libitm --disable-bootstrap --disable-libmudflap --with-system-zlib --with-linker-hash-style=gnu --enable-linker-build-id --with-ppl=no --with-cloog=no --enable-checking=release --enable-cheaders=c_global --without-isl --with-gxx-include-dir=/not/exist/usr/include/c++/7.1.0 --with-build-time-tools=/data/master-master/poky/build-11587/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-i586/7.1.0-r0/recipe-sysroot-native/usr/i586-poky-linux/bin --with-sysroot=/not/exist --with-build-sysroot=/data/master-master/poky/build-11587/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-i586/7.1.0-r0/recipe-sysroot --without-long-double-128 --enable-targets=all --enable-poison-system-directories --with-mpfr=/data/master-master/poky/build-11587/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-i586/7.1.0-r0/recipe-sysroot --with-mpc=/data/master-master/poky/build-11587/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-i586/7.1.0-r0/recipe-sysroot --disable-static --enable-nls --enable-initfini-array --enable-__cxa_atexit
Thread model: posix
gcc version 7.1.0 (GCC)
Comment 2 Juro Bystricky 2017-07-06 16:36:42 UTC
As you noticed, the culprit is "major". After adding #include <sys/types.h> to the main.cpp:

#include <sys/types.h>
#include "precompiled.h"

class Version {
public:
	void major() {}
};

int main(void){ return 0;}


We get:
$CXX $CXXFLAGS  main.cpp 
main.cpp:6:13: warning: In the GNU C Library, "major" is defined
 by <sys/sysmacros.h>. For historical compatibility, it is
 currently defined by <sys/types.h> as well, but we plan to
 remove this soon. To use "major", include <sys/sysmacros.h>
 directly. If you did not intend to use a system-defined macro
 "major", you should undefine it after including <sys/types.h>.
  void major() {}
             ^~~~                       


So a quick work-around would be to either explicitly include <sys/types.h> or
rename the method "major" to something like "version_major".
Of course, there is no good excuse for the segmentation fault so I will look further.
Comment 3 Juro Bystricky 2017-07-06 17:16:51 UTC
Just a quick observation: Works for me if the pre-compiled header is created with "-save-temps":

$CXX $CXXFLAGS -save-temps -x c++-header precompiled.h
Comment 4 Alistair 2017-07-06 21:10:09 UTC
Confirming Juro's workaround of 'save-temps' works for me.
Another workaround appears to be including <sys/sysmacros.h> before anything else in the pre-compiled header (which also defines major and minor).

The bug appears possibly limited to how the 'major()'/'minor()' macros/functions are defined in <sys/types.h>?
Comment 5 Juro Bystricky 2017-08-04 16:47:50 UTC
Filed a bug report with GCC:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81721
Comment 6 Juro Bystricky 2017-08-04 22:34:39 UTC
Created attachment 3919 [details]
This patch fixes the problem
Comment 7 Juro Bystricky 2017-08-04 22:50:20 UTC
Sent a patch to the mailing list:
http://lists.openembedded.org/pipermail/openembedded-core/2017-August/140485.html
Comment 8 Alistair 2017-08-07 01:33:38 UTC
Patch applies cleanly against the version 6.3 gcc recipe as well.
Fixes the issue in both the reported test case and where I originally encountered the issue.
In both cases I get the expected warning:

warning: In the GNU C Library, "major" is defined
 by <sys/sysmacros.h>. <snip>

Thanks for the help!
Comment 10 Andrew Pinski 2021-11-25 06:11:05 UTC
(In reply to comment #9)
> Merged with master:
> http://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/
> ?id=b9b6ede7f8a191b82e8c8c711ad7425fa774c876


This patch is no longer needed post GCC 10 as it was fixed correctly upstream.
Comment 11 Randy MacLeod 2021-11-25 21:44:24 UTC
Andrew, 
Thanks for the report! 
I've asked Naveen Gowda to take a look and remove the patch.
../Randy
Comment 12 Randy MacLeod 2022-01-21 22:06:00 UTC
FYI, The patch was dropped in: https://git.openembedded.org/openembedded-core/commit/?id=20aea61385e1a53ac245353899277ba20104ed2f