ld and the -A option

Chris Torek chris at umcp-cs.UUCP
Sun Nov 2 13:09:48 AEST 1986


In article <2517 at gitpyr.gatech.EDU> allen at 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 at 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 at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix.wizards mailing list