Overriding library functions in Mac OS X, the easy way: DYLD_INSERT_LIBRARIES

Back at MacHack 2003 Jonathan Rentzsch talked about how to override functions and inject code in Mac OS X using [several neat tricks](http://rentzsch.com/papers/overridingMacOSX). He also released a framework called [mach_star](http://rentzsch.com/mach_star/) which has two components: mach_override and mach_inject. These are great, but overkill for some simple cases.

A much easier way of doing library function overrides is using the DYLD_INSERT_LIBRARIES environment variable (analogous to LD_PRELOAD on Linux). The concept is simple: at load time the dynamic linker (dyld) will load any dynamic libraries specified in DYLD_INSERT_LIBRARIES before any libraries the executable wants loaded. By naming a function the same as one in a library function it will override any calls to the original.

The original function is also loaded, and can be retrieved using the dlsym(RTLD_NEXT, “function_name”); function. This allows a simple method of wrapping existing library functions.

Here’s a simple example which prints out the path of every file opened using the “fopen” function (lib_overrides.c):

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>

// for caching the original fopen implementation
FILE * (*original_fopen) (const char *, const char *) = NULL;

// our fopen override implmentation
FILE * fopen(const char * filename, const char * mode)
    // if we haven’t already, retrieve the original fopen implementation
    if (!original_fopen)
        original_fopen = dlsym(RTLD_NEXT, "fopen");

    // do our own processing; in this case just print the parameters
    printf("== fopen: {%s,%s} ==\n", filename, mode);
    // call the original fopen with the same arugments
    FILE* f = original_fopen(filename, mode);
    // return the result
    return f;

And a simple test program (overrides_test.c):

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
    char hello[] = "hello world";
    FILE *fp = fopen("hello.txt", "w");
    if (fp) {
        fwrite(hello, 1, strlen(hello), fp);

    return 0;

Compiled and tested:

tlrobinson$ gcc -Wall -o lib_overrides.dylib -dynamiclib lib_overrides.c
tlrobinson$ gcc -Wall -o overrides_test overrides_test.c
tlrobinson$ DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=lib_overrides.dylib overrides_test
== fopen: {hello.txt,w} ==

There are certainly scenarios far more interesting than this, though!

Warped Tour – Ventura (and my first iPhone camera photos)

I drove out to Ventura on Saturday to see my friend Jon who is on the Warped Tour doing some promotion for Capitol Records and [absolutepunk.net](http://www.absolutepunk.net). The drive there was ridiculous… about 3 hours from Los Angeles to Ventura (only about 60 miles), as was parking (I ended up parking a mile away and walking) but I got there just in time to see one of my favorite bands from my high school days, [The Matches](http://www.thematches.com/). Last time I saw them was two years ago on the same stage at Warped Tour, which was cool. I also saw New Found Glory and Bad Religion, which were both great.

I also got a chance to test out (and show off) my iPhone. It was great for getting directions, figuring out where the hell I was, checking out traffic (which as I mentioned, was awful), and taking photos…

Here are a few photos from the day:









As you can see, the iPhone camera is quite good for a cell phone. The resolution isn’t high (2 megapixels), but it’s perfectly fine for photos destined for the web.

At first I took mostly portrait oriented photos, even when landscape would have been better. Once I realized I could take landscape photos I switched, although it was a little awkward holding the camera in landscape orientation without covering the lens.

I assumed iTunes would somehow transfer photos I took on the iPhone back to my computer, but it does not. To do that, you must use an application like iPhoto, Graphic Converter, or the free Image Capture application included with Mac OS X.

Concert: Most of "The New Deal" with part of "STS9" @ The Knitting Factory

Taking a little break from the normal geek stuff, last night the Canadian electronica jam band [The New Deal](http://www.thenewdeal.com), with the bassist from [Sound Tribe Sector 9](http://www.sts9.com), played at the [Knitting Factory](http://knittingfactory.com) in Hollywood and were awesome! I had never seen them live before, so my friends Kevin and Ariana (who introduced us to them) decided to go, and we’re glad we did.

These guys put tons of their [sets on archive.org](http://www.archive.org/details/TheNewDeal). Check it out if you’re into electronic music, or even if you’re not.