@@ -29,7 +29,7 @@ std::vector<std::string> CacheImage::GetDependencies() const
2929}
3030
3131CacheEntry::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)
3333{
3434 m_filePath = std::move (filePath);
3535 m_fileName = std::move (fileName);
@@ -88,14 +88,15 @@ std::optional<CacheEntry> CacheEntry::FromFile(const std::string& filePath, cons
8888 }
8989
9090 // 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 );
9293 dyld_cache_image_info currentImg {};
9394 for (size_t i = 0 ; i < header.imagesCount ; i++)
9495 {
9596 file->Read (
9697 ¤tImg, header.imagesOffset + (i * sizeof (dyld_cache_image_info)), sizeof (dyld_cache_image_info));
9798 auto imagePath = file->ReadNullTermString (currentImg.pathFileOffset );
98- images.insert_or_assign (imagePath, currentImg);
99+ images.emplace_back (imagePath, currentImg);
99100 }
100101
101102 // Handle old dyld format that uses old images field.
@@ -104,7 +105,7 @@ std::optional<CacheEntry> CacheEntry::FromFile(const std::string& filePath, cons
104105 file->Read (
105106 ¤tImg, header.imagesOffsetOld + (i * sizeof (dyld_cache_image_info)), sizeof (dyld_cache_image_info));
106107 auto imagePath = file->ReadNullTermString (currentImg.pathFileOffset );
107- images.insert_or_assign (imagePath, currentImg);
108+ images.emplace_back (imagePath, currentImg);
108109 }
109110
110111 // 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
119120 branchIslandImg.address = header.branchPoolsOffset + (i * sizeof (uint64_t ));
120121 // Mason: why such a long name for the image???
121122 auto imageName = fmt::format (" dyld_shared_cache_branch_islands_{}" , i);
122- images.insert_or_assign (imageName, branchIslandImg);
123+ images.emplace_back (imageName, branchIslandImg);
123124 }
124125
125126 return CacheEntry (filePath, fileName, type, header, mappings, images);
@@ -151,12 +152,12 @@ SharedCache::SharedCache(uint64_t addressSize)
151152}
152153
153154
154- void SharedCache::AddImage (CacheImage image)
155+ void SharedCache::AddImage (CacheImage&& image)
155156{
156157 m_images.insert ({image.headerAddress , std::move (image)});
157158}
158159
159- void SharedCache::AddRegion (CacheRegion region)
160+ void SharedCache::AddRegion (CacheRegion&& region)
160161{
161162 // Handle overlapping regions here.
162163 const auto regionRange = region.AsAddressRange ();
@@ -203,8 +204,8 @@ void SharedCache::AddSymbol(CacheSymbol symbol)
203204
204205void SharedCache::AddSymbols (std::vector<CacheSymbol>&& symbols)
205206{
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)} );
208209}
209210
210211CacheEntryId SharedCache::AddEntry (CacheEntry entry)
@@ -281,18 +282,17 @@ bool SharedCache::ProcessEntryImage(const std::string& path, const dyld_cache_im
281282 flags |= SegmentExecutable;
282283 sectionRegion.flags = static_cast <BNSegmentFlag>(flags);
283284
284- // Add the image section to the cache and also to the image region starts
285- AddRegion (sectionRegion);
286285 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));
287288 }
288289
289290 // Add the exported symbols to the available symbols.
290291 std::vector<CacheSymbol> exportSymbols = imageHeader->ReadExportSymbolTrie (*m_vm);
291292 AddSymbols (std::move (exportSymbols));
292293
293294 // 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));
296296
297297 AddImage (std::move (image));
298298 return true ;
0 commit comments