Lecture 013

Reason for Linking

  1. Modularity / Design (using common library)
  2. Efficiency

Static Linking

Dynamic Linking

Linking Steps

Symbol Resolution

Symbol Table: store symbol definition

Executable and Link-able Format (ELF)

Different Types of ELF

Relocatable Object File (.o)

Executable Object File (a.out)

Shared Object File (.so)

ELF Object Storage

ELF Storage 1

ELF Storage 1

ELF Storage 2

ELF Storage 2

Symbols

Global Symbols

External Symbols

Local Symbols

Not in symbol table:

static variable: exist in life time of program, but accessible only in function's scope

Step 1: Symbol Resolution

Strong Symbol: procedures and initialized globals Weak Symbol: uninitialized globals, or declared with extern.

Type Mismatch Example

Type Mismatch Example

Linking Rules

Linking Rules

  1. Multiple strong symbols are not allowed.
    • otherwise produce linker error
  2. Given a strong symbol and multiple weak symbols, the symbol is defined by strong symbol.
    • reference to weak symbol resolved to strong symbol
  3. If there are multiple weak symbols, pick an arbitrary one
  4. Linker does not do type check (including function argument type)

Linker Examples

Linker Examples

Good Practice With Global Variable

Header File

Header file: contain weak symbol that can be shared by multiple other files

Step 2: Relocation

Relocatable Object File to Executable Object File

Relocatable Object File to Executable Object File

Before relocation, global shared variables are not placed in byte code

After relocation, zero values are replaced

Loading Executable Object Files

Loading Executable Object Files

Libraries

Static Library

Static Libraries (.a archive files)

Linking with Static Library

Disadvantage

Shared Library

Dynamic Linking:

Region in file specify dynamic libraries

Dynamic Link

Dynamic Link

Dynamic Link at Load-Time

Dynamic Link at Load-Time

Dynamic Link at Run-Time 1

Dynamic Link at Run-Time 1

Dynamic Link at Run-Time 2

Dynamic Link at Run-Time 2

Dynamic Link at Run=Time

Dynamic Link at Run=Time

Table of Content