How load-foreign actually works

In order to perform the dynamic linking, load-foreign first has to figure out the file containing the relocation information for the current state of the running T process. The filename is specified as the value returned by the switch reloc-file in the t-implementation-env. It then page aligns the current end of allocated memory. Next, the file to be loaded in is relocated and linked by calling on /bin/ld -A to do the job. It is at this point that the libraries are linked in.

The actual /bin/ld command that is executed looks like

/bin/ld -N -x -A rfile -T lpoint ostr ofile -o tfile lstr -lc
rfile is the file containing the current relocation information. lpoint is the load point, that is, the point in memory where the resultant relocated file is going to be loaded in. ostr is the otherString argument (optionally) passed to load-foreign. ofile is the .o file that is to be loaded in. tfile is the output file created by this /bin/ld run and contains the result of the relocation. This file also contains the new relocation information and after successful loading, the switch reloc-file is set to be this file name. tfile is created in /tmp and has a name that starts with the letters dyn. str is the library string specified in the load-foreign command.

After successful relocation, the temporary file's header is read to figure out how much extra space is required. Next, that much extra space is requested from UNIX and the data from the temporary file is read in.