Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 646440 - >=sys-devel/gcc-6 freestanding compilation issue in 13_all_default-ssp-fix.patch
Summary: >=sys-devel/gcc-6 freestanding compilation issue in 13_all_default-ssp-fix.patch
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-02-02 21:08 UTC by Nick Bowler
Modified: 2018-05-20 14:53 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Updated patch (13_all_default-ssp-fix.patch,1.29 KB, patch)
2018-03-02 15:53 UTC, Magnus Granberg
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Bowler 2018-02-02 21:08:19 UTC
For some reason, -ffreestanding compilation is behaving weirdly on all
recent gcc packages from Gentoo (every version I tested that is gcc-6
or newer.  gcc-5.4.0 works as expected).  Normally, when using this option,
gcc will pre-#define the macro __STDC_HOSTED__ to the value 0 (it is 1
in hosted compilations).  This is documented behaviour and the gcc-
provided standard header files contain conditional code that depends
on this.

However, on Gentoo, if -ffreestanding is combined with -nostdlib, the
following happens:

  - When using preprocessor-only (-E option), everything works correctly.

  - Otherwise, when compiling (including compiling+linking, where the
    -nostdlib option actually makes sense), the __STDC_HOSTED__ macro
    ends up being defined incorrectly, with the value 1.  This typically
    causes gcc's own header files to miscompile, or whatever else that
    depends on __STDC_HOSTED__.

I file this here because it seems to be a Gentoo-specific bug.  If I build
from the upstream gcc packages I do not have this problem at all, and I
also do not have this problem with the gcc packages on Debian.

Example source:

  % cat >test.c <<'EOF'
  #if __STDC_HOSTED__ == 1
  #  error wtf hosted
  #else
  #  warning zomg freestanding
  #endif
EOF

  % gcc -v
  gcc version 6.4.0 (Gentoo 6.4.0-r1 p1.3)

hosted mode works, #error is expected:

  % gcc test.c
  test.c:2:2: error: #error wtf hosted

freestanding mode works, (link subsequently fails due to nostdlib but
this is expected for the example):

  % gcc -ffreestanding test.c
  test.c:4:2: warning: #warning zomg freestanding [-Wcpp]

freestanding+nostdlib, should work but doesn't:

  % gcc -ffreestanding -nostdlib test.c
  test.c:2:2: error: #error wtf hosted

But things magically fix themselves when running the preprocessor only
(which is fun because the issue is in a macro definition):

  % gcc -ffreestanding -nostdlib -E test.c
  test.c:4:2: warning: #warning zomg freestanding [-Wcpp]

Portage 2.3.19 (python 3.5.4-final-0, default/linux/amd64/17.0, gcc-6.4.0, glibc-2.25-r9, 4.14.14 x86_64)
=================================================================
System uname: Linux-4.14.14-x86_64-Intel-R-_Core-TM-_i3-2120T_CPU_@_2.60GHz-with-gentoo-2.4.1
KiB Mem:    16126244 total,   2099616 free
KiB Swap:          0 total,         0 free
Timestamp of repository gentoo: Mon, 29 Jan 2018 00:45:01 +0000
sh dash 0.5.9.1-r3
ld GNU ld (Gentoo 2.29.1 p3) 2.29.1
ccache version 3.2.4 [disabled]
app-shells/bash:          4.4_p12::gentoo
dev-lang/perl:            5.24.3::gentoo
dev-lang/python:          2.7.14-r1::gentoo, 3.5.4-r1::gentoo
dev-util/ccache:          3.2.4::gentoo
dev-util/cmake:           3.9.6::gentoo
sys-apps/baselayout:      2.4.1-r2::gentoo
sys-apps/openrc:          0.34.11::gentoo
sys-apps/sandbox:         2.12::gentoo
sys-devel/autoconf:       2.13::gentoo, 2.69-r4::gentoo
sys-devel/automake:       1.13.4::gentoo, 1.15.1-r1::gentoo
sys-devel/binutils:       2.29.1-r1::gentoo
sys-devel/gcc:            6.4.0-r1::gentoo
sys-devel/gcc-config:     1.8-r1::gentoo
sys-devel/libtool:        2.4.6-r3::gentoo
sys-devel/make:           4.2.1::gentoo
sys-kernel/linux-headers: 4.13::gentoo (virtual/os-headers)
sys-libs/glibc:           2.25-r9::gentoo
Repositories:

gentoo
    location: /srv/repos/gentoo
    sync-type: null
    sync-uri: rsync://rsync.gentoo.org/gentoo-portage
    priority: -1000

