Bug 28712 - GCC 12 miscompiles PIE and PIC elf/dl-tunables.c
Summary: GCC 12 miscompiles PIE and PIC elf/dl-tunables.c
Status: RESOLVED MOVED
Alias: None
Product: glibc
Classification: Unclassified
Component: build (show other bugs)
Version: 2.35
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-12-18 15:26 UTC by H.J. Lu
Modified: 2021-12-27 13:42 UTC (History)
2 users (show)

See Also:
Host:
Target: x86-64
Build:
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-12-18 15:26:01 UTC
GCC 12 miscompiles elf/dl-tunables.o:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103762

Good __tunables_init code:

.L35:
        movq    $-88, %rax 
        leaq    tunable_list(%rip), %rbx 
        movq    %r8, %r12 
        subq    %rbx, %rax 
        movq    %rax, %r15 

Bad __tunables_init code:

.L34:
        movq    $-88, %rax 
        subq    .LC3(%rip), %rax 
        movq    %r8, %rbx 
        movq    %rax, 8(%rsp)
...
        .section        .data.rel.ro.local,"aw"
        .align 8
.LC3:
        .quad   tunable_list

The bad code requires run-time relocation and a memory load.  But this piece
of code is called from

  __tunables_init (__environ);

  ARCH_INIT_CPU_FEATURES (); 

  /* Do static pie self relocation after tunables and cpu features
     are setup for ifunc resolvers. Before this point relocations
     must be avoided.  */
  _dl_relocate_static_pie (); 

__tunables_init fails to run before static PIE has been relocated.
Comment 1 H.J. Lu 2021-12-18 16:34:07 UTC
dl-tunables.os is also miscompiled:

Relocation section '.rela.data.rel.ro.local' at offset 0x4178 contains 1 entry:
    Offset             Info             Type               Symbol's Value  Symbo
l's Name + Addend
0000000000000000  0000000700000001 R_X86_64_64            0000000000000000 .data
.rel.ro + 0

This also requires run-time relocation.
Comment 2 Florian Weimer 2021-12-18 22:45:35 UTC
This GCC bug essentially makes x86-64 a !PI_STATIC_AND_HIDDEN target. This implies changes far beyond that.
Comment 3 H.J. Lu 2021-12-27 13:42:33 UTC
Moved to

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103762