public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* optimization/6004: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp
@ 2002-03-19  9:36 Daniel.Diaz
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel.Diaz @ 2002-03-19  9:36 UTC (permalink / raw)
  To: gcc-gnats


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: optimization/6004: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp
@ 2002-04-03  2:36 rth
  0 siblings, 0 replies; 3+ messages in thread
From: rth @ 2002-04-03  2:36 UTC (permalink / raw)
  To: Daniel.Diaz, gcc-bugs, gcc-prs, nobody

Synopsis: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp

State-Changed-From-To: analyzed->closed
State-Changed-By: rth
State-Changed-When: Wed Apr  3 02:36:04 2002
State-Changed-Why:
    Correct analysis.  One cannot absolutely prevent the
    compiler from using the hard frame pointer.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6004


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: optimization/6004: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp
@ 2002-03-20  1:57 jakub
  0 siblings, 0 replies; 3+ messages in thread
From: jakub @ 2002-03-20  1:57 UTC (permalink / raw)
  To: Daniel.Diaz, gcc-bugs, gcc-prs, nobody

Synopsis: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp

State-Changed-From-To: open->analyzed
State-Changed-By: jakub
State-Changed-When: Wed Mar 20 01:57:57 2002
State-Changed-Why:
    -fomit-frame-pointer documentation sais:
    Don't keep the frame pointer in a register for functions that
    don't need one, ...
    which means the compiler makes no guarantees it will be
    actually eliminated.
    Particularly main if it aligns the stack pointer.
    You might try
    gcc -O2 -fomit-frame-pointer -ffixed-ebp -mpreferred-stack-boundary=2 x.c
    for the .c file containing main.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6004


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-04-03 10:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-19  9:36 optimization/6004: gcc 3.0.4 always compiles main() with frame (and ebp) even with -fomit-frame-pointer and -ffixed-ebp Daniel.Diaz
2002-03-20  1:57 jakub
2002-04-03  2:36 rth

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).