From 9dc108ade2acc1189fc8c5c29cb8d469e692b120 Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Thu, 16 Jul 2015 00:43:48 +0000
Subject: [PATCH] Continue to simplify stack frame analysis.

Former-commit-id: 9c823779d074ae6da120f41315e6ff1996fbfff2
---
 src/base/SMPFunction.cpp | 25 +++----------------------
 src/base/SMPInstr.cpp    |  3 ---
 2 files changed, 3 insertions(+), 25 deletions(-)

diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 9ce571f3..250077ea 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -1809,24 +1809,8 @@ void SMPFunction::FindAllAllocsAndDeallocs(void) {
 #if SMP_DEBUG_CONTROLFLOW
 				SMP_msg("Returned from MDIsFrameAllocInstr()\n");
 #endif
-				this->LocalVarsAllocInstr = addr;
 				FoundAllocInstr = true;
 				if (DebugFlag) SMP_msg("Found alloc: %s\n", CurrInst->GetDisasm());
-				// As soon as we have found the local vars allocation,
-				//  we can try to fix incorrect sets of UseFP by IDA.
-				// NOTE: We might want to extend this in the future to
-				//  handle functions that have no locals.  **!!**
-#if 0
-				bool FixedUseFP = MDFixUseFP();
-#if SMP_DEBUG_FRAMEFIXUP
-				if (FixedUseFP) {
-					SMP_msg("Fixed UseFP in %s\n", this->GetFuncName());
-				}
-#endif
-#endif
-				if (this->UsesFramePointer()) { // now that MDFixUseFP() has validated this flag ...
-					this->FindFramePointerDelta(); // find stack delta that is saved in frame pointer in function prologue
-				}
 			}
 			else if (FoundAllocInstr) {
 				// We can now start searching for the DeallocInstr.
@@ -1920,9 +1904,6 @@ void SMPFunction::FindAllAllocsAndDeallocs(void) {
 		}
 #endif
 #endif
-		if (this->UsesFramePointer()) { // now that MDFixUseFP() has validated this flag ...
-			this->FindFramePointerDelta(); // find stack delta that is saved in frame pointer in function prologue
-		}
 	} // end if (LocalVarsSize > 0) ... else ...
 
 	if (!FoundAllocInstr && (0 < this->LocalVarsSize) && this->IsLeaf()) {
@@ -1938,7 +1919,6 @@ void SMPFunction::FindAllAllocsAndDeallocs(void) {
 #endif
 #endif
 		if (this->UsesFramePointer()) { // now that MDFixUseFP() has validated this flag ...
-			this->FindFramePointerDelta(); // find stack delta that is saved in frame pointer in function prologue
 			if (0 != this->FramePointerStackDelta) {
 				SMP_msg("INFO: Found FramePointerStackDelta of %ld in frameless leaf function %s\n", 
 					(long) this->FramePointerStackDelta, this->GetFuncName());
@@ -2145,7 +2125,7 @@ bool SMPFunction::MDFixFrameInfo(void) {
 				}
 			}
 		}
-		else if (CurrInstr->MDIsFrameAllocInstr()) {
+		else if (CurrInstr->MDIsFrameAllocInstr() && (!CurrInstr->HasAllocaRTL())) {
 			if (DebugFlag) SMP_msg("libc_csu_init allocinstr: %s\n", CurrInstr->GetDisasm());
 			// Get the size being allocated.
 			set<DefOrUse, LessDefUse>::iterator CurrUse;
@@ -2165,7 +2145,8 @@ bool SMPFunction::MDFixFrameInfo(void) {
 			} // end for all uses
 		} // end if (push) .. elsif frame allocating instr
 		else if (CurrInstr->HasStackPointerDEF() && (DEFAULT == CurrInstr->GetDataFlowType())) { 
-			// Handle all other possible changes to stack pointer register from non-control-flow instructions
+			// Handle all other possible changes to stack pointer register from non-control-flow instructions.
+			//  This includes the alloca() inlined call pattern (sub esp,reg) which is assigned a default size.
 			if (CurrInstr->MDIsLeaveInstr() && (STARS_BADADDR != this->LocalVarsAllocInstr)) {
 				SMP_msg("ERROR: MDFixFrameInfo() found LEAVE opcode at %llx before finding a frame allocation.\n",
 					(unsigned long long) CurrInstr->GetAddr());
diff --git a/src/base/SMPInstr.cpp b/src/base/SMPInstr.cpp
index bfdd07da..86ddab8b 100644
--- a/src/base/SMPInstr.cpp
+++ b/src/base/SMPInstr.cpp
@@ -2856,8 +2856,6 @@ int SMPInstr::operator<=(const SMPInstr &rhs) const {
 // Is this instruction one that allocates space on the
 //  stack for the local variables?
 bool SMPInstr::MDIsFrameAllocInstr(void) {
-	if (this->IsAllocaCall())
-		return true;
 
 	// The frame allocating instruction should look like:
 	//   sub esp,48   or   add esp,-64   etc.
@@ -2880,7 +2878,6 @@ bool SMPInstr::MDIsFrameAllocInstr(void) {
 			//  a positive immediate value. We will throw in, free of
 			//  charge, a subtraction of a register, which is how alloca()
 			//  usually allocates stack space.
-			// PHASE ORDERING: Should we use the Operands[] instead of the USE list? **!!**
 			set<DefOrUse, LessDefUse>::iterator CurrUse;
 			for (CurrUse = this->GetFirstUse(); CurrUse != this->GetLastUse(); ++CurrUse) {
 				if (CurrUse->GetOp()->IsImmedOp()) {
-- 
GitLab