ECEC 201 · Week 6
Modern Computer: Von Neumann
A C file becomes a running program in steps: the preprocessor, the compiler, the assembler, the linker, and the loader.
Week 6 of 10 · Das Frite
ECEC 201 · Week 6 · In class
A C file becomes a running program in steps: the preprocessor, the compiler, the assembler, the linker, and the loader.
- Run each step by hand once and keep the file each one writes.
- A link error names a symbol;
nmshows which file has it.
ECEC 201 · Week 6 · In class
In 64-bit Arm assembly, a function's first arguments arrive in x0 and x1 and its result leaves in x0.
- In
gdb, a recursive call is a stack of frames. - Count how far the stack pointer moves for each frame.
ECEC 201 · Week 6 · Lecture
What each step does
| Step | Does |
|---|---|
| Preprocessor | Pastes in #include files, expands macros, drops comments |
| Compiler | Turns C into assembly for this processor |
| Assembler | Turns assembly into machine code with unresolved names |
| Linker | Joins object files and libraries, gives every name an address |
| Loader | The kernel maps the program into a new process and starts it |
ECEC 201 · Week 6 · Lecture
A link error names a symbol
$ gcc main.o -o prog
main.c:(.text+0x10): undefined reference to `scale'
$ nm main.o util.o
main.o:
U scale
0000000000000000 T main
util.o:
0000000000000000 T scale
U: used here, defined elsewhere.T: defined here, in text.- The fix is to link the file with the
T, not to declare the name again.
ECEC 201 · Week 6 · Lecture
Arguments in registers
long add(long a, long b)
{
return a + b;
}
add: // gcc -O2 -S, 64-bit Arm
add x0, x0, x1 // a in x0, b in x1, result to x0
ret // return to the address in x30
- The first eight integer or pointer arguments arrive in
x0tox7. - An
intuses the low half of the same register, namedw0,w1.
ECEC 201 · Week 6 · Lecture
Stack frames in gdb
- Build with
-g.break fact,run, thencontinuea few times to go deeper. btprints one line per live call, newest first.frame 2selects an older call;p $spprints the stack pointer in that frame.- Each recursive call pushes a frame; the difference in
$spbetween neighbouring frames is that frame's size.
ECEC 201 · Week 6 · Lab
Lab
- Build a two-file program step by step with `gcc -E`, `-S`, `-c`, and a link, and fix one undefined-symbol error using `nm`.
- In the assembly of one two-argument function, mark where the arguments arrive in `x0` and `x1` and where the result leaves in `x0`. In `gdb`, stop inside a recursive function and print the backtrace. Second pass: recurse three levels deeper and compare the stack pointer between frames.
ECEC 201 · Week 6 · Lab
Lab (continued)
- Assemble the lab hand-in: The annotated assembly of the function, the `nm` line that fixed the link error, and the backtrace with the stack-pointer difference per frame.
ECEC 201 · Week 6 · Lab
Hand in
The annotated assembly of the function, the `nm` line that fixed the link error, and the backtrace with the stack-pointer difference per frame.
ECEC 201 · Week 6 · Homework
Homework
- Write the four build steps from source to executable and the file each one produces, before the lab.
ECEC 201 · Week 6 · Homework
Turn in
The four steps and their output files.
ECEC 201 · Week 6 · Read
Read
On the lab site: Module 8, Linux boots, shows the kernel starting programs. This week grades one program's build and its stack.
ECEC 201 · Week 6 · Next
Week 7
Linux shows the system as files.