[comp.sys.sun] Dynamic linking on SunOS 4.x

jon@ee.ecn.purdue.edu (Jon Reid) (03/22/90)

I am looking for information on how to do "dynamic linking" under SunOS.
Our 4GL products have a "load function" facility to load a user-written C
function into memory and execute it, like so:

	load function "myfunc"		! load user-written function from disk
					! into already-executing program.
	output myfunc(a,b,c)		! execute user-written C function
					! as if it were a built-in function.

Under all systems, the user writes a C function which follows our special
conventions for getting arguments from and returning a value to the 4GL.

Under OS/2, the user C function is linked into a a dynamic-link library
(.DLL file) which we load using the DosLoadModule call.

Under MSDOS, the user C function is linked with a library that includes
dummy main() routine to make the linker make an executable file; the .exe
is then loaded using the Terminate-And-Stay-Resident mechanism.  Once in
memory, our 4GL can determine what routines are in the loaded .exe and
branch to them as needed.

Under VMS, we create a shared image which is loaded dynamically using the
LIB$FIND_IMAGE_SYMBOL routine.

What mechanisms do I have available to me under SunOS for this?

I thought shared libraries (.so) were the answer, but I have been unable
to create a shared library.  There seems to be no documentation on
*creating* a shared library, only documentation on using one after it
exists.  Am I missing some documentation?  The cc.1v source file for the
man page has references to a -sharedlib flag which are commented out?

The SUG (Sun User Group) tape apparently has an example of loading an
object file into an already-executing program.  This might be the answer,
but the user-written C program might need to be linked with other
libraries.

If anyone out there has dealt with this problem before and would be
willing to share your techniques with me, please drop me a note.  If
anyone has the SUG load-an-object-file example and could send it to me, I
would appreciate that also.  If I'm simply missing some essential
documentation, please point out what I'm missing.

Thanks for listening:
Jon Reid                              	   UUCP: uunet!purdue!mdbs!jon
mdbs, Inc. -- KG Software Division				   
P.O. Box 5268 (Two Executive Dr.)	   Disclaimer: I speak for me 
Lafayette, IN  47903		    Quote: "Your plan worked, Muad'dib!"

guy@uunet.uu.net (Guy Harris) (03/23/90)

>What mechanisms do I have available to me under SunOS for this?

It depends on what version of SunOS you're talking about.

If you're talking about 4.0[.x], you don't have a lot; you'd have to roll
your own, and I don't think you'll be able to get much help from the
run-time loader.  (Rob, do you have more cheerful news for him?)

If you're talking about 4.1, you have:

"dlopen()", which takes the name of a ".so" file as an argument, maps that
".so" file into your address space and links it in to your program, and
returns a "handle" to it; and get back a "handle" on it;

"dlsym()", which takes a "handle" from "dlopen()" and a character string
which is the name of a function in the ".so" file referred to by the
handle, and returns a pointer to the function that you can use to call it
(after converting it from "void *" to the appropriate function type);

"dlclose()", which takes a handle from "dlopen()" and decrements the
reference count on the ".so" file, unmapping it if it goes to zero;

and one other function whose name I can't remember.

(If you're talking about System V Release 4, you will have those same four
functions, at least on 99 44/100% of the platforms it's on.)

Unfortunately, none of this helps you if you don't have 4.1, and it's not
released yet, so neither you nor most other people have it....

>I thought shared libraries (.so) were the answer, but I have been unable
>to create a shared library.  There seems to be no documentation on
>*creating* a shared library, only documentation on using one after it
>exists.

Again, not that it'd help a lot in your current situation, but check out
the "Shared Libraries" section of the "Programming Utilities and
Libraries" part of the SunOS 4.0[.x] documentation - it discusses, among
other things, how to build a shared library.

richard@aiai.edinburgh.ac.uk (Richard Tobin) (03/27/90)

> I am looking for information on how to do "dynamic linking" under SunOS.

It's a pity that there aren't standard Unix library functions for this.
Basically, this is what you do:

(1) Decide the address at which you want to load the code.  Unfortunately
    you do not yet know the size of the code, so if the address depends on
    this (as it might if you're using malloc to get the space) you may have to
    do two passes (yuk).      

(2) Run /bin/ld to link the executable corresponding to the running
    program with the .o file containing the function you want to load (and any
    libraries it needs).  You give ld the "-A" flag, which makes the resulting
    .o file contain only the new code (ie not the stuff that was already
    running).  You also use the "-T" flag to specify the address you chose in
    step (1).

(3) Now you read the header of the new .o file to find out how big the
    text and data of the new code are. 

(4) Read in the text and data from the new .o file.

(5) Use the nm library function to find the addresses of the new functions
    you want to call from the new .o file.

If you want to load in functions that reference previously-loaded
functions, it may be possible to link against the .o file produced by the
previous load, but I haven't tried this.

>I thought shared libraries (.so) were the answer

No, shared libraries are the problem.  You must link your original program
with -Bstatic so that the dynamic link works.  (Actually, it's possible to
avoid this, but it may not be worth the effort especially if you're
interested in other versions of Unix.)

-- Richard