MD XF
MD XF

Reputation: 8129

Possible drawbacks of overriding the entry point of a main program

So I was trying to set my own custom name for main in my C program, and I found this answer.

You can specify an entry point to your program using the -e flag to ld. That means you can override the entry point if you like, but you may not want to do that for a C program you intend to run normally on your machine, since start might do all kinds of OS specific stuff that's required before your program runs.

What would be the (possible) drawbacks of not calling _start from crt0.o and writing my own that simply does whatever I want it to?

Upvotes: 0

Views: 145

Answers (1)

a3f
a3f

Reputation: 8657

The entry point usually does stuff like

  • Prepare arguments and call main and handles its exit
  • Call global constructors before main and destructors after
  • Populate global variables like environ and the like
  • Initialize the C runtime, e.g. timezone, stdio streams and such
  • Maybe configure x87 to use 80-bit floating point
  • Inflate and zero .bss if your loader doesn't
  • Whatever else is necessary for hosted C programs to run on your platform

These things are tightly coupled to your C implementation, so usually you provide your own _start only when you are targeting a freestanding environment.

Upvotes: 1

Related Questions