================ Start Lecture #3 ================

Interlude on Linkers

Originally called linkage editors by IBM.

What does a Linker Do?

Link of course.

When the assembler has finished it produces an object module that is almost runnable. There are two primary problems that must be solved for the object module to be runnable. Both are involved with linking (that word, again) together multiple object modules.

  1. Relocating relative addresses.


  2. Resolving external references.


The output of a linker is called a load module because it is now ready to be loaded and run.

To see how a linker works lets consider the following example, which is the first dataset from lab #1. The description in lab1 is more detailed.

The target machine is word addressable and has a memory of 1000 words, each consisting of 4 decimal digits. The first (leftmost) digit is the opcode and the remaining three digits form an address.

Each object module contains three parts, a definition list, a use list, and the program text itself. Each definition is a pair (sym, loc). Each use is a pair (sym, loc). The address in loc points to the next use or is 999 to end the chain.

For those text entries that do not form part of a use chain a fifth (leftmost) digit is added. If it is 8, the address in the word is relocatable. If it is 0 (and hence omitted), the address is absolute.

Sample input
1 xy 2
1 z 4
5 81234 5678 2999 88888 7002
0
1 z 3
6 88888 1999 1001 3002 81002 1234
0
1 z 1
2 81234 4999
1 z 2
1 xy 2
3 8000 1999 2001

I will illustrate a two-pass approach: The first pass simply produces the symbol table giving the values for xy and z (2 and 15 respectively). The second pass does the real work (using the values in the symbol table).

It is faster (less I/O) to do a one pass approach, but is harder since you need ``fix-up code'' whenever a use occurs in a module that precedes the module with the definition.

xy=2
z=15

+0
0:      81234           1234+0=1234
1:       5678           5678
2: xy:   2999   ->z     2015
3:      88888           8888+0=8888
4: ->z   7002           7015
+5
0       88888           8888+5=8893
1        1999   ->z     1015
2        1001   ->z     1015
3 ->z    3002           3015
4       81002           1002+5=1007
5        1234           1234
+11
0       81234           1245
1 ->z    4999           4015
+13
0        8000           8000
1        1999   ->xy    1002    
2 z:->xy 2001           2002

The linker on unix is mistakenly called ld (for loader), which is unfortunate since it links but does not load.

Lab #1 A Linker. Lab handed out in class and is due in three weeks 16 February. It is available on the web as well.

End of Interlude on Linkers