38

Recently I've been trying to debug some low-level work and I could not find the crt0.S for the compiler (avr-gcc) but I did find a crt1.S (and the same with the corresponding .o files).

What is the difference between these two files? Is crt1 something completely different or what? They both seem to have to do with something for 'bootstrapping' (setting up stack frame and such), but why the distinction?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Earlz
  • 57,517
  • 89
  • 275
  • 484

1 Answers1

42

Both crt0/crt1 do the same thing, basically do what is needed before calling main() (like initializing stack, setting irqs, etc.). You should link with one or the other but not both. They are not really libraries but really inline assembly code.

As far as I understand, crt comes in two "flavors"

  • crt1 is used on systems that support constructors and destructors (functions called before and after main and exit). In this case main is treated like a normal function call.
  • crt0 is used on systems that do not support constructors/destructors.
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
kriss
  • 21,366
  • 15
  • 89
  • 109
  • 2
    What exactly are these constructors/destructors? Is this related to C++ or somethingelse? In my answer here It worked without `crtbeginT.o` and `crtend.o` which are the consructors and destructors. – Z boson Oct 22 '14 at 17:59
  • 1
    I wrote it in my answer. Not it's not (directly) related to C++. It's functions that need to be called before main and after the end of the program. It's related to the OS the program is running on. But I'm not the best person to tell about that (never wrote such functions, others did it) and it would probably merit some answer of it's own. – kriss Oct 25 '14 at 08:56
  • @kriss, thanks for the reply. I only saw it now because you did not use @ zboson. The destructor is really called after exit? – Z boson Nov 14 '14 at 10:48