@@ -111,8 +111,12 @@ void IdentifyStub(BinaryView& view, const SharedCacheController& controller, uin
111111 // Define the symbol and type (if found)
112112 auto targetFunc = view.GetAnalysisFunction (view.GetDefaultPlatform (), stubFuncAddr);
113113 if (type && targetFunc)
114+ {
114115 targetFunc->SetUserType (type);
115- // TODO: When to reanalysis function (mark updates required?)
116+ // TODO: When to reanalysis function (mark updates required?)
117+ targetFunc->Reanalyze ();
118+ }
119+
116120 view.DefineUserSymbol (new Symbol (symbol->type , STUB_PREFIX + symbol->name , stubFuncAddr));
117121}
118122
@@ -241,6 +245,7 @@ void FixupStubs(Ref<AnalysisContext> ctx)
241245 auto def = mssa->GetSSAVarDefinition (dest.GetSourceSSAVariable ());
242246 auto defInstr = mssa->GetInstruction (def);
243247 auto targetOffset = defInstr.GetSourceExpr ().GetSourceExpr ().GetConstant ();
248+ // Load the region and re-analyze the current stub section.
244249 processStubImageCall (targetOffset);
245250 }
246251 }
@@ -250,6 +255,42 @@ void FixupStubs(Ref<AnalysisContext> ctx)
250255 }
251256}
252257
258+ void IdentifyStubs (Ref<AnalysisContext> ctx)
259+ {
260+ const auto func = ctx->GetFunction ();
261+ const auto view = func->GetView ();
262+ const auto mlil = ctx->GetMediumLevelILFunction ();
263+ if (!mlil)
264+ return ;
265+ const auto mssa = mlil->GetSSAForm ();
266+ if (!mssa)
267+ return ;
268+
269+ auto workflowState = GetGlobalWorkflowState (view);
270+ auto controller = SharedCacheController::GetController (*view);
271+ if (!controller)
272+ return ;
273+
274+ // Get the containing section for section specific tasks.
275+ auto funcStart = func->GetStart ();
276+ auto sections = view->GetSectionsAt (funcStart);
277+ if (sections.empty ())
278+ return ;
279+ const auto & section = sections.front ();
280+ const auto sectionName = section->GetName ();
281+
282+ auto jumpInstr = mssa->GetInstruction (0 );
283+ if (jumpInstr.operation == MLIL_JUMP)
284+ {
285+ auto dest = jumpInstr.GetDestExpr <MLIL_JUMP>();
286+ if (dest.operation == MLIL_CONST_PTR)
287+ {
288+ auto targetOffset = dest.GetConstant ();
289+ IdentifyStub (*view, *controller, funcStart, targetOffset);
290+ }
291+ }
292+ }
293+
253294// TODO: FixupOffImageAccess
254295void FixupOffImageCalls (Ref<AnalysisContext> ctx)
255296{
@@ -267,20 +308,17 @@ void FixupOffImageCalls(Ref<AnalysisContext> ctx)
267308 if (!controller)
268309 return ;
269310
270- auto processOffImageCall = [&](const uint64_t stubFuncAddr, const uint64_t symbolAddr ) {
271- const auto region = controller->GetRegionContaining (symbolAddr );
311+ auto tryAddRegion = [&](const uint64_t regionAddr ) {
312+ const auto region = controller->GetRegionContaining (regionAddr );
272313 if (!region.has_value ())
273314 return ;
274315
275316 // Load stub region if not already loaded and reanalyze the function (to pickup stub functions)
317+ // TODO: Call mark updates required instead??? Use incremental update type instead???
318+ // TODO: Should we _reanalyze_ all functions? Shouldn't analysis update because of the new region anyways?
276319 if (workflowState->autoLoadStubsAndDyldData && region->type == SharedCacheRegionTypeStubIsland)
277320 if (controller->ApplyRegion (*view, *region))
278- func->Reanalyze (); // TODO: Call mark updates required instead??? Use incremental update type instead???
279-
280- // TODO: Is there a point to this at all????
281- // TODO: is there a point to calling this if workflowState->autoLoadStubsAndDyldData is not on???
282- // Apply symbols (and possibly a type) to the relevant stub functions
283- IdentifyStub (*view, *controller, stubFuncAddr, symbolAddr);
321+ func->Reanalyze ();
284322 };
285323
286324 // Load all unmapped STUB regions / images that are called in this function.
@@ -296,7 +334,7 @@ void FixupOffImageCalls(Ref<AnalysisContext> ctx)
296334 auto targetAddr = instr.GetDestExpr <MLIL_CALL_SSA>().GetConstant ();
297335 if (!view->IsValidOffset (targetAddr))
298336 {
299- processOffImageCall (targetAddr, targetAddr);
337+ tryAddRegion ( targetAddr);
300338 }
301339 }
302340 }
@@ -324,7 +362,7 @@ void FixupOffImageCalls(Ref<AnalysisContext> ctx)
324362 auto targetAddr = ptrExpr.GetConstant ();
325363 if (!view->IsValidOffset (targetAddr))
326364 {
327- processOffImageCall (targetAddr, targetAddr);
365+ tryAddRegion ( targetAddr);
328366 }
329367 }
330368 }
@@ -336,7 +374,7 @@ void FixupOffImageCalls(Ref<AnalysisContext> ctx)
336374 auto targetAddr = destExpr.GetConstant ();
337375 if (!view->IsValidOffset (targetAddr))
338376 {
339- processOffImageCall (targetAddr, targetAddr);
377+ tryAddRegion ( targetAddr);
340378 }
341379 }
342380 else if (destExpr.operation == MLIL_LOAD_SSA)
@@ -347,7 +385,7 @@ void FixupOffImageCalls(Ref<AnalysisContext> ctx)
347385 auto targetAddr = ptrExpr.GetConstant ();
348386 if (!view->IsValidOffset (targetAddr))
349387 {
350- processOffImageCall (targetAddr, targetAddr);
388+ tryAddRegion ( targetAddr);
351389 }
352390 }
353391 }
@@ -357,6 +395,7 @@ void FixupOffImageCalls(Ref<AnalysisContext> ctx)
357395 // TODO: ^ we actually dont really need to do this, the other type of access cont..
358396 // TODO: the other two types of accesses (load & save) we dont want to load their regions, just
359397 // TODO: their symbol information if available.
398+ // TODO: See:
360399 }
361400 }
362401}
@@ -374,9 +413,10 @@ void SharedCacheWorkflow::Register()
374413 // Register and insert activities here.
375414 ObjCActivity::Register (*workflow);
376415 workflow->RegisterActivity (new Activity (" core.analysis.sharedCache.stubs" , &FixupStubs));
416+ workflow->RegisterActivity (new Activity (" core.analysis.sharedCache.identifyStubs" , &IdentifyStubs));
377417 workflow->RegisterActivity (new Activity (" core.analysis.sharedCache.calls" , &FixupOffImageCalls));
378- workflow-> Insert ( " core.function.analyzeTailCalls " , " core.analysis.sharedCache.stubs " ) ;
379- workflow->Insert (" core.function.analyzeTailCalls" , " core.analysis.sharedCache.calls " );
418+ std::vector<std::string> inserted = { " core.analysis.sharedCache.stubs " , " core.analysis.sharedCache.calls " , " core.analysis.sharedCache.identifyStubs " } ;
419+ workflow->Insert (" core.function.analyzeTailCalls" , inserted );
380420
381421 Workflow::RegisterWorkflow (workflow, WORKFLOW_DESCRIPTION);
382422}
0 commit comments