From 3f2a2a0af717930c60ea4171d8df615594098847 Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Mon, 25 Sep 2017 15:36:35 +0000
Subject: [PATCH] Better EH verbose messages for zipr, stack stamping+EH
 support, a new cfar config (NOG+SS vrs NogOF+SS), as well as an IR refinement
 technique to do recursive-decent disassembly when a landing pad is not part
 of a function.

---
 include/ehwrite.h |   5 ++
 src/ehwrite.cpp   | 141 +++++++++++++++++++---------------------------
 2 files changed, 62 insertions(+), 84 deletions(-)

diff --git a/include/ehwrite.h b/include/ehwrite.h
index 0e60676..6da0040 100644
--- a/include/ehwrite.h
+++ b/include/ehwrite.h
@@ -18,6 +18,8 @@ class EhWriterImpl_t : public EhWriter_t
 {
 	private:
 
+
+
 	class EhProgramListingManip_t : public libIRDB::EhProgramListing_t
 	{
 		public:
@@ -42,6 +44,7 @@ class EhWriterImpl_t : public EhWriter_t
 		CIErepresentation_t(libIRDB::Instruction_t*, EhWriterImpl_t<ptrsize>* ehw);
 		void emitAssembly(std::ostream& out) {}
 		bool canSupport(libIRDB::Instruction_t* insn) const;
+		libIRDB::Relocation_t* GetPersonalityReloc() const { return personality_reloc;}
 
 		private:
 			// need nothing?
@@ -58,6 +61,8 @@ class EhWriterImpl_t : public EhWriter_t
 		friend EhWriterImpl_t<ptrsize>;	
 	};
 
+	static void print_pers(libIRDB::Instruction_t* insn, CIErepresentation_t *cie);
+
 
 	class FDErepresentation_t
 	{
diff --git a/src/ehwrite.cpp b/src/ehwrite.cpp
index 2dda8f1..8708b48 100644
--- a/src/ehwrite.cpp
+++ b/src/ehwrite.cpp
@@ -107,6 +107,46 @@ EhWriterImpl_t<ptrsize>::CIErepresentation_t::CIErepresentation_t(Instruction_t*
 }
 
 
+template <int ptrsize>
+void EhWriterImpl_t<ptrsize>::print_pers(Instruction_t* insn, EhWriterImpl_t<ptrsize>::CIErepresentation_t *cie)
+{
+	const auto pretty_print= [&](Relocation_t* pr)
+		{
+			if(pr==NULL)
+			{
+				cout<<"Found no personality reloc"<<endl;
+				return;
+			}
+			const auto personality_scoop=dynamic_cast<DataScoop_t*>(pr->GetWRT());
+			const auto personality_insn=dynamic_cast<Instruction_t*>(pr->GetWRT());
+
+			if(pr->GetWRT()==NULL)
+				cout<<"\tFound null personality"<<endl;
+			else if(personality_scoop)
+				cout<<"\tFound personlity scoop "<<personality_scoop->GetName()<<"+0x"<<hex<<pr->GetAddend()<<endl;
+			else if(personality_insn)
+				cout<<"\tFound personlity instruction "<<hex<<personality_insn->GetBaseID()<<dec<<":"<<hex<<personality_insn->getDisassembly()<<endl;
+			else
+				cout<<"\tFound reloc: unexpected type? "<<endl;
+		};
+
+	cout<<"  CIE-Personality addr= "<<hex<<cie->personality_reloc<<dec<<endl;
+	pretty_print(cie->GetPersonalityReloc());
+	const auto personality_it=find_if(
+		insn->GetEhProgram()->GetRelocations().begin(), 
+		insn->GetEhProgram()->GetRelocations().end(),
+		[](const Relocation_t* r) { return r->GetType()=="personality"; });
+
+	const auto pr = (personality_it==insn->GetEhProgram()->GetRelocations().end())
+		? (Relocation_t*)NULL
+		: *personality_it;
+	cout<<"  insn personality addr= "<<hex<<pr<<dec<<endl;
+	pretty_print(pr);
+
+
+};
+
+
 template <int ptrsize>
 EhWriterImpl_t<ptrsize>::FDErepresentation_t::FDErepresentation_t(Instruction_t* insn, EhWriterImpl_t<ptrsize>* ehw)
 	: 
@@ -114,17 +154,27 @@ EhWriterImpl_t<ptrsize>::FDErepresentation_t::FDErepresentation_t(Instruction_t*
 		cie(NULL)
 {
 	auto cie_it=find_if( ehw->all_cies.begin(), ehw->all_cies.end(), [&](const CIErepresentation_t* candidate)
-	{
-		return candidate->canSupport(insn);
-	});
+			{
+				return candidate->canSupport(insn);
+			});
 
 	if(cie_it==ehw->all_cies.end())
 	{
 		cie=new CIErepresentation_t(insn,ehw);
 		ehw->all_cies.push_back(cie);
+
+		if(getenv("EH_VERBOSE")!=NULL)
+			cout<<"Creating new CIE representation"<<endl;
 	}
 	else
+	{
 		cie=*cie_it;
+		if(getenv("EH_VERBOSE")!=NULL)
+		{
+			cout<<"Re-using CIE representation"<<endl;
+			print_pers(insn, cie);
+		}
+	}
 
 	start_addr=ehw->zipr_obj.GetLocationMap()->at(insn);
 	last_advance_addr=start_addr;
@@ -525,7 +575,11 @@ void EhWriterImpl_t<ptrsize>::BuildFDEs()
 		else
 		{
 			if(getenv("EH_VERBOSE")!=NULL)
+			{
 				cout<<"Extending new FDE for "<<hex<<this_insn->GetBaseID()<<":"<<this_insn->getDisassembly()<<" at " << this_addr <<endl;
+				print_pers(this_insn,current_fde->cie);
+
+			}
 			current_fde->extend(this_insn,this);
 		}
 	}
@@ -648,87 +702,6 @@ void EhWriterImpl_t<ptrsize>::GenerateEhOutput()
 			}
 		};
 
