Bug 13903 - fails an external calling in "C" programm by inline assembler (Core i5 x86_64)
Summary: fails an external calling in "C" programm by inline assembler (Core i5 x86_64)
Status: RESOLVED INVALID
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.21
: P2 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-26 18:08 UTC by kdiman
Modified: 2012-03-26 21:25 UTC (History)
1 user (show)

See Also:
Host: x86_64-pc-linux-gnu
Target: x86_64-pc-linux-gnu
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kdiman 2012-03-26 18:08:41 UTC

    
Comment 1 kdiman 2012-03-26 18:18:54 UTC
+++++++++++++++THIS CODE IS OK (32 bit)++++++++++++++++++
#include <stdio.h>
 
// g++ -m32 call32.c -o call32 && ./call32
int main(){
 
 void *p_printf = (void *)&printf;
 const char *str = "Hello from inline assembler is GNU Assembler\n";
 
 printf("address of printf: %p\n", p_printf); // 0x8048374
 
asm ("movl %0, %%edx;" /* str into EDX */
  "movl %1, %%ecx;" /* PRINTF into ECX */
  "pushl %%edx;" /* str into stack */
    "call *%%ecx;" /* call "printf" */
    "popl %%edx;" /* remove arg from stack */
  :  /* output */
  :"r"(str),"r"(p_printf)         /* input */
  :"%edx", "%ecx"         /* clobbered register */
 );  
 
return 0;
}
+++++++++++++++THIS CODE IS OK (32 bit)++++++++++++++++++



+++++++++++++++FAILS CODE (64 bit)+++++++++++++++
#include <stdio.h>
 
// g++ -m64 call64.c -o call64 && ./call64
int main(){
 
 void *p_printf = (void *)&printf;
 const char *str = "Hello from inline assembler is GNU Assembler\n";
 
 printf("address of printf: %p\n", p_printf); // 0x400490
 
asm ("movq %0, %%rdx;" /* str into RDX */
  "movq %1, %%rcx;" /* PRINTF into RCX */
  "pushq %%rdx;" /* str into stack */
    "call *%%rcx;" /* call "printf" */
    "popq %%rdx;" /* remove arg from stack */
  :  /* output */
  :"r"(str),"r"(p_printf)         /* input */
  :"%rdx", "%rcx"         /* clobbered register */
 );  
 
return 0;
}
+++++++++++++++FAILS CODE (64 bit)+++++++++++++++
Comment 2 kdiman 2012-03-26 18:24:19 UTC
binutils-2.21.1
gcc-4.5.3
glibc-2.13 (merged with debug)
sys-kernel/gentoo-sources-3.2.1-r2

OS: Gentoo Linux x86_64

CFLAGS="-march=native -O2 -pipe" #
CXXFLAGS="${CFLAGS}"

LDFLAGS="-Wl,-O1 -Wl,--sort-common"
Comment 3 kdiman 2012-03-26 18:35:16 UTC
+++++++++++++++++OUTPUT OF VALGRIND+++++++++++++++++++



==16385== Memcheck, a memory error detector==16385== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.==16385== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==16385== Command: ./call64                                                                                                                                       
==16385==                                                                                                                                                         
address of printf: 0x400490                                                                                                                                       
==16385== Invalid read of size 4                                                                                                                                  
==16385==    at 0x5614969: vfprintf (vfprintf.c:1269)                                                                                                             
==16385==    by 0x561F52B: fprintf (fprintf.c:33)                                                                                                                 
==16385==    by 0x4005D2: main (in /home/user/gas/call64)                                                                
==16385==  Address 0x1000000bf is not stack'd, malloc'd or (recently) free'd                                                                                      
==16385==                                                                                                                                                         
==16385==                                                                                                                                                         
==16385== Process terminating with default action of signal 11 (SIGSEGV)                                                                                          
==16385==  Access not within mapped region at address 0x1000000BF                                                                                                 
==16385==    at 0x5614969: vfprintf (vfprintf.c:1269)                                                                                                             
==16385==    by 0x561F52B: fprintf (fprintf.c:33)                                                                                                                 
==16385==    by 0x4005D2: main (in /home/user/gas/call64)
==16385==  If you believe this happened as a result of a stack
==16385==  overflow in your program's main thread (unlikely but
==16385==  possible), you can try to increase the size of the
==16385==  main thread stack using the --main-stacksize= flag.
==16385==  The main thread stack size used in this run was 8388608.
==16385== 
==16385== HEAP SUMMARY:
==16385==     in use at exit: 0 bytes in 0 blocks
==16385==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==16385== 
==16385== All heap blocks were freed -- no leaks are possible
==16385== 
==16385== For counts of detected and suppressed errors, rerun with: -v
==16385== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
SEGMENTATION FAULT



+++++++++++++++++OUTPUT OF VALGRIND+++++++++++++++++++
Comment 4 Ian Lance Taylor 2012-03-26 21:25:58 UTC
This is not a bug.  You are calling printf without passing the parameters correctly.  In 64-bit mode the first six function arguments are not passed on the stack.

I don't know why you have filed this bug against the assembler.  Even if it were a bug, which it is not, it would not be a bug in the assembler.