From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15791 invoked by alias); 19 Mar 2002 17:36:08 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 15737 invoked by uid 71); 19 Mar 2002 17:36:04 -0000 Resent-Date: 19 Mar 2002 17:36:04 -0000 Resent-Message-ID: <20020319173604.15736.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-To: nobody@gcc.gnu.org Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, Daniel.Diaz@univ-paris1.fr Received:(qmail 11459 invoked by uid 61); 19 Mar 2002 17:29:27 -0000 Message-Id:<20020319172927.11457.qmail@sources.redhat.com> Date: Tue, 19 Mar 2002 09:36:00 -0000 From: Daniel.Diaz@univ-paris1.fr Reply-To: Daniel.Diaz@univ-paris1.fr To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version:gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: optimization/6004: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp X-SW-Source: 2002-03/txt/msg00723.txt.bz2 List-Id: >Number: 6004 >Category: optimization >Synopsis: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Tue Mar 19 09:36:01 PST 2002 >Closed-Date: >Last-Modified: >Originator: Daniel Diaz >Release: gcc version 3.0.4 (Red Hat Linux 7.2 3.0.4-1) >Organization: >Environment: RedHat Linux 7.2 (kernel 2.4.7-10) on a Pentium IV 1.7Ghz with 512Mb >Description: I use ebp as a global register. I inform gcc with -ffixed-ebp and also I compile with -ffomit-frame-pointer (since else ebp is used for the frame). Everything works well except for the main() function which always uses a frame and thus refers its arguments from ebp (and not from esp). Since ebp is used by my global variable the arguments are not well get by main(). Here is the file x.c (also as an attachment): register int x asm("%ebp"); main(int argc) { foo(argc); printf("Inside main() argc = %d\n", argc); } foo(int argc) { printf("Inside foo() argc = %d\n", argc); x = 100; } Compite it with: gcc -O2 -fomit-frame-pointer -ffixed-ebp x.c at execution: $ ./a.out Inside foo() argc = 1 Segmentation fault If we look at the assembly code (adding -S): gcc -O2 -fomit-frame-pointer -ffixed-ebp x.c -S we obtain: .file "x.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "Inside main() argc = %d\n" .text .align 4 .globl main .type main,@function main: pushl %ebp movl %esp, %ebp pushl %ebx pushl %ecx andl $-16, %esp movl 8(%ebp), %ebx subl $12, %esp pushl %ebx call foo popl %eax popl %edx pushl %ebx pushl $.LC0 call printf movl -4(%ebp), %ebx leave ret .Lfe1: .size main,.Lfe1-main .section .rodata.str1.1,"aMS",@progbits,1 .LC1: .string "Inside foo() argc = %d\n" .text .align 4 .globl foo .type foo,@function foo: subl $20, %esp pushl 24(%esp) pushl $.LC1 call printf movl $100, %ebp addl $28, %esp ret .Lfe2: .size foo,.Lfe2-foo .ident "GCC: (GNU) 3.0.4 (Red Hat Linux 7.2 3.0.4-1)" we can see that the main() uses ebp to save esp creating a fram and then (after the call to foo()) uses ebp to pass argc to printf(). But ebp has been set to 100 by foo() when initializing x to 100. Obviously when main() camms printf(), argc is not well passed and a segmentation violation occurs. This is a serious problem for the GNU Prolog compiler which uses ebp to store a global Prolog abstract machine register. *** This is a new behavior in gcc 3.0.4 (and surely in 3.0.x). *** *** This DID/DOES NOT occur in 2.96 20000731. *** To summarize: gcc 3.0.4 ALWAYS uses a frame with ebp for amin() even in the presence of -fomit-frame-pointer -ffixed-ebp. A smaller example: let y.c the following file: main() { } foo() { } compile it with: gcc -O2 -fomit-frame-pointer -ffixed-ebp y.c -S Here is y.s .file "y.c" .text .align 4 .globl main .type main,@function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp leave ret .Lfe1: .size main,.Lfe1-main .align 4 .globl foo .type foo,@function foo: ret .Lfe2: .size foo,.Lfe2-foo .ident "GCC: (GNU) 3.0.4 (Red Hat Linux 7.2 3.0.4-1)" So main() is NOT treated as foo() (or other functions) and uses frame+ebp whatever we declare ! >How-To-Repeat: gcc -O2 -fomit-frame-pointer -ffixed-ebp x.c >Fix: >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: application/octet-stream; name="x.c" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="x.c" cmVnaXN0ZXIgaW50IHggYXNtKCIlZWJwIik7CgptYWluKGludCBhcmdjKQp7CiAgZm9vKGFyZ2Mp OwogIHByaW50ZigiSW5zaWRlIG1haW4oKSBhcmdjID0gJWRcbiIsIGFyZ2MpOwp9Cgpmb28oaW50 IGFyZ2MpCnsKICBwcmludGYoIkluc2lkZSBmb28oKSBhcmdjID0gJWRcbiIsIGFyZ2MpOwogIHgg PSAxMDA7Cn0KCg==