http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46762
Summary: gcc crosscompiled for arm optimises away volatile struct member access when -Os Product: gcc Version: 4.5.1 Status: UNCONFIRMED Severity: major Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: do...@cba.si $ cat bug.c struct GPIO { volatile unsigned int IDR; } *GPIO = (struct GPIO *)(0x40010808); volatile unsigned int *GPIO_PTR = (volatile unsigned int *)(0x40010808); static inline int gpio_get_value_ok() { if (*GPIO_PTR & 1) return 1; return 0; } void function_with_no_bug(void) { int timeout = 0x1000000; while (gpio_get_value_ok() == 0) { if (timeout-- == 0) { return; } } } static inline int gpio_get_value_wrong() { if (GPIO->IDR & 1) return 1; return 0; } void function_with_bug(void) { int timeout = 0x1000000; while (gpio_get_value_wrong() == 0) { if (timeout-- == 0) { return; } } } $ arm-none-eabi-gcc -Wall -Os -c bug.c -o bug.o && arm-none-eabi-objdump -xd bug.o bug.o: file format elf32-littlearm bug.o architecture: arm, flags 0x00000011: HAS_RELOC, HAS_SYMS start address 0x00000000 private flags = 5000000: [Version5 EABI] Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000030 00000000 00000000 00000034 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000008 00000000 00000000 00000064 2**2 CONTENTS, ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 0000006c 2**0 ALLOC 3 .comment 0000002b 00000000 00000000 0000006c 2**0 CONTENTS, READONLY 4 .ARM.attributes 00000034 00000000 00000000 00000097 2**0 CONTENTS, READONLY SYMBOL TABLE: 00000000 l df *ABS* 00000000 bug.c 00000000 l d .text 00000000 .text 00000000 l d .data 00000000 .data 00000000 l d .bss 00000000 .bss 00000000 l d .comment 00000000 .comment 00000000 l d .ARM.attributes 00000000 .ARM.attributes 00000000 g F .text 0000002c function_with_no_bug 0000002c g F .text 00000004 function_with_bug 00000004 g O .data 00000004 GPIO 00000000 g O .data 00000004 GPIO_PTR Disassembly of section .text: 00000000 <function_with_no_bug>: 0: e59f3020 ldr r3, [pc, #32] ; 28 <function_with_no_bug+0x28> 4: e5932000 ldr r2, [r3] 8: e3a03401 mov r3, #16777216 ; 0x1000000 c: e5921000 ldr r1, [r2] 10: e3110001 tst r1, #1 14: 112fff1e bxne lr 14: R_ARM_V4BX *ABS* 18: e3530000 cmp r3, #0 1c: 012fff1e bxeq lr 1c: R_ARM_V4BX *ABS* 20: e2433001 sub r3, r3, #1 24: eafffff8 b c <function_with_no_bug+0xc> 28: 00000000 .word 0x00000000 28: R_ARM_ABS32 .data 0000002c <function_with_bug>: 2c: e12fff1e bx lr 2c: R_ARM_V4BX *ABS* $ arm-none-eabi-gcc -v Using built-in specs. COLLECT_GCC=arm-none-eabi-gcc COLLECT_LTO_WRAPPER=/home/domen/toolchains/arm-2010.09/bin/../libexec/gcc/arm-none-eabi/4.5.1/lto-wrapper Target: arm-none-eabi Configured with: /scratch/julian/2010q3-release-eabi-lite/src/gcc-4.5-2010.09/configure --build=i686-pc-linux-gnu --host=i686-pc-linux-gnu --target=arm-none-eabi --enable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-extra-sgxxlite-multilibs --with-gnu-as --with-gnu-ld --with-specs='%{save-temps: -fverbose-asm} -D__CS_SOURCERYGXX_MAJ__=2010 -D__CS_SOURCERYGXX_MIN__=9 -D__CS_SOURCERYGXX_REV__=51 %{O2:%{!fno-remove-local-statics: -fremove-local-statics}} %{O*:%{O|O0|O1|O2|Os:;:%{!fno-remove-local-statics: -fremove-local-statics}}}' --enable-languages=c,c++ --disable-shared --enable-lto --with-newlib --with-pkgversion='Sourcery G++ Lite 2010.09-51' --with-bugurl=https://support.codesourcery.com/GNUToolchain/ --disable-nls --prefix=/opt/codesourcery --with-headers=yes --with-sysroot=/opt/codesourcery/arm-none-eabi --with-build-sysroot=/scratch/julian/2010q3-release-eabi-lite/install/arm-none-eabi --with-gmp=/scratch/julian/2010q3-release-eabi-lite/obj/host-libs-2010.09-51-arm-none-eabi-i686-pc-linux-gnu/usr --with-mpfr=/scratch/julian/2010q3-release-eabi-lite/obj/host-libs-2010.09-51-arm-none-eabi-i686-pc-linux-gnu/usr --with-mpc=/scratch/julian/2010q3-release-eabi-lite/obj/host-libs-2010.09-51-arm-none-eabi-i686-pc-linux-gnu/usr --with-ppl=/scratch/julian/2010q3-release-eabi-lite/obj/host-libs-2010.09-51-arm-none-eabi-i686-pc-linux-gnu/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-cloog=/scratch/julian/2010q3-release-eabi-lite/obj/host-libs-2010.09-51-arm-none-eabi-i686-pc-linux-gnu/usr --with-libelf=/scratch/julian/2010q3-release-eabi-lite/obj/host-libs-2010.09-51-arm-none-eabi-i686-pc-linux-gnu/usr --disable-libgomp --enable-poison-system-directories --with-build-time-tools=/scratch/julian/2010q3-release-eabi-lite/install/arm-none-eabi/bin --with-build-time-tools=/scratch/julian/2010q3-release-eabi-lite/install/arm-none-eabi/bin Thread model: single gcc version 4.5.1 (Sourcery G++ Lite 2010.09-51)