Phillip Stanley-Marbell
Foundations of Embedded Systems
Department of Engineering, University of Cambridge
http://physcomp.eng.cam.ac.uk
Topic 04: Low-Level C Programming and Tools for Embedded Systems
(~30 minutes)
Version 0.2020
(Video)
38
Intended Learning Outcomes for Today
2
Design your own linker script files and use map files generated by the linker
By the end of this topic, you should be able to:
Apply binary utilities (such as objdump and objcopy) to deconstruct binaries
Design C programs comprising a mixture of C and assembly language
Enumerate steps in compiling a C program to machine instructions
37
3
Next:
We will look at four dierent variants of a simple 4-line
C program to introduce the compilation and linking process
38
Anatomy of Four C Programs: Program 1
4
More on linker script files (an input) later
More on map files (an output) later
38
Anatomy of Four C Programs: Disassembling Program 1
5
Disassembled binary is 160 lines
38
Anatomy of Four C Programs: Program 2
6
Exactly the same compilation flags and steps
38
Anatomy of Four C Programs: Disassembling Program 2
7
Disassembled binary is just 38 lines. Only difference is change of function name
What will be the dierence in binaries between Program 1 and Program 2?
38
Anatomy of Four C Programs: Program 3
8
Compared to Program 1:
What will be the dierence in binaries between Program 1 and Program 3?
Absolutely no dierence
38
Anatomy of Four C Programs: Program 4
9
(Compared to Program 3:)
Disassembled binary will now be 4116 lines (vs. 160 lines for Progs. 1 and 3)
38
Memory Map and the Parts of a Binary: Terminology
10
PC + 0: addiu sp,sp,-56
PC + 4: sw s0,24(sp)
PC + 8: move s0,a0
PC + c: lui a0,0x0
PC + 10: sw s1,28(sp)
PC + 14: addiu a0,a0,0
PC + 18: li a2,16
PC + 1c: addiu v0,sp,16
PC + 20: move s1,a1
PC + 24: lui a1,0xfff4
PC + 28: sw ra,40(sp)
PC + 2c: sw s3,36(sp)
PC + 30: sw s2,32(sp)
PC + 34: swc1 $f21,48(sp)
PC + 38: swc1 $f20,52(sp)
...
PC = Interrupt Vector Table
Save registers
...
Handle interrupt
...
Restore registers
Interrupt
(e.g., network,
battery, timer,
failure, etc.)
Memory-Mapped
Registers
Main Memory
Interrupt Vector Table
(a) Example memory map.
(b) Example flow of instruction execution in the
presence of interrupts from modeled peripherals.
Heap
Stack
.text, .data, .bss
Terminology
Memory-mapped registers
Stack
Heap
.text section
.data section
.bss section
Memory map
38
11
How do we check what is
where in memory/binary?
Stack?
Heap?
.text?
.data?
.bss?
Like maps in the physical world, map files tell us what’s where in the binary
PC + 0: addiu sp,sp,-56
PC + 4: sw s0,24(sp)
PC + 8: move s0,a0
PC + c: lui a0,0x0
PC + 10: sw s1,28(sp)
PC + 14: addiu a0,a0,0
PC + 18: li a2,16
PC + 1c: addiu v0,sp,16
PC + 20: move s1,a1
PC + 24: lui a1,0xfff4
PC + 28: sw ra,40(sp)
PC + 2c: sw s3,36(sp)
PC + 30: sw s2,32(sp)
PC + 34: swc1 $f21,48(sp)
PC + 38: swc1 $f20,52(sp)
...
PC = Interrupt Vector Table
Save registers
...
Handle interrupt
...
Restore registers
Interrupt
(e.g., network,
battery, timer,
failure, etc.)
Memory-Mapped
Registers
Main Memory
Interrupt Vector Table
(a) Example memory map.
(b) Example flow of instruction execution in the
presence of interrupts from modeled peripherals.
Heap
Stack
.text, .data, .bss
38
12
Map File:
An output file generated by the linker which indicates what
symbol names are in the final binary, which object files they
came from, their sizes, and where in the binary they are
located. (Map files often have the filename extension .map)
map | mæp |!
noun!
1 a diagrammatic representation of an area of ...
38
What’s In a Binary?
1 of 4
: Understanding Map Files
13
38
What’s In a Binary?
2 of 4
: Understanding Map Files
14
Map File Lines 1 to 7:
38
What’s In a Binary?
3 of 4
: Understanding Map Files
15
Map file, lines 13 to 21:
38
What’s In a Binary?
4 of 4
: Understanding Map Files
16
Map File
38
17
Debugging Information:
Typically part of (stored in) binary, but not loaded into processor
for execution. Contains information that allows you to
correlate processor state to program source code.
38
What’s In a Binary?
1 of 2
: Debugging Information
18
“Stabs” debugging information (dumped), lines 1 to 10:
38
What’s In a Binary?
2 of 2
: Debugging Information
19
“Stabs” debugging information (dumped), lines 27 to 36:
38
20
How do we control what goes
where in memory/binary?
PC + 0: addiu sp,sp,-56
PC + 4: sw s0,24(sp)
PC + 8: move s0,a0
PC + c: lui a0,0x0
PC + 10: sw s1,28(sp)
PC + 14: addiu a0,a0,0
PC + 18: li a2,16
PC + 1c: addiu v0,sp,16
PC + 20: move s1,a1
PC + 24: lui a1,0xfff4
PC + 28: sw ra,40(sp)
PC + 2c: sw s3,36(sp)
PC + 30: sw s2,32(sp)
PC + 34: swc1 $f21,48(sp)
PC + 38: swc1 $f20,52(sp)
...
PC = Interrupt Vector Table
Save registers
...
Handle interrupt
...
Restore registers
Interrupt
(e.g., network,
battery, timer,
failure, etc.)
Memory-Mapped
Registers
Main Memory
Interrupt Vector Table
(a) Example memory map.
(b) Example flow of instruction execution in the
presence of interrupts from modeled peripherals.
Heap
Stack
.text, .data, .bss
Stack?
Heap?
.text?
.data?
.bss?
Linker command files tell the linker how to layout the contents of binaries
38
21
Linker Script File
An input command file, used by the linker, which tells the
linker how to place code in the final linked binary.
command | kəˈmænd |!
noun!
an authoritative order: it's unlikely they'll obey your commands.!
Computing an instruction or signal that causes...
(Linker Command File)
38
Linker Script Files
1 of 2
22
OUTPUT_FORMAT("coff-sh")
OUTPUT_ARCH(sh)
SECTIONS
{
.text . :
{
_text = . ;
*(.text)
*(.strings)
_etext = . ;
}
.tors :
{
___ctors = . ;
*(.ctors)
___ctors_end = . ;
___dtors = . ;
*(.dtors)
___dtors_end = . ;
}
11 lines deleted...
.bss . :
{
_bss = . ;
*(.bss)
*(COMMON)
_ebss = . ;
_end = . ;
}
}
38
Linker Script Files
2 of 2
23
OUTPUT_FORMAT("coff-sh")
OUTPUT_ARCH(sh)
SECTIONS
{
.text . :
{
_text = . ;
*(.text)
*(.strings)
_etext = . ;
}
.tors :
{
___ctors = . ;
*(.ctors)
___ctors_end = . ;
___dtors = . ;
38
24
How do we check what is
where in memory/binary?
Stack?
Heap?
.text?
.data?
.bss?
Like maps in the physical world, map files tell us what’s where in the binary
Recap
PC + 0: addiu sp,sp,-56
PC + 4: sw s0,24(sp)
PC + 8: move s0,a0
PC + c: lui a0,0x0
PC + 10: sw s1,28(sp)
PC + 14: addiu a0,a0,0
PC + 18: li a2,16
PC + 1c: addiu v0,sp,16
PC + 20: move s1,a1
PC + 24: lui a1,0xfff4
PC + 28: sw ra,40(sp)
PC + 2c: sw s3,36(sp)
PC + 30: sw s2,32(sp)
PC + 34: swc1 $f21,48(sp)
PC + 38: swc1 $f20,52(sp)
...
PC = Interrupt Vector Table
Save registers
...
Handle interrupt
...
Restore registers
Interrupt
(e.g., network,
battery, timer,
failure, etc.)
Memory-Mapped
Registers
Main Memory
Interrupt Vector Table
(a) Example memory map.
(b) Example flow of instruction execution in the
presence of interrupts from modeled peripherals.
Heap
Stack
.text, .data, .bss
38
25
How do we control what goes
where in memory/binary?
Stack?
Heap?
.text?
.data?
.bss?
Linker command files tell the linker how to layout the contents of binaries
Recap
PC + 0: addiu sp,sp,-56
PC + 4: sw s0,24(sp)
PC + 8: move s0,a0
PC + c: lui a0,0x0
PC + 10: sw s1,28(sp)
PC + 14: addiu a0,a0,0
PC + 18: li a2,16
PC + 1c: addiu v0,sp,16
PC + 20: move s1,a1
PC + 24: lui a1,0xfff4
PC + 28: sw ra,40(sp)
PC + 2c: sw s3,36(sp)
PC + 30: sw s2,32(sp)
PC + 34: swc1 $f21,48(sp)
PC + 38: swc1 $f20,52(sp)
...
PC = Interrupt Vector Table
Save registers
...
Handle interrupt
...
Restore registers
Interrupt
(e.g., network,
battery, timer,
failure, etc.)
Memory-Mapped
Registers
Main Memory
Interrupt Vector Table
(a) Example memory map.
(b) Example flow of instruction execution in the
presence of interrupts from modeled peripherals.
Heap
Stack
.text, .data, .bss
38
26
Next:
We will look at an extended example that combines C and
assembly language to implement processor initialization
and a relocated interrupt handler
38
Combining C, Assembler, and an Interrupt Handler
1 of 9
27
and…
38
Combining C, Assembler, and an Interrupt Handler
2 of 9
28
Key Concept: explicitly referencing a specific memory address (e.g., 0x8000600) in C
38
Combining C, Assembler, and an Interrupt Handler
3 of 9
29
Note 1
Note 2
38
Combining C, Assembler, and an Interrupt Handler
4 of 9
30
Recall
Key Concept: Symbols (e.g., vec_stub_begin) in assembly/C/linker script file
Note 3
38
Combining C, Assembler, and an Interrupt Handler
5 of 9
31
Note 3
38
Combining C, Assembler, and an Interrupt Handler
6 of 9
32
Note 3
38
Combining C, Assembler, and an Interrupt Handler
7 of 9
33
Note 3
38
Combining C, Assembler, and an Interrupt Handler
8 of 9
34
Note 3!!!
Key Concept: array REGSAVESTACK[] in the C code provides a place to stash saved state
38
Combining C, Assembler, and an Interrupt Handler
9 of 9
35
Key Concept: intr_hdlr() and main() have separate 32kB stacks
38
Summary
36
38
Further Reading
37
Test your understanding:
▶︎ Complete these online self-assessments on https://f-of-e.org/
▶︎ Like learning to swim, you can’t learn all you need from a textbook
Best next step: Get some practice
https://f-of-e.org/chapter-04/#exercises
Read the handout:
▶︎ Read the chapter on Topic 04 (Low-Level C for Embedded Systems)
https://f-of-e.org/chapter-04
▶︎ Coursework #1 will give you some practice with the material
https://f-of-e.org/coursework-1
38
Things to Do
38
Complete a “muddiest point” 2-question survey using this link