https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128684
Bug ID: 128684
Summary: powerpc: gcc 16.2.0 fails to generate exit condition
from loop
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: mark.cave-ayland at ilande dot co.uk
Target Milestone: ---
Created attachment 66145
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66145&action=edit
zip file containing the example files above
Whilst testing with Fedora 44 (gcc 16.2.0), a bug has been discovered whereby
the powerpc binaries for OpenBIOS are being generated incorrectly such that the
generated code enters an infinite loop during startup.
The code itself looks like this:
static void patch_rfi_bad(void)
{
uint32_t* ptr;
uint32_t* vec_start = (uint32_t*) 0x100ULL;
uint32_t* vec_end = (uint32_t*) (__vectors_end - __vectors);
for (ptr = vec_start; ptr != vec_end; ptr++) {
if (*ptr == RFI)
*ptr = RFID;
}
}
What happens at startup is that OpenBIOS takes the vector table at 0xffff0000,
relocates it down to 0x0 and then if it detects a 64-bit CPU, patches all
instances of rfi to rfid using the function above.
Problem
=======
With gcc 16.2.0 the generated code for the for() loop above contains no exit
condition, causing an infinite loop. In my local test case here the generated
assembly for patch_rfi_bad() looks like this:
fff0104c: 3d 40 ff f0 lis r10,-16
fff01050: 39 4a 03 00 addi r10,r10,768
fff01054: 3d 20 ff f0 lis r9,-16
fff01058: 39 29 00 00 addi r9,r9,0
fff0105c: 7d 29 50 50 subf r9,r9,r10
fff01060: 2c 09 01 00 cmpwi r9,256
fff01064: 4d 82 00 20 beqlr
fff01068: 39 20 01 00 li r9,256
fff0106c: 3d 00 4c 00 lis r8,19456
fff01070: 61 08 00 64 ori r8,r8,100
fff01074: 3c e0 4c 00 lis r7,19456
fff01078: 60 e7 00 24 ori r7,r7,36
fff0107c: 48 00 00 0c b fff01088 <setup_mmu+0x88>
fff01080: 90 e9 00 00 stw r7,0(r9)
fff01084: 39 29 00 04 addi r9,r9,4
fff01088: 81 49 00 00 lwz r10,0(r9)
fff0108c: 7c 0a 40 00 cmpw r10,r8
fff01090: 40 82 ff f4 bne fff01084 <setup_mmu+0x84>
fff01094: 4b ff ff ec b fff01080 <setup_mmu+0x80>
Here we see that the loop gets stuck between fff01084 and fff01094. Compare
this with a working version patch_rfi_good() which looks like this:
static void patch_rfi_good(void)
{
uint32_t* ptr;
uint32_t* vec_start = (uint32_t*) 0x100ULL;
uint32_t* vec_end = (uint32_t*) 0x300ULL;
for (ptr = vec_start; ptr != vec_end; ptr++) {
if (*ptr == RFI)
*ptr = RFID;
}
}
and its corresponding assembly:
fff01000: 39 20 01 00 li r9,256
fff01004: 3d 00 4c 00 lis r8,19456
fff01008: 61 08 00 64 ori r8,r8,100
fff0100c: 3c e0 4c 00 lis r7,19456
fff01010: 60 e7 00 24 ori r7,r7,36
fff01014: 39 40 00 80 li r10,128
fff01018: 7d 49 03 a6 mtctr r10
fff0101c: 48 00 00 10 b fff0102c <setup_mmu+0x2c>
fff01020: 90 e9 00 00 stw r7,0(r9)
fff01024: 39 29 00 04 addi r9,r9,4
fff01028: 42 40 00 14 bdz fff0103c <setup_mmu+0x3c>
fff0102c: 81 49 00 00 lwz r10,0(r9)
fff01030: 7c 0a 40 00 cmpw r10,r8
fff01034: 40 82 ff f0 bne fff01024 <setup_mmu+0x24>
fff01038: 4b ff ff e8 b fff01020 <setup_mmu+0x20>
The difference here is that ctr is loaded at fff01018 and the bdz instruction
at fff01028 decrements ctr and takes the branch to exit the loop when ctr
reaches zero.
Analysis
========
The issue seems to be related to the use of arithmetic with the __vectors_end
and __vectors globals to calculate vec_end. patch_rfi_good() hardcodes vec_end
with its numeric constant value which appears to solve the issue.
As a reference point my local working OpenBIOS builds which still use
patch_rfi_bad() are generated with gcc 14.2.0 (Debian).
Reproducer
==========
The following files are included in a zip file attached to this bug report:
- test.c: C program containing patch_rfi_bad() and patch_rfi_good()
- testasm.s: PPC asm code containing the vector table and its globals
- ldscript: a linker script that allows for testing in QEMU
- build.sh: a simple script to build the reproducer