131 Week 9 - Compilation Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are the 4 stages for creating an executable file

A
  1. Pre-processor
  2. Compiler
  3. Assembler
  4. Linker
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Role of the pre-processor

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Role of compiler

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Role of the assembler

A

Creates machine code from assembly code.
Stored in a .o file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Role of the linker

A

Combine several object files together into 1 executable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Main formats of object files

A

ELF: Executable and Linking Format (Linux)
COFF: Common Object-File Format (Windows)
Mach-O: Mac OS X (Mach Kernel)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ELF Object file

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Symbol table

A

Stores information e.g., headers of all the functions within the object file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Relocation information

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Examine object files

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Linker

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the 2 types of linking

A

Static linking and dynamic linking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Static linking

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Pros and cons of static linking

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Dynamic linking

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Pros and cons of dynamic linking

A

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

17
Q

Examine executables

A

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.

18
Q

Examine dynamic linker files

A

ldd exeFileName
ldd stands for list dynamic dependencies and will display the shared objects the executable is dynamically linked to.

19
Q

Why is the linker separate from the compiler

A

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.

20
Q

Loader

A

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.

21
Q

Creating an executable in CODAL

A

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.

22
Q

MICROBIT.hex linking and loading

A

Only static linking is supported for MICROBIT.hex.
MICROBIT.hex does not need a loader.