4

My project depends of a third-party library that exports more symbols that it should. Some of those symbols are being, not intentionally, overwritten by other libraries and the main program.

How can I change the visibility of functions and variables of a compiled shared object?

yugr
  • 19,769
  • 3
  • 51
  • 96

3 Answers3

2

How can I change the visibility of functions and variables of a compiled shared object?

You could change visibility of symbols by modifying the dynamic symbol section (.dynsym) -- it contains a flat array of Elf32_Syms or Elf64_Syms, and you can overwrite the .st_info and .st_other fields in it (see this answer).

I am not aware of any tools that would make this simple, you would likely have to write a custom program to do this.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    "I am not aware of any tools that would make this simple" - there is https://github.com/yugr/SymbolHider now. – yugr Jan 05 '22 at 17:54
0

If you can load the problematic library dynamically via dlopen you may use the RTLD_DEEPBIND flag to force it to prefer local symbols to the ones in main executable or other shlibs.

If you don't want to manage dlopen and dlsym calls manually, you can create a simple wrapper file which would contain trampolines for necessary functions. Each trampoline would internally dlopen the library if it hasn't been loaded before and forward the call to dlsym-ed symbol with same name. Such wrapper file can be generated automatically via Implib.so tool.

yugr
  • 19,769
  • 3
  • 51
  • 96
0

Following EmployedRussian's advice, I've made a simple tool SymbolHider to patch the dynamic section of the executable:

$ sym-hider libmy.so sym1 sym2 ...

It can also be applied to object files.

yugr
  • 19,769
  • 3
  • 51
  • 96