【转】android 5.0 64bit系统加载库文件失败问题浅析
原文网址:http://blog.csdn.net/andrewblog/article/details/43601303
最近公司的一个项目使用android 5.0 64 bit平台,相对以前版本,除了android 5.0 有很大变动之外,64 bit系统和32 bit系统也存在很多差异性。
目前碰到的问题就是以前在32位上的so库文件,到64 位系统上不能加载的问题。首先来看一下相关log信息
logcat信息:
01-01 08:00:09.322 D/SensorService( 1606): nuSensorService starting...
01-01 08:00:09.323 E/HAL ( 1606): load: module=/system/lib64/hw/sensors.default.so
01-01 08:00:09.323 E/HAL ( 1606): dlopen failed: "/system/lib64/hw/sensors.default.so" is 32-bit instead of 64-bit
01-01 08:00:09.323 E/SensorService( 1606): couldn't load sensors module (Invalid argument)
ps信息:
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1172 1 1254940 56612 ffffffff 8e579300 S zygote64
root 1173 1 940684 49112 ffffffff f75da8a8 S zygote
shell 1174 1 4136 956 000b45f4 9bcef2e8 S /system/bin/sh
system 1606 1172 1411680 87808 ffffffff 8e579b88 S system_server
u0_a11 1730 1172 1298048 43684 ffffffff 8e579b88 S com.android.systemui
root 1742 1174 4136 976 000b45f4 8d71b2e8 S sh
从上面的log信息我们可以得出如下信息:
1. 64 bit 系统启动后,会运行两个进程zygote64 ,zygote ,对应的进程号为1172 和1173
2.sysrem_server进程由zygote64 启动,它的进程号为1606
3.logcat 信息里面提示的错误信息,是在sysrem_server进程调用的,也就是说sensors.default.so库是在进程sysrem_server中去加载的。
综合上面信息,不难发现,sysrem_server是由zygote64启动的,所以加载的so库也必须是64bit类型。否则就会报如下错误
dlopen failed: "/system/lib64/hw/sensors.default.so" is 32-bit instead of 64-bit
为了解决这个问题,就必须把原来so文件的源码,放到64 bit的编译环境下编译。
首先,我直接复制源码到64 bit的android源码下编译,没有做任何修改,编译信息如下,
build/core/shared_library.mk:12: sensors.default: LOCAL_MODULE_PATH for shared libraries is unsupported in multiarch builds, use LOCAL_MODULE_RELATIVE_PATH instead
Import includes file: out/target/product/coconut/obj_arm/SHARED_LIBRARIES/sensors.default_intermediates/import_includes
target thumb C: sensors.default_32 <= external/tcl_sensors/sensors.c
external/tcl_sensors/sensors.c: In function 'sensors__get_sensors_list':
external/tcl_sensors/sensors.c:203:63: warning: unused parameter 'module' [-Wunused-parameter]
static int sensors__get_sensors_list(struct sensors_module_t* module,
^
target SharedLib: sensors.default_32 (out/target/product/coconut/obj_arm/SHARED_LIBRARIES/sensors.default_intermediates/LINKED/sensors.default.so)
Import includes file: out/target/product/coconut/obj/SHARED_LIBRARIES/sensors.default_intermediates/import_includes
target C: sensors.default <= external/tcl_sensors/sensors.c
external/tcl_sensors/sensors.c: In function 'sensors__get_sensors_list':
external/tcl_sensors/sensors.c:203:63: warning: unused parameter 'module' [-Wunused-parameter]
static int sensors__get_sensors_list(struct sensors_module_t* module,
^
target SharedLib: sensors.default (out/target/product/coconut/obj/SHARED_LIBRARIES/sensors.default_intermediates/LINKED/sensors.default.so)
target Symbolic: sensors.default (out/target/product/coconut/symbols/system/lib64/hw/sensors.default.so)
Export includes file: external/tcl_sensors/Android.mk -- out/target/product/coconut/obj/SHARED_LIBRARIES/sensors.default_intermediates/export_includes
target Strip: sensors.default (out/target/product/coconut/obj/lib/sensors.default.so)
Export includes file: external/tcl_sensors/Android.mk -- out/target/product/coconut/obj_arm/SHARED_LIBRARIES/sensors.default_intermediates/export_includes
target Strip: sensors.default_32 (out/target/product/coconut/obj_arm/lib/sensors.default.so)
Install: out/target/product/coconut/system/lib64/hw/sensors.default.so
在/system/lib64/hw/目录下面生成了库文件sensors.default.so,然后我把该库文件替换到终端里面,既然发现还是会出现和上面一样的错误,找不到so库。
在网上查了一下资料,很少相关介绍,这个时候感觉没思路了,找不到问题突破点。
后来再编译操作了一遍,发现了一条很重要的信息提示,
build/core/shared_library.mk:12: sensors.default: LOCAL_MODULE_PATH for shared libraries is unsupported in multiarch builds, use LOCAL_MODULE_RELATIVE_PATH instead
这个信息提示已经说得很明白,然后我查看了一下android.mk文件,果断的把LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw语句改成了
LOCAL_MODULE_RELATIVE_PATH := hw,然后再重新编译。log如下
Import includes file: out/target/product/coconut/obj/SHARED_LIBRARIES/sensors.default_intermediates/import_includes
target C: sensors.default <= external/tcl_sensors/sensors.c
external/tcl_sensors/sensors.c: In function 'sensors__get_sensors_list':
external/tcl_sensors/sensors.c:203:63: warning: unused parameter 'module' [-Wunused-parameter]
static int sensors__get_sensors_list(struct sensors_module_t* module,
^
target SharedLib: sensors.default (out/target/product/coconut/obj/SHARED_LIBRARIES/sensors.default_intermediates/LINKED/sensors.default.so)
target Symbolic: sensors.default (out/target/product/coconut/symbols/system/lib64/hw/sensors.default.so)
Export includes file: external/tcl_sensors/Android.mk -- out/target/product/coconut/obj/SHARED_LIBRARIES/sensors.default_intermediates/export_includes
target Strip: sensors.default (out/target/product/coconut/obj/lib/sensors.default.so)
Install: out/target/product/coconut/system/lib64/hw/sensors.default.so
Import includes file: out/target/product/coconut/obj_arm/SHARED_LIBRARIES/sensors.default_intermediates/import_includes
target thumb C: sensors.default_32 <= external/tcl_sensors/sensors.c
external/tcl_sensors/sensors.c: In function 'sensors__get_sensors_list':
external/tcl_sensors/sensors.c:203:63: warning: unused parameter 'module' [-Wunused-parameter]
static int sensors__get_sensors_list(struct sensors_module_t* module,
^
target SharedLib: sensors.default_32 (out/target/product/coconut/obj_arm/SHARED_LIBRARIES/sensors.default_intermediates/LINKED/sensors.default.so)
target Symbolic: sensors.default_32 (out/target/product/coconut/symbols/system/lib/hw/sensors.default.so)
Export includes file: external/tcl_sensors/Android.mk -- out/target/product/coconut/obj_arm/SHARED_LIBRARIES/sensors.default_intermediates/export_includes
target Strip: sensors.default_32 (out/target/product/coconut/obj_arm/lib/sensors.default.so)
Install: out/target/product/coconut/system/lib/hw/sensors.default.so
注意看这次的log信息,没有上面的提示信息,并且还有一个关键的不同点,就是在system/lib64/hw 和system/lib/hw两个目录下都生成了sensors.default.so库文件
赶紧把system/lib64/hw/目录下的sensors.default.so替换到终端的system/lib64/hw/目录,重启验证,发现库文件可以正常运行。
小结一下:
1. 初次在64bit系统上开发,相对在32bit上,会发现出现很多意想不到问题,以前很多直接移植的东西可能在64bit上就不能使用。
针对这类问题,解决问题的核心思想一定要放在分析64bit 和32bit的差异性,包括编译、数据处理方式、数据类型等方面。
2.针对64bit系统平台的一些运行机制,还存在很多疑问,这个还得在实践中慢慢去摸索了。