This is the mail archive of the
mailing list for the binutils project.
Re: How can I do calling for external address in my "C" programm by inline GAS?
SORRY! I have forgot to paste the new code into my last letter... SORRY!
I have changed my last code, but the programm do not working too.
Although error of calling is standing closed to real address of "printf":
address of printf: 0x400490
Unrecognised instruction: 0x4006dc
here is new code:
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"
here is output of valgrind:
==1262== Memcheck, a memory error detector
==1262== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==1262== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==1262== Command: ./call_pr
addr of printf=0x400490
vex amd64->IR: unhandled instruction bytes: 0x48 0x65 0x6C 0x6C 0x6F 0x0
==1262== valgrind: Unrecognised instruction at address 0x4006dc.
==1262== Your program just tried to execute an instruction that Valgrind
==1262== did not recognise. There are two possible reasons for this.
==1262== 1. Your program has a bug and erroneously jumped to a non-code
==1262== location. If you are running Memcheck and you just saw a
==1262== warning about a bad jump, it's probably your program's fault.
==1262== 2. The instruction is legitimate but Valgrind doesn't handle it,
==1262== i.e. it's Valgrind's fault. If you think this is the case or
==1262== you are not sure, please let us know and we'll try to fix it.
==1262== Either way, Valgrind will now raise a SIGILL signal which will
==1262== probably kill your program.
==1262== Process terminating with default action of signal 4 (SIGILL)
==1262== Illegal opcode at address 0x4006DC
==1262== at 0x4006DC: ??? (in /home/user/asm/call_pr)
==1262== by 0x40048F: ??? (in /home/user/asm/call_pr)
==1262== by 0x7FEFFFBEF: ???
==1262== HEAP SUMMARY:
==1262== in use at exit: 0 bytes in 0 blocks
==1262== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==1262== All heap blocks were freed -- no leaks are possible
==1262== For counts of detected and suppressed errors, rerun with: -v
==1262== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
The best Regards.