Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict access to symbols in shared object?

I have a plug-in in the form of a shared library (bar.so) that links into a larger program (foo). Both foo and bar.so depend on the same third party library (baz) but they need to keep their implementations of baz completely separate. So when I link foo (using the supplied object files and archives) I need it to ignore any use of baz in bar.so and vice versa.

Right now if I link foo with --trace-symbol=baz_fun where baz_fun is one of the offending symbols I get the following output:

bar.so: definition of baz_fun
foo/src.a(baz.o): reference to baz_fun

I believe this is telling me that foo is referencing baz_fun from bar.so (and execution of foo confirms this).

Solutions that I have tried:

  • Using objcopy to "localize" the symbols of interest: objcopy --localize-symbols=local.syms bar.so where local.syms contains all of the symbols of interest. I think I might just be confused here and maybe "local" doesn't mean what I think it means. Regardless, I get the same output from the link above. I should note that if I run the nm tool on bar.so prior to using objcopy all of the symbols in question have the T flag (upper-case indicating global) and after objcopy they have a t indicating they are local now. So it appears I am using objcopy correctly.
  • Compiling with -fvisibility=hidden however due to some other constraints I need to use GCC 3.3 which doesn't appear to support that feature. I might be able to upgrade to a newer version of GCC but would like confirmation that compiling with this flag will help me before heading down that road.

Other things to note:

  • I do not have access to the source code of either foo or baz
  • I would prefer to keep all of my plug-in in one shared object (bar.so). baz is actually a licensing library so I don't want it separated
like image 686
brady Avatar asked May 31 '11 23:05

brady


1 Answers

Use dlopen to load your plugin with RTLD_DEEPBIND flag.

(edit)

Please note that RTLD_DEEPBIND is Linux-specific and need glibc 2.3.4 or newer.

like image 56
n. 1.8e9-where's-my-share m. Avatar answered Nov 13 '22 01:11

n. 1.8e9-where's-my-share m.