Outline
What is the build process
Refrences
The build-process in C
Abdelrahman Aboghanima
February 23, 2023
Abdelrahman Aboghanima The build-process in C
Outline
What is the build process
Refrences
What is the build process
Preprpcessor
Compiler
Assembler
Linker
Locator
Refrences
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
What is the build process
The process of converting the
*.c, *.h files, the human readable code, to machine code.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Preprocessor
The preprocessor provides the ability for the inclusion of header
files, macro expansions, conditional compilation, and line control.
*.c, *.h → Preprocessor → *.i
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Example
1 #ifndef TYPES H 1 /∗func1.c∗/
2 #define TYPES H 2 #include ”func1.h”
3 3 #include ”types.h”
4 typedef unsigned char u8; 4
5 5 #define X 55
6 #endif /∗ TYPES H ∗/ 6
7 u8 initialized var =55;
8 const u8 const global var = 70;
9 u8 uninitialized var ;
1 #ifndef FUNC1 H 10
2 #define FUNC1 H 11 u8 func1(u8 num)
3 #include ”types.h” 12 {
4 13 u8 local var =10;
5 u8 func1(u8 num); 14 return num+local var+X;
6 15 }
7 #endif /∗ FUNC1 H ∗/
1 $ gcc -E -P func1.c
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Preprocessor Output
1 typedef unsigned char u8;
2 u8 func1(u8 num);
3 u8 initialized_var=55;
4 const u8 const_global_var= 70;
5 u8 uninitialized_var;
6 u8 func1(u8 num)
7 {
8 u8 local_var=10;
9 return num+local_var+55;
10 }
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Compiler
The process of converting the portable C code into the architecture
specific assembly code The compiler takes a translation unit (A
preporecessed source code) and converts it to assembly code.
*.i → Compiler → *.asm
1 $ gcc -S func1.c
1 $ avr-gcc -S func1.c
1 $ arm-none-eabi-gcc -S func1.c
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
1 . file ”func1.c” 1 .cpu arm7tdmi 1 . file ”func1.c”
2 . text 2 . arch armv4t 2 SP H = 0x3e
3 . globl func1 3 .fpu softvfp 3 SP L = 0x3d
4 . type func1, @function 4 . eabi attribute 20, 1 4 SREG = 0x3f
5 func1: 5 . eabi attribute 21, 1 5 tmp reg = 0
6 .LFB0: 6 . eabi attribute 23, 3 6 zero reg =1
7 . cfi startproc 7 . eabi attribute 24, 1 7 . text
8 pushq %rbp 8 . eabi attribute 25, 1 8 . global func1
9 . cfi def cfa offset 16 9 . eabi attribute 26, 1 9 . type func1, @function
10 . cfi offset 6, −16 10 . eabi attribute 30, 6 10 func1:
11 movq %rsp, %rbp 11 . eabi attribute 34, 0 11 push r28
12 . cfi def cfa register 6 12 . eabi attribute 18, 4 12 push r29
13 movl %edi, %eax 13 . file ”func1.c” 13 push zero reg
14 movb %al, −4(%rbp) 14 . text 14 in r28, SP L
15 movzbl −4(%rbp), %eax 15 . align 2 15 in r29, SP H
16 addl $55, %eax 16 . global func1 16 /∗ prologue: function ∗/
17 popq %rbp 17 . syntax unified 17 /∗ frame size = 1 ∗/
18 . cfi def cfa 7, 8 18 .arm 18 /∗ stack size = 3 ∗/
19 ret 19 . type func1, %function 19 . L stack usage = 3
20 . cfi endproc 20 func1: 20 std Y+1,r24
21 .LFE0: 21 @ Function supports 21 ldd r24,Y+1
22 . size func1, .−func1 interworking . 22 subi r24, lo8(−(55))
23 . ident ”GCC: (Ubuntu 22 @ args = 0, pretend = 0, frame23 /∗ epilogue start ∗/
7.5.0−3ubuntu1˜18.04) =8 24 pop tmp reg
7.5.0” 23 @ frame needed = 1, 25 pop r29
24 . section uses anonymous args = 026 pop r28
.note.GNU−stack,””,@progbits
24 @ link register save eliminated27. ret
25 str fp , [sp, #−4]! 28 . size func1, .−func1
26 add fp , sp, #0 29 . ident ”GCC: (GNU) 5.4.0”
27 sub sp, sp, #12
28 mov r3 , r0
29 strb r3 , [ fp , #−5]
30 ldrb r3 , [ fp , #−5]
31 add r3 , r3 , #55
32 and r3 , r3 , #255
33 mov r0 , r3
34 add sp, fp , #0
35 @ sp needed
36 ldr fp , [sp ], #4
37 bx lr
38 . size func1, .−func1
39 . ident ”GCC: (GNU Arm
Embedded Toolchain
10.3−2021.10) 10.3.1
20210824 (release)”
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Assembler
The process of transforming the architecture assembly code to its
assocaiated machine code. The assembler takes the assembly code
and converts it to a machine code in an object file
*.asm → Assembler → *.o
1 $ gcc -c func1.s -o func1.x68.o
1 $ avr-gcc -c func1.s -o func1.avr.o
1 $ arm-none-eabi-gcc -c func1.s -o func1.arm.o
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Figure: The machine code for x86 architecture.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Figure: The machine code for avr architecture.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Figure: The machine code for arm architecture.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Linker
Definition
Linker: is a computer system program that takes one or more
object files (generated by a compiler or an assembler) and
combines them into a single executable file, library file, or another
”object” file.
*.o, *.a or *.so or *.dll → Linker → relocatable file
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Figure: The linker inputs and outputs.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Types of linking
I Static Linking
Details
The code for all routines called by your program becomes part of
the executable file.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Types of linking
I Static Linking
I Dynamic Linking
Details
Every dynamically linked program contains a small, statically linked
function that is called when the program starts. This static
function only maps the link library into memory and runs the code
that the function contains. The link library determines what are all
the dynamic libraries which the program requires along with the
names of the variables and functions needed from those libraries by
reading the information contained in sections of the library.
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Linking our program
1 $ avr-ld main.o func1.o && avr-objdump -d a.out
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
1 Disassembly of section .text:
2 00000000 <main>:
3 0: cf 93 push r28
4 2: df 93 push r29
5 4: cd b7 in r28, 0x3d ; 61
6 6: de b7 in r29, 0x3e ; 62
7 8: 80 e0 ldi r24, 0x00 ; 0
8 a: 90 e0 ldi r25, 0x00 ; 0
9 c: df 91 pop r29
10 e: cf 91 pop r28
11 10: 08 95 ret
12 00000012 <func1>:
13 12: cf 93 push r28
14 14: df 93 push r29
15 16: 00 d0 rcall .+0 ; 0x18 <func1+0x6>
16 18: cd b7 in r28, 0x3d ; 61
17 1a: de b7 in r29, 0x3e ; 62
18 1c: 8a 83 std Y+2, r24 ; 0x02
19 1e: 8a e0 ldi r24, 0x0A ; 10
20 20: 89 83 std Y+1, r24 ; 0x01
21 22: 9a 81 ldd r25, Y+2 ; 0x02
22 24: 89 81 ldd r24, Y+1 ; 0x01
23 26: 89 0f add r24, r25
24 28: 89 5c subi r24, 0xC9 ; 201
25 2a: 0f 90 pop r0
26 2c: 0f 90 pop r0
27 2e: df 91 pop r29
28 30: cf 91 pop r28
29 32: 08 95 ret
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Locator
The locator main process is mapping the memory virtual addrsses
of the sections to the physical addresses with the aid of the linker
script file.
Linker Script file
Relocatable file → Locator → Execultable file
1 SECTIONS
2 {
3 . = 0x10000;
4 .text : { *(.text) }
5 . = 0x8000000;
6 .data : { *(.data) }
7 .bss : { *(.bss) }
8 }
Listing 1: A simple linker script file
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Abdelrahman Aboghanima The build-process in C
Preprpcessor
Outline Compiler
What is the build process Assembler
Refrences Linker
Locator
Figure: Merging and placement done by linker and the locator.
Abdelrahman Aboghanima The build-process in C
Outline
What is the build process
Refrences
Refrences
Relocation (computing)
Object File
Linker
Static and Dynamic Linking
Dynamic and static linking, IBM
Linker, Bravegnu
Abdelrahman Aboghanima The build-process in C