Also see older version http://toves.freeshell.org/interpose/interpose_old.html for libkeepalive implementation for MacOSX
While OSX supports the dlopen(3) interface natively since 10.4 it doesn't support LD_PRELOAD library interposition (cf unix and linux.)
The corresponding environment variable is DYLD_INSERT_LIBRARIES
but is supported by the run time loader (dyld) using a different mechanism.
For each intercepted function there is an entry in a special object file
section (__DATA, __interpose) which lists the entry point of the replacement
function followed by the function to be replaced.
Presumably at run(load) time dyld detects these sections in dynamic
libraries passed to it. While linking dyld uses the replacement function's
address to resolve references to the original symbol.
I assume it is pretty much like the elf procedure linkage table(plt).
See
Mac OS X Internals: A Systems Approach
Amit Singh
Chapt. 2, p. 73 sect. 2.6.3.4 dyld interposing
/* // @(#) example function interception */ # include <sys/types.h> # include <sys/socket.h> int MY_connect (int sd, const struct sockaddr* addr, socklen_t alen); /* The names of everything except the section are arbitrary */ typedef struct interposer { void* replacement; void* original; } interpose_t static const interpose_t interposers[] \ __attribute__ ((section("__DATA, __interpose"))) = { { .replacement = MY_connect, .original = connect }, }; int MY_connect (int sd, const struct sockaddr* addr, socklen_t alen) { log_connect (addr); return connect (sd, addr, alen); // call original connect() } gcc -Wall -dynamiclib -o libmyconnect.dylib src.c env DYLD_INSERT_LIBRARIES=`pwd`/libmyconnect.dylib prog
Source files
LICENSE
Creative Commons CC0
http://creativecommons.org/publicdomain/zero/1.0/legalcode
AUTHOR
James Sainsbury