Previous: Auto-library-opening usage, Up: libstubs.a

6.5.2 Interface

Implementing such a feature is no hard work if you know how - this implementation uses a (not so good known) feature of the gnu linker called set elements:

  1. You write a library entry for every library base and link this library as the last one. This means that the linker uses this library for every library base pointer that is not defined but referenced somewhere.
  2. You tell the linker to collect these library bases together into a set element.
  3. You write a function that opens all libraries in the set element at program start and cleans them up later.

Some details:

There are two object files in the library for every library base pointer. The first one is a

     struct lib
     { struct Library *base;
       char *name; };

containing the library base pointer (a ‘NULL’ pointer at program start) and a pointer to the name of the library. This name

extern char name[]

is the second object. All these structs are collected together into one single set element called

extern struct lib *__LIB_LIST__[]

To open and close the shared libraries there are two functions in libstubs:

void __initlibraries(void)


void __exitlibraries(void)

Since it is still possible to open the shared libraries by hand I had to take care about the library base pointers for libnix itself - they are used in the commandline parsers - even before anybody could open them. There exists a (library private) duplicate library base pointer for each of these. They have normal names with two underscores in front.

So don't be alarmed if some system monitor tells you that your program opened dos.library twice - this is normal behaviour, most libraries do this.

Opening libraries by hand works exactly the same way as on any other compiler: