99using namespace std ;
1010using namespace BinaryNinja ;
1111
12- // TODO: Decomposed from BinaryView::IsOffsetCodeSemantics BinaryView::IsOffsetExternSemantics
13- // TODO: When the better sections model is merged, remove this
14- static bool IsOffsetCodeSemanticsFast (BinaryView* data, const vector<Section*>& readOnlySections, const vector<Section*>& dataExternSections, uint64_t offset)
15- {
16- if (!data->IsOffsetBackedByFile (offset))
17- return false ;
18-
19- for (const auto & i : readOnlySections)
20- {
21- if ((offset >= i->GetStart ()) && (offset < i->GetEnd ()))
22- return true ;
23- }
24- for (const auto & i : dataExternSections)
25- {
26- if ((offset >= i->GetStart ()) && (offset < i->GetEnd ()))
27- return false ;
28- }
29-
30- return data->IsOffsetExecutable (offset);
31- }
32-
33-
34- static bool IsOffsetExternSemanticsFast (BinaryView* data, const vector<Section*>& externSections, uint64_t offset)
35- {
36- if (data->IsOffsetBackedByFile (offset))
37- return false ;
38- if (data->IsOffsetExecutable (offset))
39- return false ;
40-
41- for (const auto & i : externSections)
42- {
43- if ((offset >= i->GetStart ()) && (offset < i->GetEnd ()))
44- return true ;
45- }
46-
47- return false ;
48- }
49-
5012
5113static bool GetNextFunctionAfterAddress (Ref<BinaryView> data, Ref<Platform> platform, uint64_t address, Ref<Function>& nextFunc)
5214{
@@ -59,6 +21,7 @@ static bool GetNextFunctionAfterAddress(Ref<BinaryView> data, Ref<Platform> plat
5921void Architecture::DefaultAnalyzeBasicBlocks (Function* function, BasicBlockAnalysisContext& context)
6022{
6123 auto data = function->GetView ();
24+ auto sectionMap = data->GetSectionMap ();
6225 queue<ArchAndAddr> blocksToProcess;
6326 map<ArchAndAddr, Ref<BasicBlock>> instrBlocks;
6427 set<ArchAndAddr> seenBlocks;
@@ -93,31 +56,6 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
9356 return (strRef.length >= byteLimit);
9457 };
9558
96- // TODO: Decomposed from BinaryView::IsOffsetCodeSemantics BinaryView::IsOffsetExternSemantics
97- // TODO: When the better sections model is merged, remove this
98- auto sections = data->GetSections ();
99- vector<Section*> externSections, readOnlySections, dataExternSections;
100- externSections.reserve (sections.size ());
101- readOnlySections.reserve (sections.size ());
102- dataExternSections.reserve (sections.size ());
103- for (auto & section: sections)
104- {
105- if (section->GetSemantics () == ExternalSectionSemantics)
106- {
107- externSections.push_back (section);
108- }
109- if (section->GetSemantics () == ReadOnlyCodeSectionSemantics)
110- {
111- readOnlySections.push_back (section);
112- }
113- if ((section->GetSemantics () == ReadOnlyDataSectionSemantics) ||
114- (section->GetSemantics () == ReadWriteDataSectionSemantics) ||
115- (section->GetSemantics () == ExternalSectionSemantics))
116- {
117- dataExternSections.push_back (section);
118- }
119- }
120-
12159 // Start by processing the entry point of the function
12260 Ref<Platform> funcPlatform = function->GetPlatform ();
12361 auto start = function->GetStart ();
@@ -136,7 +74,7 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
13674 // Extract the bounds of the section containing this
13775 // function, to avoid calling into the BinaryView on
13876 // every instruction.
139- for (auto & sec : data ->GetSectionsAt (start))
77+ for (auto & sec : sectionMap ->GetSectionsAt (start))
14078 {
14179 if (sec->GetSemantics () == ReadOnlyDataSectionSemantics)
14280 continue ;
@@ -295,7 +233,7 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
295233 uint64_t instrEnd = location.address + info.length - 1 ;
296234 bool slowPath = !fastValidate || (instrEnd < fastStartAddr) || (instrEnd > fastEndAddr);
297235 if (slowPath &&
298- ((!IsOffsetCodeSemanticsFast (data, readOnlySections, dataExternSections, instrEnd) && IsOffsetCodeSemanticsFast (data, readOnlySections, dataExternSections, location.address )) ||
236+ ((!sectionMap-> IsOffsetCodeSemantics ( instrEnd) && sectionMap-> IsOffsetCodeSemantics ( location.address )) ||
299237 (!data->IsOffsetBackedByFile (instrEnd) && data->IsOffsetBackedByFile (location.address ))))
300238 {
301239 string text = fmt::format (" Instruction at {:#x} straddles a non-code section" , location.address );
@@ -410,7 +348,7 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
410348 // Normal branch, resume disassembly at targets
411349 endsBlock = true ;
412350 // Target of a call instruction, add the function to the analysis
413- if (IsOffsetExternSemanticsFast (data, externSections, info.branchTarget [i]))
351+ if (sectionMap-> IsOffsetExternSemantics ( info.branchTarget [i]))
414352 {
415353 // Deal with direct pointers into the extern section
416354 DataVariable dataVar;
@@ -487,7 +425,7 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
487425
488426 case CallDestination:
489427 // Target of a call instruction, add the function to the analysis
490- if (IsOffsetExternSemanticsFast (data, externSections, info.branchTarget [i]))
428+ if (sectionMap-> IsOffsetExternSemantics ( info.branchTarget [i]))
491429 {
492430 // Deal with direct pointers into the extern section
493431 DataVariable dataVar;
@@ -514,8 +452,7 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
514452 {
515453 target = ArchAndAddr (info.branchArch [i] ? new CoreArchitecture (info.branchArch [i]) : location.arch , info.branchTarget [i]);
516454
517- if (!fastPath && !IsOffsetCodeSemanticsFast (data, readOnlySections, dataExternSections, target.address ) &&
518- IsOffsetCodeSemanticsFast (data, readOnlySections, dataExternSections, location.address ))
455+ if (!fastPath && !sectionMap->IsOffsetCodeSemantics (target.address ) && sectionMap->IsOffsetCodeSemantics (location.address ))
519456 {
520457 string message = fmt::format (" Non-code call target {:#x}" , target.address );
521458 function->CreateAutoAddressTag (target.arch , location.address , " Non-code Branch" , message, true );
0 commit comments