From a C Program to an ARM Executable
In questa pagina 6
In questa pagina 4
The translation chain
| Step | Input to output | What it does |
|---|---|---|
| preprocessor | .c to .i |
expands #include and #define |
| compiler | .c to .s |
translates into assembly; allocates registers, optimises |
| assembler | .s to .o |
translates the instructions into machine code; expands pseudo-instructions (LDR r0, =x) |
| linker | several .o + libraries to executable |
joins the files, resolves references between files, assigns final addresses |
| loader (operating system) | executable to process | loads into memory, prepares stack and arguments, jumps to the start |
With GCC for ARM: arm-linux-gnueabihf-gcc -S prog.c produces the assembly, -c the object, with no option the executable.
Object file
It contains: header, text segment (machine code), data segment (initialised global variables), relocation information (instructions and data that depend on absolute addresses), symbol table (defined and external labels), debug information.
The assembler usually works in two passes: in the first it assigns an address to every label (so forward branches can be resolved), in the second it generates the code.
Linker
Definition (tasks of the linker)
- Places the text and data segments of all the files one after the other.
- Resolves external symbols: a
BL printfinmain.ois linked to the definition ofprintfin the library.- Corrects the addresses marked as relocatable.
Example. If main.o has bytes of text and max.o has , in the executable max.o starts bytes after the start of the text and the relocatable addresses of max.o are corrected by .
Static linking: the libraries are copied into the executable. Dynamic: the executable contains only a reference; the shared library (.so, .dll) is loaded at run time and shared among processes.
Memory of a process
From low to high addresses: text (code, read-only), static data (globals and static), heap (dynamic allocation with malloc, grows upward), free space, stack (activation records, grows downward, see Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →).
Assembly and C together
Property (assembly function callable from C). An assembly function is callable from C if it respects the calling convention (arguments in
r0-r3, result inr0,r4-r11preserved).
Example. With maximum(3, 8) the C code puts r0=3 and r1=8 and the function in max.s returns r0=8.
max.s:
.text
.global maximum @ visible to the linker
maximum: @ int maximum(int a, int b)
CMP r0, r1
MOVLT r0, r1
BX lrmain.c:
#include <stdio.h>
int maximum(int a, int b); // declaration: defined in max.s
int main(void) {
printf("%d\n", maximum(3, 8)); // prints 8
return 0;
}Compilation: gcc main.c max.s -o prog. Conversely, from assembly one calls C functions (BL printf) by placing the arguments in r0-r3 and first saving whatever is needed after the call.
Reading the assembly produced by the compiler (gcc -O1 -S) is the quickest way to see how loops, arrays and calls are translated (see Control Structures in ARM AssemblyBranch B and conditional branches, signed and unsigned condition codes; translation of if, if-else, while, for and do-while from C to ARM; conditional execution to remove short branches; switch with a jump table.Control Structures in ARM Assembly → and Arrays, Strings and Structures in ARM AssemblyArrays in memory and the address of an element; walking with an index or with a pointer; worked examples (sum, maximum, copy); zero-terminated strings and length computation; row-major matrices; structs with field offsets and alignment.Arrays, Strings and Structures in ARM Assembly →).
Typical mistakes
- Forgetting
.global: the linker reports an undefined reference. - Confusing compiler and assembler: the latter translates assembly into machine code almost one to one, the former translates a high-level language.
Versione ripasso
Chain
preprocessor (#include, #define) compiler (.c to .s, allocates registers) assembler (.s to .o, expands pseudo-instructions) linker (several .o + libraries to executable) loader (puts the program in memory, prepares stack, jumps to start). GCC: -S assembly, -c object.
Object file and linker
- Object: header, text, data, relocation information, symbol table, debug. Assembler in two passes (labels first, then code).
- Linker: concatenates segments, resolves external symbols (
BL printf), corrects relocatable addresses.main.o100 bytes of text, thenmax.ostarts at +100 and its addresses shift by 100. - Static linking copies libraries into the executable; dynamic loads shared
.so/.dllat run time.
Process memory
Low to high: text (read-only), static data, heap (grows up), free space, stack (grows down) (Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →).
Assembly with C
Callable from C if it follows the convention (r0-r3, result r0, r4-r11 preserved); .global maximum then CMP r0,r1; MOVLT r0,r1; BX lr; C declares int maximum(int,int); and gcc main.c max.s. gcc -O1 -S shows how loops, arrays and calls are compiled (Control Structures in ARM AssemblyBranch B and conditional branches, signed and unsigned condition codes; translation of if, if-else, while, for and do-while from C to ARM; conditional execution to remove short branches; switch with a jump table.Control Structures in ARM Assembly →, Arrays, Strings and Structures in ARM AssemblyArrays in memory and the address of an element; walking with an index or with a pointer; worked examples (sum, maximum, copy); zero-terminated strings and length computation; row-major matrices; structs with field offsets and alignment.Arrays, Strings and Structures in ARM Assembly →).
Typical mistakes: forgetting .global (undefined reference); confusing compiler and assembler.