0

I compiled this simple C program int main() {} in gcc in windows 10 with -O3 and -Os options. Then I objdumped the text section of generated exe file. It generated whopping 2000 lines of assembly code.

I know, compiler has to do some hidden things like fetching command line args and passing it to main, calling atexit registered function at the end of a program. But other than these two, what else does the standard says, that actually leads to this huge assembly code? Can someone explain this..

I have written some very primitive assembly codes. There a program to print hello world hardly reaches 20 lines of assembly. Obviously this is too naive, but still the difference is huge!!! So I am curious.

Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
  • *It generated whopping 2000* - doesn't sound realistic. You might have done something wrong. Other than that, you might look at what [crt0](https://en.wikipedia.org/wiki/Crt0) is doing. – Eugene Sh. Jul 14 '21 at 14:17
  • I have all the steps I done. GCC v 8.1.0 Windows 10, 64 bit machine. Compiled using `gcc test.c -O3 -Os`. Disassembled using `objdump -d a.exe -M intel >> out.txt`. Exact commands used are here. – Sourav Kannantha B Jul 14 '21 at 14:20
  • 2
    Indeed, tried this on my side, looks like dragging different library codes along... Also much of it is PE format related – Eugene Sh. Jul 14 '21 at 14:25
  • 1
    Note that `-O3 -Os` is exactly the same as `-Os` by itself. The last `-O` option on the command line wins. – zwol Jul 14 '21 at 18:32
  • 2000 lines are not that much for a thing like the CRT. The CRT has some global vars that need to be initialized. It's probably easier to look at the sources rather than the disassembly. Possibly the reverse engineering site can be more appropriate and, please, if you do ask there, include an example binary and disassembly dump for those people that don't feel like setting up a whole VM :) – Margaret Bloom Jul 14 '21 at 21:18
  • @MargaretBloom Should I include 2000 lines of disassembly code in the question? – Sourav Kannantha B Jul 15 '21 at 08:35
  • As a link to a downloadable file. And possibly the binary so that people can use other tools than `objdump`. But again, this question is more on-topic on RE. – Margaret Bloom Jul 15 '21 at 15:25

0 Answers0