Loading and Executing Object Code at Runtime

Barry Shein bzs at world.std.com
Thu Feb 14 20:43:21 AEST 1991


>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.
>
>If it's in TFM, could someone point me in the right direction, or, if it's
>trivially simple, could someone please explain how to go about it?

It's not trivially simple by any means and can be quite subtle to get
right (e.g. loading multiple functions which reference each other and
external functions.)

It also is heavily dependent on the particular version of Unix, this
is not portable, although one solution might work on several different
unixes. BSD-based systems will use "ld -A" for starters. Encore had a
routine to do this which made it relatively easy (dynaload(), which I
think originated at AT&T but was never made part of the regular
distribution? Or do I say this every two years when it comes up and
someone corrects me that it's original to Encore? Anyhow.)

In theory you should be able to do this on any Unix system, barring
hideous ideosyncractic restrictions, it's mostly a matter of how much
of a link-editor you might have to implement yourself. For starters,
if you had a trivial routine that had no external references you can
usually just read it into a malloc'd array of bytes from the .o and
jump to it.  It's the external references that make this harder than
that, you have to resolve the externals against any which are already
in the running image (e.g. printf) and load in any externals from the
libraries (e.g. libc.a) which aren't.

Your best bet is to find a piece of code which does this and read it.
KCL does this and is available in source from various FTP sources.
-- 
        -Barry Shein

Software Tool & Die    | bzs at world.std.com          | uunet!world!bzs
Purveyors to the Trade | Voice: 617-739-0202        | Login: 617-739-WRLD



More information about the Comp.unix.wizards mailing list