Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #145697

Re: Calling a machine code/binary function given its address, return type, & parameter types

Subject Re: Calling a machine code/binary function given its address, return type, & parameter types
Newsgroups comp.lang.c
References <66319c70-be79-466d-ba04-f8bb1b2d3a8a@googlegroups.com>
From Richard Damon <Richard@Damon-Family.org>
Message-ID <mZT5F.193$423.30@fx07.iad> (permalink)
Organization Forte - www.forteinc.com
Date 2019-08-17 10:14 -0400

Show all headers | View raw


On 8/17/19 9:58 AM, David Williams wrote:
> I have the address of a function that has already been compiled (so it's now effectively a machine code routine that I've loaded into memory and for which I have obtained the address). I pass this address by value as a parameter (long or uint64_t) to my C subroutine. I know the return type of the function (int), and that it takes 4 parameters (ptr, ptr, const ptr, const ptr). How do I call this function from my C subroutine?
> 
> I have Googled about function pointers, and I've noticed that others have asked a similar question, but the answers they received somehow don't seem directly applicable to my requirement. In any case, I've had no joy in my attempts at getting my (test) C subroutine to compile without errors. I obviously still have a weak grasp of C pointers and function pointers (although I'm learning!).
> 
> The platform is 64-bit Linux (x86-64).
> 
> 
> Regards,
> David.
> 

We can declair the function pointer you need as:

int (*funcp)(void*, void*, const void*, const void*)

(You haven't defined what types of pointer you have, so I have used
void*, you really should define the pointer with the right types, but
for x86-64 all object pointer types have the same representation so it
might not cause a real issue).

You can make that function pointer point to your function with a
statement like:

  funcp = (int (*)(void*, void*, const void*, const void*))(addr);

Note, this is implementation defined on how it exactly works, but if
addr is a 64 bit intergral value, for x86-64 this should work.

You can then call the function through that pointer as

   result = funcp(ptr1, ptr2, cptr3, cptr4);

It is possible to wrap all this up into a single statement, (casting the
value into a function pointer type and then calling that function
through the just created function pointer value) but it is clearer doing
it in steps.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Calling a machine code/binary function given its address, return type, & parameter types David Williams <dwilliams1664@gmail.com> - 2019-08-17 06:58 -0700
  Re: Calling a machine code/binary function given its address, return type, & parameter types Richard Damon <Richard@Damon-Family.org> - 2019-08-17 10:14 -0400
  Re: Calling a machine code/binary function given its address, return type, & parameter types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-08-17 07:17 -0700
  Re: Calling a machine code/binary function given its address, return type, & parameter types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-08-17 15:26 +0100
  Re: Calling a machine code/binary function given its address, return type, & parameter types Siri Cruise <chine.bleu@yahoo.com> - 2019-08-17 12:15 -0700
  Re: Calling a machine code/binary function given its address, return type, & parameter types Bart <bc@freeuk.com> - 2019-08-17 20:34 +0100
    Re: Calling a machine code/binary function given its address, return type, & parameter types Keith Thompson <kst-u@mib.org> - 2019-08-17 14:31 -0700
  Re: Calling a machine code/binary function given its address, return type, & parameter types Keith Thompson <kst-u@mib.org> - 2019-08-17 15:10 -0700
    Re: Calling a machine code/binary function given its address, return type, & parameter types Richard Damon <Richard@Damon-Family.org> - 2019-08-17 20:09 -0400
      Re: Calling a machine code/binary function given its address, return type, & parameter types Keith Thompson <kst-u@mib.org> - 2019-08-17 17:54 -0700
  Re: Calling a machine code/binary function given its address, return type, & parameter types David Williams <dwilliams1664@gmail.com> - 2019-08-18 09:57 -0700
    Re: Calling a machine code/binary function given its address, return type, & parameter types Bart <bc@freeuk.com> - 2019-08-18 18:37 +0100
    Re: Calling a machine code/binary function given its address, return type, & parameter types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-08-18 21:40 +0100
    Re: Calling a machine code/binary function given its address, return type, & parameter types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-21 09:05 -0400

csiph-web