------- Comment #2 from devin at freeshell dot org 2006-02-07 17:07 ------- I could reproduce it with that dated build. And it isn't just debian but also the buildroot gcc. Here is the assembly produced from -O3. Following that is the assembly from -O2. What seems to happen is that with -O3 foo() is optimized out and in it's place .LC0 is stored on the stack. Seems fine. But then the original value of "ptr" is used for calling printf instead of the updated value from the stack.
.file "test.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "" .text .p2align 4,,15 .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl $.LC0, (%eax) popl %ebp ret .size foo, .-foo .section .rodata.str1.1 .LC1: .string "%p" .text .p2align 4,,15 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $24, %esp movl 12(%ebp), %eax andl $-16, %esp subl $16, %esp movl 4(%eax), %eax testl %eax, %eax movl %eax, -4(%ebp) je .L8 movl %eax, 4(%esp) movl $.LC1, (%esp) call printf xorl %eax, %eax leave ret .p2align 4,,7 .L8: movl $.LC0, -4(%ebp) movl %eax, 4(%esp) movl $.LC1, (%esp) call printf xorl %eax, %eax leave ret .size main, .-main .ident "GCC: (GNU) 4.0.3 20060128 (prerelease) (Debian 4.0.2-8)" .section .note.GNU-stack,"",@progbits ############## Correct Version ######################### .file "test.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "" .text .p2align 4,,15 .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl $.LC0, (%eax) popl %ebp ret .size foo, .-foo .section .rodata.str1.1 .LC1: .string "%p" .text .p2align 4,,15 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $24, %esp movl 12(%ebp), %eax andl $-16, %esp subl $16, %esp movl 4(%eax), %eax testl %eax, %eax movl %eax, -4(%ebp) je .L8 movl -4(%ebp), %eax movl $.LC1, (%esp) movl %eax, 4(%esp) call printf xorl %eax, %eax leave ret .p2align 4,,7 .L8: leal -4(%ebp), %eax movl %eax, (%esp) call foo movl -4(%ebp), %eax movl $.LC1, (%esp) movl %eax, 4(%esp) call printf xorl %eax, %eax leave ret .size main, .-main .ident "GCC: (GNU) 4.0.3 20060128 (prerelease) (Debian 4.0.2-8)" .section .note.GNU-stack,"",@progbits -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26144