<<

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 (cc1)  .i file to .s Compiler Assembler (as)  .s to “relocatable” object file .o driver (ld)  creates object file called a.out as default

Loader  the shell invokes a function in the OS to call the 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 combines it with an executable file and other shared objects to create a process image

396 Hello World 0000000000000000

: 0: 55 push %rbp revisited 1: 48 89 e5 mov %rsp,%rbp 4: bf 00 00 00 00 mov $0x0,%edi #include Object file 9: b8 00 00 00 00 mov $0x0,%eax int main() { has not e: e8 00 00 00 00 callq 13 printf("Hello World"); been 13: b8 00 00 00 00 mov $0x0,%eax return 0; linked yet 18: c9 leaveq } 19: c3 retq

00000000004004cc

: 4004cc: 55 push %rbp Relocatable 4004cd: 48 89 e5 mov %rsp,%rbp 4004d0: bf dc 05 40 00 mov $0x4005dc,%edi vs 4004d5: b8 00 00 00 00 mov $0x0,%eax Executable 4004da: e8 e1 fe ff ff callq 4003c0 4004df: b8 00 00 00 00 mov $0x0,%eax 4004e4: c9 leaveq 4004e5: c3 retq

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 “”  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. 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 /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