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

Info icon This 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!”
Image of page 1

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

View Full Document Right 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() {}
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)
Image of page 3

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

View Full Document Right 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 )
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>
Image of page 5

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

View Full Document Right 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 */
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...
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '02
  • RandalE.BryantandDavidR.O'Hallaron
  • Object file, Linker, executable object file, int *ep=&e, Math Library, a.c extern int

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern