From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10b612,54bc8baa71752026,start X-Google-Attributes: gid10b612,public X-Google-Thread: 10f9b1,54bc8baa71752026,start X-Google-Attributes: gid10f9b1,public X-Google-Thread: 1164f8,54bc8baa71752026,start X-Google-Attributes: gid1164f8,public X-Google-ArrivalTime: 1992-10-21 00:52:18 PST Xref: sparky comp.unix.wizards:4387 comp.unix.ultrix:7751 comp.sys.dec:5575 Newsgroups: comp.unix.wizards,comp.unix.ultrix,comp.sys.dec Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!dale.ksc.nasa.gov!rhill From: rhill@dale.ksc.nasa.gov (Russ Hill) Subject: DYNAMICALLY LINKING SHARED LIBRARIES? Message-ID: <1992Oct21.124129.6101@dale.ksc.nasa.gov> Organization: NASA Date: Wed, 21 Oct 1992 12:41:29 GMT I've been trying to dynamically share libraries such as (but not neccessarily) stdio at runtime. Suppose two processes use the resources of a library. Under normal circumstances, this library is linked to each of the two programs at compile time. After the link-load phase, we end up with two programs, each containing a copy of the same library. Notice that there is some redundancy here. If both programs are running, two copies of the libraries are in memory at the same time. This wastes valuable memory and disk space. Performance is also affected because of potential page faults. My idea, although not new, is to create a library maintainer process which loads libraries into and out of memory. Running processes would then call a simple function: LibBase = OpenLibrary ("LibName"); /* normal program goes here */ CloseLibrary (LibBase); to gain or relinquish access to a library. The Librarian is responsible for creating a shared memory segment, copying the library into the shared memory, and returning the base address of the library to the calling process. When the process is finished with the library, it calls CloseLibrary(), which sends a message to the Librarian to remove the library and deallocate the memory if no one else is using it. In order to do this, I have decided that I first should try to dynamically link a library to a process at runtime. Here's the steps that I'm taking: 1. Compile the library functions into object code: cc -c *.c 2. link together all of the library functions: ld -o sharedlib *.o 3. create a dummy library with all of the global data and functions defined at the correct addresses and without any code or data defined: nm sharedlib | egrep -e 'T|D' | awk '{printf ".globl %s\n%s = \ %s\n", $3, $3, $1}' > dummy.a as -o dummy.o dummy.a ar q dummylib dummy.o 4. link the dummy library to the executable program as usual. This program should open the file containing the real library and use the system call mmap () to map a virtual address to the library. This should all be performed before any calls to the library take place. I was able to get to step three with little difficulty. However, I could go no further than that. Someone suggested that I use mmap () but I have had no success in learning what the call does and how it would be used in this context. Can anyone help me out? Is there an easier way? Is there any good documentation which covers this topic? Thanks! -- Russ Hill rhill@dale.ksc.nasa.gov (407) 633-3950