I have been working on an application for a while, and would like others to
be able to add modules for it. The problem is, I don't know how to implement
them.
I am using the dlopen() library set. I understand how to load modules into t
he program and how to execute the code. The problem is, I want these modules
to be able to modify ANY part of the program. For example, if there are 100
functions in my application, I want the program to be able to modify any si
ngle one of those functions to do something different. Is there a way to do
this, without having to make every single function in my code a function poi
nter?
I don't think you can do that with dlopen. You'll have to do this hardcore -- libelf to parse the so files, and mmap to map executable pages into memory from it. If you figure it out let me know, since that's one secret I haven't yet managed to penetrate, libelf has got to be among the most important and least documented libraries on earth...
Plugins work this way. That way, things like codecs don't have to be hardcoded -- or even linked -- into the program, they can be loaded on command. it's usually blocks of functions that get loaded though.
First you'd have to define an interface to which the module writers would conform. My guess is that "modify every function in my program" does not really explain what and how to modify it. You are at the incorrect level of abstraction to make this work if you're going to have everything be function pointers.
Basically, you first want to define a concrete set of functionality that module writers should be able to hook into. Then you have to specify an interface agreement that the module writers can conform to.
One of the first things I think you may want to define is a procedure to initialize the module. Part of this could be that the module passes you back a set of function pointers (into itself) that you can call. You define the datastructure that gets passed and the module conforms and uses it to tell you want it wants to overload or hook into and passes you the callback or overload routine it wants you to call.