Bug 96190 - --enable-default-pie on sparc uses crtbeginS.o (correct) and crtend.o (incorrect)
Summary: --enable-default-pie on sparc uses crtbeginS.o (correct) and crtend.o (incorr...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: 10.3
Assignee: Sergei Trofimovich
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-13 23:12 UTC by Sergei Trofimovich
Modified: 2023-01-14 17:14 UTC (History)
3 users (show)

See Also:
Host:
Target: sparc
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2020-07-13 23:12:09 UTC
The side-effect of the crtbeginS.o/crtend.o mismatch are:
- R_SPARC_NONE relocations in final executables: https://sourceware.org/PR26236
- TEXTRELs in binaries

Simple reproducer:

$ echo 'int main(){}' | sparc-unknown-linux-gnu-gcc -x c - -o a
/usr/libexec/gcc/sparc-unknown-linux-gnu/ld: warning: creating a DT_TEXTREL in object
$ echo 'int main(){}' | sparc-unknown-linux-gnu-gcc -x c - -o a -no-pie
$ echo 'int main(){}' | sparc-unknown-linux-gnu-gcc -x c - -o a -pie

gcc is built as:

"""
Using built-in specs.
COLLECT_GCC=/usr/bin/sparc-unknown-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/sparc-unknown-linux-gnu/10.1.0/lto-wrapper
Target: sparc-unknown-linux-gnu
Configured with: /tmp/portage-tmpdir/portage/cross-sparc-unknown-linux-gnu/gcc-10.1.0-r2/work/gcc-10.1.0/configure --host=x86_64-pc-linux-gnu --target=sparc-unknown-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/sparc-unknown-linux-gnu/gcc-bin/10.1.0 --includedir=/usr/lib/gcc/sparc-unknown-linux-gnu/10.1.0/include --datadir=/usr/share/gcc-data/sparc-unknown-linux-gnu/10.1.0 --mandir=/usr/share/gcc-data/sparc-unknown-linux-gnu/10.1.0/man --infodir=/usr/share/gcc-data/sparc-unknown-linux-gnu/10.1.0/info --with-gxx-include-dir=/usr/lib/gcc/sparc-unknown-linux-gnu/10.1.0/include/g++-v10 --with-python-dir=/share/gcc-data/sparc-unknown-linux-gnu/10.1.0/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 10.1.0-r2 p3' --disable-esp --enable-libstdcxx-time --enable-poison-system-directories --with-sysroot=/usr/sparc-unknown-linux-gnu --disable-bootstrap --enable-__cxa_atexit --enable-clocale=gnu --disable-multilib --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libmudflap --disable-libssp --disable-libada --disable-systemtap --enable-vtable-verify --without-zstd --enable-lto --without-isl --enable-default-pie --enable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.1.0 (Gentoo 10.1.0-r2 p3)
"""

I think the bug here is in SPEC files where crtendS.o is only pulled by explicit options:

  gcc/config/sparc/linux.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s\
  gcc/config/sparc/linux64.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s\

AFAIu it should be something similar to generic case:

  gcc/config/gnu-user.h:     shared|static-pie|" PIE_SPEC ":crtendS.o%s; \

Does it sound about right? Should other targets follow as well? Specifically:

gcc/config/alpha/linux.h:   crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/alpha/linux.h:   crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/alpha/linux.h:   %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"

gcc/config/bfin/linux.h:   crti.o%s %{shared|pie:crtbeginS.o%s;:crtbegin.o%s}"

gcc/config/c6x/uclinux-elf.h:  "%{!shared:crt1%O%s} crti%O%s %{shared|pie:crtbeginS.o%s;:crtbegin.o%s}"

gcc/config/csky/csky-linux-elf.h:  crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/csky/csky-linux-elf.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"

gcc/config/frv/linux.h:   crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/frv/linux.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"

gcc/config/ia64/linux.h:   crti.o%s %{shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/ia64/linux.h:   crti.o%s %{shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/ia64/linux.h:   %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"

gcc/config/m32r/linux.h:   crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}"
gcc/config/m32r/linux.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"

gcc/config/sparc/linux.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s\
gcc/config/sparc/linux64.h:  "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s\
Comment 1 Sergei Trofimovich 2020-07-14 07:02:22 UTC
Sent https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549983.html for review. If it's a reasonable thing to do I'll convert the rest of targets.
Comment 2 GCC Commits 2020-07-20 19:47:47 UTC
The master branch has been updated by Sergei Trofimovich <slyfox@gcc.gnu.org>:

https://gcc.gnu.org/g:87891d5eafe8d1de90b9d9b056eca81c508d1c77

commit r11-2235-g87891d5eafe8d1de90b9d9b056eca81c508d1c77
Author: Sergei Trofimovich <siarheit@google.com>
Date:   Tue Jul 14 07:48:42 2020 +0100

    sparc/sparc64: use crtendS.o for default-pie executables [PR96190]
    
    In --enable-default-pie mode compiler should switch from
    using crtend.o to crtendS.o. On sparc it is especially important
    because crtend.o contains PIC-unfriendly code.
    
    We use GNU_USER_TARGET_ENDFILE_SPEC as a baseline spec to get
    crtendS.o instead of crtend.o in !no-pie mode.
    
    gcc:
    
    2020-07-14  Sergei Trofimovich  <siarheit@google.com>
    
            PR target/96190
            * config/sparc/linux.h (ENDFILE_SPEC): Use GNU_USER_TARGET_ENDFILE_SPEC
            to get crtendS.o for !no-pie mode.
            * config/sparc/linux64.h (ENDFILE_SPEC): Ditto.
Comment 3 Sergei Trofimovich 2020-07-20 21:48:53 UTC
As suggested by Eric I plan to apply sparc fix from master into gcc-10 branch after 10.2 release cut.
Comment 4 GCC Commits 2020-07-23 23:07:03 UTC
The releases/gcc-10 branch has been updated by Sergei Trofimovich <slyfox@gcc.gnu.org>:

https://gcc.gnu.org/g:ca47325f300462b6f4ea84de2103eef81a4a7684

commit r10-8527-gca47325f300462b6f4ea84de2103eef81a4a7684
Author: Sergei Trofimovich <siarheit@google.com>
Date:   Thu Jul 23 23:58:35 2020 +0100

    sparc/sparc64: use crtendS.o for default-pie executables [PR96190]
    
    In --enable-default-pie mode compiler should switch from
    using crtend.o to crtendS.o. On sparc it is especially important
    because crtend.o contains PIC-unfriendly code.
    
    We use GNU_USER_TARGET_ENDFILE_SPEC as a baseline spec to get
    crtendS.o instead of crtend.o in !no-pie mode.
    
    gcc:
    
    2020-07-14  Sergei Trofimovich  <siarheit@google.com>
    
            PR target/96190
            * config/sparc/linux.h (ENDFILE_SPEC): Use GNU_USER_TARGET_ENDFILE_SPEC
            to get crtendS.o for !no-pie mode.
            * config/sparc/linux64.h (ENDFILE_SPEC): Ditto.
    
    (cherry picked from commit 87891d5eafe8d1de90b9d9b056eca81c508d1c77)
Comment 5 Sergei Trofimovich 2020-07-25 20:12:43 UTC
Should be fixed for sparc for 10.3. Will handle other targets separately.