The linker does several things when combining object modules:
First, the command line is parsed from left to right. For each object file encountered (object files are recognized by a magic word in the header, so the linker does not care about the name), imported and exported identifiers are read from the file and inserted in a table. If a library name is given (libraries are also recognized by a magic word, there are no special naming conventions), all modules in the library are checked if an export from this module would satisfy an import from other modules. All modules where this is the case are marked. If duplicate identifiers are found, the linker issues a warning.
This procedure (parsing and reading from left to right) does mean, that a library may only satisfy references for object modules (given directly or from a library) named before that library. With the command line
ld65 crt0.o clib.lib test.o
the module test.o may not contain references to modules in the library clib.lib. If this is the case, you have to change the order of the modules on the command line:
ld65 crt0.o test.o clib.lib
Step two is, to read the configuration file, and assign start addresses for the segments and define any linker symbols (see Configuration files).
After that, the linker is ready to produce an output file. Before doing that, it checks it's data for consistency. That is, it checks for unresolved externals (if the output format is not relocatable) and for symbol type mismatches (for example a zero page symbol is imported by a module as absolute symbol).
Step four is, to write the actual target files. In this step, the linker will resolve any expressions contained in the segment data. Circular references are also detected in this step (a symbol may have a circular reference that goes unnoticed if the symbol is not used).
Step five is to output a map file with a detailed list of all modules, segments and symbols encountered.
And, last step, if you give the
twice, you get a dump of the segment data. However, this may be quite
unreadable if you're not a developer:-)