Make a call using the gcc constructor attribute
__attribute__ ((constructor)) void construct() { // Инициализация } fails because of the -nostdlib flag. Is there any other way (library - pure C)?
Here is exactly your question. How to run constructor even if “-nostdlib” option is defined . True, the answers are disappointing.
And the dlopen documentation says the following:
The obsolete symbols _init () and _fini ()
The linker recognizes special symbols
_initand_fini. If this is a custom library_init, it was executed after thedlopen(), beforedlopen()returns. If the library is_fini, it is called In this case, it can be done by giving the gcc parameter "the -nostartfiles" parameter on the command line.Using these routines, or the gcc
-nostartfilesor-nostdliboptions, is not recommended. It has been noted that since it has been adopted, it has been the case that it has been adopted.Instead, library should export routines using the
__attribute__((constructor))and__attribute__((destructor))function attributes. See the gcc info pages for information on these. Constructor routines are executed beforedlopen()returns, and destructor routines are executed beforedlclose()returns.
So, try to dig in the direction of _init and _fini.
Source: https://ru.stackoverflow.com/questions/2121/
All Articles