Loading and Executing Object Code at Runtime

Conor P. Cahill cpcahil at virtech.uucp
Sun Feb 17 03:35:27 AEST 1991


sef at kithrup.COM (Sean Eric Fagan) writes:
>*However*, you still cannot execute data; you have to execute code. 
> Consider it as an alias of forms.

Obviously you cannot execute data since it probably doesn't make much
sense as a stream of instructions.  However, if you copied a function from
code to data space and then branched throught a pointer to that data area,
it does work.  So you can execute from data space.  This works on ISC UNIX,
Bell Tech UNIX, Sun OS and several other OS's.  I don't have SCO lying
around to try, but I would bet that it does in fact work.

Here is a sample program that will verify that it works:

Two notes about the program:

	1. Yes all error checking has been removed.  I'm
	2. Yes I know that it uses non-portable stuff.


main() {
	char * addr; char test[100]; char * malloc();
	int func1(); int func2(); int (*funcp)();

	strcpy(test,"YES will appear here:     if it worked\n");

	addr = malloc(3000);
	docopy(addr,func1,func2);
	funcp = addr;		/* you will get a warning about this line */
	(*funcp)(test);
	puts(test);
	exit(0);
}
docopy(tgt,src,srcend) 
	char *tgt; char*src; char *srcend; 
{
	while( src != srcend )
		*tgt++ = *src++;
}
int func1(str) char * str; { str[22] = 'Y'; str[23] = 'E'; str[24] = 'S';}
int func2(str) char * str; { str[22] = 'N'; str[23] = 'O'; }
-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.unix.internals mailing list