Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way a C/C++ program can crash before main()?

Is there any way a program can crash before main()?

like image 624
Thi Avatar asked Mar 25 '10 18:03

Thi


2 Answers

With gcc, you can tag a function with the constructor attribute (which causes the function to be run before main). In the following function, premain will be called before main:

#include <stdio.h>

void premain() __attribute__ ((constructor));

void premain()
{
    fputs("premain\n", stdout);
}

int main()
{
    fputs("main\n", stdout);
    return 0;
}

So, if there is a crashing bug in premain you will crash before main.

like image 56
R Samuel Klatchko Avatar answered Sep 19 '22 13:09

R Samuel Klatchko


Yes, at least under Windows. If the program utilizes DLLs they can be loaded before main() starts. The DllMain functions of those DLLs will be executed before main(). If they encounter an error they could cause the entire process to halt or crash.

like image 24
Anders Abel Avatar answered Sep 20 '22 13:09

Anders Abel