gpt4 book ai didi

android NDK : objcopy --rename-sym does not work (need to rename a function in a . 所以文件)

转载 作者:行者123 更新时间:2023-11-30 02:57:49 27 4
gpt4 key购买 nike

我无法让 objcopy --rename-sym 工作。

在一个新的 Android 项目中,我创建了目录 jni 和文件 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; }

命令 ~/an/ndk-build -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()

如何重命名 .so 文件中的函数?

最佳答案

重命名函数的最简单方法是在不更改长度和哈希值的情况下更改名称。

保持相同的哈希值有点棘手,您必须了解 elf_hash() 的工作原理::

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
(它找到一个具有相同哈希值的名称)
]]

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表的相关部分是:

  ! " # $ % & ' ( ) * + , - . / 
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-sym does not work (need to rename a function in a . 所以文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com