Compile a short C program and dissect the resulting ELF with readelf, objdump, nm, and strings. Understand what the linker puts in each section and why.
Overview
You compile a provided C source file (two functions, one global variable, one string literal) and then systematically examine every major section of the resulting ELF binary. You compare the stripped and unstripped forms, locate the PLT and GOT, and explain dynamic linking in your own words.
Tools: gcc, readelf, objdump, nm, strings, strip
Time: ~90 minutes.
Setup
Create a working directory and save the following C source as lab2.c:
#include <stdio.h>
#include <string.h>
int call_count = 0;
int check_key(const char *input) {
call_count++;
return strcmp(input, "re011lab2") == 0;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <key>\n", argv[0]);
return 1;
}
if (check_key(argv[1])) {
printf("Correct\n");
return 0;
}
printf("Wrong\n");
return 1;
}
Compile it in two forms:
# Unstripped (debug-friendly)
gcc -O0 -o lab2_unstripped lab2.c
# Stripped
cp lab2_unstripped lab2_stripped
strip lab2_stripped
Part A: ELF header
Examine the ELF header of the unstripped binary:
readelf -h lab2_unstripped
Record and explain:
- What is
e_type? What does ET_EXEC or ET_DYN mean for this binary? - What is
e_machine? What CPU architecture does this binary target? - What is
e_entry? Is this the address ofmain? (Check withreadelf -s lab2_unstripped | grep mainto compare.) - What is
e_phoffande_shoff? What do these two offsets point to?
Part B: Section inventory
List all sections:
readelf -S lab2_unstripped
For each of the following sections, record: its name, type, size in bytes, and one sentence about what it contains:
.text.data.rodata.bss.symtab.strtab.dynsym.dynstr.plt.got.plt
If a section appears to be missing (zero size or absent), explain why (e.g., .bss has zero size if there are no uninitialized globals).
Part C: Symbol table
Compare symbol visibility between the two binaries:
nm lab2_unstripped
nm lab2_stripped
nm -D lab2_unstripped
nm -D lab2_stripped
Answer:
- What symbols are present in
lab2_unstrippedthat are absent fromlab2_stripped? - What symbols are present in BOTH (from
-D, dynamic symbols)? - Where is
call_countin the symbol table? What section does it belong to and why? - Where is the string
"re011lab2"? Usestrings -tx lab2_unstripped | grep re011to find its offset. What section is it in?
Part D: PLT and dynamic linking
Disassemble the binary and examine the PLT:
objdump -d lab2_unstripped | grep -A 10 '<printf@plt>'
objdump -d lab2_unstripped | grep -A 10 '<strcmp@plt>'
Look at what a PLT stub does:
- Describe in your own words what the PLT stub for
printfdoes (at least 3 steps). - What is the role of
.got.pltin this process? - After the first call to
printf, what happens the second time the PLT stub is called? (Lazy binding resolution.)
Part E: Stripped vs. unstripped comparison
Load the stripped binary and explain what changed:
readelf -S lab2_stripped | grep -E '(symtab|strtab|debug)'
nm lab2_stripped
objdump -d lab2_stripped | head -40
- Which sections were removed by
strip? List them. - Is the code in
.textthe same in both binaries? Check:objdump -d lab2_unstripped > u.asm && objdump -d lab2_stripped > s.asm && diff u.asm s.asm. What do you find? - What is the practical consequence for someone analyzing
lab2_strippedin Ghidra versuslab2_unstripped?
Lab Report
Submit a structured report with a section for each Part (A through E). Each section should include:
- The commands you ran (you do not need to include full output -- quote the relevant lines)
- Your answers to the specific questions
- At least one observation you made that surprised you or was not obvious from the Week 3 lecture
Grading
| Criterion | Points |
|---|---|
| Part A: ELF header fields correctly identified and explained | 15 |
| Part B: Section inventory complete and accurate | 25 |
| Part C: Symbol table comparison correct; call_count and string location found | 20 |
| Part D: PLT and dynamic linking explanation accurate | 25 |
| Part E: Stripped vs. unstripped difference correctly characterized | 15 |
| Total | 100 |
Lab 2 of 9. Due: end of Week 3. The sections you identify here reappear in every binary you analyze for the rest of the course.