4.2. Linker Operation

LWLINK takes one or more files in supported input formats and links them into a single binary. Currently supported formats are the LWTOOLS object file format and the archive format used by LWAR. While the precise method is slightly different, linking can be conceptualized as the following steps.

  1. First, the linker loads a linking script. If no script is specified, it loads a built-in default script based on the output format selected. This script tells the linker how to lay out the various sections in the final binary.

  2. Next, the linker reads all the input files into memory. At this time, it flags any format errors in those files. It constructs a table of symbols for each object at this time.

  3. The linker then proceeds with organizing the sections loaded from each file according to the linking script. As it does so, it is able to assign addresses to each symbol defined in each object file. At this time, the linker may also collapse different instances of the same section name into a single section by appending the data from each subsequent instance of the section to the first instance of the section.

  4. Next, the linker looks through every object file for every incomplete reference. It then attempts to fully resolve that reference. If it cannot do so, it throws an error. Once a reference is resolved, the value is placed into the binary code at the specified section. It should be noted that an incomplete reference can reference either a symbol internal to the object file or an external symbol which is in the export list of another object file.

  5. If all of the above steps are successful, the linker opens the output file and actually constructs the binary.