[net.unix-wizards] ld and the -A option

allen@gitpyr.gatech.EDU (P. Allen Jensen) (10/28/86)

Can anyone explain exactly how this feature may be used ?  The man page
indicates that it may be used to generate an a.out file that may be
read into an already executing program (eg like an overlay or dynamic
loading ???).  It is not clear how this can be done.

chris@umcp-cs.UUCP (Chris Torek) (11/02/86)

In article <2517@gitpyr.gatech.EDU> allen@gitpyr.gatech.EDU (P.
Allen Jensen) writes:
>... Can anyone give me any documentation on how to make use of a
>file generated using the -A option (eg for dynamic loading or
>overlay loading)?

Two years ago, padpowell@wateng (PAD Powell) posted a package
that used `ld -A' for dynamic loading.  I am not going to repost
it.  The idea, though, goes about like this:

	cc -o prog prog.c	# note no `-s'
	ld -A prog -T <textaddr1> newsubs.o -o tmp1
	<read tmp1 at <textaddr1>; use nlist to find symbol addresses>
	ld -A tmp1 -T <textaddr2> moresubs.o -o tmp2
	<read tmp2 at <textaddr2>; use nlist to find symbol addresses>
	ld -A tmp2 -T <textaddr3> yetmore.o -o tmp1
	<read tmp1 at <textaddr3>>

`ld' can (should) be invoked directly from `prog'.  The text
addresses should be a suitable base address into which the new code
can be read.  `Suitable' is system dependent.  4BSD Vax Unix wants
something that is a multiple of 1K.  Other machines may require
special system calls to make things text segments before they can
be run.  Other useful options are -N (keep ld from rounding up text
and data segments to 1K---but beware more system dependencies) and
-x (discard local symbols, e.g., those from `static' function
declarations).

The output files (tmp1 and tmp2) hold both the new code and the
combined set of symbols from the new subroutines and the original
program.  After the three `ld' commands above, tmp1 has all
symbols from prog, newsubs.o, moresubs.o, and yetmore.o.

Once you have found symbol addresses with nlist, you can call
the functions via C pointers:

	struct nlist nl[] = {
		{ "_foo" },
	};

	...
	nlist("tmp1", nl);
	if (nl[0].n_type == 0)
		whoops, could not find function foo
	(*(int (*)())nl[0].n_value)(arg1, arg2);	/* call foo */
	...

I am not sure if this is quite the same as what Patrick Powell's
code does, but it is bound to be similar.  Note that you need only
two output files to keep symbols forever, although Franz Lisp uses
a new one each time you call cfasl.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

bzs@bu-cs.BU.EDU (Barry Shein) (11/03/86)

>Can anyone explain exactly how this feature may be used ?  The man page
>indicates that it may be used to generate an a.out file that may be
>read into an already executing program (eg like an overlay or dynamic
>loading ???).  It is not clear how this can be done.

Yes, you (and the man page) are correct, for example Franz Lisp uses
the -A flag to relocate compiled lisp modules for loading. Similarly
I once wrote a dynamic link loader which used -A similarly.
(sorry, it was under contract, I don't own it.)

The basic idea is that you figure out where the code will want to be
loaded and build and run an 'ld -A xyzzy' command to re-locate it, the
rest is fairly straightforward (after staring at things for a while.)
You may want to have a look at the -T option also.

	-Barry Shein, Boston University