C: linking Flashcards

1
Q

True or False: c source files are compiled over each other and linked in order

A

False. c source files are compiled separately into relocatable object files, which then get linked.

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

How do linkers offer modularity?

A

cus you can have multiple files duh

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

why are linkers efficient?

A

because source files are compiled separately, when you edit something you just need to recompile the files changed, and relink.

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

what is a symbol? what is the symbol table? where are symbols stored?

A

symbols are essentially variables, function names, things that get referenced and defined. its the name itself.

the symbol table holds symbols. its an array of structs. its stored in the object files (by assembler), but linkers need to resolve all symbol tables into a single one.

a symbol struct may look like this:

struct SymbolEntry {
char name[255]; // Symbol name, assuming a maximum of 255 characters
int dataType; // Data type (e.g., int, float)
void* memoryLocation; // Memory location where the symbol is stored
};

struct SymbolTable {
struct SymbolEntry entries[100]; // Array of symbol entries, assuming a max of 100 symbols
int count; // Number of entries in the table
};

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

symbol relocation

A

separate code files need to be merged into a single data section.

Relocates symbols from their relative locations in the .o files to their final absolute memory locations in the executable.

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

what is a relocatable object file?

A

(.o file)
Contains code and data in a form that can be combined with other relocatable object files to form executable object file.
each .c file becomes a .o file

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

what is an executable object file?

A

(.out file)
Contains code and data in a form that can be copied directly into memory and then executed.

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

what is a shared object file?

A

(.so on linux, .dll on windows)
Special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run- time.

essentially, they are “global” libraries that just exist to you, at any time.

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

executable and linkable format (ELF)

A

ELF object file format are a standard binary format for object files. .o, .out, .so (relocatable, executable, and shared)

also called ELF Binaries

(more info in the flash card set for elf binaries)

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

global vs external vs local symbols

A

global: symbols defined by a module that can referenced by other modules. (non-static C functions, non-static globals, aka the default)

external: symbols referenced by a module, but defined in another

local: defined and referenced exclusively by a single module. (static functions/global variables)

note: local linker symbols are not local program variables. Linker symbols are used to manage functions and data across your program.

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

Local non-static C variables vs. local static C variables

A

non static: stored on the stack

static: stored in .bss or .data

if i have a static int x = 0; in two different functions, they can both exist independently in the .data section. the symbol table may store these as x.1, x.2

regularly defined variables, however (int x = 0;), just go on the stack.

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

strong vs weak symbol

A

strong: procedures & initialized globals

weak: initialized globals

strong: int foo = 5;
p1(), cry()

weak: int foo; (notice its not initialized)

RECOMMENDED: look at slide 17 of C: Linkers for some linker puzzles

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

are multiple strong symbols allowed? if not, what error will occur

A

No. Linker error

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

ilovec.c
int foo = 0;

ihatec.c
int foo;

allowed?

A

yes. references to the weak symbol will point to the strong symbol.

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

are multiple weak symbol allowed

A

yes. it’ll pick a random one

overrideable via gcc -fno-common

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

is it advisable to use extern when referencing an external global?

A

yes. do that.

17
Q

the two step process 😳

A
  1. symbol resolution
  2. relocation aka MERGE
18
Q

Executable Object File format (im just adding it here for fun ok probably dont memorize this) (memorize the notes tho lowkey)

A

ELF header

Program header table (required for executables)

.init section

.text section

.rodata section

.data section

.bss section

.symtab

.debug

.line

.strtab

 Section header table (required for relocatables)

notes:
stack grows down (%rsp)
heap grows up (malloc, brk)
between these areas is a memory mapping for shared libraries ( .so)

below heap is read/write (.data,.bss)
below that is read only (.init, .text, .rodata)

19
Q

static libraries (.a) archive files

A

Concatenate related relocatable object files into a single file
with an index (called an archive).

linker will look for unresolved references here and link it in.

each function of a library may exist as its own object file to be added to an archive

linker links object files and archive files (otherwise known as static libraries)

ex.
libc.a: Standard C Library, 4.6 MB of 1496 object files

io, memory allocation, signal handing, string handling, random, etc.

libm.a: the C Math Library, 2 MB of 444 object files

sin cos tan log exp sqrt etc

20
Q

dynamic linking

A

Dynamic linking can also occur after program has begun
(run-time linking).
In Linux, this is done by calls to the dlopen() interface.

uses shared libraries. ex. libc.so (the c standard library usually gets dynamically linked)

The linker creates a partially linked executable with object files and from .so files, relocation and symbol table info

the dynamic linker creates a fully linked executable in memory adding in code and data sections of .so files.

Shared libraries are typically loaded using memory- mapped files, which are a feature of the VM system.
.so libraries are compiled to position-independent code (PIC) to enable mapping at any address.

21
Q

when can linking occur?
a. compile time
b. load time
c. run time
d. all of the above

A

d