how to get function pointer?

Programming, for all ages and all languages.
Post Reply
posman
Posts: 19
Joined: Fri Sep 05, 2008 12:55 pm

how to get function pointer?

Post 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?
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: how to get function pointer?

Post 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;
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: how to get function pointer?

Post 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,

Code: Select all

addr = (unsigned int *)myfunc;
should be read as

Code: Select all

addr = (unsigned int *)&myfunc;
I don't recommend casting a function pointer to a scalar pointer, though. ;)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: how to get function pointer?

Post 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.
Every good solution is obvious once you've found it.
posman
Posts: 19
Joined: Fri Sep 05, 2008 12:55 pm

Re: how to get function pointer?

Post by posman »

:?

Thank you all
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: how to get function pointer?

Post by CodeCat »

If you're using C++, then std::map might be useful.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: how to get function pointer?

Post 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...
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to get function pointer?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: how to get function pointer?

Post 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
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: how to get function pointer?

Post 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;)
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: how to get function pointer?

Post by CodeCat »

I think you can get the same result by asking ld for a link map.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to get function pointer?

Post by Combuster »

great, whe have three ways doing exactly the same thing :D
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply