Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #145703
| 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 14:31 -0700 |
| Organization | None to speak of |
| Message-ID | <lna7c75vhu.fsf@kst-u.example.com> (permalink) |
| References | <66319c70-be79-466d-ba04-f8bb1b2d3a8a@googlegroups.com> <%EY5F.44947$665.27757@fx28.am4> |
Bart <bc@freeuk.com> writes:
> On 17/08/2019 14:58, 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).
>
> What code did you try, and with which compiler?
>
> C allows functions and function pointers to be declared withour
> specifying the number and types of the parameters. Example:
>
> #include <stdio.h>
>
> int(*fnptr)();
>
> int main(void) {
> int result;
>
> result = fnptr(NULL,NULL,NULL,NULL);
> }
>
> Since you say it takes four pointers, four pointers (in the form of
> zeros or NULLs) have been supplied.
NULL is a macro that expands to a null pointer constant, but a null
pointer constant isn't necessarily of pointer type. (Yes, that's weird
and counterintuitive, but it's true.) A null pointer constant yields a
null pointer value when it's converted to a pointer type. If you call a
function with no prototype, no such conversion is done.
If, for example, NULL is defined as 0, int is 32 bits, and pointers are
64 bits, then fnptr(NULL,NULL,NULL,NULL) will pass 4 32-bit integer
values to a function that presumably expects 4 64-bit pointer values.
I *hope* that the actual types of the four parameters are known. If
that's the case, then declare the function type (or, if you insist,
the function pointer type) with a prototype that specifies those
parameter types. With a visible prototype, NULL will be implicitly
converted to the correct type.
In the unlikely event that you really need to use an old-style
non-prototype declaration, and you're willing to assume that all object
pointers are interchangeable, then cast the NULL arguments to void*:
result = fnptr((void*)NULL, (void*)NULL, (void*)NULL, (void*)NULL);
But if you know the actual types, then old-style non-prototype
declarations are a can of worms you don't need to open.
(The OP didn't say that the arguments should be null pointers, so it's
likely that the NULL stuff is irrelevant.)
> But it would be much better if the actual parameter types, if known,
> were provided, as then more checking can be done. For example:
>
> int(*fnptr)(int*, float*, char*, char*);
Yes.
[...]
--
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