android so的加载流程(Android 13~14)

news2024/9/20 0:59:34

序言

分析环境: Android 13~14

其实大佬 << 安卓so加载流程源码分析 >> 已经写得非常好了,我就没必要再写了

建议读者看看这篇文字,比较新,质量很高<< 安卓so加载流程源码分析 >>

为什么要分析 android so的加载流程 ???

我想明白

  • so是怎么打开的,
  • so的DT_INIT何时执行
  • so的DT_INIT_ARRAY何时执行
  • JNI_LOAD何时执行

整体的加载流程是怎么样的

通过阅读源码以及文章, 作图如下

看上去还是比较清晰的

image-20240829114602571

1), so怎么被打开的? 用dlopen吗

从图中我们可以看出

so是在linker.cpp中,通过函数open_libiary调用open函数打开的so文件

于是我们知道so是open打开的,不是dlopen

2), init_array, init 何时被执行的?

从图中可以看出,

在完成so的加载后,do_dlopen函数会调用 soinfo *si->call_constructors() 来实现构造函数

call_constructors中又会执行下面的代码来执行DT_INIT和DT_INIT_ARRAY

    // DT_INIT should be called before DT_INIT_ARRAY if both are present.
    call_function("DT_INIT", init_func_, get_realpath());
    call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false, get_realpath());

3), JNI_LOAD何时被执行的

从图中可以看出 在so加载完毕后,

在java_vm_ext.cc中, 执行了(*jni_on_load), 从而调用了JNI_LOAD

int version = (*jni_on_load)(this, nullptr);

so的路径从哪里查找?

这里只分析一部分, 貌似还有什么mLibPaths可以找so的路径,没去分析…懒了

在 Runtime.java中,有这样的代码

// Runtime.java
private synchronized void loadLibrary0(ClassLoader loader, Class<?> callerClass, String libname) 
{  
    if (libname.indexOf((int)File.separatorChar) != -1) //不能存在路径的`/`,传递进来的只能是xxx
    {  
        throw new UnsatisfiedLinkError(  "Directory separator should not appear in library name: " + libname);  
    }  
    String libraryName = libname;  
    if (loader != null && !(loader instanceof BootClassLoader)) 
    {  
    	// BootClassLoader 继承了 ClassLoader的 findLibrary(), 但并没有去重写它. ClassLoader.findLibrary()一直返回null
        String filename = loader.findLibrary(libraryName);  
        if (filename == null &&  (loader.getClass() == PathClassLoader.class ||  loader.getClass() == DelegateLastClassLoader.class)) 
        {
            filename = System.mapLibraryName(libraryName);//xxx - > libxxx.so
        }  
        if (filename == null) 
        {  	
        	//看上去mapLibraryName不仅仅是字符串的替换
            throw new UnsatisfiedLinkError(loader + " couldn't find \"" +   System.mapLibraryName(libraryName) + "\"");
        }  
        String error = nativeLoad(filename, loader);  
        if (error != null) 
        {  
            throw new UnsatisfiedLinkError(error);  
        }  
        return;  
    }  

	// 直接使用mLibPaths的情况, 没有loader
    // We know some apps use mLibPaths directly, potentially assuming it's not null.  
    // Initialize it here to make sure apps see a non-null value.    getLibPaths();  
    String filename = System.mapLibraryName(libraryName);  
    String error = nativeLoad(filename, loader, callerClass);  
    if (error != null) 
    {  
        throw new UnsatisfiedLinkError(error);  
    }  
}

其中调用String filename = loader.findLibrary(libraryName); 来获取so_path

PathClassLoader 和 DexClassLoader 都没有实现 findLibrary

BaseDexClassLoader 重写了 ClassLoader.findLibrary

//BaseDexClassLoader.java
@Override  
public String findLibrary(String name)  
{  
    return pathList.findLibrary(name); //可以看到它调用了PathList.findLibrary
}

进入DexPathList.java

