From a4953080e84b4180301cf02d3648e168a6430c98 Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Tue, 31 May 2016 14:02:46 +0000
Subject: [PATCH] Added support for placing scoops in a zipr plugin.  fix-call
 comments

---
 include/plugin_man.h |  7 +++++++
 src/elfwrite.cpp     | 10 +++++++---
 src/plugin_man.cpp   | 22 ++++++++++++++++++++++
 src/zipr.cpp         | 10 +++++-----
 4 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/include/plugin_man.h b/include/plugin_man.h
index 86c404f7e..58f464c4b 100644
--- a/include/plugin_man.h
+++ b/include/plugin_man.h
@@ -40,12 +40,19 @@ class ZiprPluginManager_t : public ZiprPluginInterface_t
         	virtual void CallbackLinkingBegin();
         	virtual void CallbackLinkingEnd();
 
+		virtual RangeAddress_t PlaceScoopsBegin(const RangeAddress_t max_addr);
+		virtual RangeAddress_t PlaceScoopsEnd(const RangeAddress_t max_addr);
+
 		virtual bool DoesPluginPlop(libIRDB::Instruction_t*,DLFunctionHandle_t&);
 
 		virtual bool DoesPluginAddress(const Dollop_t *, const RangeAddress_t &, Range_t &, bool &coalesce, DLFunctionHandle_t &);
 		virtual bool DoesPluginRetargetCallback(const RangeAddress_t &, const DollopEntry_t *, RangeAddress_t &, DLFunctionHandle_t &) ;
 		virtual bool DoesPluginRetargetPin(const RangeAddress_t &, const Dollop_t *, RangeAddress_t &, DLFunctionHandle_t &) ;
 
+
+
+
+
 	private:
 
 		void open_plugins
diff --git a/src/elfwrite.cpp b/src/elfwrite.cpp
index 32c1c7078..006fa9ff5 100644
--- a/src/elfwrite.cpp
+++ b/src/elfwrite.cpp
@@ -453,7 +453,7 @@ void  ElfWriterImpl<T_Elf_Ehdr,T_Elf_Phdr,T_Elf_Addr>::update_phdr_for_scoop_sec
 			{PT_DYNAMIC, ".dynamic"},
 			{PT_NOTE, ".note.ABI-tag"},
 			{PT_GNU_EH_FRAME, ".eh_frame_hdr"},
-			{PT_GNU_RELRO, ".init_array"}
+//			{PT_GNU_RELRO, ".init_array"}
 		};
 
 		// check if a type of header listed above.
