Implementation of static linker in xv6 xv6-picoc

4.3 Linkage editor

4.3.1 Implementation of static linker in xv6


The newly modified linker first reads the given object files and for each object it generates two lists, one is of defined symbols and other is of undefined entries using get undefined entries(). These lists are then processed subsequently in further stages such as symbol resolution and relocation.

4.3.2 Symbol resolution


For Symbol resolution, symbol references are resolved from each pair of object files. resolve undefined reference() takes up as an argument a list of defined symbols of some object file and another list of undefined symbols of any other object file to be linked together. It then finds out if the entry from undefined list is present in the defined list if so, it moves that symbol from list of undefined entries to defined. If such undefined entry is resolved then it sets the argindex flag of that symbol to -1. Finally after all the pairs of files are resolved, definition not found() is called. This function traverses undefined list of every object file and checks if the argindex flag of all of these symbols is set to -1. If an entry is found whose symbol entry doesn’t have argindex set to -1. It implies that some symbol reference is not resolved and prints out appropriate error message. See Appendix D for code of these functions and symbol structure definition.

4.3.3 Relocation


Before relocation, linker calculates total size of resultant binary along with individual size of data section and text section. Then do relocation() takes an argument each object file and it’s text section, data section and their respective position offset in final executable. This is calculated by taking into consideration modulo 32 byte order. That is for second object file in the list it’s text section would start exactly at the address modulo 32 which is next to after end of text section of first object file. Then it reads relocation table of that object file and finds out it’s entry in corresponding list of defined symbols. Then as mentioned in the design section it identifies the type of symbol i.e either R 386 32 or R 386 PC32. Depending upon it’s type it calculates the new offset of the corresponding symbol and puts that value at the address given by that symbol’s relocation table entry.
Finally all the sections of different object files are merged together and value of entry point is set in Program header. Also total filesize of final executable is updated