public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug inline-asm/39078]  New: Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
@ 2009-02-02 16:54 valery_reznic at yahoo dot com
  2009-02-06  1:43 ` [Bug inline-asm/39078] " pinskia at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: valery_reznic at yahoo dot com @ 2009-02-02 16:54 UTC (permalink / raw)
  To: gcc-bugs

Source file:


file test.c
=======================================================
#define __NR_close 2
#define CLOBBER_LIST "memory", "cc", "r11", "r12", "r13", "r14", "r15", "rbx"
#define MY_SYSCALL_1(ARG)                                                      
      \
        __extension__ ((                                                       
      \
                {                                                              
      \
                        register unsigned long result;                         
      \
                        register unsigned long _arg1 asm("rdi") = (unsigned
long)ARG; \
                        asm volatile (                                         
      \
                                "call my_syscall\n\t"                          
      \
                                : "=a" (result)                                
      \
                                : "0" (__NR_close), "r" (_arg1)                
      \
                                : CLOBBER_LIST                                 
      \
                        );                                                     
      \
                        result;                                                
      \
                } ))                                                           
      \

void func_1(int arg)
{
        MY_SYSCALL_1(arg);
}
================================================================================

Compilation command:

gcc  -O2 -S -o test.S test.c

Output file (test.S)
=====================================================================
        .file   "bb.c"
        .text
        .p2align 4,,15
.globl func_1
        .type   func_1, @function
func_1:
.LFB2:
        movq    %rbx, -40(%rsp)
.LCFI0:
        movq    %r12, -32(%rsp)
.LCFI1:
        movslq  %edi,%rdi
        movq    %r13, -24(%rsp)
.LCFI2:
        movq    %r14, -16(%rsp)
.LCFI3:
        movl    $2, %eax
        movq    %r15, -8(%rsp)
.LCFI4:
#APP
        call my_syscall

#NO_APP
        movq    -40(%rsp), %rbx
        movq    -32(%rsp), %r12
        movq    -24(%rsp), %r13
        movq    -16(%rsp), %r14
        movq    -8(%rsp), %r15
        ret
=====================================================================
All registers mentioned in the CLOBERR_LIST (but r11) are moved to the stack.
Stack pointer doesn't adjusted !!!
Than 'call my_syscall' overwrite on the stack saved value of 'r15'.
Than, future instructions in the 'my_syscall' can overwrite other saved
registers.
After return from 'my_syscall' overwritten values copied to registers.

I.e while compiler tried to save CLOBER_LIST registers' content it failed to do
it.

I tried to compile code both with gcc-3.4.2 (FedoraCore 3) and gcc-4.1.2
(Fedora 8) - results are same.

Result are same when compile with any of -O1,-O2 or -O3.

When compile without optimization, stack pointer is adjusted, i.e generated
code is OK.


-- 
           Summary: Registers in on clober list are cloberred when compiled
                    with optimization  (x86_64) ?
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: inline-asm
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: valery_reznic at yahoo dot com
  GCC host triplet: file bb.c
                    =====================================================
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
@ 2009-02-06  1:43 ` pinskia at gcc dot gnu dot org
  2009-02-06  7:12 ` valery_reznic at yahoo dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-02-06  1:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2009-02-06 01:42 -------
r11 is saved by the caller so this is the generated code is valid. 
Since nothing else uses r11 in the inline-asm, the code is correct.
>From the i386.h header:
   The value is zero if the register is not call used on either 32 or
   64 bit targets, one if the register if call used on both 32 and 64
   bit targets, two if it is only call used on 32bit targets and three
   if its only call used on 64bit targets.
/*  r8,  r9, r10, r11, r12, r13, r14, r15*/                     \
     1,   1,   1,   1,   2,   2,   2,   2,                      \


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
   GCC host triplet|file bb.c                   |file bb.c
                   |============================|
                   |=========================   |
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
  2009-02-06  1:43 ` [Bug inline-asm/39078] " pinskia at gcc dot gnu dot org
@ 2009-02-06  7:12 ` valery_reznic at yahoo dot com
  2009-02-06 11:51 ` ubizjak at gmail dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: valery_reznic at yahoo dot com @ 2009-02-06  7:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from valery_reznic at yahoo dot com  2009-02-06 07:12 -------
