Loading and Executing Object Code at Runtime

Ugo Manfredi um at bologics.UUCP
Thu Feb 21 05:14:03 AEST 1991


In article <1991Feb13.212704.7016 at cec1.wustl.edu>, flan at 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 <addr> via the sbrk() routine. 
   Eventually round at a page boundary.

2) Use the link editor ("ld" command) 
   "ld -N -x -A <old_exec> -T <addr> -o <temp_file> <file_list> <lib_list> -lc"
   where the flags means: 
   "-N"  place data section immediately after text (see a.out)  
   "-A <old_exec>"  specifies incremental loading 
   "-T <addr>"  sets the text segment origin at <addr>
   "-x"  do not preserve local symbol
   "<file_list>" is the list of one or more .o containing your functions
   "<lib_list"   additional libraries needed to resolve externals (if any)

3) Inspect the header of <temp_file> 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 <temp_file> 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 at bologics.uucp	(Ugo Manfredi @ DS Logics) 



More information about the Comp.unix.wizards mailing list