This project demonstrates how to wrap a C function -- specifically, printf(3).
Here's how it works:
-
WrappedLibcexports aprintffunction whose prototype matches the one declared instdio.h. -
WrappedLibc'sprintfcallsdlsymwith the module handleRTLD_NEXTto find the originalprintfat runtime and invoke it with its own arguments. It can't invoke it with the original arguments becauseprintfis variadic. So it creates ava_listand callsvprintfinstead. -
WrappedLibc'sOTHER_LDFLAGSincludes-sub_library libSystem, which makeslibWrappedLibc.dylibreexport all the symbols fromlibSystem(to whichlibcis an alias on OS X). -
WrapperTesterlinks againstlibWrappedLibc.dylib. It does not link againstlibSystem---LINK_WITH_STANDARD_LIBRARIESis set toNO. This causes it to look up all standard library functions as indirect symbols vialibWrappedLibc.