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).