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

--- Comment #2 from Mark Cave-Ayland <mark.cave-ayland at ilande dot co.uk> ---
Thanks for the links and reading.

My understanding is that the undefined behaviour comes from the fact that both
arrays are considered separate entities, which causes the optimiser to assume
that the loop can never exit. There is also mention that the role of the
optimisation barrier is to cause gcc to "drop" its understanding that the
external symbol is array, and that's what prevents the pointer comparison from
being eliminated.

Taking this on board I attempted use uintptr_t to in effect convert the
pointers to numeric values, and found that this simple diff was enough to make
it work:

diff --git a/arch/ppc/qemu/ofmem.c b/arch/ppc/qemu/ofmem.c
index 3a21d25..1416eae 100644
--- a/arch/ppc/qemu/ofmem.c
+++ b/arch/ppc/qemu/ofmem.c
@@ -493,13 +493,13 @@ extern char __vectors_end[];
 static void patch_rfi(void)
 {
     uint32_t* ptr;
-    uint32_t* vec_start = (uint32_t*) 0x100UL;
-    uint32_t* vec_end = (uint32_t*) (__vectors_end - __vectors);
+    uintptr_t vec_start = 0x100UL;
+    uintptr_t vec_end = (uintptr_t)__vectors_end - (uintptr_t)__vectors;

     if (!is_ppc64())
         return;

-    for (ptr = vec_start; ptr != vec_end; ptr++)  {
+    for (ptr = (uint32_t *)vec_start; ptr != (uint32_t *)vec_end; ptr++)  {
         if (*ptr == RFI)
             *ptr = RFID;
     } 

Is the use of uintptr_t in this way considered a suitable workaround to avoid
the undefined behaviour? Or is there something else I'm still missing?

Reply via email to