One annoying thing about Mac OS X…

is that although it has this great UNIX base that people always talk about (and which I appreciate much of the time), they didn’t use the ubiquitous ELF and instead went with a proprietary object format called Mach-O

This means that dynamic linking works totally differently and so you can’t use a lot of mainstays like LD_PRELOAD (analogue is DYLD\_INSERT\_LIBRARIES) and the ldd command (analogue is otool -L). I’ve run headlong into these differences this morning as I’m attempting to port tsocks to Mac OS X. Check out Thomas Strömberg’s post – he’s working through some of these issues too. Another resource: http://www.kernelthread.com/mac/apme/library/

It took me a fair amount of web searching to find out the magic incantation to create an initialization function for a dynamic shared library (that is, a function that gets called when the dynamic library is loaded):


void _init(void) \_\_attribute\_\_ ((constructor));

Note that the word “constructor” in this context has nothing to do with C++ or objects at all. This is just Apple’s bizarrely-named gcc extension for creating initialization functions. I think I might’ve preferred their older, deprecated syntax: #pragma CALL\_ON\_LOAD \_init. Is it just me or does the former syntax seem more aptly named? Where did I find this? See Apple’s Tech Note on the subject, which took me too long to find.

The thing that I’ve always hated about Windows programming is how Microsoft tends to put in platform-specific stuff. I’m now realizing that Apple is guilty of the same thing, although perhaps less so than Microsoft.

The good part about Mach-O is that it supports universal binaries which have both PowerPC and Intel code, which should ease the transition to the new Intel-based Macs.

Leave a Reply

Your email address will not be published.