Android的NDK:objcopy把--rename - 均无法正常工作(需要在.so文件重命名功能) [英] android NDK: objcopy --rename-sym does not work (need to rename a function in a .so file)

查看:1825
本文介绍了Android的NDK:objcopy把--rename - 均无法正常工作(需要在.so文件重命名功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让 objcopy把--rename - 均工作

在一个新的Andr​​oid项目,我已经创建的目录JNI和文件stub.c:

In a new Android project, I have created the directory jni and the file stub.c:

#include <jni.h>
#include "dlog.h"

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
DLOG("~~~~~~~~~~~~~~~~~~~~~~~ JNI_OnLoad ~~~~~~~~~~~~~~~~~~~~~~~~~");
    return JNI_VERSION_1_6;
}
int myfunc() { return 0; }

命令〜/的/ NDK-建立-j 4 说:

[armeabi-v7a] Install        : libTest.so => libs/armeabi-v7a/libTest.so
[armeabi] Install        : libTest.so => libs/armeabi/libTest.so
[x86] Install        : libTest.so => libs/x86/libTest.so
[mips] Install        : libTest.so => libs/mips/libTest.so

(有链接:

an -> ~/android-ndk-r9d/
ax -> android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/
ay -> ~/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/

然后我

~/ax/arm-linux-androideabi-objcopy --redefine-sym myfunc=ourfunc libTest.so libTest-x.so

和获得的一样的libTest-x.so。我,当然,试图〜/ AY / * objcopy把,具有相同的结果。 我没有得到任何错误信息。而 MYFUNC()的依然存在,并没有的 ourfunc()的。

and get an identical libTest-x.so. I, of course, tried ~/ay/*objcopy, with the same result. I get no error messages. And myfunc() is still there, and no ourfunc().

如何重命名功能.so文件?

How do I rename a function in the .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.

保持相同的哈希值是有点棘手,你必须了解如何 elf_hash()作品::

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;
}

[编辑:较新的版本是
HTTPS ://github.com/18446744073709551615/reDroid/blob/master/hosttools/elfhash.c
(它找到具有相同散列的名称)
]

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

GCC 它,而且用法是:

$ ./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

ASCII表的相关部分是:

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 { | } ~  

那么无论是

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

或手动通过 hexedit的libStuff.so 更换。

这篇关于Android的NDK:objcopy把--rename - 均无法正常工作(需要在.so文件重命名功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