From d952f56fdff835b1806b4b85b8b7a860dca53ccf Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Fri, 31 Mar 2017 16:50:58 +0000
Subject: [PATCH] gah, forgot to commit a few files.

Former-commit-id: 09bd427e933a6f89ca6df4257f682b847b8850ff
---
 .gitattributes                       |   2 +
 libIRDB/include/core/instruction.hpp |   2 +-
 libIRDB/src/core/eh.cpp              | 125 +++++++++++++++++++++++++++
 libIRDB/src/core/reloc.cpp           |  42 +++++++++
 4 files changed, 170 insertions(+), 1 deletion(-)
 create mode 100644 libIRDB/src/core/eh.cpp
 create mode 100644 libIRDB/src/core/reloc.cpp

diff --git a/.gitattributes b/.gitattributes
index 7f03d823d..aa5b9ebac 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -247,6 +247,7 @@ libIRDB/src/core/address.cpp -text
 libIRDB/src/core/all.hpp -text
 libIRDB/src/core/baseobj.cpp -text
 libIRDB/src/core/dbinterface.cpp -text
+libIRDB/src/core/eh.cpp -text
 libIRDB/src/core/file.cpp -text
 libIRDB/src/core/fileir.cpp -text
 libIRDB/src/core/function.cpp -text
@@ -254,6 +255,7 @@ libIRDB/src/core/generate_spri.cpp -text
 libIRDB/src/core/icfs.cpp -text
 libIRDB/src/core/instruction.cpp -text
 libIRDB/src/core/pqxxdb.cpp -text
+libIRDB/src/core/reloc.cpp -text
 libIRDB/src/core/scoop.cpp -text
 libIRDB/src/core/type.cpp -text
 libIRDB/src/core/variantid.cpp -text
diff --git a/libIRDB/include/core/instruction.hpp b/libIRDB/include/core/instruction.hpp
index ba1364041..c52db17af 100644
--- a/libIRDB/include/core/instruction.hpp
+++ b/libIRDB/include/core/instruction.hpp
@@ -45,7 +45,7 @@ class Instruction_t : public BaseObj_t
 		std::string GetCallback()  const { return callback; } 
 		std::string GetComment()   const { return comment; } 
 		EhProgram_t* GetEhProgram() const { return eh_pgm; }
-		EhCallSite_t* GetEhCallSite_t() const { return eh_cs; }
+		EhCallSite_t* GetEhCallSite() const { return eh_cs; }
 
   
 		void SetAddress(AddressID_t* newaddr)  { my_address=newaddr; }