-#if 0
-		{
-			// determine if cleanup is requested.
-			const auto cleanup_it=find(action_reloc_set.begin(), action_reloc_set.end(), (Relocation_t*)NULL);
-			const auto has_cleanup=(cleanup_it!=action_reloc_set.end());
-
-			// determine if catch all is requested.
-			const auto catch_all_it=find_if(action_reloc_set.begin(), action_reloc_set.end(), 
-				[&](const Relocation_t* reloc) { return reloc && reloc->GetWRT()==NULL; });
-			const auto has_catch_all=(catch_all_it!=action_reloc_set.end());
-
-			// counter for the element in this action table entry.
-			auto act_entry_num=action_reloc_set.size()-1;
-
-			// sanity
-			if(has_cleanup && has_catch_all)
-				// what?
-				assert(0);
-
-			// emit any cleanup first.
-			if(has_cleanup)
-			{
-				// has to be first	
-				assert(act_entry_num==action_reloc_set.size()-1);
-
-				// output entry.
-				out<<"LSDA"<<dec<<lsda_num<<"_act"<<act_num<<"_start_entry"<<act_entry_num<<":"<<endl;
-				out<<"	.uleb128 0 #cleanup"<<endl;
-
-				out<<"	.uleb128 0 "<<endl; // always comes last in this action_table set
-				act_entry_num--;
-
-			}
-			if(has_catch_all)
-			{
-				const auto catch_all_reloc=*catch_all_it;
-				const auto tt_it=find_if(lsda->type_table.begin(), lsda->type_table.end(), 
-					[&](const Relocation_t* candidate) { return candidate && RelocsEqual(candidate, catch_all_reloc); });
-				assert(tt_it != lsda->type_table.end());
-				const auto tt_index=tt_it-lsda->type_table.begin();
-
-				out<<"LSDA"<<dec<<lsda_num<<"_act"<<act_num<<"_start_entry"<<act_entry_num<<":"<<endl;
-				out<<"	.uleb128 "<<dec<<1+tt_index<<endl;        
-			
-				if(act_entry_num==action_reloc_set.size()-1)
-					out<<"	.uleb128 0 "<<endl;
-				else
-					out<<"	.uleb128  LSDA"<<lsda_num<<"_act"<<act_num<<"_start_entry"<<act_entry_num+1<<" - . "<<endl;
-				act_entry_num--;
-			}
-
-			for(const auto& action_reloc : action_reloc_set)
-			{
-				// indicates has_cleanup -- taken care of specially above.
-				if(action_reloc==NULL)
-				{
-					continue; 
-				}
-
-				// which indicates has catch all -- taken care of specially above.
-				if(action_reloc->GetWRT()==NULL)
-				{
-					continue;
-				}
-
-				const auto tt_it=find_if(lsda->type_table.begin(), lsda->type_table.end(), 
-					[action_reloc](const Relocation_t* candidate) { return candidate!=NULL && RelocsEqual(action_reloc,candidate); } );
-				assert(tt_it != lsda->type_table.end());
-				const auto tt_index=tt_it-lsda->type_table.begin();
-				out<<"LSDA"<<dec<<lsda_num<<"_act"<<act_num<<"_start_entry"<<act_entry_num<<""<<":"<<endl;
-				out<<"	.uleb128 "<<dec<<1+tt_index<<endl;        
-				if(act_entry_num==action_reloc_set.size()-1)
-					out<<"	.uleb128 0 "<<endl;
-				else
-					out<<"	.uleb128  LSDA"<<lsda_num<<"_act"<<act_num<<"_start_entry"<<act_entry_num+1<<" - . "<<endl;
-				act_entry_num--;
-			}
-        		//out<<"	.equ  LSDA"<<lsda_num<<"_act"<<act_num<<"_start,  LSDA"<<lsda_num<<"_act"<<act_num<<"_start_entry0"<<endl;
-		};
-#endif
-
 
 		const auto output_callsite=[&](const typename FDErepresentation_t::LSDArepresentation_t::call_site_t &cs, const uint32_t cs_num) -> void
 		{
-- 
GitLab