Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #145705
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Calling a machine code/binary function given its address, return type, & parameter types |
| Date | 2019-08-17 15:10 -0700 |
| Organization | None to speak of |
| Message-ID | <ln5zmv5tpo.fsf@kst-u.example.com> (permalink) |
| References | <66319c70-be79-466d-ba04-f8bb1b2d3a8a@googlegroups.com> |
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. 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).
Others have discussed how you can do this *if it can be done*. But
there is no way to do this in portable C. But since you're dealing with
calling machine code routines, you're probably not too concerned about
portability.
Do you know the actual types of the four parameters, or do you only know
that they're pointers? Knowing their actual types is going to make this
easier.
If you could get the address of the machine code routine *as a function
pointer value* rather than as an integer, that's also going to help,
since you can call it directly via the function pointer. If it has to
be an integer, using uintptr_t (defined in <stdint.h>) would be a bit
cleaner, since that integer type is specified to be able to hold
converted pointer values.
If you aren't starting with a function pointer, converting to a function
pointer is tricky. (We've had some long boring discussions here about
whether converting from an object pointer to a function pointer is
"illegal" or merely has undefined behavior.) The only way this might
affect you is that if you're using gcc with options that tell it to
strictly enforce the language rules (e.g., "gcc -std=c11 -pedantic" or
"gcc -std=c11 -pedantic-errors") you might get a warning or error
message about the conversion.
Having said all that, here's an outline of something that's likely to
work. (It compiles, but blows up when I run it because I'm not using a
valid function address.)
#include <stdint.h>
const uintptr_t the_address = 0xDEADBEEF;
// or whatever the address is, and however you got it
int call_the_function(uintptr_t addr, void *arg1, void *arg2, const void
*arg3, const void *arg4) {
// Define the type of the called function
typedef int(func)(void*, void*, const void*, const void*);
// Construct a pointer to the function
func *fptr = (func*)addr;
// and call it
return fptr(arg1, arg2, arg3, arg4);
}
int main(void) {
// Declare the arguments
// Hopefully they'll have more specific pointer types,
// and you'll have values for them.
void *arg1;
void *arg2;
const void *arg3;
const void *arg4;
int result = call_the_function(the_address, arg1, arg2, arg3, arg4);
}
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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