Difference between revisions of "SPO600 Compiled C Lab"
Chris Tyler (talk | contribs) |
Chris Tyler (talk | contribs) (→Lab 2) |
||
Line 14: | Line 14: | ||
} | } | ||
− | 2. Compile the program using the GCC compiler. Include these compiler options: | + | 2. Compile the program using the GCC compiler. Include these compiler options (refer to the gcc manpage for details): |
-g # enable debugging information | -g # enable debugging information | ||
Line 31: | Line 31: | ||
4. Try to gain a basic understanding of what the compiled code is doing. | 4. Try to gain a basic understanding of what the compiled code is doing. | ||
− | 5. Recompile the code with these changes | + | 5. Recompile the code with these changes: |
− | (1) Add <code>-static</code>. Note and explain the change in size, section headers, and the function call. | + | (1) Add the compiler option <code>-static</code>. Note and explain the change in size, section headers, and the function call. |
− | (2) Remove <code>-fno-builtins</code>. Note and explain the change in the function call. | + | (2) Remove the compiler option <code>-fno-builtins</code>. Note and explain the change in the function call. |
− | (3) Remove <code>-g</code>. Note and explain the change in size, section headers, and disassembly output. | + | (3) Remove the compiler option <code>-g</code>. Note and explain the change in size, section headers, and disassembly output. |
− | (4) Add additional arguments to the <code>printf()</code> function in | + | (4) Add additional arguments to the <code>printf()</code> function in your program. Note which register each argument is placed in. (Tip: Use sequential integer arguments after the first string argument. Go up to 10 arguments and note the pattern). |
(5) Move the <code>printf()</code> call to a separate function, and call that function from <code>main()</code>. Explain the changes in the object code. | (5) Move the <code>printf()</code> call to a separate function, and call that function from <code>main()</code>. Explain the changes in the object code. | ||
Line 47: | Line 47: | ||
6. Blog about your results. Important! -- explain what you're doing so that a reader coming across your blog post understands the context. | 6. Blog about your results. Important! -- explain what you're doing so that a reader coming across your blog post understands the context. | ||
− | {{Admon/tip|Make|Learn how to use [[Make and Makefiles|make]] -- your life as a programmer will be much easier!}} | + | {{Admon/tip|Make|Learn how to use [[Make and Makefiles|make]] -- your life as a programmer will be much easier, and you'll get back years of your life!}} |
== External Resources == | == External Resources == | ||
* For a general overview of ELF, see the Wikipedia article on [http://en.wikipedia.org/wiki/Executable_and_Linkable_Format Executable and Linkable Format] | * For a general overview of ELF, see the Wikipedia article on [http://en.wikipedia.org/wiki/Executable_and_Linkable_Format Executable and Linkable Format] |
Revision as of 11:35, 17 January 2014
Lab 2
1. Write a basic C program which prints a message on the screen, Hello World!-style -- something like this:
#include <stdio.h> int main() { printf("Hello World!\n"); }
2. Compile the program using the GCC compiler. Include these compiler options (refer to the gcc manpage for details):
-g # enable debugging information -O0 # do not optimize (that's a capital letter and then the digit zero) -fno-builtins # do not use builtin function optimizations
3. The resulting binary is an ELF (Executable and Linkable Format) file, which contains multiple sections. These sections may contain object code, link tables, debugging symbols, program data (such as constants and the initial values of variables), metadata about the program and ELF sections, and comments.
Examine the binary produced by the previous step using the objdump program. These options may be useful -- see the manpage for objdump for other options:
-f # display header information for the entire file -s # display per-section summary information -d # disassemble sections containing code --source # (implies -d) show source code, if available, along with disassembly
4. Try to gain a basic understanding of what the compiled code is doing.
5. Recompile the code with these changes:
(1) Add the compiler option -static
. Note and explain the change in size, section headers, and the function call.
(2) Remove the compiler option -fno-builtins
. Note and explain the change in the function call.
(3) Remove the compiler option -g
. Note and explain the change in size, section headers, and disassembly output.
(4) Add additional arguments to the printf()
function in your program. Note which register each argument is placed in. (Tip: Use sequential integer arguments after the first string argument. Go up to 10 arguments and note the pattern).
(5) Move the printf()
call to a separate function, and call that function from main()
. Explain the changes in the object code.
(6) Remove -O0
and add -O3
to the gcc options. Note and explain the difference in the compiled code.
6. Blog about your results. Important! -- explain what you're doing so that a reader coming across your blog post understands the context.
External Resources
- For a general overview of ELF, see the Wikipedia article on Executable and Linkable Format