w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
android NDK: objcopy --rename-sym does not work (need to rename a function in a .so file)

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]
",name);
    unsigned long hash = elf_hash(name);
    printf("0x%lx
",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.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.