//DexPathList.java
public String findLibrary(String libraryName)  
{  
    String fileName = System.mapLibraryName(libraryName);  
    //首先调用System.mapLibraryName拿到so的前缀和后缀名,如libname为hello,那么经过此函数转换后变成了libhello.so
  
    for (NativeLibraryElement element : this.nativeLibraryPathElements)  //NativeLibraryElement[] nativeLibraryPathElements 
    {  
        String path = element.findNativeLibrary(fileName);  
  
        if (path != null)  
        {  
            return path;  
        }  
    }  
  
    return null;  
}

关于System.mapLibraryName(libraryName); 的作用

假如so_name = “xxx”, 通过调用mapLibraryName

so_filename = “libxxx.so”

在findLibrary中, 我们会发现这么一个变量 this.nativeLibraryPathElements

this.nativeLibraryPathElements是什么???

这得追溯到DexPathList的构造函数

//DexPathList.java 构造函数中完成..

//记录所有的dexFile文件
this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory, suppressedExceptions, definingContext, isTrusted);
// Native libraries may exist in both the system and  
// application library paths, and we use this search order:  
//  
//   1. This class loader's library path for application libraries (librarySearchPath):  
//   1.1. Native library directories  
//   1.2. Path to libraries in apk-files  
//   2. The VM's library path from the system property for system libraries  
//      also known as java.library.path  
//  
// This order was reversed prior to Gingerbread; see http://b/2933456.  
//app目录的native库
this.nativeLibraryDirectories = splitPaths(librarySearchPath, false);  //librarySearchPath是构造函数的参数,从ClassLoader那边传递过来
//系统目录的native库
this.systemNativeLibraryDirectories = splitPaths(System.getProperty("java.library.path"), true);  
//记录所有的Native动态库
this.nativeLibraryPathElements = makePathElements(getAllNativeLibraryDirectories());//getAllNativeLibraryDirectories拿到所有的NativeLibrary

于是这里就牵扯出一个问题,so的搜索路径

于是路径有

  • 系统会优先查找自己目录的libraries
  • 然后再找APK 压缩文件中
  • 最后才会查找Android 虚拟机环境变量目录中的libraries

我自己尝试去找一个 classloader 看看

this.nativeLibraryDirectories

/data/app/com.example.myapplication2-YBD_9i86XuDyqMpoEGKHQQ==/lib/arm64
/data/app/com.example.myapplication2-YBD_9i86XuDyqMpoEGKHQQ==/base.apk!/lib/arm64-v8a

this.systemNativeLibraryDirectories

/system/lib64
/system/product/lib64

然后查看 this.nativeLibraryPathElements 是一个什么鬼

image-20240828160938707

发现是 this.nativeLibraryDirectories + this.systemNativeLibraryDirectories + VM's libraryNativeLibraryPathElement 对象

this.nativeLibraryPathElements包含了所有要搜索的路径

然后我们抽取一个 DexPathList$NativeLibraryPathElement 对象查看一下

image-20240828162456693

以其中一个 zip 的搜索路径为例,

zipDir = “lib/arm64-v8a”

然后 urlHandle 的 (String)fileUri 指向了当前 app 的安装路径, urlHandle 的 jarfile 也指向了 app 的安装路径

继续回到 DexPathList.findLibrary

//DexPathList.java
public String findLibrary(String libraryName)  
{  
    String fileName = System.mapLibraryName(libraryName);  
    //首先调用System.mapLibraryName拿到so的前缀和后缀名,如libname为hello,那么经过此函数转换后变成了libhello.so
  
    for (NativeLibraryElement element : this.nativeLibraryPathElements)  //NativeLibraryElement[] nativeLibraryPathElements 
    {  
        String path = element.findNativeLibrary(fileName);  
  
        if (path != null)  
        {  
            return path;  
        }  
    }  
  
    return null;  
}


//DexPathList$NativeLibraryElement.findNativeLibrary
public String findNativeLibrary(String name)  
{  
    maybeInit();  
  
    if (zipDir == null)  //优先会去找非zipDir目录的library, 大概是非apk目录
    {  
        String entryPath = new File(path, name).getPath();  
        if (IoUtils.canOpenReadOnly(entryPath))  
        {  
            return entryPath;  
        }  
    } 
    else if (urlHandler != null)  //再去找zipDir目录的library,而这个zipDir 实际上就是APP原本的目录
    {  
        // Having a urlHandler means the element has a zip file.  
        // In this case Android supports loading the library iff        // it is stored in the zip uncompressed.        String entryName = zipDir + '/' + name;  
        if (urlHandler.isEntryStored(entryName))  //这段代码用于判断给定entryName在指定的 JAR 文件中是否存在,并且是否是以“存储”方式存储的。
        {  
        	//apk好像被当作jar的形式打开
            return path.getPath() + zipSeparator + entryName;  
        }  
    }  
  
    return null;  
}

