ELF Plugins for Linux in C++

Dynamically Loaded C++/ELF Plugins for LINUX

Nov 24 1995
By Jeff Koftinoff, jeffk@jdkoftinoff.com
http://www.jdkoftinoff.com/


Referenced by the paper "Design Patterns for Derivatives Software" by Maarten van der Meij1, Diederik Schouten1, and Anton Eliens

Released in November 2008 under the GPLv2 or later

Please see the file COPYING for license information.

You can download the package jdkelf.tar.gz here.


- OVERVIEW -

Ok, here is an example C++ program that can exploit the cool things that we can do with ELF. With a system like this, you can dynamically extend a c++ program with new sub-classes after the program has been compiled, and without having to re-compile the program.

I am using GCC 2.7.1, libc.so.5.2.10, libdl.so.1.7.9, ld.so.1.7.9, libg++.so.27.0.2 and libstd++.so.27.0.2

It will work with GCC 2.7.0 and with older libc.so. libg++ and libstd++ seem crucial to RTTI and I haven't yet figured out what to do to compile a shared library version of libg++-2.7.1

The example code uses the C++ headers <typeinfo> <iostream.h> and <string>. if your system isn't working with typeinfo yet you can modify world.h and the test files to not use RTTI.

Take a look at dll.h and plugin.h. This stuff is really quite straightforward.

- COMPILING AND RUNNING -

To compile, run 'make'. It will make the executable "plugtest" as well as the two plug in libraries "PlugInFun.so" and "PlugInHappy.so"

To run the test program, type "./plugtest"

It will ask you for a name of a dll. You may give the full path name of one of the previously made plug in libraires. If you want, you can

export LD_LIBRARY_PATH=".:$LD_LIBRARY_PATH"
This will allow you to enter in the file name of the plug in library you want without the full path name. Probably it is best to give the full path name or put your plugins in a special place.

Once you give it a valid name, it will attempt to open that library file and create an object from the class defined within. It will then call the Show() method of that object, and then will display the C++ RTTI typeid name. After all this it will destroy all the objects created.

- GUIDELINES -

The key for this whole system to work is to:

- PROBLEMS -

Here are some problems I encountered:

If you find any info regarding these problems, please drop me a line!

- FIXED Things -

News flash! Silly me- Many of the previous 'Problems' were my fault!

I still have the problem with ld.so outputting errors via stderr when I don't want it to.

- FILES -

Files included here:

        Makefile:
                The Makefile

        world.h:
                common include file for everything

        plugtest.cc:
                main program to test plugins
                
        dll.h and dll.cc:
                Defines the DLLManager, DLLFactoryBase, and DLLFactory<T> classes

        plugin.h:
                Abstract base classes for PlugIn and PlugInFactory
                
        PlugInHappy.cc:
                A plug in class and factory, called "PlugInHappy"
                and "PlugInHappyFactory". Used to make PlugInHappy.so
                
        PlugInFun.cc            
                A plug in class and factory, called "PlugInFun"
                and "PlugInFunFactory". Used to make PlugInFun.so

e-mail: jeffk@jdkoftinoff.com

PGP/GNU PG Public Key