dex文件结构(二):dex文件加载基本原理

news2025/2/8 15:09:24

return mClassLoader;

}

1.3 ApplicationLoaders.getClassLoader

public ClassLoader getClassLoader(String zip, String libPath, ClassLoader parent){

//Class.getSystemClassLoader返回的是一个PathClassLoader

//baseParent是BootClassLoader

ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent();

synchronized (mLoaders) {

if (parent == null) {

parent = baseParent;

}

if (parent == baseParent) {

ClassLoader loader = mLoaders.get(zip);

if (loader != null) {

return loader;

}

Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);

//创建一个PathClassLoader,并放入缓存,方便以后直接获取

PathClassLoader pathClassloader =

new PathClassLoader(zip, libPath, parent);

Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

mLoaders.put(zip, pathClassloader);

return pathClassloader;

}

Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);

PathClassLoader pathClassloader = new PathClassLoader(zip, parent);

Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

return pathClassloader;

}

}

1.4 PathClassLoader()

public PathClassLoader(String dexPath, String libraryPath,

ClassLoader parent) {

//PathClassLoader继承自BaseDexClassLoader,所以会调用

//BaseDexClassLoader的构造函数

super(dexPath, null, libraryPath, parent);

}

1.5 BaseDexClassLoader()

//由PathClassLoader传入的参数可以知道第二个参数optimizedDirectory=null

public BaseDexClassLoader(String dexPath, File optimizedDirectory,

String libraryPath, ClassLoader parent) {

super(parent);

this.pathList = new DexPathList(this, dexPath, libraryPath, optimizedDirectory);

}

1.6 DexPathList()

public DexPathList(ClassLoader definingContext, String dexPath,

String libraryPath, File optimizedDirectory) {

this.definingContext = definingContext;

ArrayList suppressedExceptions = new ArrayList();

//加载dex文件

this.dexElements = makePathElements(splitDexPath(dexPath), optimizedDirectory,

suppressedExceptions);

this.nativeLibraryDirectories = splitPaths(libraryPath, false);

this.systemNativeLibraryDirectories =

splitPaths(System.getProperty(“java.library.path”), true);

List allNativeLibraryDirectories = new ArrayList<>(nativeLibraryDirectories);

allNativeLibraryDirectories.addAll(systemNativeLibraryDirectories);

//加载library文件

this.nativeLibraryPathElements = makePathElements(allNativeLibraryDirectories, null,

suppressedExceptions);

1.7 DexPathList.makePathElements()

private static Element[] makePathElements(List files, File optimizedDirectory,

List suppressedExceptions) {

List elements = new ArrayList<>();

for (File file : files) {

File zip = null;

File dir = new File(“”);

DexFile dex = null;

String path = file.getPath();

String name = file.getName();

if (path.contains(zipSeparator)) {

String split[] = path.split(zipSeparator, 2);

zip = new File(split[0]);

dir = new File(split[1]);

} else if (file.isDirectory()) {

//如果file是library会执行这一分支

elements.add(new Element(file, true, null, null));

} else if (file.isFile()) {

if (name.endsWith(DEX_SUFFIX)) {

//如果文件以.dex结尾,直接加载

try {

dex = loadDexFile(file, optimizedDirectory);

} catch (IOException ex) {

System.logE("Unable to load dex file: " + file, ex);

}

} else {

zip = file;

//加载位于.zip或者.apk文件内的dex文件

try {

dex = loadDexFile(file, optimizedDirectory);

} catch (IOException suppressed) {

suppressedExceptions.add(suppressed);

}

}

} else {

System.logW("ClassLoader referenced unknown path: " + file);

}

if ((zip != null) || (dex != null)) {

elements.add(new Element(dir, false, zip, dex));

}

}

return elements.toArray(new Element[elements.size()]);

}

1.8 DexPathList.loadDexFile()

private static DexFile loadDexFile(File file, File optimizedDirectory)

throws IOException {

if (optimizedDirectory == null) {

//如果是PathClassLoader,则执行这一步

return new DexFile(file);

} else {

//首先确保输出后的文件是以.dex结尾

String optimizedPath = optimizedPathFor(file, optimizedDirectory);

return DexFile.loadDex(file.getPath(), optimizedPath, 0);

}

}

1.8.1 DexFile(String fileName)

public DexFile(String fileName) throws IOException {

//mCookie是一个Object类型的对象,用这个对象来保存dex文件也可能是dex文件列表

//之后在加载class的时候,会把这个对象传入,从而加载相应的class

mCookie = openDexFile(fileName, null, 0);

mFileName = fileName;

guard.open(“close”);

}

1.8.2 DexFile.loadDex()

static public DexFile loadDex(String sourcePathName, String outputPathName,

int flags) throws IOException {

return new DexFile(sourcePathName, outputPathName, flags);

}

1.8.3 DexFile()

private DexFile(String sourceName, String outputName, int flags) throws IOException {

if (outputName != null) {

try {

String parent = new File(outputName).getParent();

//确保输出目录属于当前应用,即输出目录是应用的私有目录/data/data/package_name/xxx

if (Libcore.os.getuid() != Libcore.os.stat(parent).st_uid) {

throw new IllegalArgumentException("Optimized data directory " + parent

  • " is not owned by the current user. Shared storage cannot protect"

  • " your application from code injection attacks.");

}

} catch (ErrnoException ignored) {

}

}

//mCookie是一个Object类型的对象,用这个对象来保存dex文件也可能是dex文件列表

//之后在加载class的时候,会把这个对象传入,从而加载相应的class

mCookie = openDexFile(sourceName, outputName, flags);

mFileName = sourceName;

guard.open(“close”);

}

从以上代码片段中可以看出,最后加载dex文件都是调用DexFile.openDexFile这个方法

1.9. DexFile.openDexFile()

private static Object openDexFile(String sourceName, String outputName, int flags) throws IOException {

return openDexFileNative(new File(sourceName).getAbsolutePath(),

(outputName == null) ? null : new File(outputName).getAbsolutePath(),

flags);

}

openDexFileNative是一个native方法,之后的调用进入native层

二.Native调用链


[](http

s://blog.csdn.net/weixin_47933729/article/details/113146251)2.1 DexFile_openDexFileNative()

static jobject DexFile_openDexFileNative(

JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {

ClassLinker* linker = Runtime::Current()->GetClassLinker();

std::vector<std::unique_ptr> dex_files;

std::vectorstd::string error_msgs;

dex_files = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs);

if (!dex_files.empty()) {

jlongArray array = ConvertNativeToJavaArray(env, dex_files);

… //错误处理,释放相应资源

//返回给Java层,由DexFile的mCookie指向

return array;

} else {

… //抛出异常

return nullptr;

}

}

2.2 ClassLinker::OpenDexFilesFromOat()

std::vector<std::unique_ptr> ClassLinker::OpenDexFilesFromOat(

const char* dex_location, const char* oat_location,

std::vectorstd::string* error_msgs) {

CHECK(error_msgs != nullptr);

// Verify we aren’t holding the mutator lock, which could starve GC if we

// have to generate or relocate an oat file.

Locks::mutator_lock_->AssertNotHeld(Thread::Current());

//Runtime::Current()->IsAotCompiler用来判断Runtime是否用于dex2oat,dex2oat程序

//也会创建一个Runtime,专门用来Compile

OatFileAssistant oat_file_assistant(dex_location, oat_location, kRuntimeISA,

!Runtime::Current()->IsAotCompiler());

// Lock the target oat location to avoid races generating and loading the

// oat file.

std::string error_msg;

if (!oat_file_assistant.Lock(&error_msg)) {

}

// Check if we already have an up-to-date oat file open.

const OatFile* source_oat_file = nullptr;

{

ReaderMutexLock mu(Thread::Current(), dex_lock_);

//oat_file_变量属于ClassLinker,用来存放oat文件

for (const OatFile* oat_file : oat_files_) {

// 判断oat文件是否正确

if (oat_file_assistant.GivenOatFileIsUpToDate(*oat_file)) {

source_oat_file = oat_file;

break;

}

}

}

// If we didn’t have an up-to-date oat file open, try to load one from disk.

if (source_oat_file == nullptr) {

// Update the oat file on disk if we can. This may fail, but that’s okay.

// Best effort is all that matters here.

// 根据dex文件产生对应的oat文件

if (!oat_file_assistant.MakeUpToDate(&error_msg)) {

}

// Get the oat file on disk.

std::unique_ptr oat_file = oat_file_assistant.GetBestOatFile();

if (oat_file.get() != nullptr) {

// Take the file only if it has no collisions, or we must take it because of preopting.

bool accept_oat_file = !HasCollisions(oat_file.get(), &error_msg);

if (!accept_oat_file) {

// However, if the app was part of /system and preopted, there is no original dex file

// available. In that case grudgingly accept the oat file.

if (!DexFile::MaybeDex(dex_location)) {

accept_oat_file = true;

}

}

if (accept_oat_file) {

source_oat_file = oat_file.release();

//将oat_file添加至oat_file_

RegisterOatFile(source_oat_file);

}

}

}

std::vector<std::unique_ptr> dex_files;

// Load the dex files from the oat file.

if (source_oat_file != nullptr) {

//通过OatFileAssistant加载dex文件

dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);

if (dex_files.empty()) {

}

}

// Fall back to running out of the original dex file if we couldn’t load any

// dex_files from the oat file.

if (dex_files.empty()) {

}

return dex_files;

}

2.3 OatFileAssistant::LoadDexFiles()

std::vector<std::unique_ptr> OatFileAssistant::LoadDexFiles(

const OatFile& oat_file, const char* dex_location) {

std::vector<std::unique_ptr> dex_files;

//先加载主dex文件.

//先从OatFile中获取OatDexFile, 在oat文件中每一个OatDexFile记录了相应的

//dex文件的文件名,文件偏移地址等关键信息

std::string error_msg;

const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(

dex_location, nullptr, false);

if (oat_dex_file == nullptr) {

return std::vector<std::unique_ptr>();

}

//通过OatDexFile加载主dex, [2.4]

std::unique_ptr dex_file = oat_dex_file->OpenDexFile(&error_msg);

if (dex_file.get() == nullptr) {

LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;

return std::vector<std::unique_ptr>();

}

dex_files.push_back(std::move(dex_file));

//加载其余的次级dex文件

for (size_t i = 1; ; i++) {

//先获取次级dex文件的索引位置

std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);

//根据索引位置获取对应的OatDexFile结构

oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);

if (oat_dex_file == nullptr) {

// There are no more secondary dex files to load.

break;

}

//加载次级dex文件

dex_file = oat_dex_file->OpenDexFile(&error_msg);

if (dex_file.get() == nullptr) {

LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;

return std::vector<std::unique_ptr>();

}

dex_files.push_back(std::move(dex_file));

}

