Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find code-functions that called before c++ main

Tags:

c++

how to find code-functions that is called before c++ main? I am trying to locate which functions are called in order to initialize static variables by functions and thus executed before main.

like image 681
A. Papadopoulos Avatar asked Mar 21 '23 14:03

A. Papadopoulos


1 Answers

In GCCs case you have:

__CTOR_LIST__ (list of initialization functions called - pointers) 
__DTOR_LIST__ (list of functions called for cleanup)

They are automatically generated by the compiler for each translation unit (C/C++ file).

You can find more information about this here. For other compilers this must be similar but you can "guess" it by using utilities like objdump or nm (for your compiler suite, of course).

We called those pointer functions ( void (*func)(void) ) manually on a custom embedded platform on which we used g++.


I saw that you are interested in finding the static/global variables.

For this task you can really use some tools like objdump and nm (previously mentioned). First create 2-3 test files (and compile them to obtain the .o/.obj) and see how the static/global variables are seen by those tools (in the .obj/.o files. Once you see that, you can go through all the .obj/.o files with a script and make a list of the static/global variables.

like image 96
INS Avatar answered Mar 31 '23 20:03

INS