To begin, write a basic ‘hello world’-esque program in C. You know, a printf(“hello world\n") sort of thing. After writing that program, we were to add different options to the compiler or objdump as required for each test case.
TL;DR - compilers are power tools, not be underestimated. Respect them, or they will retaliate.
Test 4 with additional integer arguments |
Test 4 with ridiculous long integer argument |
With the -fno-builtin removed from the compile as part of Test 2, the compiler uses its own built-in (ahem) optimizers to make the source code more efficient than it would be normally (with -fno-builtin enabled for compilation). In this case, the callq doesn't call printf, instead calling puts, which is a more direct way of showing output on screen than printf which has to jump through other hoops before printing to the screen. (The executable size is also slightly smaller. It also removes a mov (0x0 int %eax, so a 0 is no longer being moved into the eax register), and changes the nopw to %cs:0x0(%rax,%rax,1) from 0x0(%rar, %rar,1), which I believe is a change to where the No Operation instruction is stored (from memory to the top of the stack).
--Note, on my laptop, even though I'm doing all the processing on the Ireland server, there was no change in the object dump between enabling and disabling -fno-builtin. I had another student watch me compile, and objdump both versions with no noticeable difference. My initial thought was that my laptop's cpu being as old as it is (and 32bit to as well), may be the cause of these issues
Test 3 asked to have the -g option disabled during compilation. Without -g, debug sections of code are removed from the elf file, as is any extra debugging information provided for each section of the file. Without the debug section headers, there is a lot less code (relatively speaking), and as such the program itself is about a kilobyte smaller than with -g enabled (8k without comparable to 9k with). There is a lot more debugging information than one might think for such a small, simple program like "Hello world\n".
Test 5 required moving the printf into a separate function on its own, which causes the string and integer additions to be loaded outside the main. Instead, the compiler pushes the main straight to the stack, calls the printf function, does all instructions/work to be done with the printf function, finishes with that function, and retrieves the original information from the stack with pop %rbp. The printf function is right below the main.
The last test, test 6 called for the replacing the -O0 from the compile with
-O3, that increases optimization from nothing to [shall we say] level 3.
Doing so removes push and pop instructions, in other words, nothing
is being put on the stack for later use. O3 simply moves the
code/values directly into memory, uses xor to automatically switch
the value in the eax register to 0 (whereas -O0 moved a 0 value from
memory into that register to achieve the same goal) and instead of
the compiler using callq on address 400410 <printf@ptl> it uses
jmpq 400410 <printf@ptl>, which means it jumps directly to the
value at that address (and in this case, that's our line of text to
be printed on screen). After jumping to that memory location, the no
operation instruction is given (nop), which more or less ends the
program.
No comments:
Post a Comment