@@ -528,13 +528,17 @@ bool ElfWriterImpl<T_Elf_Ehdr,T_Elf_Phdr,T_Elf_Addr>::CreateNewPhdrs_internal(
 	// go through orig. phdrs any copy any that aren't of type pt_load or pt_hdr
 	for(unsigned int i=0;i<phdrs.size();i++)
 	{
-		// skip any load sections, the irdb tells us what to load.
+		// skip any load headers, the irdb tells us what to load.
 		if(phdrs[i].p_type == PT_LOAD)
 			continue;
 
-		// skip phdr section.
+		// skip phdr header.
 		if(phdrs[i].p_type == PT_PHDR)
 			continue;
+
+		// skip RELRO header, we're relocating stuff and wil have to create 1 or more.
+		if(phdrs[i].p_type == PT_GNU_RELRO)
+			continue;
 		
 		T_Elf_Phdr newphdr=phdrs[i];
 
diff --git a/src/plugin_man.cpp b/src/plugin_man.cpp
index ee5ed1b37..8f928bae0 100644
--- a/src/plugin_man.cpp
+++ b/src/plugin_man.cpp
@@ -52,6 +52,13 @@ for(DLFunctionHandleSet_t::iterator it=m_handleList.begin(); it!=m_handleList.en
 	ZiprPluginInterface_t* zpi=(ZiprPluginInterface_t*)*it; \
 	zpi->func(); \
 } 
+
+#define dispatch_to_with_var(func, var) \
+for(DLFunctionHandleSet_t::iterator it=m_handleList.begin(); it!=m_handleList.end();++it) \
+{ \
+	ZiprPluginInterface_t* zpi=(ZiprPluginInterface_t*)*it; \
+	var=zpi->func(var); \
+} 
  
 void ZiprPluginManager_t::PinningBegin()
 {
@@ -81,6 +88,21 @@ void ZiprPluginManager_t::CallbackLinkingEnd()
 	dispatch_to(CallbackLinkingEnd);
 }
 
+RangeAddress_t ZiprPluginManager_t::PlaceScoopsBegin(const RangeAddress_t max_addr)
+{
+	RangeAddress_t ret=max_addr;
+	dispatch_to_with_var(PlaceScoopsBegin,ret);
+	return ret;
+}
+
+RangeAddress_t ZiprPluginManager_t::PlaceScoopsEnd(const RangeAddress_t max_addr)
+{
+	RangeAddress_t ret=max_addr;
+	dispatch_to_with_var(PlaceScoopsEnd,ret);
+	return ret;
+}
+
+
 bool ZiprPluginManager_t::DoesPluginAddress(const Dollop_t *dollop, const RangeAddress_t &source, Range_t &place, bool &coalesce, DLFunctionHandle_t &placer)
 {
 	DLFunctionHandleSet_t::iterator it=m_handleList.begin();
diff --git a/src/zipr.cpp b/src/zipr.cpp
index b465283ac..0b16c33e4 100644
--- a/src/zipr.cpp
+++ b/src/zipr.cpp
@@ -483,7 +483,7 @@ void ZiprImpl_t::CreateExecutableScoops(const std::map<RangeAddress_t, int> &ord
 		// zero init is OK, after zipring we'll update with the right bytes.
 		string text_contents;
 		text_contents.resize(text_end->GetVirtualOffset() - text_start->GetVirtualOffset());
-		DataScoop_t* text_scoop=new DataScoop_t(BaseObj_t::NOT_IN_DATABASE, string(".zipr_text_")+to_string(count++), text_start, text_end, NULL, 5, text_contents);
+		DataScoop_t* text_scoop=new DataScoop_t(BaseObj_t::NOT_IN_DATABASE, string(".zipr_text_")+to_string(count++), text_start, text_end, NULL, 5, false, text_contents);
 		m_firp->GetDataScoops().insert(text_scoop);
 	
 		cout<<"Adding scoop "<<text_scoop->GetName()<<hex<<" at "<<hex<<text_start->GetVirtualOffset()<<" - "<<text_end->GetVirtualOffset()<<endl;
@@ -494,6 +494,8 @@ void ZiprImpl_t::CreateExecutableScoops(const std::map<RangeAddress_t, int> &ord
 
 RangeAddress_t ZiprImpl_t::PlaceUnplacedScoops(RangeAddress_t max_addr)
 {
+	max_addr=plugman.PlaceScoopsBegin(max_addr);
+
 	map<int,DataScoopSet_t> scoops_by_perms;
 
 	for(
@@ -508,9 +510,6 @@ RangeAddress_t ZiprImpl_t::PlaceUnplacedScoops(RangeAddress_t max_addr)
 		if(scoop->GetStart()->GetVirtualOffset()==0)
 			scoops_by_perms[scoop->getRawPerms()].insert(scoop);
 	}
-
-
-
 	
 	for(map<int,DataScoopSet_t>::iterator pit=scoops_by_perms.begin(); pit!=scoops_by_perms.end(); ++pit)
 	{
@@ -533,6 +532,7 @@ RangeAddress_t ZiprImpl_t::PlaceUnplacedScoops(RangeAddress_t max_addr)
 	}
 	
 	
+	max_addr=plugman.PlaceScoopsEnd(max_addr);
 
 	return max_addr;
 }
@@ -2837,7 +2837,7 @@ void ZiprImpl_t::OutputBinaryFile(const string &name)
 	m_firp->GetAddresses().insert(textra_end);
 	string textra_contents;
 	textra_contents.resize(end_of_new_space-start_of_new_space);
-	DataScoop_t* textra_scoop=new DataScoop_t(BaseObj_t::NOT_IN_DATABASE, ".textra", textra_start, textra_end, NULL, 5, textra_contents);
+	DataScoop_t* textra_scoop=new DataScoop_t(BaseObj_t::NOT_IN_DATABASE, ".textra", textra_start, textra_end, NULL, 5, false, textra_contents);
 	m_firp->GetDataScoops().insert(textra_scoop);
 
 
-- 
GitLab