www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - redefine extern(C) function in a shared library

reply "timotheecour" <thelastmammoth gmail.com> writes:
Is there a way to redefine a extern(C) function in a dynamic load 
library? (platform is OSX if that matters).

In a normal D program, I can usually redefine a dynamic load 
library as follows: eg, for _d_assertm:
extern(C) void _d_assertm(ModuleInfo* m, uint line){...}
which allows to do stuff before exiting; there are many other use 
cases.

In a dynamic load library, redefining this (or other extern(C) 
functions) doesn't seem to work: the new definition is ignored.

This probably explains the problem I had here:
http://forum.dlang.org/thread/tddzekqtgkqhwjiaknte forum.dlang.org#post-tddzekqtgkqhwjiaknte:40forum.dlang.org
Oct 03 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-04 01:35, timotheecour wrote:
 Is there a way to redefine a extern(C) function in a dynamic load
 library? (platform is OSX if that matters).

 In a normal D program, I can usually redefine a dynamic load library as
 follows: eg, for _d_assertm:
 extern(C) void _d_assertm(ModuleInfo* m, uint line){...}
 which allows to do stuff before exiting; there are many other use cases.

 In a dynamic load library, redefining this (or other extern(C)
 functions) doesn't seem to work: the new definition is ignored.

 This probably explains the problem I had here:
 http://forum.dlang.org/thread/tddzekqtgkqhwjiaknte forum.dlang.org#post-tddzekqtgkqhwjiaknte:40forum.dlang.org

BTW, do you want to replace an function or just calling an existing extern(C) function. In your previous post it seemed like you just wanted to call an existing function. Reading this, it sounds like you want to replace one? If you want to just call an existing function and you're using "dlopen" you can just get the the function using "dlsym". Even if you don't use "dlopen" now you could probably just add a call to that to load the dynamic library and then use "dlsym". http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html -- /Jacob Carlborg
Oct 03 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-10-04 09:20, timotheecour wrote:
 Here, I want to redefine an extern(C) function, not call the existing
 definition. I mentioned my previous post because I realized that when
 adding (for example) assert(0) inside the definition extern(C)
 std_stdio_static_this, it had no effect, which led me to ask this more
 general question: how to redefine an extern(C) function in a shared lib,
 which is very often needed (eg handling asserts, stacktraces,
 redirecting io to a log etc). It does work inside a normal D program.
 Interestingly, I also tried linking against a library libredefine that
 just re-defines that extern(C) symbol BEFORE linking against
 libphobos2.a, but that too had no effect. weird.
 (it was something like: rdmd -lredefine -L-Lpath/to/phobos etc)

This would be a very ugly hack, but it should be possible to modifying the loaded image (dynamic library, executable) at runtime and inserting whatever functions you want at whatever address you want. This is a very old library it uses the same concept, if I recall correctly: http://flectioned.kuehne.cn/ For Mac OS X you want these docs: http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html https://developer.apple.com/library/mac/#documentation/developertools/Reference/MachOReference/Reference/reference.html -- /Jacob Carlborg
Oct 04 2012
prev sibling parent "timotheecour" <thelastmammoth gmail.com> writes:
Here, I want to redefine an extern(C) function, not call the 
existing definition. I mentioned my previous post because I 
realized that when adding (for example) assert(0) inside the 
definition extern(C) std_stdio_static_this, it had no effect, 
which led me to ask this more general question: how to redefine 
an extern(C) function in a shared lib, which is very often needed 
(eg handling asserts, stacktraces, redirecting io to a log etc). 
It does work inside a normal D program.
Interestingly, I also tried linking against a library libredefine 
that just re-defines that extern(C) symbol BEFORE linking against 
libphobos2.a, but that too had no effect. weird.
(it was something like: rdmd -lredefine -L-Lpath/to/phobos etc)
Oct 04 2012