gentoo-draconx
    location: /srv/repos/gentoo-draconx
    masters: gentoo

gentoo-fixes
    location: /srv/repos/gentoo-fixes
    masters: gentoo

ACCEPT_KEYWORDS="amd64"
ACCEPT_LICENSE="@FREE @OTHER-FREE"
CBUILD="x86_64-pc-linux-gnu"
CFLAGS="-Os -march=sandybridge -pipe"
CHOST="x86_64-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/share/gnupg/qualified.txt"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo"
CXXFLAGS="-Os -march=sandybridge -pipe"
DISTDIR="/srv/repos/gentoo/distfiles"
EMERGE_DEFAULT_OPTS="-j3 --keep-going --autounmask-write=n --unordered-display --dynamic-deps=n --verbose-conflicts --binpkg-respect-use=y --with-bdeps-auto=n"
FCFLAGS="-O2 -pipe"
FEATURES="assume-digests binpkg-logs binpkg-multi-instance config-protect-if-modified distlocks ebuild-locks fixlafiles merge-sync multilib-strict news parallel-fetch preserve-libs protect-owned sandbox sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch userpriv usersandbox usersync xattr"
FFLAGS="-O2 -pipe"
GENTOO_MIRRORS="http://distfiles.gentoo.org"
INSTALL_MASK="/usr/share/cursors/xorg-x11/default 	/usr/share/alsa/alsa.conf.d/51-pulseaudio-probe.conf 	/etc/portage/*postsync.d 	/etc/profile.d/qtgui4.sh 	/etc/gssproxy/??-*.conf 	/usr/share/gtk-doc 	/usr/share/doc 	/usr/lib*/ghc-*/**/lib*_p.a 	/usr/lib*/ghc-*/**/*.p_hi"
LANG="en_CA.UTF-8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
LINGUAS="en_CA en ja"
MAKEOPTS="-j4"
PKGDIR="/var/cache/portage/packages"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --omit-dir-times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages --exclude=/.git"
PORTAGE_TMPDIR="/var/tmp"
USE="X acl alsa amd64 berkdb bs2b bzip2 cairo cjk cli crypt cups curl cxx djvu dri exif fbcon ffmpeg flac fontconfig fontforge fortran gdbm gif gpg gtk iconv icu idn ipv6 jpeg jpeg2k kerberos libass mad mikmod mmx mmxext mod modules mp3 mp4 multilib ncurses nls nptl ogg opengl openmp opus pam pango pcre perl png python readline sdl seccomp sse sse2 sse3 ssl ssse3 svg threads truetype unicode vaapi vim-syntax vorbis xattr xft xinerama zlib" ABI_X86="64" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="kexi words flow plan sheets stage tables krita karbon braindump author" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" CPU_FLAGS_X86="avx mmx mmxext popcnt sse sse2 sse3 sse4_1 sse4_2 ssse3" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock isync itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf skytraq superstar2 timing tsip tripmate tnt ublox ubx" GRUB_PLATFORMS="efi-64" INPUT_DEVICES="evdev joystick" KERNEL="linux" L10N="en-CA en ja ko" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIBREOFFICE_EXTENSIONS="presenter-console presenter-minimizer" OFFICE_IMPLEMENTATION="libreoffice" PHP_TARGETS="php5-6 php7-0" POSTGRES_TARGETS="postgres9_5" PYTHON_SINGLE_TARGET="python3_5" PYTHON_TARGETS="python2_7 python3_5" RUBY_TARGETS="ruby22 ruby23" USERLAND="GNU" VIDEO_CARDS="intel fbdev" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account"
Unset:  CC, CPPFLAGS, CTARGET, CXX, LC_ALL, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS
Comment 1 Felix Janda 2018-02-03 01:11:13 UTC
I think the problem is with 13_all_default-ssp-fix.patch, especially
the part

