Bug 31676

Summary: Configuring with CC="gcc -march=x86-64-v3" --with-rtld-early-cflags=-march=x86-64 results in linker failure
Product: glibc Reporter: Florian Weimer <fweimer>
Component: dynamic-linkAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: dilfridge, fweimer, hjl.tools, sam, skpgkp1, skpgkp2
Priority: P2 Flags: fweimer: security-
Version: 2.40   
Target Milestone: 2.40   
See Also: https://sourceware.org/bugzilla/show_bug.cgi?id=31429
https://sourceware.org/bugzilla/show_bug.cgi?id=31867
Host: Target:
Build: Last reconfirmed:
Attachments: A patch

Description Florian Weimer 2024-04-23 19:12:41 UTC
The error is:

gcc -march=x86-64-v3   -nostdlib -nostartfiles -shared -o …/build/elf/ld.so.new               \
          -Wl,-z,relro -Wl,-z,defs      \
           \
          …/build/elf/librtld.os -Wl,--version-script=…/build/ld.map                \
          -Wl,-soname=ld-linux-x86-64.so.2
/usr/bin/ld: …/build/elf/librtld.os: in function `init_cpu_features':
…/git/elf/../sysdeps/x86/cpu-features.c:1202: undefined reference to `_dl_runtime_resolve_fxsave'
/usr/bin/ld: …/build/elf/librtld.os: relocation R_X86_64_PC32 against undefined hidden symbol `_dl_runtime_resolve_fxsave' can not be used when making a shared object
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

The  reason is that we build with sysdeps/x86/dl-get-cpu-features.c with the early CFLAGS (so -march=x86-64). This is necessary because the code runs very early. But since

commit befe2d3c4dec8be2cdd01a47132e47bdb7020922
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Feb 28 09:51:14 2024 -0800

    x86-64: Don't use SSE resolvers for ISA level 3 or above
    
    When glibc is built with ISA level 3 or above enabled, SSE resolvers
    aren't available and glibc fails to build:
    
    ld: .../elf/librtld.os: in function `init_cpu_features':
    .../elf/../sysdeps/x86/cpu-features.c:1200:(.text+0x1445f): undefined reference to `_dl_runtime_resolve_fxsave'
    ld: .../elf/librtld.os: relocation R_X86_64_PC32 against undefined hidden symbol `_dl_runtime_resolve_fxsave' can not be used when making a shared object
    /usr/local/bin/ld: final link failed: bad value
    
    For ISA level 3 or above, don't use _dl_runtime_resolve_fxsave nor
    _dl_tlsdesc_dynamic_fxsave.
    
    This fixes BZ #31429.
    Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

the referenced _dl_runtime_resolve_fxsave trampoline does not exist when the rest of the dynamic linker is built for x86-64-v3.
Comment 1 H.J. Lu 2024-04-23 19:23:13 UTC
Does such glibc binary require x86-64-v3 to run?
Comment 2 Florian Weimer 2024-04-23 19:40:12 UTC
(In reply to H.J. Lu from comment #1)
> Does such glibc binary require x86-64-v3 to run?

Yes, but we really want ld.so be able to print an error message on all CPUs, as in “Fatal glibc error: CPU does not support x86-64-v3". See sysdeps/x86/dl-get-cpu-features.c. This requires that we build part of ld.so with -march=x86-64.
Comment 3 H.J. Lu 2024-04-23 20:17:29 UTC
Created attachment 15480 [details]
A patch

Please try this.
Comment 4 skpgkp1 2024-04-23 20:45:28 UTC
I reproduce the issue and try attached patch; it fixes the problem.

Patch looks good to me.
Comment 5 Sourceware Commits 2024-04-24 11:51:43 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=46c999741340ea559784c20a45077955b50aca43

commit 46c999741340ea559784c20a45077955b50aca43
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Apr 23 13:59:50 2024 -0700

    x86: Define MINIMUM_X86_ISA_LEVEL in config.h [BZ #31676]
    
    Define MINIMUM_X86_ISA_LEVEL at configure time to avoid
    
    /usr/bin/ld: â¦/build/elf/librtld.os: in function `init_cpu_features':
    â¦/git/elf/../sysdeps/x86/cpu-features.c:1202: undefined reference to `_dl_runtime_resolve_fxsave'
    /usr/bin/ld: â¦/build/elf/librtld.os: relocation R_X86_64_PC32 against undefined hidden symbol `_dl_runtime_resolve_fxsave' can not be used when making a shared object
    /usr/bin/ld: final link failed: bad value
    collect2: error: ld returned 1 exit status
    
    when glibc is built with -march=x86-64-v3 and configured with
    --with-rtld-early-cflags=-march=x86-64, which is used to allow ld.so to
    print an error message on unsupported CPUs:
    
    Fatal glibc error: CPU does not support x86-64-v3
    
    This fixes BZ #31676.
    Reviewed-by: Sunil K Pandey <skpgkp2@gmail.com>
Comment 6 Sourceware Commits 2024-04-25 11:18:20 UTC
The release/2.39/master branch has been updated by Florian Weimer <fw@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=2f8f157eb0cc7f1d8d9a3fcaa8c55bed53b092a8

commit 2f8f157eb0cc7f1d8d9a3fcaa8c55bed53b092a8
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Apr 23 13:59:50 2024 -0700

    x86: Define MINIMUM_X86_ISA_LEVEL in config.h [BZ #31676]
    
    Define MINIMUM_X86_ISA_LEVEL at configure time to avoid
    
    /usr/bin/ld: â¦/build/elf/librtld.os: in function `init_cpu_features':
    â¦/git/elf/../sysdeps/x86/cpu-features.c:1202: undefined reference to `_dl_runtime_resolve_fxsave'
    /usr/bin/ld: â¦/build/elf/librtld.os: relocation R_X86_64_PC32 against undefined hidden symbol `_dl_runtime_resolve_fxsave' can not be used when making a shared object
    /usr/bin/ld: final link failed: bad value
    collect2: error: ld returned 1 exit status
    
    when glibc is built with -march=x86-64-v3 and configured with
    --with-rtld-early-cflags=-march=x86-64, which is used to allow ld.so to
    print an error message on unsupported CPUs:
    
    Fatal glibc error: CPU does not support x86-64-v3
    
    This fixes BZ #31676.
    Reviewed-by: Sunil K Pandey <skpgkp2@gmail.com>
    
    (cherry picked from commit 46c999741340ea559784c20a45077955b50aca43)
Comment 7 Andreas K. Huettel 2024-06-19 21:49:05 UTC
This is fixed on master / 2.40 then.