calling a c function at runtime knowing its name.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
aladdin

calling a c function at runtime knowing its name.

Post by aladdin »

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: Select all

//example
call_func("func_1");
call_func("func_2");
...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:calling a c function at runtime knowing its name.

Post by Candy »

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: Select all

//example
call_func("func_1");
call_func("func_2");
...
Dynamic linking.

Something like:

Code: Select all

void (*fp)(int, bool, char, char*) = dl_get("function");
*fp(0123, true, 'a', "abcdef");
Been discussed here before btw.
aladdin

Re:calling a c function at runtime knowing its name.

Post by aladdin »

can u give me the link to this thread (if you have it). coze i can't find it :-[
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:calling a c function at runtime knowing its name.

Post by Pype.Clicker »

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 ?
aladdin

Re:calling a c function at runtime knowing its name.

Post by aladdin »

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.
mystran

Re:calling a c function at runtime knowing its name.

Post by mystran »

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.
aladdin

Re:calling a c function at runtime knowing its name.

Post by aladdin »

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 :(

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.

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).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:calling a c function at runtime knowing its name.

Post by Pype.Clicker »

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
aladdin

Re:calling a c function at runtime knowing its name.

Post by aladdin »

very interesting liks thank u very much ;D
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:calling a c function at runtime knowing its name.

Post by Pype.Clicker »

not forgetting to mention the http://www.iecc.com/linker/ "Linkers and Loaders" online book, of course ;)
Post Reply