ld and the -A option

Chris Torek chris at mimsy.UUCP
Mon Dec 8 19:41:39 AEST 1986


In article <591 at rdin.UUCP> perl at rdin.UUCP (Robert Perlberg) writes:
>...  Just how do you load the code into a running process and how do
>you know where to load it and where the various functions within it
>start?

Every one of those questions has an answer.  The answer is different
on every machine.

On a Vax running 4BSD, load the code by reading it into a data area
that is aligned on a `page' (1K byte, or getpagesize()) boundary.
The function entry points may be found by using nlist(3) on the
ld- generated symbol table.  `ld' should be invoked with the -T
option to set the text address for the new code to start at the
address into which you will read it.  There is one small problem
here: to run ld, you need the address; to get the address, you need
the size of the code before you can call valloc(3).  One solution
is to use sbrk() directly:

		int page_size, page_offset;
		long addr;
		char txtaddr[30];
		...
		page_size = getpagesize();
		page_offset = page_size - 1;
		addr = (long) sbrk(0);
		/* align to a page boundary */
		(void) sbrk((page_size - addr) & page_offset);
		addr = (long) sbrk(0);
		sprintf(txtaddr, "%x", addr);
		...			/* run ld "-T" txtaddr ... */
		/* read a.out header, obtaining `size' */
		if (sbrk(size) != addr)
			/* trouble */
		/* seek to code */
		if (read(fd, (char *) addr, size) != size)
			/* more trouble */
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.unix.questions mailing list