Compiler Drivers = GCC
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking, as needed, on behalf of the user accepts options and file names as operands % gcc –O1 -g -o p main.c swap.c % man gcc
Pre-processor (cpp) .c to .i Compiler (cc1) .i file to .s Compiler Assembler (as) .s to “relocatable” object file .o driver Linker (ld) creates executable object file called a.out as default
Loader the unix shell invokes a function in the OS to call the loader Copies the code and data in the executable file into memory Transfers control to the beginning of the program
See slide #13 and #206 (3 times a charm?!)
394 Chp. 7 Linking – building a program vs program execution which runs a program
EFFICIENCY issue: Small change requires recompilation i.e. text file MODULARITY issue: hard to share common functions (i.e. printf) SOLUTION static LINKER
Compile each C program like LAB4 separately to create .o file: % gcc –c m.c % gcc –c a.c
Combined .o files into single executable, then run: % gcc m.o a.o –o p % p 395 Types of object files
Object files, created by the assembler and link editor, are binary representations of programs intended to be executed directly on a processor A relocatable object file holds code and data suitable for linking with other object files to create an executable or a shared object file. Each .o file is produced from exactly one source (.c) file An executable object file (default is a.out) holds code and data that can be copied directly into memory then executed.
A shared object file (.so file) is a special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run time. Load time: the link editor processes the shared object file with other relocatable and shared object files to create another object file Run time: the dynamic linker combines it with an executable file and other shared objects to create a process image
396 Hello World 0000000000000000
00000000004004cc
397 Static linking – What do linkers do?
Step 1. Symbol resolution Programs define and reference “symbols” (variables and functions): void swap() {…} // define symbol swap swap(); // reference to a symbol swap int *xp = &x; // define symbol xp, reference x Symbol definitions are stored (by compiler) in a “symbol table” A symbol table is an array of structs Each entry includes name, size, and location of symbol Linker associates each symbol reference with exactly one symbol definition
398 Static linking – What do linkers do?
Step 2. Relocation Merges separate code and data sections into single sections Relocates symbols from their relative locations in the .o files to their final absolute memory locations in the executable. Updates all references to these symbols to reflect their new positions.
399 Object File Format/Organization The object file formats provide parallel views of a file's contents, reflecting the differing needs of those activities ELF header (executable and linkable format) resides at the beginning and holds a “road map” describing the file's organization. Program header table Tells the system how to create a process image Files used to build a process image (execute a program) must have a program header table; relocatable files do not need one.
400 Object File Format/Organization (cont) Section header table Contains information describing the file's sections Every section has an entry in the table each entry gives information such as the section name, the section size, and so on. Sections Hold the bulk of object file information for the linking view: instructions, data, symbol table, relocation information, etc. Files used during linking must have a section header table; other object files may or may not have one.
FYI: Although the figure shows the program header table immediately after the ELF header, and the section header table following the sections, actual files may differ. Moreover, sections and segments have no specified order. Only the ELF header has a fixed position in the file. 401 ELF Object File Format (details)
402 ELF Object File Format (cont)
403 Processes (section 8.2)
What is a process? Provides each program with the illusion that is has exclusive use of the processor and memory What is a process image? Process address space
404