views:

38

answers:

4

I have two shared libraries in Linux that when built are identical in all ways except that the one function that each exposes has a different interface (one takes in some 3rd-party array types and the other takes in strings). I can build both shared library versions, and I was wondering if there was a way that I can insert meta-information into the shared library itself (much like how you are able to insert Comments/Company Name/etc at build time into a DLL, which can then be queried for at runtime).

I do not want to name the two flavors of .so files differently, because they really are the same thing and they are interchangeable pieces of a larger system. I need my code that does a dlopen and dlsym on the library to know which flavor it is so that it has the correct interface.

The kludgy way I am currently accomplishing this is by adding a no-op function called "Flavor_A" in one and "Flavor_B" in the other. Then, I dlsym Flavor_A or Flavor_B to verify which shared library I am dealing with. If I get a NULL back I know Flavor_X doesn't exist, in which case it must be the other flavor.

I figure there has to be a better way. So, that's why I'm here.

Anyone have suggestions on how to build in some runtime-queryable information into the .so binary itself?

Thanks.

A: 

That is the best way. There is no standardized way to put something into a .so other than a symbol.

bmargulies
+1  A: 

Why don't you add a GetFlavor/GetVersion method which lets you know which flavor/version it is?

Moron
+4  A: 

Instead of having two different symbols, I'd recommend having one symbol that is always there and can return the correct info.

FLAVOR getFlavor()
{
#ifdef FLAVOR_A
    return Flavor_A;
#else
    return Flavor_B;
#endif
}

You could also directly export a variable the has the correct flavor;

extern FLAVOR flavor = 
#ifdef FLAVOR_A
    Flavor_A;
#else
    Flavor_B;
#endif

For the latter, you would access the variable like this:

*(FLAVOR *)dlsym(lib, "flavor")
R Samuel Klatchko
A: 

You can add arbitrary comment sections etc, to an ELF binary with a linker script. These won't be used by the linker at runtime, but can contain information such as build data. GCC puts a few in by default to say what version was used (such as .gnu.version) .

Use objdump to examine or dump them.

MarkR