@@ -29,7 +29,7 @@ std::vector<std::string> CacheImage::GetDependencies() const
29
29
}
30
30
31
31
CacheEntry::CacheEntry (std::string filePath, std::string fileName, CacheEntryType type, dyld_cache_header header,
32
- std::vector<dyld_cache_mapping_info> mappings, std::unordered_map <std::string, dyld_cache_image_info> images)
32
+ std::vector<dyld_cache_mapping_info> mappings, std::vector <std::pair<std:: string, dyld_cache_image_info> > images)
33
33
{
34
34
m_filePath = std::move (filePath);
35
35
m_fileName = std::move (fileName);
@@ -88,14 +88,15 @@ std::optional<CacheEntry> CacheEntry::FromFile(const std::string& filePath, cons
88
88
}
89
89
90
90
// Gather all images for the entry.
91
- std::unordered_map<std::string, dyld_cache_image_info> images;
91
+ std::vector<std::pair<std::string, dyld_cache_image_info>> images;
92
+ images.reserve (header.imagesCountOld ? header.imagesCountOld : header.imagesCount );
92
93
dyld_cache_image_info currentImg {};
93
94
for (size_t i = 0 ; i < header.imagesCount ; i++)
94
95
{
95
96
file->Read (
96
97
¤tImg, header.imagesOffset + (i * sizeof (dyld_cache_image_info)), sizeof (dyld_cache_image_info));
97
98
auto imagePath = file->ReadNullTermString (currentImg.pathFileOffset );
98
- images.insert_or_assign (imagePath, currentImg);
99
+ images.emplace_back (imagePath, currentImg);
99
100
}
100
101
101
102
// Handle old dyld format that uses old images field.
@@ -104,7 +105,7 @@ std::optional<CacheEntry> CacheEntry::FromFile(const std::string& filePath, cons
104
105
file->Read (
105
106
¤tImg, header.imagesOffsetOld + (i * sizeof (dyld_cache_image_info)), sizeof (dyld_cache_image_info));
106
107
auto imagePath = file->ReadNullTermString (currentImg.pathFileOffset );
107
- images.insert_or_assign (imagePath, currentImg);
108
+ images.emplace_back (imagePath, currentImg);
108
109
}
109
110
110
111
// NOTE: I am not sure how the header type has changed over time but if apple is replacing fields with other ones
@@ -119,7 +120,7 @@ std::optional<CacheEntry> CacheEntry::FromFile(const std::string& filePath, cons
119
120
branchIslandImg.address = header.branchPoolsOffset + (i * sizeof (uint64_t ));
120
121
// Mason: why such a long name for the image???
121
122
auto imageName = fmt::format (" dyld_shared_cache_branch_islands_{}" , i);
122
- images.insert_or_assign (imageName, branchIslandImg);
123
+ images.emplace_back (imageName, branchIslandImg);
123
124
}
124
125
125
126
return CacheEntry (filePath, fileName, type, header, mappings, images);
@@ -151,12 +152,12 @@ SharedCache::SharedCache(uint64_t addressSize)
151
152
}
152
153
153
154
154
- void SharedCache::AddImage (CacheImage image)
155
+ void SharedCache::AddImage (CacheImage&& image)
155
156
{
156
157
m_images.insert ({image.headerAddress , std::move (image)});
157
158
}
158
159
159
- void SharedCache::AddRegion (CacheRegion region)
160
+ void SharedCache::AddRegion (CacheRegion&& region)
160
161
{
161
162
// Handle overlapping regions here.
162
163
const auto regionRange = region.AsAddressRange ();
@@ -203,8 +204,8 @@ void SharedCache::AddSymbol(CacheSymbol symbol)
203
204
204
205
void SharedCache::AddSymbols (std::vector<CacheSymbol>&& symbols)
205
206
{
206
- for (auto && symbol : symbols)
207
- m_symbols.emplace ( symbol.address , std::move (symbol));
207
+ for (auto & symbol : symbols)
208
+ m_symbols.insert ({ symbol.address , std::move (symbol)} );
208
209
}
209
210
210
211
CacheEntryId SharedCache::AddEntry (CacheEntry entry)
@@ -281,18 +282,17 @@ bool SharedCache::ProcessEntryImage(const std::string& path, const dyld_cache_im
281
282
flags |= SegmentExecutable;
282
283
sectionRegion.flags = static_cast <BNSegmentFlag>(flags);
283
284
284
- // Add the image section to the cache and also to the image region starts
285
- AddRegion (sectionRegion);
286
285
image.regionStarts .push_back (sectionRegion.start );
286
+ // Add the image section to the cache and also to the image region starts
287
+ AddRegion (std::move (sectionRegion));
287
288
}
288
289
289
290
// Add the exported symbols to the available symbols.
290
291
std::vector<CacheSymbol> exportSymbols = imageHeader->ReadExportSymbolTrie (*m_vm);
291
292
AddSymbols (std::move (exportSymbols));
292
293
293
294
// This is behind a shared pointer as the header itself is very large.
294
- // TODO: Make this a unique pointer? I think the image should own the header at this point?
295
- image.header = std::make_shared<SharedCacheMachOHeader>(*imageHeader);
295
+ image.header = std::make_shared<SharedCacheMachOHeader>(std::move (*imageHeader));
296
296
297
297
AddImage (std::move (image));
298
298
return true ;
0 commit comments