perl@rdin.UUCP (11/20/86)
OK, I've stared and stared and experimented and I can't figure it out. Could you please reveil the secret of how exactly this feature is used? 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? Robert Perlberg Resource Dynamics Inc. New York {philabs|delftcc}!rdin!perl
chris@mimsy.UUCP (Chris Torek) (12/08/86)
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