CS162 lecture notes 2/23/09 - Richard Mar (cs162-db) Linkers/Loaders Object Code- consists of three parts: text, data, stack Compile pieces of code separately; data created at run-time Dynamic linking Code contains addresses, may not be known at compile/assembly time, or separate file(s)'s addresses not known Linker combines separately compiled object files into one Loader takes object file and adjusts addresses Run-time library gives standard routines (mem allocation/mgmt) Linker/Loader - 3 steps: Determine location of each segment Calculate values of symbolic and update symbol table Scan relocation table and relocate addresses Compiler provides, along w/ object code: Segment table For each segment- name, size, base address (where assumed to be loaded) Symbol table Global definitions - labels needed in other segments; symbol, seg_name, offset from seg_base Relocation table Table of addresses that need to be fixed; address location, symbol_name, offset to symbol_address, address_length Storage Allocation Allocate space for program/process Free space when done Dynamic allocation - 2 general ways: Stack allocation (hierarchical)- restricted but simple/efficient organization: predictable; free space in one place Heap allocation - more general but less efficient, more difficult to implement organization: unpredictable; arbitrary list structures, complex data organizations allocated/free areas of storage; prone to fragmentation best-fit, next-fit, fixed-size, slab, buddy system allocation reference-count, mark-and-sweep reclamation, garbage collection Best-fit find the smallest area of free space that fits memory allocation needs reduces external fragmentation but is slower Next-fit keep a pointer to the last place memory was allocated; find the next area of free space that fits needs faster than best-fit but more external fragmentation Fixed-size fixed-size blocks of memory fast but lots of internal fragmentation Slab allocation keeps track of memory chunks for different types (caches); each cache has a different number of memory slabs associated with it no internal fragmentation but more management overhead Buddy system memory block sizes in powers of two; if block is bigger than necessary, split in half and use one of the halves faster than best-fit and less internal fragmentation than fixed-size