66 Chapter 3
Linker Tasks
Using Linker commands
8. Get the addresses of routines and data that are referenced in the
module.
Step 1: Determine how much space is required to load the
module.
There must be enough contiguous memory to hold the
module's text, data, and bss segments. You can make a liberal guess as to
how much memory is needed, and hope that you've guessed correctly. Or
you can be more precise by pre-linking the module and getting size
information from its header.
Step 2: Allocate the required memory and obtain its
starting address.
Typically, you use malloc(3C) to allocate the
required memory. You must modify the starting address returned by
malloc to ensure that it starts on a memory page boundary (address
MOD 4096 == 0).
Step 3: Link the module from the running application. Use
the following options when invoking the linker from the program:
-o mod_name Name of the output module that will be loaded by the
running program.
-A base_prog Tells the linker to prepare the output file for
incremental loading. Also causes the linker to include
symbol table information from base_prog in the output
file.
-R hex_addr Specifies the hexadecimal address at which the module
will be loaded. This is the address calculated in Step 2.
-N Causes the data segment to be placed immediately
after the text segment.
-e entry_pt If specified (it is optional), causes the symbol named
entry_pt to be the entry point into the module. The
location of the entry point is stored in the module's
header.
Step 4: Get information about the module's text, data, and
bss segments from the module's header.
There are two header
structures stored at the start of the file: struct header (defined in
<filehdr.h>) and struct som_exec_auxhdr (defined in
<aouthdr.h>). The required information is stored in the second header,
so to get it, a program must seek past the first header before reading the
second one.