0

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?

Community
  • 1
  • 1
MD XF
  • 7,860
  • 7
  • 40
  • 71
  • 3
    If `_start` is not called, the runtime is not initialized, so calling *any* function provided by the runtime is probably undefined. – EOF Feb 21 '17 at 17:05
  • Probbly only in cross-compilation (development) would you want to replace it with your own. But then, you will want to override the whole run time and startup with your own. – Paul Ogilvie Feb 21 '17 at 17:08
  • 1
    You cannot use a a different name for the entry point for C semantics in a conforming C program in a hosted environment. The entry point name and signature are defined by the language. It's in any case unclear what advantage you see to doing so if you could. – John Bollinger Feb 21 '17 at 17:08

1 Answers1

1

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.

Community
  • 1
  • 1
a3f
  • 8,517
  • 1
  • 41
  • 46