public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: Daniel.Diaz@univ-paris1.fr
To: gcc-gnats@gcc.gnu.org
Subject: optimization/6004: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp
Date: Tue, 19 Mar 2002 09:36:00 -0000	[thread overview]
Message-ID: <20020319172927.11457.qmail@sources.redhat.com> (raw)


>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==


             reply	other threads:[~2002-03-19 17:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-19  9:36 Daniel.Diaz [this message]
2002-03-20  1:57 jakub
2002-04-03  2:36 rth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020319172927.11457.qmail@sources.redhat.com \
    --to=daniel.diaz@univ-paris1.fr \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).