Docsity

Linking Loading Libraries - Programming Languages | CS 4610, Study notes of Programming Languages

Partial preview of the text

Download Linking Loading Libraries - Programming Languages | CS 4610 and more Study notes Programming Languages in PDF only on Docsity!

Linking, Loading, Libraries Linking, Loading, Libraries

One-Slide Summary

• We want separate compilation for

program pieces. So we must link those

compiled pieces together later. We

must resolve references from one

object to another.

• We also want to share libraries

between programs.

• We also want to typecheck separately-

compiled modules.

Separate Compilation

• Compile different parts of your program at

different times

• And then link them together later

• This is a big win

  • (^) Faster compile times on small changes
  • (^) Software Engineering (modularity)
  • (^) Independently develop different parts (libraries)

• All major languages and all big projects use

this

Pieces

• A compiled program fragment is called an

object file

• An object file contains

  • (^) Code (for methods, etc.)
  • (^) Variables (e.g., values for global variables)
  • (^) Debugging information
  • (^) References to code and data that appear

elsewhere (e.g., printf)

  • (^) Tables for organizing the above

• Object files are implicit for interpreters

Relocatable Object Files

  • (^) For this to work, a relocatable object file comes

equipped with three tables

  • (^) Import Table : points to places in the code where an external symbol (variable or method) is references - List of (external_symbol_name, where_in_code) pairs - One external_symbol_name may come up many times!
  • (^) Export Table : points to symbol definitions in the code that are exported for use by others - (^) List of (internal_symbol_name, where_in_code) pairs
  • (^) Relocation Table : points to places in the code where local symbols are referenced - (^) List of (internal_symbol_name, where_in_code) pairs - (^) One internal_symbol may come up many times!

So Many Tables

• Tables must contain

quite a bit of

information

• Tables must also be

easy to understand

• Let's see some

examples ...

Imports

extern double sqrt(double x); static double temp = 0.0; double quadratic(double a, b, c) { temp = bb – 4.0ac; if (temp >= 0.0) { goto has_roots; } throw Invalid_Argument; has_roots: return (–b + sqrt(temp)) / (2.0a); }**

0x1000 …

0x1004 push r

0x1008 call loc

sqrt Import Table: Replace address used at 0x with final location of sqrt.

Exports

extern double sqrt(double x); static double temp = 0.0; double quadratic(double a, b, c) { temp = bb – 4.0ac; if (temp >= 0.0) { goto has_roots; } throw Invalid_Argument; has_roots: return (–b + sqrt(temp)) / (2.0a); } Export Table:** We provide quadratic. If anyone else wants its, they can figure out where 0x0200 is finally relocated to. Call that new location R. They then replace all of their references to locquadratic with R.

0x0200 r1 = b

0x0204 r1 = r1 * r

0x0208 r2 = 4.

0x020c r2 = r2 * a

Big Linking Example

Where did these numbers come from?

Summary

  • (^) Your relocatable object file: main.o
    • (^) Exports main(), imports sqrt(), relocations …
  • (^) Your math library: math.o
    • (^) Exports sqrt(), relocations
    • (^) Libraries can have imports: give an example!
    • (^) In Unix, math.o lives in libmath.a and –lmath on the command line will find it
  • (^) The linker reads them in, picks a fixed final

relocation address for all code and data (

st

pass)

and then goes through and modifies every

instruction with a symbol reference (

nd

pass)

Q: TV (051 / 842)

• This 1993 television series

typically begins with the phrase

"The Truth Is Out There".

Q: TV (089 / 842)

• Name any two of the five

"Satellite Of Love" characters

mentioned by name in the first-

season opening theme song to

Mystery Science Theatre:

Something Missing?

• That was fine, but if two programs both use

math.o they will each get a copy of it

Are We Done?

• That was fine, but if two programs both use

math.o they will each get a copy of it

  • (^) You can optimize this a bit by only linking and

copying in the parts of a library that you really

need (transitive closure of dependencies), but

that’s just a band-aid

• If we run both programs we will load both

copies of math.o into memory – wasting

memory (recall: they’re identical)!

• How could we go about sharing math.o?