OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Oct 22, 2019 9:29 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: calling a c function at runtime knowing its name.
PostPosted: Sun Sep 26, 2004 5:04 am 
Is there a way to call a function at runtime knowing only its name? for example, by passing its name as a parametre to another function that will call it
Code:
//example
call_func("func_1");
call_func("func_2");
...


Top
  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Sun Sep 26, 2004 5:52 am 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3875
Location: Eindhoven
aladdin wrote:
Is there a way to call a function at runtime knowing only its name? for example, by passing its name as a parametre to another function that will call it
Code:
//example
call_func("func_1");
call_func("func_2");
...



Dynamic linking.

Something like:
Code:
void (*fp)(int, bool, char, char*) = dl_get("function");
*fp(0123, true, 'a', "abcdef");


Been discussed here before btw.


Top
 Profile  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Mon Sep 27, 2004 2:51 am 
can u give me the link to this thread (if you have it). coze i can't find it :-[


Top
  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Wed Sep 29, 2004 7:04 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
actually, all depends on the environment you're running. dlopen/dlsym/dlclose exists in linux (man dlsym will give you basic info), but you cannot do it with "bare" C/C++ programs (e.g. without help of a library)

Why exactly do you need this ?

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Wed Sep 29, 2004 1:06 pm 
I want to implement a function that will run kernel services at run-time by reading them from a file, I don't speak about external modules, but only kernel services such as memory manager, scheduler...
this will allow (for example) to test multiple versions of some modules easily (by adding/removing the name from the list).
I'm already doing this but the modules list is in a .h file, so I must recompile kernel everytime I want to do modification.


Top
  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Wed Sep 29, 2004 1:38 pm 
What you need is something like the implementation of dlopen/dlsym/dlclose, so you have a few options:

Forget about a general solution, instead use a special module format, where you'd have some code at the beginning of the module to check from some table whether it's already loaded, then return a table of functions it provides. If you keep names in that table, you get by name lookup of function pointers, which you can then call normally. You might need a second table for functions the module imports, which you fill in by querying the exports of other functions. This way you don't need to lookup a name every time you want to call a function, just once.

You can keep track of imports too so if you unload a module you can repatch the table so calling an unloaded function goes into an error handler or something, which can check if the function is available by some newly loaded replacement module, or panic if necessary (or unload the troubly module or whatever) or require that modules be removed in such an order that no module loaded can depend on something not loaded (like Linux does I think).

You can also do what Linux does: read normal .o files, which already contain such a table, parse that, and proceed as in the previous option. In this case you do the imports by "linking" the object into your running kernel.

Finally, you could implement full dlopen/dlsym/dlclose if you want, and use something like ELF .so files, which is designed to to mostly what described above. A hack (such as that in Linux) for .o files might be easier though, but YMMV.


Top
  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Thu Sep 30, 2004 3:35 am 
Quote:
Forget about a general solution, instead use a special module format, where you'd have some code at the beginning of the module to check from some table whether it's already loaded, then return a table of functions it provides. If you keep names in that table, you get by name lookup of function pointers, which you can then call normally. You might need a second table for functions the module imports, which you fill in by querying the exports of other functions. This way you don't need to lookup a name every time you want to call a function, just once.

I'm actually using this solution, but i don't like it :(


Quote:
You can also do what Linux does: read normal .o files, which already contain such a table, parse that, and proceed as in the previous option. In this case you do the imports by "linking" the object into your running kernel.


this is what I m planning to do in the future since i want to implement a fully modulable kernel.


Quote:
Finally, you could implement full dlopen/dlsym/dlclose if you want, and use something like ELF .so files, which is designed to to mostly what described above. A hack (such as that in Linux) for .o files might be easier though, but YMMV.

I think this is a good solution, but it's not too important at the moment. coze this is used for libraries and what I want to do is only calling "internal modules" (compiled with the kernel).


Top
  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Thu Sep 30, 2004 3:51 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
http://clicker.sourceforge.net/wiclicke ... p/ModMaker and http://clicker.sourceforge.net/wiclicke ... KmodFormat may perhaps help you. Also, you could go to www.wotsit.org and get the specs of COFF/ELF files (depending on what your compiler outputs). ELF is a bit overcomplicated (especially, i discouradge you to use PIC code or shared libraries).

Mainly, the steps to take to load a module will be
- load the 'program bits' and 'data bits' to some avl. location
- process the relocations list to patch references to internal symbols
- process the symbols list to identify 'imports' required and resolve them using the symbol table
- process your export list to add symbols in the table (for other modules)
- locate and run the module initialisation function.

HTH

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Fri Oct 01, 2004 2:26 pm 
very interesting liks thank u very much ;D


Top
  
 
 Post subject: Re:calling a c function at runtime knowing its name.
PostPosted: Mon Oct 04, 2004 2:06 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
not forgetting to mention the http://www.iecc.com/linker/ "Linkers and Loaders" online book, of course ;)

_________________
Image May the source be with you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: MSN [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group