Page 1 of 1
how to get function pointer?
Posted: Wed Oct 22, 2008 11:48 am
by posman
Hi.
How can I get address of a function where the name of that function is in a variable?
Look: right now I can get the address of a function with any of this:
Code: Select all
addr = &myfunc;
addr = (unsigned int *)myfunc;
I don't know if there is a big difference between those two lines, but both work.
But that is not the problem. I want to know if is it possible to get the address of
myfunc if the name is in a variable. What I want to do is to get the address of several functions in a loop. So I want to have those function names in an array.
Is it possible?
Re: how to get function pointer?
Posted: Wed Oct 22, 2008 12:43 pm
by earlz
Do you mean like an array of function pointers?
Code: Select all
typedef void (*MyFunctionType)(int);
MyFunctionType fun_array[100];
...
void foo(int n){}
....
fun_array[0]=&foo;
Re: how to get function pointer?
Posted: Wed Oct 22, 2008 1:58 pm
by CodeCat
From what I've seen, function names are similar to array names in that using the function's name bare, without following parentheses, turns it into a pointer to that function.
Therefore,
should be read as
I don't recommend casting a function pointer to a scalar pointer, though.
Re: how to get function pointer?
Posted: Wed Oct 22, 2008 2:37 pm
by Solar
Do I understand correctly that you have an array of char * holding several function names, and want to retrieve the function pointer for each of them?
Sorry, no luck. At least not without third party help - the standard doesn't hold any tricks to that end that I am aware of.
Re: how to get function pointer?
Posted: Wed Oct 22, 2008 4:58 pm
by posman
Thank you all
Re: how to get function pointer?
Posted: Wed Oct 22, 2008 6:40 pm
by CodeCat
If you're using C++, then std::map might be useful.
Re: how to get function pointer?
Posted: Thu Oct 23, 2008 2:42 am
by Solar
How do you figure?
He can't get the function pointers. So he doesn't have anything to store in the map, either...
Re: how to get function pointer?
Posted: Thu Oct 23, 2008 5:27 am
by Combuster
I'd try to do some incremental linking (shove everything but the symbol table into one object file), objdump the .text section, run some formatting script to create a .asm file with the symbol table, then assemble it to finally link that together with the intermediate version to complete the binary.
Re: how to get function pointer?
Posted: Mon Nov 10, 2008 6:34 am
by DeletedAccount
I also do not think there is a standard way to do this , But you can always check the compiler documentation for functions that dump the compilers internal symbol tables , which may be created for debugging purposes.But i guess there is no easy way out , But why would you actually want to do such a thing ? Most of the problems of the above nature can be easily solved using a state machine sort of construct .
Regards
Sandeep
Re: how to get function pointer?
Posted: Tue Nov 11, 2008 1:23 am
by eddyb
Combuster wrote:I'd try to do some incremental linking (shove everything but the symbol table into one object file), objdump the .text section, run some formatting script to create a .asm file with the symbol table, then assemble it to finally link that together with the intermediate version to complete the binary.
I think it's simpler... it's called symbols tables and I saw it in some OS...
Oh, yes: use
nm(a command from binutils) to list the symbols. this is an example of output:
Code: Select all
00001000 A PageSize
00100000 A PhysBase
0010000c A __phys_start
c0000000 A VirtBase
c0100000 A __text_start
c0100000 t mb_header
c010000c T _start
then, make some script that make a symbol table(it's relative simple, because of the format:
address type name). then add it to your kernel. then, use it to find the address of some function, knowing the function's name;)
Re: how to get function pointer?
Posted: Tue Nov 11, 2008 8:55 am
by CodeCat
I think you can get the same result by asking ld for a link map.
Re: how to get function pointer?
Posted: Tue Nov 11, 2008 9:26 am
by Combuster
great, whe have three ways doing exactly the same thing