1

I have a requirement on changing certain symbols from shared library (.so) files like below example: i need to rename symbol abc in .so file to symbol xyz. I have a tool to replace it in header files, only challenge i am facing for changing in my .so shared libraries. For shared libraries, are there any tools or commands which can help me to disassemble .so files, update the symbols and reassemble again to .so files? For shared libraries, we need a mechanism to reconstruct symbol tables. Need help if any tools exists to do my task?

FYI: changing source is the best approach, but for short-term looking at solution which can help to replace code words at symbols level.

yugr
  • 19,769
  • 3
  • 51
  • 96
praveen
  • 21
  • 3
  • In general modifying `.so` file is very hard (e.g. you'll need to rebuild symtab hashtables). A much easier option is to redefine `xyz` in a small helper library and call `abc` inside of it. – yugr Aug 20 '21 at 19:12
  • @yugr writing helper is not easy in my case, as I need to do for thousands of helper fns. Ate there any tools which can help to rebuild symbol tbles & hash tables? – praveen Aug 21 '21 at 20:04
  • Not that I know of but you can check answers to similar question [here](https://stackoverflow.com/questions/20492225/how-to-rename-dynamic-symbols-in-arm-elf-so-file). Also note that the helpers will have very simple form in asm: `.global xyz; .extern abc; xyz: jmp abc` and can be generated automatically via script. – yugr Aug 22 '21 at 11:20

2 Answers2

4

objcopy doesn't work to rename symbols on shared libraries, only in object files (.o).

The ability to change names in shared libraries was added in Patchelf: https://github.com/NixOS/patchelf/commit/da035d6acee1e5a608aafe5f6572a67609b0198a

It should be available in the next release (after 0.17.2). Meanwhile you can build the tool following the instructions in https://github.com/NixOS/patchelf#compiling-and-testing

To use it, create a map file with two names (old and new) per line, and invoke Patchelf with:

$ patchelf --output libPatched.so --rename-dynamic-symbols map_file libOriginal.so
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

You can use objcopy from GNU binutils to do this.

objcopy --redefine-sym abc=xyz <input_so_file> [output_so]
objcopy --redefine-syms <Filename> [output_so]

https://linux.die.net/man/1/objcopy