[comp.unix.wizards] dynamic linking C code with ld link editor

aed@netcom.COM (Andrew Davidson) (02/12/91)

hi

I am trying to fiqure out how to use the ld link editor to dynamicly link
some C code.  Are there any refference? All I have are the man pages 
which are less than usefull for the beginer

I am working on a sun workstation, but must also get this to run on a
sys V unix(SCO ODT UNIX sysV 3.2.0).

-- 
-----------------------------------------------------------------
                  "bede-bede-bede Thats all Folks"
				Porky Pig
Andy Davidson
Woodside CA.
aed@netcom.COM
-----------------------------------------------------------------

alter@ttidca.TTI.COM (Steve Alter) (02/13/91)

In article <23713@netcom.COM> aed@netcom.COM (Andrew Davidson) writes:
} I am trying to fiqure out how to use the ld link editor to dynamicly link
} some C code.  Are there any refference? All I have are the man pages 
} which are less than usefull for the beginer
} 
} I am working on a sun workstation, but must also get this to run on a
} sys V unix(SCO ODT UNIX sysV 3.2.0).

This info is for SunOS (4.1, but I believe also applies to 4.0.)

Load the "shlib.etc" package from your distrubution media (under 4.1
the command to do this is "/usr/etc/install/add_services") and then
look in directory /usr/lib/shlib.etc at the README and Makefile files.
That stuff only describes how to build a new version of the shared libc
library, but it shows some of the basics that might be useful/needed in
building your own library.

Highlights:

-- You have to build all of your object modules (for this library) with
   the "-pic" option to generate position-independent (a.k.a.
   relocatable) code.

-- The Makefile uses the "-assert pure-text" option on the "ld" command
   to ensure (I believe) that everything in the library is read-only,
   i.e. no writable global data areas because read-only is a must for
   anything that is shared between anonymous users.  If you want a
   global writable buffer then you've got to create a shared-memory
   segment.

-- The Makefile also plays some games to ensure that the object modules
   go into the library in the correct order.  The concepts of ranlib and
   the __.SYMDEF module don't apply to shared libraries because they're
   mapped directly into the address space of the process, whereas a
   normal library is just a collection of .o files with a symbol table
   up front and .o files still have to be linked with ld.

Read the "ld" manual-page very carefully as it explains a lot of how
the ld and ld.so programs, the LD_LIBRARY_PATH, and the version numbers
on the /usr/lib/lib*.s[ao].* files work together.

My ultimate suggestion: buy a set of the manuals; there's a whole
chapter on shared libraries in there.
-- 
Steve Alter        <alter@ttidca.tti.com>
{philabs,psivax,pyramid,quad1,rdlvax,retix,rutgers}!ttidca!alter
Transaction Technology Inc. a subsidiary of Citicorp  (213) 450-9111 x2541

kemnitz@gaia.berkeley.edu (Greg Kemnitz) (02/14/91)

I had this problem for several months until (after much hacking) I got my
dynamic loaders to work with Postgres.  It is a rather different idea than
shared libraries - the idea is to load and execute a function (whose name is
unknown beforehand) in an object file given by the user.  It is quite tricky,
since you have to know about the a.out format, and getting it to work requires
using lots of undocumented or almost undocumented features and options
(especially on DECstations).  Also, Postgres had to be able to unload and
reload a user function (since the user may have changed it) which is easy but
not entirely straightforward.

I have two versions - one for Sun 3's, Sparcs, and Sequents, and another version
for DECstations running Ultrix >= 4.0.  They all use "ld -A", but there's lots
of stuff you need to know that you only discover after much inspection.  Nobody
has a man page that is at all descriptive on "ld -A"'s use.  If there's
interest, I'll post them to the net.

-----------------------------------------------------------------------
Greg Kemnitz                  |      "I ran out of the room - I
Postgres Chief Programmer     |      didn't want to be killed by a pile
278 Cory Hall, UCB            |      of VMS manuals" :-)
(415) 642-7520                |
kemnitz@postgres.berkeley.edu |      --A friend at DEC Palo Alto in the Quake

guy@auspex.auspex.com (Guy Harris) (02/15/91)

>I had this problem for several months until (after much hacking) I got my
>dynamic loaders to work with Postgres.  It is a rather different idea than
>shared libraries - the idea is to load and execute a function (whose name is
>unknown beforehand) in an object file given by the user.

Different idea, but the SunOS 4.1/S5R4 implementation of it (and, I
think, the OSF/1 implementation of it) uses a lot of stuff from the
shared library mechanism in the OSes in question.  If you're on one of
those systems, you don't have to know about executable file formats, or
all the other obscure stuff; just use the dynamic loading mechanism that
comes with the OS.