Compilers, linkers, loaders, relocation hardware, program counter relative addressing. source files machine language files main.c void main() {...} afunct.c main.o int afunct() {...} ===> compiler cc -o ... ===> afunct.o asub.o asub.c void asub() {...} a machine language module (.o file) 0 |---------------| | asub: | -------------------- relative to 0 | . | | | | i.e. in range 0..N | . | v v v | . | ------------------------------------ | | add a, b, c | | add | addr a | addr b | addr c | ++| call afunct | ------------------------------------ | | . | ------------------ v | . | | call | afunct | | . | ------------------ | a: | ^ | | | | b: | ---- left empty since external symbol | | | c: | | | |---------------| | relocation | | table i.e. | | symbol table: | | a, b, c local,| | afunct is | | external, | | asub is entry | | point | N |---------------| linker takes .o files and links (combines) them together, performing relocation of local symbols and filling in external symbols 0 |---------------| | main | | | | mul p, q, r | | call asub | | | | p: | | q: | N1-1| r: | -------------------- relative to N1 ....|...............| | | | i.e. in the range N1 | asub | v v v N1..N2-1 | | ------------------------------------ | add a, b, c | | add | addr a | addr b | addr c | | call afunct | ------------------------------------ | | | a: | ------------------ | b: | | call | afunct | N2-1| c: | ------------------ ....|...............| | N2 | afunct | ---- filled in with N2 | | | s: | | t: | N3-1| u: | ... |---------------| | relocation | | table for all | | symbols | |---------------| loader, depends on the architecture absolute load, take above linked file, copy into physical main memory at address M and then use relocation table at end of linked file to add M to all addresses in the file, e.g. a, b, c, p, q, r, s, t, u, afunct, asub M called the load point address architecture with relocation hardware copy linked file as is into memory, i.e. addresses in program in range 0..N3-1 CPU registers: PC (program counter) loaded with address of main base register loaded with M length register loaded with N3 as the process loaded at M runs, the CPU generates addresses to fetch and execute instructions each address generated by the CPU is checked against the length register for being in bounds if address < length register, then M is added to it before it is placed on the system bus (else the hardware generates a memory exception or trap) absolute load -- program is "stuck" where it is loaded until finished base, length -- program can be moved i.e. copied to another address M2 in main memory by only changing base register PC relative addressing -- feature of architecture and instruction set another way program can be moved ----- PC relative addressing mode bit | v --------------------- PC --> | add |.| a | b | c | --------------------- ^ | what gets put here is address(a) - current_value(PC) when this instruction is executed, the hardware adds current_value(PC) to the address for operand a before sending it to the bus program can be moved and it will still run correctly as long as PC is changed to the new load point