Loading...
--- dyld/dyld-1235.2/common/DyldSharedCache.cpp
+++ dyld/dyld-1042.1/common/DyldSharedCache.cpp
@@ -22,9 +22,6 @@
* @APPLE_LICENSE_HEADER_END@
*/
-#include <TargetConditionals.h>
-
-#if !TARGET_OS_EXCLAVEKIT
#include <dirent.h>
#include <sys/errno.h>
@@ -39,14 +36,13 @@
#include <unistd.h>
#include <dlfcn.h>
-#include "OptimizerSwift.h"
-
#if BUILDING_CACHE_BUILDER
#include <set>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
+// #include "SharedCacheBuilder.h"
#include "FileUtils.h"
#endif
@@ -283,66 +279,6 @@
}
}
-void DyldSharedCache::forEachTPRORegion(void (^handler)(const void* content, uint64_t unslidVMAddr, uint64_t vmSize,
- bool& stopRegion)) const
-{
- if ( header.mappingOffset <= __offsetof(dyld_cache_header, tproMappingsCount) )
- return;
-
- uint64_t baseAddress = this->unslidLoadAddress();
-
- const dyld_cache_tpro_mapping_info* mappings = (const dyld_cache_tpro_mapping_info*)((char*)this + header.tproMappingsOffset);
- const dyld_cache_tpro_mapping_info* mappingsEnd = &mappings[header.tproMappingsCount];
- for (const dyld_cache_tpro_mapping_info* m = mappings; m < mappingsEnd; ++m) {
- bool stop = false;
- uint64_t offsetInCache = m->unslidAddress - baseAddress;
- handler((char*)this + (long)offsetInCache, m->unslidAddress, m->size, stop);
- if ( stop )
- return;
- }
-}
-
-const char* DyldSharedCache::mappingName(uint32_t maxProt, uint64_t flags)
-{
- const char* mappingName = "";
- if ( maxProt & VM_PROT_EXECUTE ) {
- if ( flags & DYLD_CACHE_MAPPING_TEXT_STUBS ) {
- mappingName = "__TEXT_STUBS";
- } else {
- mappingName = "__TEXT";
- }
- } else if ( maxProt & VM_PROT_WRITE ) {
- if ( flags & DYLD_CACHE_MAPPING_AUTH_DATA ) {
- if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
- mappingName = "__AUTH_DIRTY";
- else if ( flags & DYLD_CACHE_MAPPING_CONST_TPRO_DATA )
- mappingName = "__AUTH_TPRO_CONST";
- else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
- mappingName = "__AUTH_CONST";
- else
- mappingName = "__AUTH";
- } else {
- if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
- mappingName = "__DATA_DIRTY";
- else if ( flags & DYLD_CACHE_MAPPING_CONST_TPRO_DATA )
- mappingName = "__TPRO_CONST";
- else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
- mappingName = "__DATA_CONST";
- else
- mappingName = "__DATA";
- }
- }
- else if ( maxProt & VM_PROT_READ ) {
- if ( flags & DYLD_CACHE_READ_ONLY_DATA )
- mappingName = "__READ_ONLY";
- else
- mappingName = "__LINKEDIT";
- } else {
- mappingName = "*unknown*";
- }
- return mappingName;
-}
-
void DyldSharedCache::forEachRange(void (^handler)(const char* mappingName,
uint64_t unslidVMAddr, uint64_t vmSize,
uint32_t cacheFileIndex, uint64_t fileOffset,
@@ -354,7 +290,35 @@
forEachCache(^(const DyldSharedCache *cache, bool& stopCache) {
cache->forEachRegion(^(const void *content, uint64_t unslidVMAddr, uint64_t size,
uint32_t initProt, uint32_t maxProt, uint64_t flags, bool& stopRegion) {
- const char* mappingName = DyldSharedCache::mappingName(maxProt, flags);
+ const char* mappingName = "";
+ if ( maxProt & VM_PROT_EXECUTE ) {
+ if ( flags & DYLD_CACHE_MAPPING_TEXT_STUBS ) {
+ mappingName = "__TEXT_STUBS";
+ } else {
+ mappingName = "__TEXT";
+ }
+ } else if ( maxProt & VM_PROT_WRITE ) {
+ if ( flags & DYLD_CACHE_MAPPING_AUTH_DATA ) {
+ if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
+ mappingName = "__AUTH_DIRTY";
+ else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
+ mappingName = "__AUTH_CONST";
+ else
+ mappingName = "__AUTH";
+ } else {
+ if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
+ mappingName = "__DATA_DIRTY";
+ else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
+ mappingName = "__DATA_CONST";
+ else
+ mappingName = "__DATA";
+ }
+ }
+ else if ( maxProt & VM_PROT_READ ) {
+ mappingName = "__LINKEDIT";
+ } else {
+ mappingName = "*unknown*";
+ }
uint64_t fileOffset = (uint8_t*)content - (uint8_t*)cache;
bool stop = false;
handler(mappingName, unslidVMAddr, size, cacheFileIndex, fileOffset, initProt, maxProt, stop);
@@ -388,7 +352,7 @@
return;
for (uint32_t i = 0; i != header.subCacheArrayCount; ++i) {
- const DyldSharedCache* cache = (const DyldSharedCache*)((uintptr_t)this + this->getSubCacheVmOffset(i));
+ const DyldSharedCache* cache = (const DyldSharedCache*)((uint8_t*)this + this->getSubCacheVmOffset(i));
handler(cache, stop);
if ( stop )
return;
@@ -422,25 +386,25 @@
void DyldSharedCache::getSubCacheUuid(uint8_t index, uint8_t uuid[]) const {
if (header.mappingOffset <= __offsetof(dyld_cache_header, cacheSubType) ) {
- const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uintptr_t)this + header.subCacheArrayOffset);
+ const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uint8_t*)this + header.subCacheArrayOffset);
memcpy(uuid, subCacheEntries[index].uuid, 16);
} else {
- const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uintptr_t)this + header.subCacheArrayOffset);
+ const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uint8_t*)this + header.subCacheArrayOffset);
memcpy(uuid, subCacheEntries[index].uuid, 16);
}
}
uint64_t DyldSharedCache::getSubCacheVmOffset(uint8_t index) const {
if (header.mappingOffset <= __offsetof(dyld_cache_header, cacheSubType) ) {
- const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uintptr_t)this + header.subCacheArrayOffset);
+ const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uint8_t*)this + header.subCacheArrayOffset);
return subCacheEntries[index].cacheVMOffset;
} else {
- const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uintptr_t)this + header.subCacheArrayOffset);
+ const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uint8_t*)this + header.subCacheArrayOffset);
return subCacheEntries[index].cacheVMOffset;
}
}
-bool DyldSharedCache::inCache(const void* addr, size_t length, bool& immutable) const
+bool DyldSharedCache::inCache(const void* addr, size_t length, bool& readOnly) const
{
// quick out if before start of cache
if ( addr < this )
@@ -450,19 +414,22 @@
uintptr_t slide = (uintptr_t)this - (uintptr_t)(mappings[0].address);
uintptr_t unslidStart = (uintptr_t)addr - slide;
- // walk cache ranges
- __block bool found = false;
- auto inRange = ^(const char* mappingName, uint64_t unslidVMAddr, uint64_t vmSize, uint32_t cacheFileIndex,
- uint64_t fileOffset, uint32_t initProt, uint32_t maxProt, bool& stopRange) {
- if ( (unslidVMAddr <= unslidStart) && ((unslidStart+length) < (unslidVMAddr+vmSize)) ) {
- found = true;
- immutable = ((maxProt & VM_PROT_WRITE) == 0);
- stopRange = true;
- }
- };
- this->forEachRange(inRange, nullptr);
-
- return found;
+ // quick out if after end of cache
+ const dyld_cache_mapping_info* lastMapping = &mappings[header.mappingCount - 1];
+ if ( unslidStart > (lastMapping->address + lastMapping->size) )
+ return false;
+
+ // walk cache regions
+ const dyld_cache_mapping_info* mappingsEnd = &mappings[header.mappingCount];
+ uintptr_t unslidEnd = unslidStart + length;
+ for (const dyld_cache_mapping_info* m=mappings; m < mappingsEnd; ++m) {
+ if ( (unslidStart >= m->address) && (unslidEnd < (m->address+m->size)) ) {
+ readOnly = ((m->initProt & VM_PROT_WRITE) == 0);
+ return true;
+ }
+ }
+
+ return false;
}
bool DyldSharedCache::isAlias(const char* path) const {
@@ -575,7 +542,7 @@
// check for cache without local symbols info
if (!this->hasLocalSymbolsInfo())
return nullptr;
- const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+ const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
return getLocalNlistEntries(localInfo);
}
@@ -584,7 +551,7 @@
// check for cache without local symbols info
if (!this->hasLocalSymbolsInfo())
return 0;
- const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+ const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
return localInfo->nlistCount;
}
@@ -598,7 +565,7 @@
// check for cache without local symbols info
if (!this->hasLocalSymbolsInfo())
return nullptr;
- const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+ const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
return getLocalStrings(localInfo);
}
@@ -607,7 +574,7 @@
// check for cache without local symbols info
if (!this->hasLocalSymbolsInfo())
return 0;
- const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+ const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
return localInfo->stringsSize;
}
@@ -617,7 +584,7 @@
const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
uint32_t dyldCacheImageIndex;
if ( hasImagePath(dylibPath, dyldCacheImageIndex) )
- return (dyld3::MachOFile*)((uintptr_t)this + dylibs[dyldCacheImageIndex].address - mappings[0].address);
+ return (dyld3::MachOFile*)((uint8_t*)this + dylibs[dyldCacheImageIndex].address - mappings[0].address);
return nullptr;
}
@@ -626,7 +593,7 @@
// check for cache without local symbols info
if (!this->hasLocalSymbolsInfo())
return;
- const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+ const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
if ( header.mappingOffset >= __offsetof(dyld_cache_header, symbolFileUUID) ) {
// On new caches, the dylibOffset is 64-bits, and is a VM offset
@@ -651,10 +618,10 @@
const mach_header* DyldSharedCache::getIndexedImageEntry(uint32_t index, uint64_t& mTime, uint64_t& inode) const
{
const dyld_cache_image_info* dylibs = images();
- const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((uintptr_t)this + header.mappingOffset);
+ const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
mTime = dylibs[index].modTime;
inode = dylibs[index].inode;
- return (mach_header*)((uintptr_t)this + dylibs[index].address - mappings[0].address);
+ return (mach_header*)((uint8_t*)this + dylibs[index].address - mappings[0].address);
}
const mach_header* DyldSharedCache::getIndexedImageEntry(uint32_t index) const
@@ -1168,8 +1135,7 @@
#if BUILDING_SHARED_CACHE_UTIL
void DyldSharedCache::forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl,
void (^handler)(uint32_t userImageIndex, uint32_t userVMOffset,
- MachOLoaded::PointerMetaData pmd, uint64_t addend,
- bool isWeakImport)) const {
+ MachOLoaded::PointerMetaData pmd, uint64_t addend)) const {
if ( header.patchInfoAddr == 0 )
return;
@@ -1275,7 +1241,7 @@
pmd.key = patchLocation.key;
pmd.usesAddrDiversity = patchLocation.usesAddressDiversity;
- handler(userImageIndex, userVMOffset, pmd, patchLocation.getAddend(), false);
+ handler(userImageIndex, userVMOffset, pmd, patchLocation.getAddend());
}
}
return;
@@ -1305,8 +1271,7 @@
}
void DyldSharedCache::forEachPatchableUseOfExportInImage(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl, uint32_t userImageIndex,
- void (^handler)(uint32_t userVMOffset, MachOLoaded::PointerMetaData pmd, uint64_t addend,
- bool isWeakImport)) const {
+ void (^handler)(uint32_t userVMOffset, MachOLoaded::PointerMetaData pmd, uint64_t addend)) const {
if ( header.patchInfoAddr == 0 )
return;
@@ -1414,7 +1379,7 @@
pmd.key = patchLocation.key;
pmd.usesAddrDiversity = patchLocation.usesAddressDiversity;
- handler(userVMOffset, pmd, patchLocation.getAddend(), false);
+ handler(userVMOffset, pmd, patchLocation.getAddend());
}
}
}
@@ -1429,8 +1394,7 @@
void DyldSharedCache::forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl,
void (^handler)(uint64_t cacheVMOffset,
- MachOLoaded::PointerMetaData pmd, uint64_t addend,
- bool isWeakImport)) const {
+ MachOLoaded::PointerMetaData pmd, uint64_t addend)) const {
if ( header.patchInfoAddr == 0 )
return;
@@ -1478,7 +1442,7 @@
pmd.key = patchLocation.key;
pmd.usesAddrDiversity = patchLocation.usesAddressDiversity;
- handler(patchLocation.cacheOffset, pmd, patchLocation.getAddend(), false);
+ handler(patchLocation.cacheOffset, pmd, patchLocation.getAddend());
}
}
return;
@@ -1500,8 +1464,7 @@
void DyldSharedCache::forEachPatchableGOTUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl,
void (^handler)(uint64_t cacheVMOffset,
MachOFile::PointerMetaData pmd,
- uint64_t addend,
- bool isWeakImport)) const {
+ uint64_t addend)) const {
if ( header.patchInfoAddr == 0 )
return;
@@ -1629,9 +1592,6 @@
assert(slideInfo->delta_mask == 0x00000000C0000000);
pointerFormat = VMAddrConverter::SharedCacheFormat::v4;
pointerValueAdd = slideInfo->value_add;
- } else if ( slideInfoHeader->version == 5 ) {
- pointerFormat = VMAddrConverter::SharedCacheFormat::v5;
- pointerValueAdd = unslidLoadAddress();
} else {
assert(false);
}
@@ -2093,8 +2053,7 @@
if ( header.swiftOptsOffset == 0 )
return nullptr;
- SwiftOptimizationHeader* optHeader = (SwiftOptimizationHeader*)((char*)this + header.swiftOptsOffset);
- return optHeader;
+ return (SwiftOptimizationHeader*)((char*)this + header.swiftOptsOffset);
}
std::pair<const void*, uint64_t> DyldSharedCache::getObjCConstantRange() const
@@ -2241,5 +2200,3 @@
});
}
#endif
-
-#endif // !TARGET_OS_EXCLAVEKIT