Search code examples
cgcclinkerstatic-librariesobjcopy

Unforeseen consequences of objcopy on static libraries


Background

I have a set of three nearly identical static libraries (compiled with -fPIC), for which I am not able to have them recompiled. All libraries export the same symbols, so the only way I can bundle them together is to to modify the symbols with a prefix for each static library via objcopy, i.e.:

for i in pineapple coconut banana
do
    objcopy --prefix-symbols=${i}_ lib${i}.a
done

Now, I have three libraries with unique symbols, and can proceed to hack away writing a header to deal with the three near-duplicate header/API files for each of the libraries.


Question

Is just renaming the exported symbols of a library like this safe? Does it have any "gotchas" or unforeseen consequences that could lead to stability problems at runtime? Are all references to the symbol within the library itself automagically corrected, or is it possible that some piece of library code (other than things like dlsym() calls) will try to reference the old symbol and segfault?


Solution

  • Assuming the libraries were constructed from multiple source files, the symbols exported from the library will almost certainly be referenced by other places in the library. So changing the names of the symbols will result in link problems later.