在Dalvik虛擬機(jī)對(duì)dex的加載過(guò)程的基礎(chǔ)上,我們來(lái)分析下面代碼中的第三步州袒。
/dalvik/vm/native/dalvik_system_DexFile.cpp
static void Dalvik_dalvik_system_DexFile_openDexFileNative(const u4* args,
JValue* pResult)
{
StringObject* sourceNameObj = (StringObject*) args[0];
StringObject* outputNameObj = (StringObject*) args[1];
DexOrJar* pDexOrJar = NULL;
JarFile* pJarFile;
RawDexFile* pRawDexFile;
char* sourceName;
char* outputName;
sourceName = dvmCreateCstrFromString(sourceNameObj);
if (outputNameObj != NULL)
outputName = dvmCreateCstrFromString(outputNameObj);
else
outputName = NULL;
// 1卓箫、嘗試把它當(dāng)做一個(gè)后綴為.dex的DEX文件進(jìn)行打開(kāi)厘贼,得到RawDexFile結(jié)構(gòu)數(shù)據(jù)
// 2颂龙、如果打開(kāi)失敗,則把它當(dāng)做一個(gè)包含有classes.dex文件的Zip文件進(jìn)行打開(kāi)旺隙,得到JarFile結(jié)構(gòu)數(shù)據(jù)
if (hasDexExtension(sourceName)
&& dvmRawDexFileOpen(sourceName, outputName, &pRawDexFile, false) == 0) {
pDexOrJar = (DexOrJar*) malloc(sizeof(DexOrJar));
pDexOrJar->isDex = true;
pDexOrJar->pRawDexFile = pRawDexFile;
pDexOrJar->pDexMemory = NULL;
} else if (dvmJarFileOpen(sourceName, outputName, &pJarFile, false) == 0) {
ALOGV("Opening DEX file '%s' (Jar)", sourceName);
pDexOrJar = (DexOrJar*) malloc(sizeof(DexOrJar));
pDexOrJar->isDex = false;
pDexOrJar->pJarFile = pJarFile;
pDexOrJar->pDexMemory = NULL;
}
if (pDexOrJar != NULL) {
pDexOrJar->fileName = sourceName;
// 3、把pDexOrJar這個(gè)結(jié)構(gòu)體中的內(nèi)容加到gDvm中的userDexFile結(jié)構(gòu)的hash表中浆洗,以便dalvik以后的查找
addToDexFileTable(pDexOrJar);
} else {
free(sourceName);
}
free(outputName);
RETURN_PTR(pDexOrJar);
}
可以看到DexOrJar文件最終添加到gDvm中的userDexFile結(jié)構(gòu)的hash表中催束,下面我們來(lái)具體查看addToDexFileTable方法。
static void addToDexFileTable(DexOrJar* pDexOrJar) {
/*
* Later on, we will receive this pointer as an argument and need
* to find it in the hash table without knowing if it's valid or
* not, which means we can't compute a hash value from anything
* inside DexOrJar. We don't share DexOrJar structs when the same
* file is opened multiple times, so we can just use the low 32
* bits of the pointer as the hash.
*/
u4 hash = (u4) pDexOrJar;
void* result;
dvmHashTableLock(gDvm.userDexFiles);
result = dvmHashTableLookup(gDvm.userDexFiles, hash, pDexOrJar,
hashcmpDexOrJar, true);
dvmHashTableUnlock(gDvm.userDexFiles);
if (result != pDexOrJar) {
ALOGE("Pointer has already been added?");
dvmAbort();
}
pDexOrJar->okayToFree = true;
}
需要說(shuō)明的是伏社,gDvm是一個(gè)DvmGlobals結(jié)構(gòu)的全局變量抠刺。在DvmGlobals結(jié)構(gòu)中有一個(gè)成員HashTable* userDexFiles;
DvmGlobals在/dalvik/vm/Globals.h中
也就是說(shuō),userDexFiles是一個(gè)HashTabled的引用摘昌,HashTabled的結(jié)構(gòu)為:
/dalvik/vm/Hash.h
struct HashTable {
int tableSize; /* must be power of 2 */
int numEntries; /* current #of "live" entries */
int numDeadEntries; /* current #of tombstone entries */
HashEntry* pEntries; /* array on heap */
HashFreeFunc freeFunc;
pthread_mutex_t lock;
};
HashEntry* pEntries指向一個(gè)HashEntry的數(shù)組,HashEntry結(jié)構(gòu)體中的data變量存放的就是pDexOrJar速妖。
/dalvik/vm/Hash.h
struct HashEntry {
u4 hashValue;
void* data; //DexOrJar *pDexOrJar
};
上面代碼的關(guān)鍵就是dvmHashTableLookup方法,所以我們繼續(xù)查看該方法聪黎。
/dalvik/vm/Hash.cpp
void* dvmHashTableLookup(HashTable* pHashTable, u4 itemHash, void* item,
HashCompareFunc cmpFunc, bool doAdd)
{
HashEntry* pEntry;
HashEntry* pEnd;
void* result = NULL;
assert(pHashTable->tableSize > 0);
assert(item != HASH_TOMBSTONE);
assert(item != NULL);
/* jump to the first entry and probe for a match */
pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
pEnd = &pHashTable->pEntries[pHashTable->tableSize];
while (pEntry->data != NULL) {
if (pEntry->data != HASH_TOMBSTONE &&
pEntry->hashValue == itemHash &&
(*cmpFunc)(pEntry->data, item) == 0)
{
/* match */
//ALOGD("+++ match on entry %d", pEntry - pHashTable->pEntries);
break;
}
pEntry++;
if (pEntry == pEnd) { /* wrap around to start */
if (pHashTable->tableSize == 1)
break; /* edge case - single-entry table */
pEntry = pHashTable->pEntries;
}
//ALOGI("+++ look probing %d...", pEntry - pHashTable->pEntries);
}
if (pEntry->data == NULL) {
if (doAdd) {
pEntry->hashValue = itemHash;
pEntry->data = item;
pHashTable->numEntries++;
/*
* We've added an entry. See if this brings us too close to full.
*/
if ((pHashTable->numEntries+pHashTable->numDeadEntries) * LOAD_DENOM
> pHashTable->tableSize * LOAD_NUMER)
{
if (!resizeHash(pHashTable, pHashTable->tableSize * 2)) {
/* don't really have a way to indicate failure */
ALOGE("Dalvik hash resize failure");
dvmAbort();
}
/* note "pEntry" is now invalid */
} else {
//ALOGW("okay %d/%d/%d",
// pHashTable->numEntries, pHashTable->tableSize,
// (pHashTable->tableSize * LOAD_NUMER) / LOAD_DENOM);
}
/* full table is bad -- search for nonexistent never halts */
assert(pHashTable->numEntries < pHashTable->tableSize);
result = item;
} else {
assert(result == NULL);
}
} else {
result = pEntry->data;
}
return result;
}
上面的操作就是將pDexOrJar以及它對(duì)應(yīng)的hashValue封裝到一個(gè)HashEntry罕容,然后將這個(gè)HashEntry存放到HashEntry* pEntries指向的HashEntry數(shù)組中去。
他們之間的整個(gè)引用過(guò)程如下圖所示:
也就是說(shuō)我們可以通過(guò)gDvm.userDexFiles找到具體DexOrJar的位置稿饰,然后通過(guò)DexOrJar的pRawDexFile定位到RawDexFile的位置锦秒,進(jìn)而通過(guò)RawDexFile的pDvmDex找到DvmDex的位置,最終就可以通過(guò)DvmDex的pDexFile來(lái)定位到DexFile的位置喉镰,它就是內(nèi)存中的.odex文件內(nèi)容旅择,它里面同時(shí)也包含了.dex的內(nèi)容。