210 Chapter 6
Shared Library Management Routines
Initializers for Shared Libraries
TYPE_PROCEDURE, (void *) &oz_to_g))
perror(“shl_findsym: errror finding oz_to_g”), exit(1);
/*
* Load libtwo.sl and find the required symbols:
*/
if ((hndl_two = shl_load(“libtwo.sl”, BIND_IMMEDIATE, 0)) ==
NULL)
perror(“shl_load: error loading libtwo.sl”), exit(1);
if (shl_findsym(&hndl_two, “foo”, TYPE_PROCEDURE, (void *) &foo))
perror(“shl_findsym: error finding foo”), exit(1);
if (shl_findsym(&hndl_two, “bar”, TYPE_PROCEDURE, (void *) &bar))
perror(“shl_findsym: error finding bar”), exit(1);
/*
* Call routines from libunits.sl:
*/
printf(“1.0in = %5.2fcm\n”, (*in_to_cm)(1.0));
printf(“1.0gal = %5.2fl\n”, (*gal_to_l)(1.0));
printf(“1.0oz = %5.2fg\n”, (*oz_to_g)(1.0));
/*
* Call routines from libtwo.sl:
*/
(*foo)();
(*bar)();
/*
* Unload the libraries so we can see messages displayed by
initializer:
*/
shl_unload(hndl_units);
shl_unload(hndl_two);
}
Here is the compiler command used to create the executable testlib2:
$cc -Aa -Wl,-E -o testlib2 testlib2.c init.c -ldld
Note that the -Wl,-E option is required to cause the linker to export all
symbols from the main program. This allows the shared libraries to find
the _INITIALIZER function in the main executable.
Finally, the output from runningtestlib2 is shown:
Output of testlib2
libfoo loaded
1.0in = 2.54cm
1.0gal = 3.79l
1.0oz = 28.35g
libfoo unloaded
64-bit Mode Initializers
The 64-bit mode linker support both styles of initializers:
HP-UX 10.X style: see “HP-UX-10.X-Style Initializers” and “32-bit
Mode Initializers” for more information.