CS611
Spring 2002
Programming Assignment 4
Due Sunday March 31


Write a linker for object modules produced by the as611 assembler. The linker should take two object modules, bind them together, resolve external references, and output the result as one object module.

The two input filenames will be given on the command line. The output filename should be a.exe.

If the output object module does not have an empty outsymbol table, print to stderr the names of all undefined symbols. Also, if there is a duplicate symbol, write an error message. Finally, if either input file is not structurally correct, an appropriate error message should be printed and the linker should exit. (Note: "Segmentation fault" is not an appropriate message!)

Here are the steps the linker should perform:

  1. Read in all sections of both input files and store in memory for later processing. As you read in the two sets of object code, place them physically together in memory (the file whose name appeared first on the command line should appear first in memory, and eventually in the output).

  2. Add the length of the first module's object code to all the addresses in the second module's object code (the `first' module appeared first on the command line). Remember: the relocation bits tell you where a address is in the object code.

  3. Concatenate the two sets of relocation bits together in the same order that you put the object code together. Be careful! You can't just concatenate bytes, you must concatenate bits! Knowing the lengths of the two modules' object code will be necessary. (And, no, you may not pad the object code from the first file!)

  4. Combine the two insymbol tables by installing all insymbols into a symbol table. If a symbol appears in both insymbol tables, it is a duplicately defined symbol and an error message should be printed. As you process the second insymbol table, add the length of the first module's object code to the offset fields.

  5. Process the two outsymbol tables. As you process the second outsymbol table, add the length of the first module's object code to the offset fields. Look up each symbol which is in an outsymbol table in the new, combined, insymbol table you've built in memory (step 4). If it is found, pick up its address and fill in all the address fields on the chain emanating from the offset field of the outsymbol table. If the symbol was not found in the new, combined, insymbol table, the symbol remains an outsymbol. Place it in a new outsymbol table and print an error message.

  6. Write out a new object module. You've got everything you need to do this in memory.

This is not the only way to do the assignment, but it gives you an idea of what has to be done. However, the file that appears first in the command line must be placed first in the output object module.

Actually there is a subtle bug in the above description concerning the end-of-list marker in the outsymbol linked lists. Think about it!

Use the symbol table routines from as611 for the linker's symbol table.

Look in ~cs611/public/prog4 for direction on how the source code will be organized. Your task is to complete the `link' function in the file link.c.

Six public test cases (pairs of assembly language source files) are available in ~cs611/public/prog4/tests. Each case tests a different aspect of the assignment. Read the file TESTS for information about the test cases. Most of these cases are simply meant to be used to test the linker and the output is not intended to be executed by sim611. Two hidden test cases will be used to test error handling and other items not covered in the public files. Test 1 will be worth 50 points, tests 2-6 will be worth 8 points each, and the two hidden test cases will be worth 5 points each.

Your implementation must be performed using C.

Your program will be graded primarily by testing it for correct functionality. However, you may lose points if your program is not properly structured or adequately documented.

Your code should be submitted for grading from a CIS Alpha machine (e.g. cisunix.unh.edu). To turn in this assignment, type:
~cs611/bin/submit prog4 link.c

Do not turn in any other files!

Submissions can be checked by typing:
~cs611/bin/scheck prog4

To receive full credit for the assignment, you must turn in your files prior to 8am on Monday April 1. Late submissions will be accepted at the penalty of 5 points per day up to one week late.

Your programs will be graded using an CIS Alpha machine (e.g. cisunix.unh.edu) so be sure to test in that environment.

Remember: as always you are expected to do your own work on this assignment.


Last modified on January 14, 2002.

Comments and questions should be directed to hatcher@unh.edu