dynamically loading static library?
A .a is an archive containing one or more .o elf objects. Readelf and objdump won't parse them. You must use ar to xtract the .o files from the archive. It is important to realize that if you are willing to spend the time writing and debugging a variant of load_elf() that can wrap one or more static libraries in a HAL you can load them dynamically and provide clients with a way to introspect their call entry points. This is nonstandard, and I can already feel the literati twitching like The Walking Jed. However, the ELF contains enough information to drop a library into a runtime environment and give properly coded client functions a way to discover the interface to the functions provided, and call them. This isn't rocket science. It is simply tedious. An important concept here is that a developer who provides the .a archive and a include suite with the idea that they are restricting your use of the libraries, is just being annoying. It is not a serious impediment to using the library, or discovering how it does it's job.
A static library is more or less just a collection of object files. If you want to use a static library in a program, you have to link the executable with it. The executable will then contain the static library (or the parts that you used).
If you want to load a static library at runtime using dlopen
, you will have to first create a dynamic library libfoo.so
containing it.
Opening a .a
file using dlopen
does not work (tested on Ubuntu 10.04). With the following example program:
#include <dlfcn.h>
#include <stdio.h>
int main()
{
void *lib_handle = dlopen("/usr/lib/libz.a",RTLD_LAZY);
printf("dlopen error=%s\n",dlerror());
printf("lib_handle=%p\n",lib_handle);
}
I get:
dlopen error=/usr/lib/libz.a: invalid ELF header
lib_handle=(nil)
while when using /usr/lib/libz.so
instead, I get:
dlopen error=(null)
lib_handle=0x19d6030
so the same code works for a shared object.