(In reply to comment #1)
> r11 is saved by the caller so this is the generated code is valid. 
> Since nothing else uses r11 in the inline-asm, the code is correct.
The problem is not that r11 not saved at stack, but that saved on the stack
value of r15 ALWAYS will be overwritten - by the 'call my_syscall' instruction
and saved on the stack values of rbx, r12, r13 and r14 MAY BE overritten if
my_syscall function wrote something to the stack

> From the i386.h header:
>    The value is zero if the register is not call used on either 32 or
>    64 bit targets, one if the register if call used on both 32 and 64
>    bit targets, two if it is only call used on 32bit targets and three
>    if its only call used on 64bit targets.
> /*  r8,  r9, r10, r11, r12, r13, r14, r15*/                     \
>      1,   1,   1,   1,   2,   2,   2,   2,                      \
> 


-- 

valery_reznic at yahoo dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
  2009-02-06  1:43 ` [Bug inline-asm/39078] " pinskia at gcc dot gnu dot org
  2009-02-06  7:12 ` valery_reznic at yahoo dot com
@ 2009-02-06 11:51 ` ubizjak at gmail dot com
  2009-02-06 12:08 ` jakub at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2009-02-06 11:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from ubizjak at gmail dot com  2009-02-06 11:51 -------
> > r11 is saved by the caller so this is the generated code is valid. 
> > Since nothing else uses r11 in the inline-asm, the code is correct.
> The problem is not that r11 not saved at stack, but that saved on the stack
> value of r15 ALWAYS will be overwritten - by the 'call my_syscall' instruction
> and saved on the stack values of rbx, r12, r13 and r14 MAY BE overritten if
> my_syscall function wrote something to the stack

These values have been saved into red-zone area. Since gcc does not know that
you have a call in the asm it thinks that func_1 is a leaf function where
redzone area can be used for temporary storage.

You can use -mno-red-zone to disable red-zone, but in reality, you should not
call other functions from inside asm.


-- 

ubizjak at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (2 preceding siblings ...)
  2009-02-06 11:51 ` ubizjak at gmail dot com
@ 2009-02-06 12:08 ` jakub at gcc dot gnu dot org
  2009-02-09 16:08 ` valery_reznic at yahoo dot com
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2009-02-06 12:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jakub at gcc dot gnu dot org  2009-02-06 12:08 -------
Or you can subq $128, %rsp; call my_syscall; addq $128, %rsp in your inline
assembly.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (3 preceding siblings ...)
  2009-02-06 12:08 ` jakub at gcc dot gnu dot org
@ 2009-02-09 16:08 ` valery_reznic at yahoo dot com
  2009-02-09 16:09 ` valery_reznic at yahoo dot com
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: valery_reznic at yahoo dot com @ 2009-02-09 16:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from valery_reznic at yahoo dot com  2009-02-09 16:07 -------
(In reply to comment #3)
> > > r11 is saved by the caller so this is the generated code is valid. 
> > > Since nothing else uses r11 in the inline-asm, the code is correct.
> > The problem is not that r11 not saved at stack, but that saved on the stack
> > value of r15 ALWAYS will be overwritten - by the 'call my_syscall' instruction
> > and saved on the stack values of rbx, r12, r13 and r14 MAY BE overritten if
> > my_syscall function wrote something to the stack
> 
> These values have been saved into red-zone area. Since gcc does not know that
> you have a call in the asm it thinks that func_1 is a leaf function where
> redzone area can be used for temporary storage.
> 
> You can use -mno-red-zone to disable red-zone, but in reality, you should not
> call other functions from inside asm.
> 
Aha, no I realized why compiler is right. Still if it will mentioned in the
gcc's docs about inline assembler it will be nice.

Any why you say I shouldn't call other function from inside asm ?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (4 preceding siblings ...)
  2009-02-09 16:08 ` valery_reznic at yahoo dot com
@ 2009-02-09 16:09 ` valery_reznic at yahoo dot com
  2009-02-11  7:50 ` ubizjak at gmail dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: valery_reznic at yahoo dot com @ 2009-02-09 16:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from valery_reznic at yahoo dot com  2009-02-09 16:09 -------
(In reply to comment #4)
> Or you can subq $128, %rsp; call my_syscall; addq $128, %rsp in your inline
> assembly.
> 
When I understood what happened I did it, but thank you anyway.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (5 preceding siblings ...)
  2009-02-09 16:09 ` valery_reznic at yahoo dot com
@ 2009-02-11  7:50 ` ubizjak at gmail dot com
  2009-02-11 14:26 ` valery_reznic at yahoo dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2009-02-11  7:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from ubizjak at gmail dot com  2009-02-11 07:50 -------
(In reply to comment #5)

> Any why you say I shouldn't call other function from inside asm ?

See for example [1].

[1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16331#c14


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (6 preceding siblings ...)
  2009-02-11  7:50 ` ubizjak at gmail dot com
@ 2009-02-11 14:26 ` valery_reznic at yahoo dot com
  2009-02-11 15:32 ` ubizjak at gmail dot com
  2009-02-11 15:56 ` valery_reznic at yahoo dot com
  9 siblings, 0 replies; 11+ messages in thread
From: valery_reznic at yahoo dot com @ 2009-02-11 14:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from valery_reznic at yahoo dot com  2009-02-11 14:26 -------
(In reply to comment #7)
> (In reply to comment #5)
> 
> > Any why you say I shouldn't call other function from inside asm ?
> 
> See for example [1].
> 
> [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16331#c14
> 
I read it. Still I don't get the point.
In the inline assembler I MYSELF put arguments in the places where MY (by the
way, written in assembler) function expect to get them.

I need nothing from gcc.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (7 preceding siblings ...)
  2009-02-11 14:26 ` valery_reznic at yahoo dot com
@ 2009-02-11 15:32 ` ubizjak at gmail dot com
  2009-02-11 15:56 ` valery_reznic at yahoo dot com
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2009-02-11 15:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from ubizjak at gmail dot com  2009-02-11 15:32 -------
(In reply to comment #8)

> In the inline assembler I MYSELF put arguments in the places where MY (by the
> way, written in assembler) function expect to get them.

Then you actually don't need a compiler... ;)

> I need nothing from gcc.

True.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

* [Bug inline-asm/39078] Registers in on clober list are cloberred when compiled with optimization  (x86_64) ?
  2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
                   ` (8 preceding siblings ...)
  2009-02-11 15:32 ` ubizjak at gmail dot com
@ 2009-02-11 15:56 ` valery_reznic at yahoo dot com
  9 siblings, 0 replies; 11+ messages in thread
From: valery_reznic at yahoo dot com @ 2009-02-11 15:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from valery_reznic at yahoo dot com  2009-02-11 15:56 -------
(In reply to comment #9)
> (In reply to comment #8)
> 
> > In the inline assembler I MYSELF put arguments in the places where MY (by the
> > way, written in assembler) function expect to get them.
> 
> Then you actually don't need a compiler... ;)
My assembler code is inline. It's surrounded by fairly large amount of the C
code, so I need compiler. I just don't care if it know how to pass arguments to
my function - I do it myself (and it work)

So could you please explain why you said that I shouldn't use 'call' inside
inline asm ?


> 
> > I need nothing from gcc.
> 
> True.
I mean "I don't need it to know how pass args to my function"

> 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39078


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

end of thread, other threads:[~2009-02-11 15:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-02 16:54 [Bug inline-asm/39078] New: Registers in on clober list are cloberred when compiled with optimization (x86_64) ? valery_reznic at yahoo dot com
2009-02-06  1:43 ` [Bug inline-asm/39078] " pinskia at gcc dot gnu dot org
2009-02-06  7:12 ` valery_reznic at yahoo dot com
2009-02-06 11:51 ` ubizjak at gmail dot com
2009-02-06 12:08 ` jakub at gcc dot gnu dot org
2009-02-09 16:08 ` valery_reznic at yahoo dot com
2009-02-09 16:09 ` valery_reznic at yahoo dot com
2009-02-11  7:50 ` ubizjak at gmail dot com
2009-02-11 14:26 ` valery_reznic at yahoo dot com
2009-02-11 15:32 ` ubizjak at gmail dot com
2009-02-11 15:56 ` valery_reznic at yahoo dot com

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