redefine extern(C) function in a shared library

Jacob Carlborg doob at me.com
Thu Oct 4 12:14:25 PDT 2012


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


More information about the Digitalmars-d-learn mailing list