Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda