在android中如何用Java加载解析so
作者:小道安全 发布时间:2023-09-14 18:16:05
理论基础
so的加载是一种解析式装载,这与dex有一定区别,dex是先加载进行优化验证生成odex,再去解析odex文件,而so更像边解析边装载,在加载过程中主要解析是load段。
下面主要是以java层的so加载进行从源码上进行解析加载流程。
java层的so加载流程分析
System.loadLibrary入口点
在java层我们知道加载so文件是通过System.loadLibrary函数其实现的,下面就以其作为入口点进行分析它的调用关系和实现。
System.loadLibrary在的函数定义系统source\libcore\luni\src\main\java\java\lang\system.java的文件中。
下面是其函数定义实现。
//参数就是要加载的so文件名称
public static void loadLibrary(String libName) {
//通过调用Runtime下面的loadLibrary函数实现
//函数有两个参数,参数1是加载的so文件名,参数2 类加载器。
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
}
Runtime的loadLibray解析
通过上面的System.java的loadLibrary函数我们需要继续分析Runtime.java文件中的loadLibray函数的定义实现。
Runtime的loadLibrary函数在android系统中的位置是
source\libcore\luni\src\main\java\java\lang\Runtime.java文件。
下面是Runtime的 loadLibrary函数的定义实现源码。
/*
* Searches for and loads the given shared library using the given ClassLoader.
*/
void loadLibrary(String libraryName, ClassLoader loader) {
if (loader != null) {
//通过加载器去查找要加载的so文件名
String filename = loader.findLibrary(libraryName);
//查找失败
if (filename == null) {
// It's not necessarily true that the ClassLoader used
// System.mapLibraryName, but the default setup does, and it's
// misleading to say we didn't find "libMyLibrary.so" when we
// actually searched for "liblibMyLibrary.so.so".
throw new UnsatisfiedLinkError(loader + " couldn't find \"" +
System.mapLibraryName(libraryName) + "\"");
}
//加载so文件名
String error = doLoad(filename, loader);
if (error != null) {
throw new UnsatisfiedLinkError(error);
}
return;
}
String filename = System.mapLibraryName(libraryName);
List<String> candidates = new ArrayList<String>();
String lastError = null;
//循环遍历文件路径
for (String directory : mLibPaths) {
//文件路径和文件名进行拼接
String candidate = directory + filename;
candidates.add(candidate);
if (IoUtils.canOpenReadOnly(candidate)) {
String error = doLoad(candidate, loader);
if (error == null) {
return; // We successfully loaded the library. Job done.
}
lastError = error;
}
}
if (lastError != null) {
throw new UnsatisfiedLinkError(lastError);
}
throw new UnsatisfiedLinkError("Library " + libraryName + " not found; tried " + candidates);
}
Runtime的doLoad解析
通过上面的Runtime的loadLibrary函数,我们看到加载so的函数是走到doLoad函数,那么我们就需要继续分析Runtime下的doload函数的定义实现。
Rutime下的doload函数在系统中的
source\libcore\luni\src\main\java\java\lang\Runtime.java文件中。
下面的代码是Runtime的doload函数的定义实现。
private String doLoad(String name, ClassLoader loader) {
// Android apps are forked from the zygote, so they can't have a custom LD_LIBRARY_PATH,
// which means that by default an app's shared library directory isn't on LD_LIBRARY_PATH.
// The PathClassLoader set up by frameworks/base knows the appropriate path, so we can load
// libraries with no dependencies just fine, but an app that has multiple libraries that
// depend on each other needed to load them in most-dependent-first order.
// We added API to Android's dynamic linker so we can update the library path used for
// the currently-running process. We pull the desired path out of the ClassLoader here
// and pass it to nativeLoad so that it can call the private dynamic linker API.
// We didn't just change frameworks/base to update the LD_LIBRARY_PATH once at the
// beginning because multiple apks can run in the same process and third party code can
// use its own BaseDexClassLoader.
// We didn't just add a dlopen_with_custom_LD_LIBRARY_PATH call because we wanted any
// dlopen(3) calls made from a .so's JNI_OnLoad to work too.
// So, find out what the native library search path is for the ClassLoader in question...
String ldLibraryPath = null;
if (loader != null && loader instanceof BaseDexClassLoader) {
ldLibraryPath = ((BaseDexClassLoader) loader).getLdLibraryPath();
}
// nativeLoad should be synchronized so there's only one LD_LIBRARY_PATH in use regardless
// of how many ClassLoaders are in the system, but dalvik doesn't support synchronized
// internal natives.
synchronized (this) {
return nativeLoad(name, loader, ldLibraryPath);
}
}
来源:https://blog.csdn.net/c_kongfei/article/details/120010316


猜你喜欢
- 将数组元素反转有多种实现方式,这里介绍常见的三种.直接数组元素对换@Testpublic void testReverseSelf() th
- 一、动态编译简介new创建对象是静态加载类,在编译时刻就需要加载所有可能使用到的类。一百个类,有一个类错了,都无法编译。通过动态加载类可以解
- 使用springboot创建多module项目,以前也做过多次,一段时间不用又忘了,在这里做个记录项目名称作用说明demo-root根项目父
- 在实际项目使用中,必须要考虑服务的安全性,当服务部署到互联网以后,就要考虑服务被恶意请求和暴力攻击的情况,下面的教程,通过intercept
- 看了这个排行榜, 小编只想说:流水的编程语言,铁打的Java,C/C++!!人工智能的前景已经不用多说了,越来越多的人看重人工智能的前景,想
- 本文实例为大家分享了C++实现大整数乘法的具体代码,供大家参考,具体内容如下#include<iostream>#include
- 最近接触了Android自定义控件,涉及到自定义xml中得属性(attribute),其实也很简单,但是写着写着,发现代码不完美了,就是在a
- 获取Android的ROOT权限其实很简单,只要在Runtime下执行命令"su"就可以了。// 获取ROOT权限pub
- 一、this 关键字的使用1. 概述this是什么?在Java中,this关键字比较难理解,它的作用和其词义很接近,表示&ldquo
- 写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址代码量不多,很容易看懂/** * 作者:叶应是叶 * 时间:2
- 本文实例讲述了C#获取进程或线程相关信息的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.
- eclipse 创建 user library 方法1、Window - Preferences - Java - Build Path -
- mybatis update并非所有字段需要更新mybatis update需求:更新字段作为参数,未更新字段不传入解决办法<upda
- 大家好,今天我们继续来学习Android 8.0系统的适配。之前我们已经讲到了,Android 8.0系统最主要需要进行适配的地方有两处:应
- 一.数组的三种声明方式总结public class WhatEver { public static void main(Str
- JdbcTemplate概述它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了
- Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsof
- 一、历史版本delegate void StudentDelegate(string name, int age);public class
- 目录一、前言(1)Timer(2)DelayedQueue 延迟队列(3)ScheduledThreadPoolExecutor(4)Sch
- Java 字节码以二进制的形式存储在 .class 文件中,每一个 .class 文件包含一个 Java 类或接口。Javaassist 就