所以this.nativeLibraryPathElements 记录了所有要查找的目录

然后调用element.findNativeLibrary(fileName) , 在逐个逐个element对应目录下查找so文件是否存在

  • 优先会去找非zipDir目录的library,
  • 再去找zipDir(也就是apk压缩)目录的library

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2102442.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

无人机之反制系统篇

无人机的反制系统是一个复杂而精细的系统&#xff0c;旨在应对无人机的不当使用或潜在威胁。该系统通常由多个关键部分组成&#xff0c;包括搜索系统、光电跟踪系统、射频干扰系统及显控单元等&#xff0c;这些部分共同协作以实现对无人机的有效反制。以下是对无人机反制系统的…

SpringBoot开发——初步了解SpringBoot

文章目录 一、SpringBoot简介1、什么是Spring Boot2、Spring Boot的优点3、Spring Boot功能 二、Spring与Spring Boot对比三、Spring Boot与Spring MVC四、Spring Boot体系结构五、Springboot Initializr1、Spring Initializr2、Spring Initializr模块 一、SpringBoot简介 1、…

docker ps 得到的ports列的含义

前言 每次使用docker ps 查询容器运行情况的时候就很容易搞混ports列的含义&#xff0c;今天浅记一下 docker ps ports列含义 首先看docker ps的查询结果显示&#xff1a; CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 容器ID …

Axure中继器教程及案例详解

Axure RP 是一款强大的原型设计工具&#xff0c;广泛应用于产品设计、UI/UX 设计及交互设计中。中继器&#xff08;Repeater&#xff09;作为 Axure 中的一个重要元件&#xff0c;以其强大的数据处理和动态交互能力&#xff0c;成为设计师们不可或缺的工具。本文将从中继器基础…

LLM agentic模式之multi-agent: ChatDev,MetaGPT, AutoGen思路

文章目录 Multi-agentChatDev设计阶段编码阶段测试阶段文档编写 MetaGPTSOP模式下的Agent通信协议带执行反馈的迭代编程 AutoGenconversable agentsConversation ProgrammingAutoGen的应用 参考资料 Multi-agent ChatDev ChatDev出自2023年7月的论文《ChatDev: Communicative…

告别文档处理烦恼,PDF Guru Anki一键搞定所有

前言 知识就像烛光&#xff0c;能照亮一个人&#xff0c;也能照亮无数人&#xff0c;科技之光更是如此&#xff1b;这一理念深刻地影响了我们如何看待和应用新技术。正是在这样的背景下&#xff0c;一款集PDF处理与高效学习工具于一体的软件——PDF Guru Anki应运而生&#xf…

AI论文生成可靠吗?分享6款AI论文题目生成器网站

在当今学术研究和写作领域&#xff0c;AI论文题目生成器网站的出现极大地简化了学术写作流程。这些工具不仅能够帮助用户快速生成高质量的论文题目&#xff0c;还能提供文献推荐、论文润色等功能&#xff0c;从而提高写作效率和质量。以下是六款值得推荐的AI论文题目生成器网站…

glsl着色器学习(六)

准备工作已经做完&#xff0c;下面开始渲染 gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);gl.clearColor(0.5, 0.7, 1.0, 1.0); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);gl.enable(gl.DEPTH_TEST); gl.enable(gl.CULL_FACE);设置视口 gl.viewport(0,…

轻量级的git-server工具:docker部署gogs

背景 创建一个自己使用的git server&#xff0c;让平时使用的代码之类的可以直接传到自己的服务器上&#xff0c;进行远程管理。由于一个人使用&#xff0c;gitlab 太重&#xff0c;所以选择gogs来实现功能。 系统&#xff1a;openEuler 22.03 (LTS-SP3) ip: 192.168.100.31 …