diff --git a/libIRDB/src/core/eh.cpp b/libIRDB/src/core/eh.cpp
new file mode 100644
index 000000000..6b2ca98d7
--- /dev/null
+++ b/libIRDB/src/core/eh.cpp
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#include <all.hpp>
+#include <utils.hpp>
+#include <tuple>
+#include <functional>
+#include <assert.h>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+
+using namespace libIRDB;
+using namespace std;
+
+bool libIRDB::operator<(const EhProgram_t&a, const EhProgram_t&b)
+{
+        return  tie(a.cie_program,a.fde_program,a.code_alignment_factor,a.data_alignment_factor,a.ptrsize)
+                <
+                tie(b.cie_program,b.fde_program,b.code_alignment_factor,b.data_alignment_factor,b.ptrsize);
+}
+
+
+namespace std
+{
+template<>
+class hash<EhProgramListing_t> 
+{
+	public:
+		std::size_t operator()(const EhProgramListing_t& s) const
+		{
+			auto sub_hasher=std::hash<string>();
+			auto res=std::size_t(0);
+			for(const auto& i: s)
+			{
+				res ^= sub_hasher(i);
+			}
+			return res;
+		}
+	
+};
+}
+
+
+void libIRDB::EhProgram_t::print() const
+{
+	std::hash<EhProgramListing_t> ehpgmlist_hash;
+	auto cie_hash=ehpgmlist_hash(cie_program);
+	auto fde_hash=ehpgmlist_hash(fde_program);
+	cout<<dec<<"CAF: "<<code_alignment_factor<<" DAF: "<<data_alignment_factor<<" ptrsize="<< +ptrsize<<" cie_hash: "<<hex<<cie_hash<<" fde_hash: "<<fde_hash<<endl;
+}
+
+std::string EhProgram_t::WriteToDB(File_t* fid)    // writes to DB, ID is not -1.
+{
+	auto str_to_encoded_string=[](const string& data) -> string
+	{
+		ostringstream hex_data;
+		hex_data << setfill('0') << hex;
+		for (size_t i = 0; i < data.length(); ++i)
+			hex_data << setw(2) << (int)(data[i]&0xff);
+		return hex_data.str();
+	};
+
+	auto vec_to_encoded_string=[&](const vector<string>& in) -> string
+	{
+		string q="";
+		for(const auto& i : in)
+		{
+			if(q!="") 	
+				q+=",";
+			q+=str_to_encoded_string(i);	
+		}
+		return q;
+	};
+
+	string encoded_cie_program=vec_to_encoded_string(cie_program);
+	string encoded_fde_program=vec_to_encoded_string(fde_program);
+
+	string q;
+	q ="insert into " + fid->GetEhProgramTableName();
+	q+="(eh_pgm_id,caf,daf,ptrsize,cie_program,fde_program) "+
+		string(" VALUES (") +
+		string("'") + to_string(GetBaseID())          + string("', ") +
+		string("'") + to_string(code_alignment_factor)               + string("', ") +
+		string("'") + to_string(data_alignment_factor)               + string("', ") +
+		string("'") + to_string(+ptrsize)               + string("', ") +
+		string("'") + encoded_cie_program               + string("', ") +
+		string("'") + encoded_fde_program               + string("') ; ");
+	return q;
+
+}
+
+std::string EhCallSite_t::WriteToDB(File_t* fid)    // writes to DB, ID is not -1.
+{
+	string q;
+
+	auto landing_pad_id=BaseObj_t::NOT_IN_DATABASE;
+	if(landing_pad != NULL)
+		landing_pad_id=landing_pad->GetBaseID();
+
+	q ="insert into " + fid->GetEhCallSiteTableName();
+	q+="(ehcs_id,tt_encoding,lp_insn_id) "+
+		string(" VALUES (") +
+		string("'") + to_string(GetBaseID())         + string("', ") +
+		string("'") + to_string(+tt_encoding)        + string("', ") +
+		string("'") + to_string(landing_pad_id)      + string("') ;");
+	return q;
+}
diff --git a/libIRDB/src/core/reloc.cpp b/libIRDB/src/core/reloc.cpp
new file mode 100644
index 000000000..bc5587fc4
--- /dev/null
+++ b/libIRDB/src/core/reloc.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#include <all.hpp>
+#include <utils.hpp>
+
+using namespace std;
+
+std::string Relocation_t::WriteToDB(File_t* fid, BaseObj_t* myinsn)
+{
+        string q;
+        db_id_t wrt_id=wrt_obj ? wrt_obj->GetBaseID() : BaseObj_t::NOT_IN_DATABASE;
+        q ="insert into " + fid->relocs_table_name;
+        q+="(reloc_id,reloc_offset,reloc_type,instruction_id,wrt_id,addend,doip_id) "+
+                string(" VALUES (") +
+                string("'") + to_string(GetBaseID())          + string("', ") +
+                string("'") + to_string(offset)               + string("', ") +
+                string("'") + (type)                          + string("', ") +
+                string("'") + to_string(myinsn->GetBaseID())  + string("', ") +
+                string("'") + to_string(wrt_id)  + string("', ") +
+                string("'") + to_string(addend)  + string("', ") +
+                string("'") + to_string(GetDoipID())          + string("') ; ") ;
+        return q;
+}
+
-- 
GitLab