11
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 Assembler (as) .s to “relocatable” object file .o 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 Compiler driver

Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

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 Assembler (as) .s to “relocatable” object file .o 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

Compiler driver

Page 2: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

Chp. 7 Linking – building a program vs program execution which runs a program

395

i.e. text file

like LAB4 Compile each C program 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

EFFICIENCY issue: Small change requires recompilation MODULARITY issue: hard to share common functions (i.e. printf) SOLUTION static LINKER

Page 3: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

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

Page 4: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

Hello World revisited

397

#include <stdio.h> int main() { printf("Hello World"); return 0; }

0000000000000000 <main>: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: bf 00 00 00 00 mov $0x0,%edi 9: b8 00 00 00 00 mov $0x0,%eax e: e8 00 00 00 00 callq 13 <main+0x13> 13: b8 00 00 00 00 mov $0x0,%eax 18: c9 leaveq 19: c3 retq

Object file has not been linked yet

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

Relocatable vs Executable object code

Page 5: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

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

Page 6: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

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

Page 7: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

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

Page 8: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

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

Object File Format/Organization (cont)

Page 9: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

ELF Object File Format (details)

402

Page 10: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

ELF Object File Format (cont)

403

Page 11: Compiler Drivers = GCCweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay51.pdf · Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly

Processes (section 8.2)

404

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