如何查看动态加载的so中函数java jni调用so动态库情况

Android 加载so的函数调用 - 简书
Android 加载so的函数调用
如果一个so没有加载,系统首先会调用dlopen函数处理库文件。以下源码来自android6.0.1
dlopen:static void dlopen_ext(const char filename, int flags, const android_dlextinfo* extinfo) {
ScopedPthreadMutexLocker locker(&g_dl_mutex);
soinfo* result = do_dlopen(filename, flags, extinfo);
if (result == nullptr) {
__bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
void dlopen(const char filename, int flags) {
return dlopen_ext(filename, flags, nullptr);
}dlopen_ext函数会继续调用do_dlopen函数soinfo do_dlopen(const char name, int flags, const android_dlextinfo* extinfo) {
if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
DL_ERR("invalid flags to dlopen: %x", flags);
if (extinfo != nullptr) {
if ((extinfo-&flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo-&flags);
if ((extinfo-&flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
(extinfo-&flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
"ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo-&flags);
ProtectedDataG
soinfo* si = find_library(name, flags, extinfo);
if (si != nullptr) {
si-&call_constructors();
}函数首先会对参数进行一些判断,随后调用find_library函数
static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
if (name == nullptr) {
} else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
find_libraries函数会调用find_library_internal函数,在find_library_internal中
static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name,
int rtld_flags, const android_dlextinfo* extinfo) {
if (find_loaded_library_by_soname(name, &candidate)) {
// Library might still be loaded, the accurate detection
// of this fact is done by load_library.
TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
name, candidate == nullptr ? "n/a" : candidate-&get_realpath(), candidate);
soinfo* si = load_library(load_tasks, name, rtld_flags, extinfo);
// In case we were unable to load the library but there
// is a candidate loaded under the same soname but different
// sdk level - return it anyways.
if (si == nullptr && candidate != nullptr) {
load_library进行真正的加载,返回一个soinfo结构体指针。该结构体存储了so的信息。
OK!!目光回到do_dlopen函数,si-&call_constructors();
void soinfo::call_constructors() {
if (constructors_called) {
// We set constructors_called before actually calling the constructors, otherwise it doesn't
// protect against recursive constructor calls. One simple example of constructor recursion
// is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
// 1. The program depends on libc, so libc's constructor is called here.
// 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
// 3. dlopen() calls the constructors on the newly created
soinfo for libc_malloc_debug_leak.so.
// 4. The debug .so depends on libc, so CallConstructors is
called again with the libc soinfo. If it doesn't trigger the early-
out above, the libc constructor will be called again (recursively!).
constructors_called =
if (!is_main_executable() && preinit_array_ != nullptr) {
// The GNU dynamic linker silently ignores these, but we warn the developer.
PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
get_realpath(), preinit_array_count_);
get_children().for_each([] (soinfo* si) {
si-&call_constructors();
TRACE("\"%s\": calling constructors", get_realpath());
// DT_INIT should be called before DT_INIT_ARRAY if both are present.
call_function("DT_INIT", init_func_);
call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
call_function("DT_INIT", init_func_);
call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
下面我们看一下call_function和call_array函数的实现:
void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
size_t count, bool reverse) {
if (functions == nullptr) {
TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
int begin = reverse ? (count - 1) : 0;
int end = reverse ? -1 :
int step = reverse ? -1 : 1;
for (int i = i != i += step) {
TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
call_function("function", functions[i]);
TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
if (function == nullptr || reinterpret_cast&uintptr_t&(function) == static_cast&uintptr_t&(-1)) {
TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
function();
TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
可以看到最终就是调用init_func_和init_array_,init_func_和init_array_又是什么,是如何赋值的呢?
switch (d-&d_tag)
..........
case DT_INIT:
init_func_ = reinterpret_cast&linker_function_t&(load_bias + d-&d_un.d_ptr);
DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
case DT_INIT_ARRAY:
init_array_ = reinterpret_cast&linker_function_t*&(load_bias + d-&d_un.d_ptr);
DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
要解释上面的代码还要涉及到elf文件的结构,DT_INIT和DT_INIT_ARRAY均为动态节区.dynamic中结构体的d_tag值,结构体如下所示:(DT_INIT为可选)
typedef struct {
Elf32_Sword d_
Elf32_Word
Elf32_Addr
其中define 12
/ Address of initialization function /define
/ Address of initialization function array /
上面的函数中d-&d_un.d_ptr的d其实是Elf32_Dyn的指针。联合体d_run存储的是虚拟地址,init_func_和init_array_的地址就是.init和.init_array节的地址。所以,call_function("DT_INIT", init_func_);call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false)最终调用的是.init和.init_array节区的代码。所以,逻辑就走完了,在调用dlopen函数的同时不仅仅是加载库文件还会调用.init和.inti_array节区的代码。这一点在脱壳中尤为重要!
Android IOS
技术改变生活新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
白手起家, 积分 144, 距离下一级还需 56 积分
论坛徽章:0
gprof查看程序中函数使用情况,这个大家都知道了,但是我发现通过gprof只能查看本程序的函数,这对于动态加载了很多so的程序来说,就不够了,请问各位兄弟指教。
当然用的别的方法也可以,我主要想知道查看动态库so中个函数调用情况。谢谢兄弟们。
&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp
白手起家, 积分 130, 距离下一级还需 70 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
白手起家, 积分 144, 距离下一级还需 56 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
我是c写的程序也可以看吗?
白手起家, 积分 144, 距离下一级还需 56 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
有知道的吗?
白手起家, 积分 130, 距离下一级还需 70 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
白手起家, 积分 144, 距离下一级还需 56 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
有没有知道的朋友?
白手起家, 积分 130, 距离下一级还需 70 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
magic c++ 是在windows平台下调试c/c++ 程序 很方便的 你怎么不用呢
白手起家, 积分 144, 距离下一级还需 56 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
程序是在linux下写的,magic c++ 我还没用过,研究下好了。
白手起家, 积分 144, 距离下一级还需 56 积分
论坛徽章:0
如何查看动态加载的so中函数调用情况
why not up
北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:
广播电视节目制作经营许可证(京) 字第1234号
中国互联网协会会员&&联系我们:
感谢所有关心和支持过ChinaUnix的朋友们
转载本站内容请注明原作者名及出处Android下动态链接库.so调用的简单例子
在这篇文章中(),我在Android下使用dlopen函数调用.so文件没有成功,于是只得改用在编译过程中指定.so文件的方式加以调用,这次在Android下倒是通过了。
先在(Android源码目录)/development/目录下建立一个文件夹,比如起名叫 test123
在test123目录下建立以下三个文件:test.c、max.c、Android.mk
extern max(int,int);
int main()
a=5,b=3,c;
c=max(a,b);
printf("%d与%d相比,%d大。\n",a,b,c);
&&& return
int max(int x,int y)
&&& return
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE:= libmax
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
&&& test.c
LOCAL_CFLAGS=-lmax
LOCAL_LDFLAGS:= \
-Lout/target/product/generic/obj/lib
LOCAL_SHARED_LIBRARIES := \
&&& libmax
LOCAL_MODULE:= test
include $(BUILD_EXECUTABLE)
建立好这三个文件后,用cd命令回到Android源码目录下,执行make
test。注意不是文件夹名 test123
(Android源码目录)/out/target/product/generic/system/bin/test
out/target/product/generic/system/lib/libmax.so
/system/lib
将test程序和编译好的libmax.so送进模拟器下相应的位置,其中libmax.so应该放到
/system/lib下,不然test找不到它,test程序就无所谓了,放在什么地方都行。
用adb shell进入模拟器的控制台,当然事先要先把emulator启动起来,否则adb
shell会失败。
进入test所在目录,./test运行
<font COLOR="#与3相比,5大。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 linux 动态加载so原理 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信