public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* target/3959: Inline asm bug, when ebp is marked as clobbered.
@ 2001-08-08  5:36 mkoerner
  0 siblings, 0 replies; only message in thread
From: mkoerner @ 2001-08-08  5:36 UTC (permalink / raw)
  To: gcc-gnats

>Number:         3959
>Category:       target
>Synopsis:       Inline asm generates bad code on i386 when register ebp is clobbered.
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed Aug 08 05:36:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Mathias Koerner
>Release:        3.0
>Organization:
ETHZ
>Environment:
System: Linux sphinx 2.2.14-SMP #3 SMP Fri Jun 30 15:24:40 CEST 2000 i686 unknown
Architecture: i686

host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /scratch/local-1/gcc-3.0/configure --prefix=/usr/local/gcc-3.0
>Description:
The executable generated by compiling the following code produces
a segmentation fault:

int main() {
  int n;
  n = 5;
  asm ("movl %0,%%ebp" : : "g"(n) : "ebp" );
  return 0;
}

This happens, because the ebp register is used by gcc after
the asm instruction, before it is restored properly from the stack.

>How-To-Repeat:
Compile the program above and run it. The preprocessed code is:

# 1 "br2.cpp"
int main() {
  int n;
  n = 5;
  asm ("movl %0,%%ebp" : : "g"(n) : "ebp" );
  return 0;
}

>Fix:
The critical part of the assembler file generated from the code above is:

.LFB1:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        subl    $4, %esp
.LCFI2:
        movl    $5, -4(%ebp)
#APP
        movl -4(%ebp),%ebp
#NO_APP
        movl    $0, %eax
        movl    %ebp, %esp
        popl    %ebp

The critical line here is "movl %ebp, %esp" after #NO_APP. %ebp has been 
marked clobbered and actually clobbered. A solution would be to push
%ebp immediately before #APP and pop it after #NO_APP (the code works
properly, when I write:

#APP
	pushl %ebp
	movl -4(%ebp),%ebp
	popl %ebp
#NO_APP

I have looked through the gcc documentation. I did not find any note,
that one cannot clobber ebp. I'm not an assembler expert, so it might well
be that clobbering ebp is "insane", but it would be nice, if there was
a hint in the documentation, or if gcc would warn that clobbering ebp is
not a good thing to do.

I finally hope that this bug report is useful and would like to
thank the people looking at the report for the work they have done/
are doing.
>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-08-08  5:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-08  5:36 target/3959: Inline asm bug, when ebp is marked as clobbered mkoerner

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