Karthik
Karthik

Reputation: 820

Overriding a libc function using an injected dylib on OS X

I am trying to override some libc functions (eg: puts()) with my own implementation.

I have defined my own implementation in a dylib file as follows.

int puts ( const char * str ); 

When I link my binary with the dylib file in Xcode and build, my overridden version gets called.

However, when I inject the dylib into my binary, I see that the overridden version is not called. I have verified that the dylib is getting loaded by logging something in the entry point of the dylib.

Can someone here point me to what I'd need to do to get my overridden version called?

Upvotes: 0

Views: 1071

Answers (1)

Technologeeks
Technologeeks

Reputation: 7907

This is expected behavior, as when you link in, your library gets precedence over libSystem.B.dylib, which is where puts is exported (as a re-export of libsystem_c and friends).

To get this in runtime, you would need to explicitly use function interposing. This is a great feature of DYLD. In your library, create a small section:

static const interpose_t interposing_functions[] \
    __attribute__ ((section("__DATA, __interpose"))) = {
        { (void *)my_puts,  (void *) puts  } 
    };

Upvotes: 2

Related Questions