This is the mail archive of the mailing list for the binutils project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How can I do calling for external address in my "C" programm by inline GAS?

Dmitrij K <> writes:

> here is new code:
> +++++++++++++CODE++++++++++++++++++
> #include <stdio.h>
> int main(){
>  void *p_printf = (void *)&printf;
>  const char *str = "Hello";
>  printf("addr of printf=%p\n", &printf); // 0x400490
>  asm ("movq (%0), %%rdx;" /* str into RDX */
>      "leaq (, %1), %%rcx;" /* address of PRINTF into RCX */
>    "pushq %%rdx;" /* put str into stack */
>     "call * %%rcx;" /* call PRINTF */
>     "addq $8, %%rsp;" /* remove ARG from stack */
>   :  /* output */
>   :"r"(str),"r"(p_printf)         /* input */
>   : "rdx", "rcx"
>  );   
> return 0;
> }

In this code p_printf is the address of the printf function.  You are
running your asm with the value of p_printf in %1.  Rather than putting
the value into a register, you are putting the address of the value into
a register.  In effect you are putting an address on the stack into the
register.  Then you are calling that address, which implies calling into
the stack.  This fails.

In order to get this kind of thing working, I recommend learning how to
use the debugger, with single-stepping and display of assembly
instructions.  That would make it clear that at the point of the call
%rcx does not hold the address you want to call.


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