lecture19 - CIS 450 Computer Architecture and Organization...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CIS 450 – Computer Architecture and Organization Lecture 19: Linking Mitch Neilsen (neilsen@ksu.edu ) 219D Nichols Hall
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics ± Static linking ± Dynamic linking ± Case study: Library interpositioning
Background image of page 2
Example C Program int buf[2] = {1, 2}; int main() { swap(); return 0; } main.c swap.c extern int buf[]; static int *bufp0 = &buf[0]; static int *bufp1; void swap() { int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Static Linking Programs are translated and linked using a compiler driver : ± unix> gcc -O2 -g -o p main.c swap.c ± unix> ./p Linker ( ld ) Translators ( cpp , cc1 , as ) main.c main.o Translators ( cpp , cc1 , as ) swap.c swap.o p Source files Separately compiled relocatable object files Fully linked executable object file (contains code and data for all functions defined in main.c and swap.c
Background image of page 4
Why Linkers? Reason 1: Modularity ± Program can be written as a collection of smaller source files, rather than one monolithic mass. ± Can build libraries of common functions (more on this later) z e.g., Math library, standard C library
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why Linkers? (cont) Reason 2: Efficiency ± Time: Separate Compilation z Change one source file, compile, and then relink. z No need to recompile other source files. ± Space: Libraries z Common functions can be aggregated into a single file. .. z Yet executable files and running memory images contain only code for the functions they actually use.
Background image of page 6
What Do Linkers Do? Step 1. Symbol resolution ± Programs define and reference symbols (variables and functions): z void swap() {…} /* define symbol swap */ z swap(); /* reference symbol swap */ z int *xp = &x; /* define symbol xp, reference x */ ± Symbol definitions are stored (by compiler) in a symbol table . z Symbol table is an array of structs. z Each entry includes name, size, and location of symbol. ± Linker associates each symbol reference with exactly one symbol definition.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What Do Linkers Do? (cont) 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.
Background image of page 8
1. 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. z Each .o file is produced from exactly one source (.c) file 2. Executable object file ± Contains code and data in a form that can be copied directly into memory and then executed. 3. 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 run- time. ± Called
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/09/2008 for the course CIS 450 taught by Professor Neilsen,mitch during the Spring '08 term at Kansas State University.

Page1 / 39

lecture19 - CIS 450 Computer Architecture and Organization...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online