Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ut-sally!husc6!linus!ramsdell From: ramsdell@linus.UUCP (John D. Ramsdell) Newsgroups: comp.unix.wizards Subject: Re: Dynamic function loading Message-ID: <2773@linus.UUCP> Date: Tue, 14-Apr-87 14:48:33 EST Article-I.D.: linus.2773 Posted: Tue Apr 14 14:48:33 1987 Date-Received: Sun, 19-Apr-87 04:34:15 EST References: <600@rdin.UUCP> <6448@bu-cs.BU.EDU> Organization: The MITRE Corp., Bedford, MA Lines: 83 Summary: Dynamic function loading Here is a simple example giving the details of dynamic function loading using ld -A. John #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # dyn.c # fun.c # makefile # This archive created: Tue Apr 14 14:36:34 1987 export PATH; PATH=/bin:$PATH if test -f 'dyn.c' then echo shar: will not over-write existing file "'dyn.c'" else cat << \SHAR_EOF > 'dyn.c' /* Dynamically loads a function constructed using ld -A .. -T ... */ /* John D. Ramsdell -- April 1987 -- The MITRE Corporation. */ #include #include #include typedef int (*intfun)(); caddr_t fun, sbrk(); caddr_t funload (fun) caddr_t fun; { /* Loads a function from a file. */ FILE *obj; struct exec hdr[1]; int size; obj = fopen ("fun", "r"); /* Name hardwired in this example. */ fread (hdr, sizeof(*hdr), 1, obj); size = hdr->a_text + hdr->a_data + hdr->a_bss; brk (fun); /* Return of brk and sbrk should be checked. */ fun = sbrk (size); /* Allocate space. */ fread (fun, size, 1, obj); /* Load code. */ fclose (obj); return fun; } main () { printf ("Start at 0x%lx.\n", sbrk (0)); fun = (caddr_t) 0x25000; /* Must be same number used in ld command. */ printf ("Load at 0x%lx.\n", fun); fun = funload(fun); printf ("New end at 0x%lx.\n", sbrk (0)); printf ("The answer is %d.\n", (*((intfun) fun))()); } SHAR_EOF fi # end of overwriting check if test -f 'fun.c' then echo shar: will not over-write existing file "'fun.c'" else cat << \SHAR_EOF > 'fun.c' int dynamically_loaded_function () { return 42; } SHAR_EOF fi # end of overwriting check if test -f 'makefile' then echo shar: will not over-write existing file "'makefile'" else cat << \SHAR_EOF > 'makefile' all: dyn fun dyn: dyn.o makefile cc -u _end -o $@ dyn.o fun: fun.o dyn makefile ld -A dyn -N -X -o fun -T 25000 fun.o -lc SHAR_EOF fi # end of overwriting check # End of shell archive exit 0