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