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

349
Visualizações
How to append to __preinit_array_start on Linux?

On Linux with GCC if I define

__attribute__((constructor)) static void myfunc(void) {}

, then the address of myfunc will be appended to __init_array_start in the .ctors section. But how can I append a function pointer to __preinit_array_start?

Is __preinit_array_start relevant in a statically linked binary?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As there's no __attribute__((preconstructor)), you can just mush the code into the relevant section using some section attributes e.g.

#include <stdio.h>

int v;
int w;
int x;

__attribute__((constructor)) static void
foo(void)
{
    printf("Foo %d %d %d\n", v, w, x);
}

static void
bar(void)
{
    v = 3;
}

static void
bar1(void)
{
    w = 2;
}

static void
bar2(void)
{
    x = 1;
}

__attribute__((section(".preinit_array"))) static void (*y[])(void) = { &bar, &bar1, &bar2 };

int
main(int argc, char **argv)
{
    printf("Hello World\n");
}

File dumped into foo.c, compiled using: gcc -o foo foo.c, and then run yields an output of:

Foo 3 2 1
Hello World

File compiled using gcc -static -o foo foo.c, and then run yields the same output, so it does appear to work with statically linked binaries.

It will not work with .so files, though; the linker complains with:

/usr/bin/ld: /tmp/ccI0lMgd.o: .preinit_array section is not allowed in DSO
/usr/bin/ld: failed to set dynamic section sizes: Nonrepresentable section on output

I'd be inclined to avoid it, as code run in that section precedes all other initialization routines. If you're trying to perform some 'this is supposed to run first' initialization, then it's really not a good idea - you're just fighting a race condition which should be solved by some other mechanism.

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