From 30ad94f8eb2f18f7c5d9ca61a05d55213100bdc8 Mon Sep 17 00:00:00 2001
From: Jason Hiser <jdhiser@gmail.com>
Date: Thu, 21 Feb 2019 15:52:07 -0500
Subject: [PATCH] added isDefCFAOffset getter

---
 include/ehp.hpp  |  3 ++-
 src/ehp.cpp      | 31 +++++++++++++++----------------
 src/ehp_priv.hpp |  3 ++-
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/include/ehp.hpp b/include/ehp.hpp
index a37f757..d88a162 100644
--- a/include/ehp.hpp
+++ b/include/ehp.hpp
@@ -40,6 +40,7 @@ class EHProgramInstruction_t
 	virtual ~EHProgramInstruction_t() {}
 	virtual void print(uint64_t &pc, int64_t caf=1) const=0;
 	virtual bool isNop() const =0;
+	virtual bool isDefCFAOffset() const =0;
 	virtual bool isRestoreState() const =0;
 	virtual bool isRememberState() const =0;
 	virtual const EHProgramInstructionByteVector_t& getBytes() const =0;
@@ -164,7 +165,7 @@ class EHFrameParser_t
 	virtual void print() const=0;
 	virtual const shared_ptr<FDEVector_t> getFDEs() const =0;
 	virtual const shared_ptr<CIEVector_t> getCIEs() const =0;
-	virtual const shared_ptr<FDEContents_t> findFDE(uint64_t addr) const =0; 
+	virtual const FDEContents_t* findFDE(uint64_t addr) const =0; 
 
 	static unique_ptr<const EHFrameParser_t> factory(const string filename);
 	static unique_ptr<const EHFrameParser_t> factory(
diff --git a/src/ehp.cpp b/src/ehp.cpp
index 81b5bab..d2d4746 100644
--- a/src/ehp.cpp
+++ b/src/ehp.cpp
@@ -692,21 +692,20 @@ bool eh_program_insn_t<ptrsize>::isNop() const
 	const auto opcode=program_bytes[0];
 	const auto opcode_upper2=(uint8_t)(opcode >> 6);
 	const auto opcode_lower6=(uint8_t)(opcode & (0x3f));
-	switch(opcode_upper2)
-	{
-		case 0:
-		{
-			switch(opcode_lower6)
-			{
-			
-				case DW_CFA_nop:
-					return true;
-			}
-		}
-	}
-	return false;
+	return opcode_upper2==0 && opcode_lower6==DW_CFA_nop;
 }
 
+template <int ptrsize>
+bool eh_program_insn_t<ptrsize>::isDefCFAOffset() const 
+{
+	const auto opcode=program_bytes[0];
+	const auto opcode_upper2=(uint8_t)(opcode >> 6);
+	const auto opcode_lower6=(uint8_t)(opcode & (0x3f));
+
+	return opcode_upper2==0 && opcode_lower6==DW_CFA_def_cfa_offset;
+}
+
+
 template <int ptrsize>
 bool eh_program_insn_t<ptrsize>::isRestoreState() const 
 {
@@ -1704,13 +1703,13 @@ const shared_ptr<CIEVector_t>  split_eh_frame_impl_t<ptrsize>::getCIEs() const
 }
 
 template <int ptrsize>
-const shared_ptr<FDEContents_t> split_eh_frame_impl_t<ptrsize>::findFDE(uint64_t addr) const
+const FDEContents_t* split_eh_frame_impl_t<ptrsize>::findFDE(uint64_t addr) const
 {
 
         const auto tofind=fde_contents_t<ptrsize>( addr, addr+1);
         const auto fde_it=fdes.find(tofind);
-	const auto raw_ret_ptr = (fde_it==fdes.end()) ?  nullptr : new fde_contents_t<ptrsize>(*fde_it);
-	return shared_ptr<FDEContents_t>(raw_ret_ptr);
+	const auto raw_ret_ptr = (fde_it==fdes.end()) ?  nullptr : &*fde_it;
+	return raw_ret_ptr;
 }
 
 unique_ptr<const EHFrameParser_t> EHFrameParser_t::factory(const string filename)
diff --git a/src/ehp_priv.hpp b/src/ehp_priv.hpp
index ef49bfb..dba5759 100644
--- a/src/ehp_priv.hpp
+++ b/src/ehp_priv.hpp
@@ -109,6 +109,7 @@ class eh_program_insn_t  : public EHProgramInstruction_t
 		const uint32_t &max);
 
 	bool isNop() const ;
+	bool isDefCFAOffset() const ;
 	bool isRestoreState() const ;
 	bool isRememberState() const ;
 
@@ -416,7 +417,7 @@ class split_eh_frame_impl_t : public EHFrameParser_t
 
         virtual const shared_ptr<FDEVector_t> getFDEs() const;
         virtual const shared_ptr<CIEVector_t> getCIEs() const;
-        virtual const shared_ptr<FDEContents_t> findFDE(uint64_t addr) const; 
+        virtual const FDEContents_t* findFDE(uint64_t addr) const; 
 
 
 
-- 
GitLab