Wrong code generated with -fstack-protector and -msingle-pic-base

Bug #1871635 reported by Ilya Oleinik
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
New
Undecided
Unassigned

Bug Description

Compilation of the following piece of code with gcc-9 2019-q4 when -fstack-protector -fpie
-msingle-pic-base are set, uses wrong register for GOT access:

void test() {
  return;
}

Result:

.LFB0:
 @ args = 0, pretend = 0, frame = 8
 @ frame_needed = 0, uses_anonymous_args = 0
 push {lr}
 ldr r3, .L6
 sub sp, sp, #12
 ldr r3, [r2, r3] // r2 is used instead of r9
 ldr r3, [r3]
 str r3, [sp, #4]
 mov r3,#0
 ldr r3, .L6
 ldr r3, [r2, r3]
 ldr r2, [r3]
 ldr r3, [sp, #4]
 eors r2, r3, r2
 bne .L5
 add sp, sp, #12
 @ sp needed
 ldr pc, [sp], #4
.L5:
 bl __stack_chk_fail(PLT)
.L7:
 .align 2
.L6:
 .word __stack_chk_guard(GOT)

Without -msingle-pic-base code is correct:

.LFB0:
 @ args = 0, pretend = 0, frame = 8
 @ frame_needed = 0, uses_anonymous_args = 0
 push {lr}
 ldr r2, .L6 // r2 is initialized with GOT base
 ldr r3, .L6+4
.LPIC0:
 add r2, pc
 ldr r3, [r2, r3]
 sub sp, sp, #12
 ldr r3, [r3]
 str r3, [sp, #4]
 mov r3,#0
 ldr r2, .L6+8
 ldr r3, .L6+4
.LPIC1:
 add r2, pc
 ldr r3, [r2, r3]
 ldr r2, [r3]
 ldr r3, [sp, #4]
 eors r2, r3, r2
 bne .L5
 add sp, sp, #12
 @ sp needed
 ldr pc, [sp], #4
.L5:
 bl __stack_chk_fail(PLT)
.L7:
 .align 2
.L6:
 .word _GLOBAL_OFFSET_TABLE_-(.LPIC0+4)
 .word __stack_chk_guard(GOT)
 .word _GLOBAL_OFFSET_TABLE_-(.LPIC1+4)

Problem is not present on gcc-8 or 7:

.LFB0:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 0, uses_anonymous_args = 0
        push {lr}
        ldr r3, .L6
        ldr r3, [r9, r3] // r9 is used as base (correct)
        sub sp, sp, #12
        ldr r2, [r3]
        str r2, [sp, #4]
        ldr r2, [sp, #4]
        ldr r3, [r3]
        cmp r2, r3
        bne .L5
        add sp, sp, #12
        @ sp needed
        ldr pc, [sp], #4
.L5:
        bl __stack_chk_fail(PLT)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.