Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.wizards,comp.unix.questions Subject: Re: ld and the -A option Message-ID: <4688@mimsy.UUCP> Date: Mon, 8-Dec-86 04:41:39 EST Article-I.D.: mimsy.4688 Posted: Mon Dec 8 04:41:39 1986 Date-Received: Mon, 8-Dec-86 05:41:31 EST References: <2223@bu-cs.bu-cs.BU.EDU> <591@rdin.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 39 Xref: mnetor comp.unix.wizards:287 comp.unix.questions:284 In article <591@rdin.UUCP> perl@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@mimsy.umd.edu