Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!jarthur!uunet!mcsun!i2unix!bologics!um From: um@bologics.UUCP (Ugo Manfredi) Newsgroups: comp.unix.wizards Subject: Re: Loading and Executing Object Code at Runtime Summary: A possible solution (on SUN's) to the Dynamic Loading problem Keywords: help, load, run, .o, LISP Message-ID: <237@bologics.UUCP> Date: 20 Feb 91 18:14:03 GMT References: <1991Feb13.212704.7016@cec1.wustl.edu> Organization: DS logics S.r.l., Software Development, Bologna Italy Lines: 43 In article <1991Feb13.212704.7016@cec1.wustl.edu>, flan@cics.Berkeley.EDU (Ian Flanigan) writes: > I'm wondering how to load an object file (my_functions.o) at execution > time and execute a function contained therein. I know this is possible > since many flavors of LISP allow you to compile your functions and then > load the compiled versions later. Ok, what follows are guidelines that show how to realize what you need on SunOS. I have used such a schema to allow the Prolog system we (at DS Logics) are currently developing to call external functions written in C, Fortran, ecc. This schema works well on SunOS and other BSD-based operating system, while other schemas are needed, for example, on IBM R6000 AIXV3 operating system. I don't know SCO-Unix, so I cannot tell you anything on this regard. 1) Compute current program break point via the sbrk() routine. Eventually round at a page boundary. 2) Use the link editor ("ld" command) "ld -N -x -A -T -o -lc" where the flags means: "-N" place data section immediately after text (see a.out) "-A " specifies incremental loading "-T " sets the text segment origin at "-x" do not preserve local symbol "" is the list of one or more .o containing your functions " to get the size of the executable (text + data portions). 4) Allocate new memory via malloc() or valloc() routines to make room for the new code. 5) Read the executable in the allocated memory. 6) Finally to call the new functions you must obtain their addresses: the address corresponding to a given name can be obtained by reading the symbol table of or, in a simpler way, by using the nlist() function. This is the basic schema. Good luck. If you have problems you can contact me via e-mail at: um@bologics.uucp (Ugo Manfredi @ DS Logics)