Computer Systems Organization 1
Outline 2
A software view User Interface 3
How it works 4
The gcc compilation system 5
The gcc compilation system hello.c (source code) Pre-processor (cpp) hello.i (modified source) Compiler (cc) Assembler (as) hello.s (assembly code) hello.o (object file) Linker (ld) hello (executable) 6
The Preprocessor: cpp First step: gcc compiler driver invokes cpp Output is expanded C source cpp does text substitution! Converts the C source file to another C source file! Expands #define #include #if! Output is another C source file 7
The Preprocessor: cpp 8
Macros Whitespace required Whitespace forbidden 9
Macros - Why the parens? Whitespace required Whitespace forbidden 10
Macros Just Textual Substitution Perfectly Okay No syntax error, after all! 11
Conditional Compilation 12
Compiler Assembler 13
Linker Libraries libc.a hello.o there.o Linker (ld) hello The executable file 14
The gcc compilation system hello.c (source code) Pre-processor (cpp) hello.i (modified source) Compiler (cc) Assembler (as) hello.s (assembly code) hello.o (object file) Linker (ld) hello (executable) 15
GCC variations default is a.out 16
GCC variations default is hello.i default is hello.s default is hello.o default is a.out 17
GCC variations default is hello.i default is hello.s default is hello.o default is a.out 18
GCC variations 19
The Virtual Address Space FFFFFFFF 000000000 20
The Virtual Address Space FFFFFFFF Program Code 000000000 21
The Virtual Address Space FFFFFFFF Constants Program Code 000000000 22
The Virtual Address Space FFFFFFFF Data Constants Program Code 000000000 23
The Virtual Address Space FFFFFFFF Stack Data Constants Program Code 000000000 24
The Virtual Address Space FFFFFFFF Stack Heap Data Constants Program Code 000000000 25
The Virtual Address Space FFFFFFFF Stack Memory Maped Pages Heap Data Constants Program Code 000000000 26
The Virtual Address Space FFFFFFFF Stack Memory Maped Pages Heap.data segment.text segment Read-Write Data Constants Read-Only Program Code 000000000 27
The Executable File FFFFFFFF Additional Info.data segment Read-Write.text segment Read-Only 000000000 Header 28
Why Link? 29
The linking process (ld) 30
The Linking Process Header.text Header.data Additional Info main.o.text Header.text.data Additional Info foo.o.data Additional Info Executable File 31
Resolving External References Header.text Header.data Additional Info main.o.text Header.text.data Additional Info foo.o.data Additional Info Executable File 32
Example Virtual Address Space 0xffffffff 0xc0000000 Memory used by the kernel for this process invisible to user code stack %rsp (stack pointer) 0x40000000 memory mapped region for shared library functions shared by other processes 0x08048000 0x00000000 run-time heap (managed by malloc) read/write segment (.data,.bss) read-only segment (.init,.text,.rodata) unused brk loaded from the executable file 33
Libraries and Linking 34
Three Kinds of Object Files (Modules) 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.o file is produced from exactly one source (.c) file Executable object file (a.out file) Contains code and data in a form that can be copied directly into memory and then executed. Shared object file (.so file) Special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or runtime. Called Dynamic Link Libraries (DLLs) by Windows 35
The Complete Picture main.c foo.c Translators (cc1, as) main.o Translators (cc1, as) foo.o libwhatever.a Partially linked executable (on disk) Linker (ld) myprog libc.so Shared library of dynamically relocatable object files libm.so Loader / Dynamic Linker (ld-linux.so) Fully linked executable myprog (in memory) libc.so functions called by main.c and foo.c are loaded, linked, and (potentially) shared among processes. 36
The Operating System 37
Operating system functions 38
Operating system functions 39
Unix file system 40
Unix file systems 41
Process abstraction 42
How does a program get executed? 43
Loading Executable Binaries Executable object file for example program p ELF header Program header table (required for executables).text section 0 Process image init and shared lib segments Virtual addr 0x080483e0.data section.bss section.symtab.rel.text.rel.data.debug Section header table (required for relocatables).text segment (r/o).data segment (initialized r/w).bss segment (uninitialized r/w) 0x08048494 0x0804a010 0x0804a3b0 44
Where are programs loaded in memory? 45
Where are programs loaded, cont d 46
Where are programs loaded, cont d 47
Modern linking and loading 48
The memory hierarchy 49
Memory heirarchy motivation 50
The memory heirarchy Smaller Faster More Expensive L1 L0 Registers Level 1 Cache On Chip Larger Slower Cheaper L3 L2 Level 2 Cache ( chip (off Main Memory L4 Local Secondary Storage L5 Remote Secondary Storage 51
Hardware organization CPU PC... Register File Bus Interface ALU System Bus I/O Bridge Memory Bus Main Memory I/O Bus USB Controller Graphics Controller... Disk Controller 52
Summary using hello.c 1. Shell process running, waiting for input CPU PC... Register File Bus Interface ALU System Bus I/O Bridge Memory Bus Main Memory I/O Bus USB Controller Graphics Controller... Disk Controller 53
Summary using hello.c 3. Command read into registers 4. Before sent to main memory before being read by shell process CPU Register File Memory Bus PC... Bus Interface ALU System Bus I/O Bridge Main Memory I/O Bus USB Controller Graphics Controller... Disk Controller 2. User types./hello 54
Summary using hello.c 5. Shell process creates new process through OS and initiates DMA of hello executable from disk to main memory CPU Register File Memory Bus PC... Bus Interface ALU System Bus I/O Bridge Main Memory I/O Bus USB Controller Graphics Controller... Disk Controller 55
Summary using hello.c 6. CPU executes hello code from main memory CPU PC... Register File Bus Interface ALU System Bus I/O Bridge Memory Bus Main Memory I/O Bus USB Controller Graphics Controller... Disk Controller 56
Summary using hello.c 7. CPU copies string hello, world\n from main memory to display CPU PC... Register File Bus Interface ALU System Bus I/O Bridge Memory Bus Main Memory I/O Bus USB Controller Graphics Controller... Disk Controller 57