Unformatted text preview:

Compiling, linking,...•Let’s see what happens when we compile a program using gcc•Some of this material is based on section 7 of Computer Systems, A Programmer’s Perspective, by Bryant O’Hallaron.1/* main.c */void swap();int buf[2] = {1,2};int main(){swap();return 0;}/* swap.c */extern int buf[]int *bufp0 = &buf[0];int *bufp1; {int temp;bufp1 = &buf[1];temp = *bufp0;*bufp0 = *bufp1;*bufp1 = temp;}2Compiling, linking,...•What happens when you use the compiler driver gcc:gcc -O2 -g -o test main.c swap.c3Step1: Preprocessor (cpp)•The C preprocessor translates the C source file main.c into an ASCII intermediate file main.i•cpp [other args] main.c /tmp/main.i4Step2: C compiler (cc1)•Driver runs C compiler cc1 •translates main.i into an ASCII assembly language file main.s•cc1 /tmp/main.i main.c -O2 [other args] -o /tmp/main.s5Step3: assembler (as)•driver runs assembler as to translate main.s into relocatable object file main.o•as [other args] -o /tmp/main.o /tmp/main.s•Same 3 steps are executed for swap.c6Step4: Linker (ld)•driver runs linker ld to combine main.o and swap.o into the executable file test•ld -o test [sys obj files and args] /tmp/main.o /tmp/swap.o7OverviewLinker (ld)!Translators!(cpp, cc1, as)!main.c main.o Translators!(cpp, cc1, as)!swap.c swap.o p!Source files!Relocatable!object files!Fully linked!executable object file!8Run executable test•unix> ./test•shell invokes function in OS called loader•copy code and data of executable test into memory•then transfers control to the beginning of the program9Static linking•What does the static linker all do?•takes as input a collection of relocatable object files and command-line arguments•generates fully linked executable object file that can be loaded and run10Static linking•Linker performs two tasks:•symbol resolution: associates each symbol reference with exactly one symbol definition•relocation: compiler and assembler generate code and data sections that start at address 0. Linker relocates these sections: associates memory location with each symbol definition, then updates all references to point to these locations.113 Types of Object Files•file.o•Relocatable object file•Executable object file•Shared object file (libraries)12Object Files•Relocatable object filecontains binary code and data in a form that can be combined with other relocatable object files at compile time to create an executable object file13Object Files•Shared object file (libraries)special type of relocatable object file that can be loaded into memory and linked dynamically, either at load time or run time.14Object Files•Executable object filecontains binary code and data that can be copied directly into memory and executed15Object Files•Putting it all together:•Compiler and assembler generate relocatable object files, including shared object files.•Linker generates executable object files.16Object Files•Object formats vary•first Unix systems used a.out format (the term is still used)•early System V used Common Object File format (COFF)•Linux and other modern unix systems use Executable and Linkable Format (ELF).


View Full Document

UI CS 270 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?