1

I have Program A, in which function Foo is implemented. Program A is able to load dll's dynamicly, durring runtime.

I'm currently writing Program B, which is compiled into a dll, so Program A is able to load it.

What I would like to do, is to "override" function Foo in Program B somehow, so when it is called from Program A, a different code is invoked from the one, that is implemented in Program A.

A found some referencing topic about trampuline functions: http://en.wikipedia.org/wiki/Trampoline_%28computers%29

My question is, is it possible? How?

Thanks, krisy

Edit: I have the source code of Program A, but I'm not allowed to modidy this. So redefining the structure of program A is out of the question :-(

Edit: I need a - preferably - open source, cross-platform solution (or at least needs to work under linux systems)

krisy
  • 43
  • 5

4 Answers4

1

This is only possible if the function you want to override would live in an external dll common to both program A and B.

Having the function implemented inside the Program A and redefining it in the DLL for Program B it won't work, because the function is already defined. The dynamic linker will only rebind the undefined functions.

Any solution would always be an hack.

It is better to use function pointers or proper class design and create an architecture that solves you problem in a clean way.

Paulo Pinto
  • 632
  • 4
  • 10
  • This might just work; what if I define a function with the same signature (and name), as the function to be overwritten, and save its function pointer? This way when the dll loads, the referenced function pointer would be Program A Foo's function pointer, would it not? – krisy Feb 02 '11 at 14:28
  • It would not, because in Program A you would might still direct references to the function. It would only work, if the only way to access the function would be via the function pointer. But then again you would need an explicit way to change the function pointer, after the DLL gets loaded. – Paulo Pinto Feb 08 '11 at 11:25
0

You could use Detours to do that.

Edit: LD_PRELOAD might help you doing the same kind of trick. (Exemple taken from What is the LD_PRELOAD trick?)

$ LD_PRELOAD=/path/to/my/malloc.so /bin/ls
Community
  • 1
  • 1
tibur
  • 11,531
  • 2
  • 37
  • 39
  • Detours looks great; but is there any open source/os independet (linux?) version, I can use? – krisy Feb 02 '11 at 13:40
  • Nice; I guess functions are identified by there signitures, right? So if the function to be overwritten looks like this: Foo(struct_1, struct_2, ...) I need to implement all struct's in program B (the same way ,as in program A)? – krisy Feb 02 '11 at 14:16
0

the other simple way is to use function pointer, and the Module A will ask B to get the function pointer and invoke it.

This technique is mostly used in C to simulate polymorphism , but I suggest you to use inheritance if you use C++;

DesignFirst
  • 329
  • 2
  • 5
  • Somethine like this was my first idea as well, but this would require the modification of program A, which is not allowed :-( – krisy Feb 02 '11 at 14:08
0
void Foo(void) {
  if (globalPointerToFooInDLL != NULL) {
    return globalPointerToFooInDLL();
  }
  elseDoSomethingElse();
}

or call Foo via some pointer all the time and redefine that pointer when loading the DLL.

Detours rewrites the original functions in the running process. That is of course also an option, but is just another form of self-modifying code with all the problems that made self-modifying code go out of fashion.

Christopher Creutzig
  • 8,656
  • 35
  • 45