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


Groups > comp.lang.c > #145699

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

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Calling a machine code/binary function given its address, return type, & parameter types
Date 2019-08-17 15:26 +0100
Organization A noiseless patient Spider
Message-ID <87v9uvc1g7.fsf@bsb.me.uk> (permalink)
References <66319c70-be79-466d-ba04-f8bb1b2d3a8a@googlegroups.com>

Show all headers | View raw


David Williams <dwilliams1664@gmail.com> writes:

> 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.

If you can, it would be better to pass the address as a function
pointer.  You can use any function pointer type, but since you know the
type, why not use that?

> 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?

Do you know the actual types?  If not, you may get away it, but you won't
have portable or "correct" C.

By "const ptr" I assume you mean the /target/ of the pointer is a
const-qualified type (otherwise the const is not really important for
getting the call right), so let's assume that the actual types are void
*, int *, const void * and const int *.  With that in mind, your
function might look like this:

  void do_call(uint64_t func_addr, /* ... more arguments ... */)
  {
      typedef int function_type(void *, int *, const void *, const int *);
      function_type *fp = (function_type *)func_addr;
      int result = fp(/* ... actual arguments ... */);

      /* ... more code ... */
  }

Of course you can do this without the typedef and/or without the extra
variable fp, but the syntax gets hairy.  Note that I prefer to define
the function type and then write a * explicitly when I want a pointer to
a function of that type.

Going back to my comment about using the right type to start with, I'd
do this if possible:

  typedef int function_type(void *, int *, const void *, const int *);

  void do_call(function_type *fp, /* ... more arguments ... */)
  {
      int result = fp(/* ... actual arguments ... */);

      /* ... more code ... */
  }

and, if you have to, cast the uint64_t to a function_type * at the point
where you call do_call.

-- 
Ben.

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