Redefining symbols from shared library
Asked Answered
I

2

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.

Ichthyic answered 20/8, 2021 at 6:51 Comment(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.Alurd
@Alurd 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?Ichthyic
Not that I know of but you can check answers to similar question here. 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.Alurd
E
5

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
Effect answered 24/2, 2023 at 15:58 Comment(1)
This does not work for me, i.e. patchelf --output libcoi_host.so.0_2 --rename-dynamic-Symbol.map /usr/lib64/libcoi_host.so.0.bak ; ldd libcoi_host.so.0_2 not a dynamic executable Symbol.map: COIEventRegisterCallback COIEventRegisterCallback_Disabled. uname -a Linux 4.18.0-477.13.1.1.el8.x86_64 #1 SMP Wed Jun 21 02:20:19 GMT 2023 x86_64 x86_64 x86_64 GNU/LinuxAalto
L
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

Letitialetizia answered 25/1, 2022 at 7:7 Comment(1)
This doesn't work for shared libraries.Kilroy

© 2022 - 2024 — McMap. All rights reserved.