class15 - 15-213 The course that gives CMU its Zip! Linking...

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

View Full Document Right Arrow Icon
Linking Oct. 15, 2002 Topics Topics Static linking Object files Static libraries Loading Dynamic linking of shared libraries class15.ppt 15-213 “The course that gives CMU its Zip!”
Background image of page 1

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

View Full DocumentRight Arrow Icon
– 2 – 15-213, F’02 Linker Puzzles int x; p1() {} int x; p2() {} int x; int y; p1() {} double x; p2() {} int x=7; int y=5; p1() {} double x; p2() {} int x=7; p1() {} int x; p2() {} int x; p1() {} p1() {}
Background image of page 2
– 3 – 15-213, F’02 A Simplistic Program Translation Scheme Problems: Efficiency: small change requires complete recompilation Modularity: hard to share common functions (e.g. printf ) Solution: Static linker (or linker) Translator m.c p ASCII source file Binary executable object file (memory image on disk)
Background image of page 3

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

View Full DocumentRight Arrow Icon
– 4 – 15-213, F’02 A Better Scheme Using a Linker Linker (ld) Translators m.c m.o Translators a.c a.o p Separately compiled relocatable object files Executable object file (contains code and data for all functions defined in m.c and a.c )
Background image of page 4
– 5 – 15-213, F’02 Translating the Example Program Compiler driver Compiler driver coordinates all steps in the translation coordinates all steps in the translation and linking process. and linking process. Typically included with each compilation system (e.g., gcc ) Invokes preprocessor ( cpp ), compiler ( cc1 ), assembler ( as ), and linker ( ld ). Passes command line arguments to appropriate phases Example: create executable Example: create executable p p from from m.c m.c and a.c a.c : bass> gcc -O2 -v -o p m.c a.c cpp [args] m.c /tmp/cca07630.i cc1 /tmp/cca07630.i m.c -O2 [args] -o /tmp/cca07630.s as [args] -o /tmp/cca076301.o /tmp/cca07630.s <similar process for a.c> ld -o p [system obj files] /tmp/cca076301.o /tmp/cca076302.o bass>
Background image of page 5

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

View Full DocumentRight Arrow Icon
– 6 – 15-213, F’02 What Does a Linker Do? Merges object files Merges multiple relocatable (. o ) object files into a single executable object file that can loaded and executed by the loader. Resolves external references Resolves external references As part of the merging process, resolves external references. External reference : reference to a symbol defined in another object file. Relocates symbols Relocates symbols Relocates symbols from their relative locations in the .o files to new absolute positions in the executable. Updates all references to these symbols to reflect their new positions. References can be in either code or data » code: a(); /* reference to symbol a */ » data: int *xp=&x; /* reference to symbol x */
Background image of page 6
– 7 – 15-213, F’02 Why Linkers? Modularity 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) e.g., Math library, standard C library Efficiency Efficiency Time: Change one source file, compile, and then relink. No need to recompile other source files. Space: Libraries of common functions can be aggregated into a single file. ..
Background image of page 7

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

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

Page1 / 30

class15 - 15-213 The course that gives CMU its Zip! Linking...

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

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