简体   繁体   中英

__attribute__((constructor)) how it change entry point?

I know that from the point of view of the C programming language main() is the entry point of the program.

But from the point of view of the operating system is __start at ctrt0 startup routines which are linked into a program that performs any initialization work required before calling the program's main() function (correct me if wrong here).

So we have some attributes which we can use for our functions, one of them is constructor attribute which is called before main(), who is responsible to call this function?

__attribute__((construtor))
void foo(void)
{
        puts("Constructor called by ... ?\n");
}

and how would it look in step by step call stack? Thanks!

Functions marked as "constructor" are placed in a special section in the executable file. The "start" function will then invoke those functions one by one, before it calls main .

The same with "destructor" functions, they are again placed in a special section and called by the code executing after exit is called or main returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM