questions on dynamic linking on SunOS

Srinivasan Jagannathan sja at cs.purdue.EDU
Sun Sep 17 13:32:39 AEST 1989


The summary of what net thought of  how dynamic loading can be done 
on Sun3s, Sun4.0 OS.

1. Sun4.0 OS DOES dynamically loading of libraries (option -Bdynamic). 
Thus while linking the loader just remembers the path name of the libraries and
dynamically loads the needed routines DURING THE EXECUTION OF THE PROGRAM. 
Btw the default option is -Bdynamic. If you wish static loading  -Bstatic
option should be used.

2. This however does not solve the application I have , which requires
dynamic loading of object files during execution and call arbitrary
functions ( decided during the execution time) from them. That is I
don't have function calls embedded in the code to exploit features 
discussed in 1.  

3. SUN4.0 OS comes with incremental loading option (-A) which allows 
one to create a object file based on the symbol table of another a.out. 
The resulting object file uses incremental loading to avoid loading routines 
which are already resolved in symbol table of the referenced a.out.

Strategy for dynamic loading
----------------------------
Let 
dl.c   : code for dynamic loader (dl is corresponding binary).
dobj.o : object file which has to be dynamically loaded. 
d      : variable which contains the name of function to be executed.

step1:    cc -Bstatic dl.c -o dl. 
Note -Bstatic flag used to force resolving of all external references 
and to statically include code for all external references.

dl.c:
-----

loadfile("dl.o");
fn=findsymbol(d);
(*fn)();


loadfile(objname)

1. valloc huge area
   p=valloc(huge area);
2. Use system command to build robjfile as follows
   sprintf(cmd,"ld -A %s -T %x -Bstatic -dc -dp -X %s -o %s -lc",dlfname,p,
           objname,robjname);
   system(cmd);
   
   Note: robjname will have code loaded incrementally w.r.t symbol table of
         dlfname. T option is used to start the text segment at address
         correspondng to p. Finally static laoding option is used.

3. Copy the file corresponding to robjname into data area starting at p.
4. Copy symbol table to some structure, and return the pointer.

findsymbol(pointer,name)

1.reference symbol table using pointer and return address of symbol 
  corresponding to name.


Other suggested strategies
--------------------------
1.Build a beast which almost does the work of the loader.
2.Use nm and grep for findsymbol avoiding need to maintain symbol table for
loaded object files.


If you are interested in actual code please send me mail.

Jags
sja at cs.purdue.edu
-- 
Jagannathan Srinivasan
{ucbvax,decvax,hplabs}!purdue!sja  -or-  sja at cs.purdue.edu



More information about the Comp.unix.wizards mailing list