Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!mit-eddie!bu-cs!bzs From: bzs@bu-cs.UUCP Newsgroups: comp.unix.wizards Subject: Re: Dynamic function loading Message-ID: <6448@bu-cs.BU.EDU> Date: Sun, 12-Apr-87 16:34:48 EST Article-I.D.: bu-cs.6448 Posted: Sun Apr 12 16:34:48 1987 Date-Received: Mon, 13-Apr-87 23:48:44 EST References: <600@rdin.UUCP> Organization: Boston U. Comp. Sci. Lines: 55 In-reply-to: perl@rdin.UUCP's message of 10 Apr 87 18:35:08 GMT Posting-Front-End: GNU Emacs 18.41.4 of Mon Mar 23 1987 on bu-cs (berkeley-unix) >I can dig that you use "ld -A -T" to create a file >that can be loaded into a specific location within a running process, >but what do you do to do the actual loading? Exec? What strange >arguments or calling sequences do I have to know about? No, not exec, wrong thought, bzzzt. You'll need to know the symbol you wish to link to (eg. the function in the file to be loaded.) Then grovel the sizes out of the header (see a.out.h) from the object deck and the offsets. Allocate the memory for the mess, toss the object into this (after having been relocated with ld of course.) Then make the "link". You will need to associate the code with a function pointer, depends on how your program plans to notice that it is going to use this stuff. For example, say you had a jump table: struct jtable { char *fnname; int (*fnptr)(); } jtable[MAXFNS]; then called via funcall("_dynsubr"), funcall would look something like: funcall(name) char *name; { struct jtable *jp; for(jp = jtable; jp < &jtable[MAXFNS]; jp++) if(strcmp(jp->fnname,name) == 0) { /* found it */ if(jp->fnptr == NULL) /* not loaded yet */ if((jp->fnptr = funload(name)) == NULL) return(CANTLOADERROR); return((*jp->fnptr)()); return(NOTFOUNDERROR); } Obviously you would adjust the code above to allow for args to the function and return values but it should give you the flavor (also, a linear search is unnecessary if you need something faster.) The code for funload() is too hairy to throw in off the top of my head but groveling the stuff I mentioned above should steer you in the right direction. Or, I could send you my consulting schedules :-) It's not that hard, but it takes some attention to detail. -Barry Shein, Boston University