Wrong code generated with -fstack-protector and -msingle-pic-base
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_
.L7:
.align 2
.L6:
.word __stack_
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_
.L7:
.align 2
.L6:
.word _GLOBAL_
.word __stack_
.word _GLOBAL_
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_