Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
With MACH-O is there a way to register a function that will run before main?

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.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar

0

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
over 4 years ago · Santiago Trujillo Denunciar

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda