Nick S
Nick S

Reputation: 1369

__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!

Upvotes: 2

Views: 900

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

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.

Upvotes: 3

Related Questions