6

Under Linux, I can register a routine that will run before main. For example:

#include <stdio.h>

void myinit(int argc, char **argv, char **envp) {
  printf("%s: %s\n", __FILE__, __FUNCTION__);
}
__attribute__((section(".init_array"))) typeof(myinit) *__init = myinit;

By compiling this with GCC and linking it in, the function myinit will be run before main.

Is there way to do this under Mac OSX and MACH-O?

Thanks.

3 Answers 3

10

You could place the function in __mod_init_func data section of Mach-O binary.

From Mach-O format reference:

__DATA,__mod_init_func

Module initialization functions. The C++ compiler places static constructors here.

example.c

#include <stdio.h>

void myinit(int argc, char **argv, char **envp) {
  printf("%s: %s\n", __FILE__, __FUNCTION__);
}
__attribute__((section("__DATA,__mod_init_func"))) typeof(myinit) *__init = myinit;

int main() {
  printf("%s: %s\n", __FILE__, __FUNCTION__);
  return 0;
}

I build your example with clang on OS X platform:

$ clang -Wall example.c
$ ./a.out
example.c: myinit
example.c: main
4
  • What I need is to be able to do is: Jun 8, 2015 at 11:46
  • If you separate main from myinit then gcc myinit.c main.c; ./a.out doesn't work but clang myinit.c main.c; ./a.out does. Any idea as to why? What is nice is that clang -c myinit.c; gcc main.c myinit.o; ./a.out does work. Jun 8, 2015 at 11:55
  • Maybe gcc optimizes it away. I don't have gcc installed here, but you can try to add used attribute. I would also add aligned to be on the safe side: __attribute__((section("__DATA,__mod_init_func"), used, aligned(sizeof(void*)))) typeof(myinit) *__init = myinit;
    – baf
    Jun 8, 2015 at 13:04
  • If gcc is /usr/bin/gcc then it is clang and it works fine. If gcc is in my case a brew installed gcc 4.9.2 then it doesn't work. Jun 8, 2015 at 15:55
4

Easiest way is to specify the function to be constructor using constructor attribute. The constructor attribute causes the function to be called automatically before execution enters main(). Similarly, the destructor attribute causes the function to be called automatically after main() completes or exit() is called. You can also specify optional priority if you have several functions

e.g. __attribute__((constructor(100)))

#include <stdio.h>

__attribute__((constructor)) void myinit() {
    printf("my init\n");
}

int main() {
    printf("my main\n");
    return 0;
}

__attribute__((destructor)) void mydeinit() {
    printf("my deinit\n");
}


$ clang -Wall example.c
$ ./a.out
my init
my main
my deinit
0

Disclaimer: I generally discourage what I'm about to say. Having code running before or after main makes things less predictable. I'm not sure why you wouldn't just let the first line of main invoke your myinit, but I suppose everyone has a reason. Here goes.

I don't know much about Mach-O, but the simplest way to run code before main, is to link in a C++ class that has a corresponding global instance defined. You can do this independently of your "C" code without having to alter anything else. You can also have this C++ code invoke C functions defined elsewhere in your code. In the example below, I show a simple example of how I would invoke your myinit.

In a standalone .cpp (or .cc) file, declare a very simple C++ class with a constructor that calls your "myinit function".

foo.cpp

// forward declare your myinit function and designate "C" linkage
extern "C" myinit(int, char**, char**);

class CodeToRunBeforeMain
{
 public:
    CodeToRunBeforeMain()
    {
        // invoke your myinit function here
        myinit(0, NULL, NULL);
    }

};

// global instance - constructor will run before main.
CodeToRunBeforeMain g_runBeforeMain;

The above approach doesn't recognize argc, argv, or envp. Hopefully, that isn't important.

2
  • Would the downvoter care to comment? I still stand by my answer. I made a reasonable attempt to give the pros/cons and was well aware that the OP was looking for a "C" answer. But the I answer I gave is fairly portable and to most anyone with C tools installed. Can't make everyone happy.
    – selbie
    Jun 15, 2015 at 6:29
  • For what it's worth, in my case, I would like some 'modules' of code to self register with a central list - so these init functions just append to some global array. I can (and was) doing all that in main, this avoids a bunch of boilerplate - main.c having to know about each and every module, etc. The array is now magically populated with whatever modules are linked into the app, which is nice. Apr 6, 2017 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.