return dex_files;

}

每一个dex文件的信息都被封装在一个OatDexFile中

OatDexFile数据结构:

const OatFile* const oat_file_:指向oat文件的指针

const std::string dex_file_location_:dex文件名

const std::string_canonical_dex_file_location_:dex文件绝对路径

const uint32_t dex_file_location_cheksum_:dex文件名的校验和

const uint8_t* const dex_file_pointer_:指向对应dex文件在oat文件中相对于oatdata的偏移地址

const uint32_t* const oat_class_offsets_pointer_:指向属于该dex文件的OatClass相对于oatdata的偏移地址

2.4 OatDexFile::OpenDexFile()

std::unique_ptr OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {

return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,

dex_file_location_checksum_, this, error_msg);

}

2.5 DexFile::Open()

static std::unique_ptr Open(const uint8_t* base, size_t size,

const std::string& location,

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取
g.csdn.net/weixin_47933729/article/details/113146251)2.4 OatDexFile::OpenDexFile()

std::unique_ptr OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {

return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,

dex_file_location_checksum_, this, error_msg);

}

2.5 DexFile::Open()

static std::unique_ptr Open(const uint8_t* base, size_t size,

const std::string& location,

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

[外链图片转存中…(img-uyIqHBKL-1719051994206)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取

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

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

相关文章

【仿真建模-anylogic】Network代码解析

Author&#xff1a;赵志乾 Date&#xff1a;2024-06-22 Declaration&#xff1a;All Right Reserved&#xff01;&#xff01;&#xff01; 1. 类图 2. 代码解析 //************************核心字段************************* // Network所属的level private transient Leve…

windows10远程桌面端口,Windows 10远程桌面端口修改的两个方法

在Windows 10系统中&#xff0c;远程桌面功能允许用户通过网络从一台计算机远程访问和控制另一台计算机。默认情况下&#xff0c;远程桌面服务使用的端口是3389。然而&#xff0c;出于安全考虑&#xff0c;许多管理员和用户希望修改这一默认端口。本指南将详细介绍如何在Window…

乌班图Ubuntu 24.04 SSH Server 修改默认端口重启无效

试用最新的乌班图版本&#xff0c;常规修改ssh端口&#xff0c;修改完毕后重启sshd提示没有找到service&#xff0c;然后尝试去掉d重启ssh后查看状态&#xff0c;端口仍然是默认的22&#xff0c;各种尝试都试了不行&#xff0c;重启服务器后倒是端口修改成功了&#xff0c;心想…

AcWing算法基础课笔记——高斯消元

高斯消元 用来求解方程组 a 11 x 1 a 12 x 2 ⋯ a 1 n x n b 1 a 21 x 1 a 22 x 2 ⋯ a 2 n x n b 2 … a n 1 x 1 a n 2 x 2 ⋯ a n n x n b n a_{11} x_1 a_{12} x_2 \dots a_{1n} x_n b_1\\ a_{21} x_1 a_{22} x_2 \dots a_{2n} x_n b_2\\ \dots \\ a…

陈好与王星越中戏传承

陈好与王星越&#xff1a;中戏传承&#xff0c;万人迷与未来之星在娱乐圈的星光璀璨中&#xff0c;我们时常被那些耀眼的明星所吸引&#xff0c;但你是否曾想过&#xff0c;他们背后的成长之路&#xff0c;是如何被一位位优秀的老师所指引的呢&#xff1f;今天&#xff0c;就让…

刷代码随想录有感(113):动态规划——爬楼梯plus

题干&#xff1a; 代码&#xff1a; #include<bits/stdc.h> using namespace std;int main(){int n,m;cin>>n>>m;vector<int>dp(n 1, 0);dp[0] 1;for(int j 0; j < n; j){for(int i 1; i < m; i){if(j > i)dp[j] dp[j - i];}}cout<&…

交互式知识库问答:一种结合大型语言模型的多轮交互方法

在当今信息爆炸的时代&#xff0c;人们每天都要处理海量的数据和信息。在这样的背景下&#xff0c;基于知识库的问答系统&#xff08;KBQA&#xff09;成为了一个重要的研究领域&#xff0c;它旨在使计算机能够理解自然语言提出的问题&#xff0c;并从结构化的知识库中检索出准…

以太坊==给合约转入/查询合约剩余/合约转给某账户/结构体/MAP

转入 必须要定义该函数&#xff0c;或者定义fallback // 接收以太币 receive() external payable {} // Corrected Line // SPDX-License-Identifier: MIT pragma solidity ^0.8.0;contract SimpleStorage {uint256 private storedData;// 事件&#xff0c;用于通知数据变更e…

【Redis】黑马点评短信登录

https://blog.csdn.net/qq_33888850/article/details/129770077 https://blog.csdn.net/weixin_51515308/article/details/128010464 https://www.bilibili.com/video/BV1cr4y1671t?p24 导入数据库 https://github.com/MagicToDo/hm-dianping sql文件在 hm-dianping-init\src…

35.简易远程数据框架的实现

上一个内容&#xff1a;34.构建核心注入代码 34.构建核心注入代码它的调用LoadLibrary函数的代码写到游戏进程中之后无法调用&#xff0c;动态链接库的路径是一个内存地址&#xff0c;写到游戏进程中只把内存地址写过去了&#xff0c;内存地址里的内容没写过去&#xff0c;导致…

【Git】 -- Part2 -- 分支管理

1. 分支 在 Git 中&#xff0c;分支&#xff08;Branch&#xff09;是用于在项目中创建独立开发线路的机制。分支使得开发者可以在不影响主干&#xff08;main 或 master&#xff09;的情况下进行实验、开发新功能或修复 Bug。 举个例子&#xff1a; 分⽀就好像是科幻电影⾥⾯…

软件开发教学:在线教育系统源码解析及教育培训小程序搭建实战

本篇文章&#xff0c;笔者将以“从零开始的软件开发教学”为主题&#xff0c;详细解析在线教育系统的源码&#xff0c;并通过实际操作来搭建一个教育培训小程序。 一、在线教育系统概述 在线教育系统是一个综合性的网络平台&#xff0c;旨在通过互联网提供教育资源和服务。该系…

http和https的区别在哪

HTTP&#xff08;超文本传输协议&#xff09;和HTTPS&#xff08;超文本传输安全协议&#xff09;之间存在几个关键区别主要涉及安全性、端口、成本、加密方式、搜索引擎优化&#xff08;SEO&#xff09;、身份验证等方面 1、安全性&#xff1a;HTTP&#xff08;超文本传输协议…

Day5(和为s的两个数字)双指针

输入一个递增排序的数组和一个数字s&#xff0c;在数组中查找两个数&#xff0c;使得它们的和正好是s。如果有多对数字的和等于s&#xff0c;则输出任意一对即可。 二、思路 1.首先&#xff0c;将不符合要求的值给排除&#xff0c;有以下三种&#xff1a; 数组元素个数不足两个…

ArrayList知识点(面试)

上一篇我们说了hashmap的相关知识点&#xff0c;这一篇我们再说一些ArrayList的相关知识&#xff0c;因为ArrayList也是我们项目中比较常用的。 ArrayList(底层是数组) 底层工作原理 首先&#xff0c;在构造ArrayList的时候会先看有没有指定容量&#xff0c;如果没有&#xf…

Flutter【组件】可折叠文本组件

简介 flutter 可折叠文本组件。 点击展开&#xff0c;收起折叠文本。支持样式自定义 github地址&#xff1a; github.com/ThinkerJack… pub地址&#xff1a;https://pub.dev/packages/jac_uikit 展开收起文本 使用方式&#xff1a; ExpandableText(content: 测试 * 50,ma…

一文搞懂Linux信号【下】

目录 &#x1f6a9;引言 &#x1f6a9;阻塞信号 &#x1f6a9;信号保存 &#x1f6a9;信号捕捉 &#x1f6a9;操作信号集 1.信号集操作函数 2.其它操作函数 &#x1f6a9;总结&#xff1a; &#x1f6a9;引言 在观看本博客之前&#xff0c;建议大家先看一文搞懂Linux信…

JR-8000系列机架式多路4K超高清光端机

集中式 4K超高清光传输设备 1 产品特性 ⚫ 支持高达 8 通道 SMPTE 全格式 SDI 信号输入 ⚫ 发送端带有 LOOPOUT 环出端口&#xff0c;具备消抖动功能&#xff0c;可作为信号调理或级联信号源使用 ⚫ 接收端支持双输出端口 ⚫ 支持传输速率&#xff1a;143Mbps-11.88Gbps ⚫…

INVS利用gatearray实现post-mask的function ECO

随着现代IC的设计发展&#xff0c;设计的规模和复杂度逐步增加&#xff0c;对于验证完备性的挑战越来越大&#xff0c;加之TO的时间压力&#xff0c;芯片设计通常会出现下列的场景&#xff1a; 芯片回片一次点亮大部分的case都可以顺利通过小部分的功能需要修正 对于重要的特…

赵丽颖纯白茉莉绽放温柔之美

赵丽颖纯白茉莉&#xff0c;绽放温柔之美在这个繁忙喧嚣的娱乐圈&#xff0c;赵丽颖以其独特的魅力&#xff0c;成为了无数人心中的白月光。近日&#xff0c;赵丽颖工作室发布了一组live图&#xff0c;她身着一袭温柔白裙&#xff0c;宛如一朵盛开的纯白茉莉花&#xff0c;美得…