【Linux】Linux项目自动化构建工具 - make/Makefile

目录 一、make/Makefile的背景二、make/Makefile的基本概念三、依赖关系四、依赖方法五、make/Makefile原理六、Makefile的伪目标七、Makefile的变量八、Makefile的推导能力结尾 一、make/Makefile的背景 一个工程中的源文件不计数&#xff0c;其按类型、功能、模块分别放在若…

【Java 基础】:三大特征之多态

✨ 杏花疏影里&#xff0c;吹笛到天明 &#x1f30f; &#x1f4c3;个人主页&#xff1a;island1314 &#x1f525;个人专栏&#xff1a;java学习 ⛺️ 欢迎关注&#xff1a;&#x1f44d;点赞 &#x1f442;&…

JVM参数有哪些?

JVM调优主要是通过定制JVM参数来提Java应用程序的运行速度 JVM参数大致可以分为三类&#xff1a; &#xff11;、标准指令&#xff1a;&#xff0d;开头&#xff0c;这些是所有的HotSPot都支持的参数。可以用java -help打印出来&#xff1b; 2、非标准指令&#xff1a;-X开头…

Mysql高级篇(上)—— Mysql架构介绍(二)

Mysql高级篇&#xff08;上&#xff09; MySQL架构介绍&#xff08;二&#xff09;逻辑架构逻辑架构剖析MySQL8.0中SQL执行流程Linux环境下MySQL8.0中SQL执行原理MySQL语法顺序Oracle中SQL执行流程&#xff08;了解&#xff09;数据库缓存池 buffer pool&#xff08;了解&#…

86、pod部署策略

一、集群的调度 集群的调度&#xff1a; 怎么把pod部署到节点的方法。 1.1、调度的过程&#xff1a; scheduler是集群的调度器&#xff0c;主要任务就是把pod部署到节点上。 1.2、自动调度&#xff1a; 1、公平&#xff0c;保证每个可用的节点都可以部署pod 2、资源的高…

什么是工控安全,产线工控安全加固的方法

一、工控安全概述 想要了解工控安全&#xff0c;首先要了解其资产对象本身&#xff0c;也就是工业控制系统。 1、什么是工控 关于工业控制系统的定义&#xff0c;网上有很多&#xff0c;我就不再赘述&#xff0c;下面这张图是我从csdn找到的&#xff0c;个人觉得还不错。对照…

【多线程】CountDownLatch的简单实现

通过上一篇对CountDownLatch的使用&#xff0c;我们也明白了他的基本原理&#xff0c;接下来我们一起来实现一个CountDownLatch的基础效果 新建一个抽象类&#xff0c;包含countDownLatch需要的参数和方法 package com.atguigu.signcenter.nosafe.chouxiang;/*** author: jd* …

k8s集群的调度

目录 自动调度的原则 调度约束机制&#xff1a;list-watch apiserver和组件之间的watch机制 调度过程的默认算法 1.预算策略 预算的算法 2.优选策略 优选的算法 *用户定制节点部署 1.强制性节点部署 2.节点标签部署&#xff08;匹配机制&#xff09; 标签的增删改查…

selenium无法定位元素的几种解决方案

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 1、frame/iframe表单嵌套 WebDriver只能在一个页面上对元素识别与定位&#xff0c;对于frame/iframe表单内嵌的页面元素无法直接定位。 解决方法&#xff1a; d…

Hadoop 下载

下载法一&#xff1a;官方下载 hadoop官网 1.选择要下载的版本&#xff0c;这里我以3.4.0为例进行说明&#xff1b; 2.跳转后&#xff0c;选择对应系统架构的&#xff0c;进行下载&#xff1b; 下载法二&#xff1a;国内镜像源下载 1.阿里云 这里我以mac m1为案例&#x…

【已解决】Halcon错误代码4104

问题描述 在运行的时候发现报错4104&#xff0c;如下图所示 解决 从字面上理解就是超出了计算机的内存空间了&#xff0c;那么着手解决的话就需要把没次训练的张数减少一些即可。去相对应的修改batch_size即可。 结果 如下图所示&#xff0c;正常在使用GPU训练。 搞定&…