Chapter 6 209
Shared Library Management Routines
Initializers for Shared Libraries
C Source for libtwo.sl
#include <stdio.h>
void load() /* called after libtwo.sl loaded */
{
printf(“libtwo.sl loaded\n”);
}
void unload() /* called after libtwo.sl unloaded
*/
{
printf(“libtwo.sl unloaded\n”);
}
extern void _INITIALIZER();
void (*init_ptr)() = _INITIALIZER;
void foo()
{
printf(“foo called\n”);
}
void bar()
{
printf(“bar called\n”);
}
Here are the commands used to build these libraries:
$ cc -Aa -c +z libunits.c
$ ld -b -o libunits.sl +I _INITIALIZER libunits.o
$ cc -Aa -c +z libtwo.c
$ ld -b -o libtwo.sl +I _INITIALIZER libtwo.o
The following is an example program that loads these two libraries:
C Source for testlib2
#include <stdio.h>
#include <dl.h>
main()
{
float (*in_to_cm)(float), (*gal_to_l)(float), (*oz_to_g)(float);
void (*foo)(), (*bar)();
shl_t hndl_units, hndl_two;
/*
* Load libunits.sl and find the required symbols:
*/
if ((hndl_units = shl_load(“libunits.sl”, BIND_IMMEDIATE, 0)) ==
NULL)
perror(“shl_load: error loading libunits.sl”), exit(1);
if (shl_findsym(&hndl_units, “in_to_cm”,
TYPE_PROCEDURE, (void *) &in_to_cm))
perror(“shl_findsym: error finding in_to_cm”), exit(1);
if (shl_findsym(&hndl_units, “gal_to_l”,
TYPE_PROCEDURE, (void *) &gal_to_l))
perror(“shl_findsym: error finding gal_to_l”), exit(1);
if (shl_findsym(&hndl_units, “oz_to_g”,