Position-Independent Code
Code that runs anywhere
Code that executes correctly regardless of memory location, using PC-relative addressing rather than absolute addresses, pioneered on the 6809 and standard in modern systems.
Overview
Position-independent code (PIC) is code that executes correctly regardless of its location in memory. Rather than using absolute addresses, PIC uses relative addressing (typically PC-relative) so the code works wherever it’s loaded. The 6809 pioneered this in microprocessors.
Fast Facts
- Concept: Code works at any address
- Method: PC-relative addressing
- Pioneer: Motorola 6809 (1978)
- Modern use: Shared libraries, ASLR
Why It Matters
| Benefit | Application |
|---|---|
| Relocation | Load anywhere in memory |
| Shared libraries | One copy serves all programs |
| Security | ASLR randomises locations |
| ROM cartridges | Work in various memory maps |
6809 Implementation
; PC-relative data access
LEAX DATA,PCR ; Load effective address, PC-relative
LDA ,X ; Access data
; Relative branching
BRA LABEL ; Always relative on 6809
LBRA FAR_LABEL ; Long relative branch
vs Absolute Code
| Absolute | Position-Independent |
|---|---|
JMP $8000 | BRA LABEL |
LDA $1234 | LDA OFFSET,PCR |
| Fixed location | Relocatable |
| Simpler | More flexible |
Modern Relevance
PIC is now standard:
- Shared libraries use PIC to load at any address
- ASLR randomises code location for security
- Dynamic linking relies on relocatable code
Legacy
The 6809’s PIC support was revolutionary for 1978. Concepts that seemed academic then—code that runs anywhere—are now fundamental to operating system security and efficiency.