From 689ab1cba2283a9da70d2b07577d274d5ce27033 Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Mon, 14 Dec 2015 04:09:53 +0000
Subject: [PATCH] Emit new COMPLETE annotations in *.STARSxrefs files.

Former-commit-id: c80168bd4f04a0fe90c69ec9076d8be10f7c5280
---
 include/interfaces/abstract/STARSProgram.h | 3 ++-
 include/interfaces/idapro/STARSProgram.h   | 2 +-
 include/interfaces/irdb/STARSProgram.h     | 2 +-
 src/base/SMPFunction.cpp                   | 5 +++--
 src/interfaces/abstract/STARSProgram.cpp   | 9 +++++++++
 src/interfaces/idapro/STARSIDAProgram.cpp  | 8 +++++++-
 6 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/include/interfaces/abstract/STARSProgram.h b/include/interfaces/abstract/STARSProgram.h
index 5522672a..406ffeac 100644
--- a/include/interfaces/abstract/STARSProgram.h
+++ b/include/interfaces/abstract/STARSProgram.h
@@ -85,7 +85,8 @@ class STARS_Program_t
 		void PrintCodeToCodeXref(STARS_ea_t FromAddr, STARS_ea_t ToAddr, std::size_t InstrSize);
 		void PrintDataToCodeXref(STARS_ea_t FromDataAddr, STARS_ea_t ToCodeAddr, std::size_t InstrSize);
 		void PrintUnknownCodeXref(STARS_ea_t ToAddr, std::size_t InstrSize);
-		virtual void PrintAllCodeToCodeXrefs(STARS_ea_t InstAddr, std::size_t InstSize) = 0;
+		void PrintCodeToCodeXrefComplete(STARS_ea_t FromAddr, std::size_t InstrSize, std::size_t IBTCount);
+		virtual void PrintAllCodeToCodeXrefs(STARS_ea_t InstAddr, std::size_t InstSize, bool CallFlag) = 0;
 
 		// Analysis methods
 		virtual bool IsImmedCodeAddress(STARS_ea_t ImmedValue) { return false; }; // Is ImmedValue a code address?
diff --git a/include/interfaces/idapro/STARSProgram.h b/include/interfaces/idapro/STARSProgram.h
index 73f686d4..d0bf5ec6 100644
--- a/include/interfaces/idapro/STARSProgram.h
+++ b/include/interfaces/idapro/STARSProgram.h
@@ -31,7 +31,7 @@ public:
 	// Query methods
 
 	// Printing methods
-	virtual void PrintAllCodeToCodeXrefs(STARS_ea_t InstAddr, std::size_t InstSize);
+	virtual void PrintAllCodeToCodeXrefs(STARS_ea_t InstAddr, std::size_t InstSize, bool CallFlag);
 
 	// Analysis methods
 
diff --git a/include/interfaces/irdb/STARSProgram.h b/include/interfaces/irdb/STARSProgram.h
index eee21452..fa44b695 100644
--- a/include/interfaces/irdb/STARSProgram.h
+++ b/include/interfaces/irdb/STARSProgram.h
@@ -31,7 +31,7 @@ public:
 	void ReportTotalCodeSize(unsigned long long TotalCodeSize);
 	void InitStaticDataTable(SMPProgram *CurrProg);
 
-	virtual void PrintAllCodeToCodeXrefs(STARS_ea_t, std::size_t) {}
+	virtual void PrintAllCodeToCodeXrefs(STARS_ea_t, std::size_t, bool CallFlag) {}
 	virtual void GetBlockSuccessorTargets(bool, STARS_InstructionID_t, std::size_t, std::list<STARS_InstructionID_t>&);
 
 
diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index ff944b24..1bf43ed9 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -9057,8 +9057,9 @@ void SMPFunction::EmitAnnotations(FILE *AnnotFile, FILE *InfoAnnotFile) {
 		SMP_fprintf(AnnotFile, "%18llx %6zu INSTR BELONGTO %llx \n",
 			(unsigned long long) addr, CurrInst->GetSize(), (unsigned long long) GetStartAddr());
 		SMPitype CurrDataFlow = CurrInst->GetDataFlowType();
-		if ((CurrDataFlow == INDIR_JUMP) || (CurrDataFlow == INDIR_CALL)) {
-			global_STARS_program->PrintAllCodeToCodeXrefs(addr, CurrInst->GetSize());
+		bool IndirCallFlag = (CurrDataFlow == INDIR_CALL);
+		if ((CurrDataFlow == INDIR_JUMP) || IndirCallFlag) {
+			global_STARS_program->PrintAllCodeToCodeXrefs(addr, CurrInst->GetSize(), IndirCallFlag);
 		}
 
 		if (this->LocalVarsAllocInstr == addr) {
diff --git a/src/interfaces/abstract/STARSProgram.cpp b/src/interfaces/abstract/STARSProgram.cpp
index 490e3376..973190ba 100644
--- a/src/interfaces/abstract/STARSProgram.cpp
+++ b/src/interfaces/abstract/STARSProgram.cpp
@@ -421,6 +421,15 @@ void STARS_Program_t::PrintUnknownCodeXref(STARS_ea_t ToAddr, std::size_t InstrS
 	return;
 }
 
+// Utility functions to signify code xrefs are complete in STARS_XrefsFile for FromAddr
+void STARS_Program_t::PrintCodeToCodeXrefComplete(STARS_ea_t FromAddr, std::size_t InstrSize, std::size_t IBTCount) {
+	if (IsAddressInCodeRange(FromAddr)) {
+		SMP_fprintf(this->GetXrefsFile(), "%18llx %6zu INSTR XREF FROMIB COMPLETE %6zu \n",
+			(unsigned long long) FromAddr, InstrSize, IBTCount);
+	}
+	return;
+}
+
 // Read the foo.exe.policy file to initialize our security policies for system calls.
 void STARS_Program_t::ZST_InitPolicies(void) {
 	string ZSTPolicyFileName(this->GetRootFileName());
diff --git a/src/interfaces/idapro/STARSIDAProgram.cpp b/src/interfaces/idapro/STARSIDAProgram.cpp
index 19412aad..fcbfdaa2 100644
--- a/src/interfaces/idapro/STARSIDAProgram.cpp
+++ b/src/interfaces/idapro/STARSIDAProgram.cpp
@@ -408,17 +408,23 @@ void STARS_IDA_Program_t::ComputeGlobalFieldOffsets(struct GlobalVar &CurrGlobal
 //  be written to the annotations file, read by the IRDB builder process, and then read back in
 //  the IRDB version of InitStaticDataTable().
 
-void STARS_IDA_Program_t::PrintAllCodeToCodeXrefs(STARS_ea_t InstAddr, std::size_t InstSize) {
+void STARS_IDA_Program_t::PrintAllCodeToCodeXrefs(STARS_ea_t InstAddr, std::size_t InstSize, bool CallFlag) {
 	SMP_xref_t xrefs;
+	std::size_t IBTCount = 0;
 	for (bool ok = xrefs.SMP_first_from(InstAddr, XREF_ALL); ok; ok = xrefs.SMP_next_from()) {
 		if (xrefs.GetTo() != 0) {
 			// Exclude the ordinary fall-through control flow type, fl_F
 			if (xrefs.GetIscode() && (xrefs.GetType() != fl_F)) {
 				// Found a code target, with its address in xrefs.to
 				global_STARS_program->PrintCodeToCodeXref(InstAddr, xrefs.GetTo(), InstSize);
+				++IBTCount;
 			}
 		}
 	}
+	if ((IBTCount > 0) && (!CallFlag)) {
+		// IDA Pro does not list xrefs for INDIR_JUMPs if it has not analyzed the xrefs completely for the IB.
+		global_STARS_program->PrintCodeToCodeXrefComplete(InstAddr, InstSize, IBTCount);
+	}
 	return;
 }
 
-- 
GitLab