C: linking Flashcards
True or False: c source files are compiled over each other and linked in order
False. c source files are compiled separately into relocatable object files, which then get linked.
How do linkers offer modularity?
cus you can have multiple files duh
why are linkers efficient?
because source files are compiled separately, when you edit something you just need to recompile the files changed, and relink.
what is a symbol? what is the symbol table? where are symbols stored?
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
};
symbol relocation
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.
what is a relocatable object file?
(.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
what is an executable object file?
(.out file)
Contains code and data in a form that can be copied directly into memory and then executed.
what is a shared object file?
(.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.
executable and linkable format (ELF)
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)
global vs external vs local symbols
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.
Local non-static C variables vs. local static C variables
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.
strong vs weak symbol
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
are multiple strong symbols allowed? if not, what error will occur
No. Linker error
ilovec.c
int foo = 0;
ihatec.c
int foo;
allowed?
yes. references to the weak symbol will point to the strong symbol.
are multiple weak symbol allowed
yes. it’ll pick a random one
overrideable via gcc -fno-common