This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

target/3959: Inline asm bug, when ebp is marked as clobbered.



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]