--- a/gcc/c-family/c-opts.c	2016-03-08 23:30:44.000000000 +0100
+++ b/gcc/c-family/c-opts.c	2016-09-18 18:42:10.231644014 +0200
@@ -436,7 +436,15 @@ c_common_handle_option (size_t scode, co
 
     case OPT_ffreestanding:
       value = !value;
+	  if (global_options.x_flag_stack_protect == -1)
+		global_options.x_flag_stack_protect = 0;
       /* Fall through....  */
+
+	case OPT_nostdlib:
+	  if (global_options.x_flag_stack_protect == -1)
+		global_options.x_flag_stack_protect = 0;
+	  /* Fall through....  */
+
     case OPT_fhosted:
       flag_hosted = value;
       flag_no_builtin = !value;


The fall-throughs don't make any sense to me: If -ffreestanding is
given, disable the stack-protector twice. If -nostdlib is given, set
-fhosted and -fno-builtin. (Note that -fhosted is the opposite of
-ffreestanding.)
Comment 2 Nick Bowler 2018-02-21 20:51:59 UTC
(In reply to Felix Janda from comment #1)
> I think the problem is with 13_all_default-ssp-fix.patch, especially
> the part
> 
> --- a/gcc/c-family/c-opts.c	2016-03-08 23:30:44.000000000 +0100
> +++ b/gcc/c-family/c-opts.c	2016-09-18 18:42:10.231644014 +0200
> @@ -436,7 +436,15 @@ c_common_handle_option (size_t scode, co
>  
>      case OPT_ffreestanding:
>        value = !value;
> +	  if (global_options.x_flag_stack_protect == -1)
> +		global_options.x_flag_stack_protect = 0;
>        /* Fall through....  */
> +
> +	case OPT_nostdlib:
> +	  if (global_options.x_flag_stack_protect == -1)
> +		global_options.x_flag_stack_protect = 0;
> +	  /* Fall through....  */
> +
>      case OPT_fhosted:
>        flag_hosted = value;
>        flag_no_builtin = !value;

Looks like a smoking gun to me.  I can confirm that reverting this
patch resolves the issue.
Comment 3 Sergei Trofimovich (RETIRED) gentoo-dev 2018-02-21 21:31:19 UTC
Good catch! zorry@, can you have a look at it?
Comment 4 Magnus Granberg gentoo-dev 2018-03-02 15:53:13 UTC
Created attachment 521848 [details, diff]
Updated patch

Test this patch if it fix the problem.
Comment 5 Nick Bowler 2018-05-18 17:19:48 UTC
(In reply to Magnus Granberg from comment #4)
> Created attachment 521848 [details, diff] [details, diff]
> Updated patch
> 
> Test this patch if it fix the problem.

Sorry for the delay.  I'm not sure exactly what you want me to test?

If I revert 13_all_default-ssp-fix.patch on 6.4.0-r1 then _this_ issue
is fixed because the problematic code is removed.

If I subsequently apply your patch on top of _that_ then this issue remains
fixed (not surprising, since the problematic code is still removed).

However, since I don't know what problem either patch is actually
intended to fix I don't know how to test that they actually do anything
useful...
Comment 6 Larry the Git Cow gentoo-dev 2018-05-19 09:12:40 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=4fe06e8fdf07b4b0dea14de464754dcd178d5d8c

commit 4fe06e8fdf07b4b0dea14de464754dcd178d5d8c
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2018-05-18 22:22:19 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2018-05-19 09:12:34 +0000

    sys-devel/gcc: bump 6.4.0 patchset up to 1.4
    
    1 update:
    - 13_all_default-ssp-fix.patch : fix breakage of -ffreestanding
    
    Not assigning any keywords as there is not many
    changes compares to previous ebuild.
    
    Bug: https://bugs.gentoo.org/646440
    Package-Manager: Portage-2.3.38, Repoman-2.3.9

 sys-devel/gcc/Manifest            |  1 +
 sys-devel/gcc/gcc-6.4.0-r2.ebuild | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=a552a2cca077a1d90b1d06c4d97e8aabcd0a3666

commit a552a2cca077a1d90b1d06c4d97e8aabcd0a3666
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2018-05-18 21:51:37 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2018-05-19 09:12:30 +0000

    sys-devel/gcc: bump 7.3.0 patchset up to 1.4
    
    1 new patch, 2 updates::
    - 13_all_default-ssp-fix.patch : fix breakage of -ffreestanding
    - 55_all_extra-options.patch : refrest -fstack-check fixes
    - 93_all_copy-constructible-fix.patch : fix -O2 enable_if selection
    
    Bug: https://bugs.gentoo.org/646440
    Package-Manager: Portage-2.3.38, Repoman-2.3.9

 sys-devel/gcc/Manifest                                     | 2 +-
 sys-devel/gcc/{gcc-7.3.0-r2.ebuild => gcc-7.3.0-r3.ebuild} | 5 ++---
 2 files changed, 3 insertions(+), 4 deletions(-)
Comment 7 Magnus Granberg gentoo-dev 2018-05-20 14:53:05 UTC
fixed in new revision of the patchset for gcc