android NDK: objcopy --rename-sym does not work (need to rename a function in a .so file)

天大地大妈咪最大 提交于 2019-12-04 18:23:48

The easiest way to rename a function is to change the name in place without changing the length and without changing the hash value.

Keeping the same hash value is a bit tricky, you have to understand how elf_hash() works::

elfhash.c:

#include <stdio.h>

unsigned long
elf_hash(const unsigned char *name)
{
    unsigned long h = 0 , g ;
    while (*name)
    {
        h = ( h << 4 ) + * name ++ ;
        if (g = h & 0xf0000000) {
            h ^= g >> 24 ;
        }
        h &= ~g ;
    }
    return h ;
}

int main(int argc, char**argv) {
    char* name = argv[1];
    printf("[%s]\n",name);
    unsigned long hash = elf_hash(name);
    printf("0x%lx\n",hash);
    return 0;
}

[[EDIT: a newer version is at
https://github.com/18446744073709551615/reDroid/blob/master/hosttools/elfhash.c
(it finds a name with the same hash)
]]

gcc it, and the usage is:

$ ./a.out myFunc
[myFunc]
0x74ddc43
$ ./a.out myFums
[myFums]
0x74ddc43
$ ./a.out myFuoC # Note: a different hash value
[myFuoC]
0x74ddc33
$ ./a.out myFupC
[myFupC]
0x74ddc43

The relevant part of the ASCII table is:

  ! " # $ % & ' ( ) * + , - . / 
0 1 2 3 4 5 6 7 8 9 : ; < = > ? 
@ A B C D E F G H I J K L M N O 
P Q R S T U V W X Y Z [ \ ] ^ _ 
` a b c d e f g h i j k l m n o 
p q r s t u v w x y z { | } ~  

Then either

sed s/myFunc/myFums/g <libStuff.so >libStufx.so

or a manual replace via hexedit libStuff.so.

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