Bug 100896 - --enable-initfini-array should be enabled for cross compiler to Linux
Summary: --enable-initfini-array should be enabled for cross compiler to Linux
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-06-03 19:39 UTC by H.J. Lu
Modified: 2021-11-05 19:00 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-*-* i?86-*-*
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 H.J. Lu 2021-06-03 19:39:45 UTC
--enable-initfini-array should be enabled for cross compiler to
all *-*-linux* and *-*-gnu* targets.
Comment 1 CVS Commits 2021-06-09 12:15:10 UTC
The master branch has been updated by H.J. Lu <hjl@gcc.gnu.org>:

https://gcc.gnu.org/g:13a39886940331149173b25d6ebde0850668d8b9

commit r12-1328-g13a39886940331149173b25d6ebde0850668d8b9
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jun 8 16:09:24 2021 -0700

    Always enable DT_INIT_ARRAY/DT_FINI_ARRAY on Linux
    
    DT_INIT_ARRAY/DT_FINI_ARRAY support was added to glibc 2.1 by
    
    commit fcf70d4114db9ff7923f5dfeb3fea6e2d623e5c2
    Author: Ulrich Drepper <drepper@redhat.com>
    Date:   Sat Jul 24 19:45:13 1999 +0000
    
        Update.
    
        1999-07-24  Ulrich Drepper  <drepper@cygnus.com>
    
                * elf/dl-fini.c: Handle DT_FINI_ARRAY.
                * elf/link.h (struct link_map): Remove l_init_running.  Add l_runcount
                and l_initcount.
                * elf/dl-init.c: Handle DT_INIT_ARRAY.
    ...
    
    and added to binutils 2.12 by
    
    commit e9682144c14fc809af72bd6c0b8c69731d38679c
    Author: H.J. Lu <hjl.tools@gmail.com>
    Date:   Mon Mar 4 20:40:48 2002 +0000
    
        2002-03-04  H.J. Lu <hjl@gnu.org>
    
                * config/obj-elf.c (special_section): Add .init_array,
                .fini_array and .preinit_array.
    
                * config/tc-ia64.h (ELF_TC_SPECIAL_SECTIONS): Remove
                .init_array and .fini_array.
    
    gcc/
    
            PR target/100896
            * config.gcc (gcc_cv_initfini_array): Set to yes for Linux and
            GNU targets.
            * doc/install.texi: Require glibc 2.1 and binutils 2.12 for
            Linux and GNU targets.
Comment 2 H.J. Lu 2021-06-09 12:16:42 UTC
Fixed for GCC 12.
Comment 3 CVS Commits 2021-08-17 13:00:34 UTC
The master branch has been updated by Christophe Lyon <clyon@gcc.gnu.org>:

https://gcc.gnu.org/g:9c560cf23996271ee26dfc4a1d8484b85173cd12

commit r12-2954-g9c560cf23996271ee26dfc4a1d8484b85173cd12
Author: Christophe Lyon <christophe.lyon@foss.st.com>
Date:   Tue Aug 17 12:59:26 2021 +0000

    Do not enable DT_INIT_ARRAY/DT_FINI_ARRAY on uclinuxfdpiceabi
    
    Commit r12-1328 enabled DT_INIT_ARRAY/DT_FINI_ARRAY for all Linux
    targets, but this does not work for arm-none-uclinuxfdpiceabi: it
    makes all the execution tests fail.
    
    This patch restores the original behavior for uclinuxfdpiceabi.
    
    2021-08-12  Christophe Lyon  <christophe.lyon@foss.st.com>
    
            gcc/
            PR target/100896
            * config.gcc (gcc_cv_initfini_array): Leave undefined for
            uclinuxfdpiceabi targets.
Comment 4 Fangrui Song 2021-11-05 18:57:51 UTC
In gcc/acinclude.m4:285, if cross-compiling, (`if test "x${build}" = "x${target}" && test "x${build}" = "x${host}"; then `) will not be taken.

  else
    case "${target}" in
      aarch64*-linux-gnu*)
        # AArch64 postdates glibc support for .init_array/.fini_array,
        # so we don't need the preprocessor test above.
        gcc_cv_initfini_array=yes
        ;;

      *)
        AC_MSG_CHECKING(cross compile... guessing)
        gcc_cv_initfini_array=no
        ;;
    esac
  fi])

On non-aarch64, `gcc_cv_initfini_array=no` will run, `HAVE_INITFINI_ARRAY_SUPPORT` will therefore be 0.

  compilers/powerpc64le-linux-gnu/gcc/gcc/auto-host.h
  1578:#define HAVE_INITFINI_ARRAY_SUPPORT 0
  
  compilers/powerpc64le-linux-gnu/gcc/gcc/config.status
  1257:D["HAVE_INITFINI_ARRAY_SUPPORT"]=" 0"
  
  compilers/powerpc64le-linux-gnu/gcc/gcc/config.log
  6900:| #define HAVE_INITFINI_ARRAY_SUPPORT 0
  7169:| #define HAVE_INITFINI_ARRAY_SUPPORT 0
  7484:| #define HAVE_INITFINI_ARRAY_SUPPORT 0
  8557:#define HAVE_INITFINI_ARRAY_SUPPORT 0

The built GCC will use the legacy .ctors

% many=/tmp/glibc-many
% cat a.c
__attribute__ ((constructor)) static int foo (void) { return 42; }
% /tmp/glibc-many/install/compilers/powerpc64le-linux-gnu/bin/powerpc64le-glibc-linux-gnu-gcc -c a.c && readelf -WS a.o | egrep 'ctors|init_array'
  [ 4] .ctors            PROGBITS        0000000000000000 000070 000008 00  WA  0   0  8
  [ 5] .rela.ctors       RELA            0000000000000000 000218 000018 18   I 10   4  8


---

Noticed the problem when using scripts/build-many-glibcs.py built GCC

(cd ~/Dev/glibc)
scripts/build-many-glibcs.py /tmp/glibc-many checkout --shallow
scripts/build-many-glibcs.py /tmp/glibc-many host-libraries
scripts/build-many-glibcs.py /tmp/glibc-many compilers powerpc64le-linux-gnu --keep all
Comment 5 Fangrui Song 2021-11-05 19:00:46 UTC
Ah, ok, my /tmp/glibc-many/src/gcc is at releases/gcc-11 while the fix is for 12.0?

Anyway, you may want to clean up  gcc/acinclude.m4