How to rename dynamic symbols in arm elf .so file?

独自空忆成欢 提交于 2021-01-16 12:07:38

问题


I need to modify a so inside an Android APK. The task is to rename the dynamic symbols in side the so (which is the function name).

For example, change from Java_com_example_abc_.... to Java_com_yahoo_zzz_....

I try to use WinHex directly search and replace text words, the apk startup error. Seems like the .hash section also needs to be updated, but i do not know how to update the .hash section.

My question is what is the correct or preferable way to rename the dynamic Symbol?

I heave been stuck for 3 days, please help me, thank you very much!

Found a post might related, but he didn't gave the alternative solution. https://sourceware.org/ml/binutils/2006-03/msg00266.html


回答1:


I heave been stuck for 3 days, please help me,

What you are trying to achieve is effectively impossible.

ELF files have complicated internal structure, and what you want to do requires that you break it up and re-assemble the parts. An analogy would be to break an Intel CPU into transistors and re-assemble an AMD CPU from them.

Found a post might related

It is related. Quote:

Basically it does the following:

  1. loop over .dynsym and re-create .dynstr
  2. re-create the .hash table from scratch
  3. calculate new lma/vma, new memory layout (.dynstr size changed!)
  4. fix the contents of .dynamic, according to the new layout
  5. fix .rel.dyn
  6. fix .rel.plt
  7. fix .dynsym again
  8. write out the new section contents

In copy_section: exclude the rewritten sections from being copyied.

Currently it seems to create a syntactically valid ELF file, but as the distance between .plt and .got changes, the relative addressing in .plt is broken. Additionally the .got has to be fixed again.

So the author managed to glue together something that looks like an AMD CPU, but which doesn't work.

Sure, you can spend another 2 weeks to understand what the author did, and then another 3 weeks to fix the remaining broken pieces. And after that, you may get something that kind of sometimes works.

Your time is likely better spent elsewhere.




回答2:


Have a look at LIEF, but try to keep them the same length

native_lib = 'my_native_lib.so'
lib = lief.parse(native_lib)
for x in lib.exported_symbols:
    if 'Java' in x.name:
        x.name = x.name.replace('Java_com_example_abc', 'Java_com_antani_zzz')
lib.write(native_lib)  # overwrite


来源:https://stackoverflow.com/questions/20492225/how-to-rename-dynamic-symbols-in-arm-elf-so-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!