From cf484f94f6b2dd85fa2c458a26ca19095698224c Mon Sep 17 00:00:00 2001
From: Anh <zenpoems@gmail.com>
Date: Wed, 3 Jul 2019 15:23:47 +0000
Subject: [PATCH] code cleanup and better debug output

---
 unpin_arm32.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/unpin_arm32.cpp b/unpin_arm32.cpp
index b4dcce2..6705e45 100644
--- a/unpin_arm32.cpp
+++ b/unpin_arm32.cpp
@@ -206,17 +206,19 @@ void UnpinArm32_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* relo
 		ms.plopBytes(L5,branch_bytes.c_str(),4);
 		zo->applyPatch(L5,FT);
 
-		// put the calculated pc-rel offset at L3
+		// put the calculated pc-rel offset at L6
 		const auto ldr_imm_field = int32_t(full_insn & mask8)*4;
 		const auto ldr_imm       = is_pos_imm ? ldr_imm_field : -ldr_imm_field;
 		const auto new_offset    = (bo_wrt == nullptr)   ?
 			int32_t(orig_insn_addr - L2 + addend) :
-			int32_t(orig_insn_addr - L2 + reloc_offset - (ldr_imm + 8));
+			int32_t(orig_insn_addr - (L2 + 8) + to_addr + addend - ldr_imm );
 		ms.plopBytes(L6,reinterpret_cast<const char*>(&new_offset),4);	// endianness of host must match target
 
 		// should be few enough of these to always print
 		cout<< "Had to trampoline " << disasm->getDisassembly() << " @"<<FA<<" to "
-		    << hex << L0 << "-" << L0+tramp_size-1 << " WRT=" << to_object_id << endl;
+		    << hex << L0 << "-" << L0+tramp_size-1 << " WRT=" << to_object_id 
+		    << " ldr_imm = " << dec << ldr_imm 
+		    << endl;
 
 	}
 	else if( is_ldr_type && !is_rd_pc && !I_bit_set)	/* ldr <not pc>, [pc, imm] */
@@ -282,7 +284,7 @@ void UnpinArm32_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* relo
 		// put the calculated pc-rel offset at L3
 		const auto ldr_imm_field = int32_t(full_insn & mask12);
 		const auto ldr_imm       = is_pos_imm ? ldr_imm_field : - ldr_imm_field;
-		const auto new_addend    =  bo_wrt == nullptr ?  8 + ldr_imm : reloc_offset;
+		const auto new_addend    = bo_wrt == nullptr ?  8 + ldr_imm : reloc_offset;
 		const auto new_offset    = int32_t(orig_insn_addr - L3 + new_addend);
 		ms.plopBytes(L3,reinterpret_cast<const char*>(&new_offset),4);	// endianness of host must match target
 
-- 
GitLab