131 Week 9 - Compilation Flashcards
What are the 4 stages for creating an executable file
- Pre-processor
- Compiler
- Assembler
- Linker
Role of the pre-processor
Identify all lines with a # (e.g., #include or #define) and resolve them to be in C e.g., pre-processor copy the headers to the program for an #include.
Results in “pure” c code as a .i file.
Role of compiler
Transforms pure C from the pre-processor into assembly code.
Assembly code is not machine code but is still human readable.
Assembly code is dependent on machine architecture.
Role of the assembler
Creates machine code from assembly code.
Stored in a .o file.
Role of the linker
Combine several object files together into 1 executable.
Main formats of object files
ELF: Executable and Linking Format (Linux)
COFF: Common Object-File Format (Windows)
Mach-O: Mac OS X (Mach Kernel)
ELF Object file
A file that contains machine code. Has the following sections:
“text” section - machine code of program
“data” section - store constants and metadata. Split into .data and .rodata (read-only data)
“bss” - block starting symbol. Stores required space for uninitialized data e.g., variables.
- symbol tables (location of functions)
- relocation information (what to modify when linking)
Symbol table
Stores information e.g., headers of all the functions within the object file.
Relocation information
Stores information about functions that are not in the object file (e.g., from libraries or other object files) and need to have their addresses resolved and added to the object file.
Examine object files
gcc -c filename.c to create an object file from the .c file. (-c means compile (first 3 stages) so linker is not used resulting in .o not .exe)
nm filename.o to inspect the object file (nm = name mangling)
Linker
Links object files and libraries. Uses relocation information to resolve all addresses in an object file. Combines symbol table and relocation information to result in a final executable file.
What are the 2 types of linking
Static linking and dynamic linking.
Static linking
When a function is needed from a library, the entire library is combined with the program at linking time.
The binding between program and library is fixed so if library is updated, it needs to be re-linked to use the new library.
Static linkers link against archives of objects (.a files).
Pros and cons of static linking
+ When you compile your program, you know what library is used and when you copying programs, you know that everything is present.
- Takes up more disk space and often more memory space.
Dynamic linking
Requires a 5th stage of compilation called the loader.
Embeds which libraries are depended on and their locations in the executable file instead of copying the library. These addresses are then resolved at runtime by the loader.
Dynamic linkers link against shared objects (.so files).
Pros and cons of dynamic linking
+ Small file size on disk. Library can be upgraded and the executable will not need to be re-linked. Programs can share libraries in memory with memory management.
- Impacts of changing libraries is not always clear.
Examine executables
gcc -o exeFileName fileName.c to compile to executable
nm exeFileName | more to inspect the executable
use | more so that it will be easier to read - creates pages you can scroll through.
Examine dynamic linker files
ldd exeFileName
ldd stands for list dynamic dependencies and will display the shared objects the executable is dynamically linked to.
Why is the linker separate from the compiler
Compiler creates object files and the linker combines them into an executable. By keeping the linker separate from the compiler, a file can be changed and re-compiled and the files can be re-linked without every single file needing to be re-compiled.
Very important for large projects with lots of files.
Loader
During runtime, the loader will find the actual address of a needed function found by the dynamic linker and resolve it in the executable so the necessary information can be found.
Creating an executable in CODAL
Very similar to creating an executable normally. Differences:
Preprocessor is prompted to include MicroBit.h by #include “MicroBit.h”
Object file (main.cpp.obj) is linked with 3 libraries to create the .hex file.
Compiled using C compiler for ARM architecture.
MICROBIT.hex linking and loading
Only static linking is supported for MICROBIT.hex.
MICROBIT.hex does not need a loader.