From 3da58cb1334869a37258f5b055d9b069372e5fb6 Mon Sep 17 00:00:00 2001 From: Jason Hiser <jdhiser@gmail.com> Date: Fri, 8 Feb 2019 15:28:08 -0500 Subject: [PATCH] sdk factorying --- unpin.cpp | 26 +++++++++--------- unpin.h | 16 +++++------ unpin_aarch64.cpp | 68 +++++++++++++++++++++++------------------------ unpin_x86.cpp | 9 +++---- 4 files changed, 57 insertions(+), 62 deletions(-) diff --git a/unpin.cpp b/unpin.cpp index 2413e72..c3d73da 100644 --- a/unpin.cpp +++ b/unpin.cpp @@ -29,7 +29,6 @@ **************************************************************************/ -#include <zipr_sdk.h> #include <string> #include <algorithm> #include "unpin.h" @@ -40,7 +39,6 @@ using namespace IRDB_SDK; using namespace std; using namespace Zipr_SDK; -using namespace ELFIO; #define ALLOF(a) begin(a),end(a) @@ -78,19 +76,19 @@ bool Unpin_t::should_cfi_pin(Instruction_t* insn) return m_should_cfi_pin; } -ZiprOptionsNamespace_t *Unpin_t::RegisterOptions(ZiprOptionsNamespace_t *global) +ZiprOptionsNamespace_t *Unpin_t::registerOptions(ZiprOptionsNamespace_t *global) { auto unpin_ns = new ZiprOptionsNamespace_t("unpin"); - global->AddOption(&m_verbose); + global->addOption(&m_verbose); m_should_cfi_pin.setDescription("Pin CFI instructions."); - unpin_ns->AddOption(&m_should_cfi_pin); + unpin_ns->addOption(&m_should_cfi_pin); m_on.setDescription("Turn unpin plugin on/off."); - unpin_ns->AddOption(&m_on); + unpin_ns->addOption(&m_on); m_max_unpins.setDescription("Set how many unpins are allowed, useful for debugging."); - unpin_ns->AddOption(&m_max_unpins); + unpin_ns->addOption(&m_max_unpins); return unpin_ns; } @@ -171,21 +169,21 @@ void Unpin_t::DoUnpinForScoops() cout<<"# ATTRIBUTE Zipr_Unpinning::scoop_unpin_missed_unpins="<<dec<<missed_unpins<<endl; } -Zipr_SDK::ZiprPreference Unpin_t::RetargetCallback( +Zipr_SDK::ZiprPreference Unpin_t::retargetCallback( const RangeAddress_t &callback_address, const DollopEntry_t *callback_entry, RangeAddress_t &target_address) { - if(!m_on) return Zipr_SDK::ZiprPluginInterface_t::RetargetCallback(callback_address, callback_entry, target_address); + if(!m_on) return Zipr_SDK::ZiprPluginInterface_t::retargetCallback(callback_address, callback_entry, target_address); unpins++;// unpinning a call to a scoop. if(m_max_unpins != -1 && unpins>=m_max_unpins) - return Zipr_SDK::ZiprPluginInterface_t::RetargetCallback(callback_address, callback_entry, target_address); + return Zipr_SDK::ZiprPluginInterface_t::retargetCallback(callback_address, callback_entry, target_address); - auto& ms=*zo->GetMemorySpace(); - auto insn = callback_entry->Instruction(); - auto& locMap=*(zo->GetLocationMap()); + auto& ms=*zo->getMemorySpace(); + auto insn = callback_entry->getInstruction(); + auto& locMap=*(zo->getLocationMap()); for(auto reloc : insn->getRelocations()) { if (reloc->getType()==string("callback_to_scoop")) @@ -261,7 +259,7 @@ void Unpin_t::DoUpdateForScoops() // getWRT returns an BaseObj, but this reloc type expects an instruction // safe cast and check. assert(insn); - Zipr_SDK::InstructionLocationMap_t &locMap=*(zo->GetLocationMap()); + Zipr_SDK::InstructionLocationMap_t &locMap=*(zo->getLocationMap()); IRDB_SDK::VirtualOffset_t newLoc=locMap[insn]; cout<<"Unpin::Unpinned data_to_insn_ptr insn ("<<hex<<insn->getBaseID()<<":" diff --git a/unpin.h b/unpin.h index 367ad7a..53a6bdf 100644 --- a/unpin.h +++ b/unpin.h @@ -32,7 +32,7 @@ #define unpin_h #include <irdb-core> -#include <zipr_sdk.h> +#include <zipr-sdk> class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t { @@ -46,8 +46,8 @@ class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t m_max_unpins("max-unpins",-1), unpins(0), missed_unpins(0), - ms(*zo->GetMemorySpace()), - locMap(*(zo->GetLocationMap())), + ms(*zo->getMemorySpace()), + locMap(*(zo->getLocationMap())), firp(*(zo->getFileIR())) { } @@ -55,23 +55,23 @@ class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t virtual ~Unpin_t() { } - virtual void PinningBegin() + virtual void doPinningBegin() override { if(!m_on) return; DoUnpin(); } - virtual void CallbackLinkingEnd() + virtual void doCallbackLinkingEnd() override { if(!m_on) return; DoUpdate(); } - virtual Zipr_SDK::ZiprOptionsNamespace_t *RegisterOptions(Zipr_SDK::ZiprOptionsNamespace_t *); + virtual Zipr_SDK::ZiprOptionsNamespace_t *registerOptions(Zipr_SDK::ZiprOptionsNamespace_t *) override; - Zipr_SDK::ZiprPreference RetargetCallback( + Zipr_SDK::ZiprPreference retargetCallback( const Zipr_SDK::RangeAddress_t &callback_address, const Zipr_SDK::DollopEntry_t *callback_entry, - Zipr_SDK::RangeAddress_t &target_address); + Zipr_SDK::RangeAddress_t &target_address) override; protected: // designed for arch-specific override. virtual void HandleRetAddrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc)=0; diff --git a/unpin_aarch64.cpp b/unpin_aarch64.cpp index 6b1e332..86a985e 100644 --- a/unpin_aarch64.cpp +++ b/unpin_aarch64.cpp @@ -29,7 +29,6 @@ **************************************************************************/ -#include <zipr_sdk.h> #include <string> #include <algorithm> #include "unpin.h" @@ -40,7 +39,6 @@ using namespace IRDB_SDK; using namespace std; using namespace Zipr_SDK; -using namespace ELFIO; #define ALLOF(a) begin(a),end(a) @@ -139,7 +137,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto clean_new_insn= full_insn & ~(mask2<<29) & ~ (mask19 << 5); const auto new_insn = clean_new_insn | ((new_immlo2&mask2) << 29) | ((new_immhi19&mask19)<<5); // put the new instruction in the output - ms.PlopBytes(from_insn_location, (const char*)&new_insn, insn_bytes_len); + ms.plopBytes(from_insn_location, (const char*)&new_insn, insn_bytes_len); if (m_verbose) { cout << "Relocating a adr(p) pcrel relocation with orig_pageno=" << hex @@ -168,7 +166,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto tramp_range=ms.getFreeRange(tramp_size); const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. - ms.SplitFreeRange({tramp_start,tramp_start+12}); + ms.splitFreeRange({tramp_start,tramp_start+12}); const auto FA=from_insn_location; @@ -190,8 +188,8 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // put an uncond branch at where the adr was. // and make it point at L0 - ms.PlopBytes(FA,branch_bytes.c_str(),4); - zo->ApplyPatch(FA,L0); + ms.plopBytes(FA,branch_bytes.c_str(),4); + zo->applyPatch(FA,L0); // adrp: 1 imm2lo 1 0000 immhi19 Rd auto adrp_bytes=string("\x00\x00\x00\x90",4); @@ -200,7 +198,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re adrp_word |= ((relocd_immlo2&mask2) << 29) | ((relocd_immhi19&mask19)<<5); cout << "Tramp for "<<L0<<", relocd_immlo2=" << relocd_immlo2 << ", relocd_immhi19=" << relocd_immhi19 << endl; - ms.PlopBytes(L0,(char*)&adrp_word,4); + ms.plopBytes(L0,(char*)&adrp_word,4); // add64 imm12 = 1001 0001 00 imm12 Rn Rd auto add_bytes=string("\x00\x00\x00\x91",4); @@ -208,12 +206,12 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re add_word|=destreg<<0; add_word|=destreg<<5; add_word|=address_to_generate_page_offset << 10 ; - ms.PlopBytes(L1,(char*)&add_word,4); + ms.plopBytes(L1,(char*)&add_word,4); // put an uncond branch the end of the trampoline // and make it jump at FT - ms.PlopBytes(L2,branch_bytes.c_str(),4); - zo->ApplyPatch(L2,FT); + ms.plopBytes(L2,branch_bytes.c_str(),4); + zo->applyPatch(L2,FT); // should be few enough of these to always print cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " @@ -233,7 +231,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto clean_new_insn = full_insn & ~(mask19 << 5); const auto new_insn = clean_new_insn | ((new_imm19_ext & mask19)<<5); // put the new instruction in the output - ms.PlopBytes(from_insn_location, (const char*)&new_insn, insn_bytes_len); + ms.plopBytes(from_insn_location, (const char*)&new_insn, insn_bytes_len); if (m_verbose) { cout << "Relocating a ldr pcrel relocation with orig_addr=" << hex @@ -269,7 +267,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto tramp_range=ms.getFreeRange(tramp_size); const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. - ms.SplitFreeRange({tramp_start,tramp_start+12}); + ms.splitFreeRange({tramp_start,tramp_start+12}); // and give the bytes some names @@ -288,15 +286,15 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // put an uncond branch at where the adr was. // and make it point at L0 - ms.PlopBytes(FA,branch_bytes.c_str(),4); - zo->ApplyPatch(FA,L0); + ms.plopBytes(FA,branch_bytes.c_str(),4); + zo->applyPatch(FA,L0); // adrp: 1 imm2lo 1 0000 immhi19 Rd auto adrp_bytes=string("\x00\x00\x00\x90",4); auto adrp_word =*(int*)adrp_bytes.c_str(); adrp_word|=destreg<<0; adrp_word |= ((relocd_immlo2&mask2) << 29) | ((relocd_immhi19&mask19)<<5); - ms.PlopBytes(L0,(char*)&adrp_word,4); + ms.plopBytes(L0,(char*)&adrp_word,4); // convert: ldr w/x reg : 0 x1 011 0 00 ---imm19---- Rt5 x1 indicates size (0,1 -> w/x) // to : ldr x/w reg : 1 x1 111 0 01 01 imm12 Rn5 Rt5 x1 indciates size (0,1 -> w/x) @@ -309,12 +307,12 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re new_ldr_word|=destreg<<5; // Rn new_ldr_word|=scaled_page_offset << 10 ; // imm12 new_ldr_word|=orig_ldr_size_bit << 30; // x1 - ms.PlopBytes(L1,(char*)&new_ldr_word,4); + ms.plopBytes(L1,(char*)&new_ldr_word,4); // put an uncond branch the end of the trampoline // and make it jump at FT - ms.PlopBytes(L2,branch_bytes.c_str(),4); - zo->ApplyPatch(L2,FT); + ms.plopBytes(L2,branch_bytes.c_str(),4); + zo->applyPatch(L2,FT); // should be few enough of these to always print cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " @@ -343,7 +341,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto tramp_range=ms.getFreeRange(tramp_size); const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. - ms.SplitFreeRange({tramp_start,tramp_start+12}); + ms.splitFreeRange({tramp_start,tramp_start+12}); // give the bytes some names @@ -363,20 +361,20 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // put an uncond branch at where the adr was. // and make it point at L0 - ms.PlopBytes(FA,branch_bytes.c_str(),4); - zo->ApplyPatch(FA,L0); + ms.plopBytes(FA,branch_bytes.c_str(),4); + zo->applyPatch(FA,L0); // put save of x0 in place. // diassembly: f81803e0 stur x0, [sp, #-128] const auto strx0_bytes=string("\xe0\x03\x18\xf8",4); - ms.PlopBytes(L0,strx0_bytes.c_str(),4); + ms.plopBytes(L0,strx0_bytes.c_str(),4); // adrp: 1 imm2lo 1 0000 immhi19 Rd auto adrp_bytes=string("\x00\x00\x00\x90",4); auto adrp_word =*(int*)adrp_bytes.c_str(); // adrp_word|=destreg<<0; ; destreg for this insn is x0. adrp_word |= ((relocd_immlo2&mask2) << 29) | ((relocd_immhi19&mask19)<<5); - ms.PlopBytes(L1,(char*)&adrp_word,4); + ms.plopBytes(L1,(char*)&adrp_word,4); // convert: ldr s/d/q reg: opc2 01 11 00 imm19 Rt5, opc2 indicate size (00,01,10 -> s/d/q) @@ -412,18 +410,18 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re new_ldr_word|=((address_to_generate_page_offset/ldr_size) << 10); // imm12 new_ldr_word|=(new_ldr_size_bits<<30); // size2 new_ldr_word|=(new_ldr_opc2_bits<<22); // opc2 - ms.PlopBytes(L2,(char*)&new_ldr_word,4); + ms.plopBytes(L2,(char*)&new_ldr_word,4); // drop in the restore of x0 // disassembly: f85803e0 ldur x0, [sp, #-128] const auto ldrx0_bytes=string("\xe0\x03\x58\xf8",4); - ms.PlopBytes(L3,ldrx0_bytes.c_str(),4); + ms.plopBytes(L3,ldrx0_bytes.c_str(),4); // put an uncond branch the end of the trampoline // and make it jump at FT - ms.PlopBytes(L4,branch_bytes.c_str(),4); - zo->ApplyPatch(L4,FT); + ms.plopBytes(L4,branch_bytes.c_str(),4); + zo->applyPatch(L4,FT); // should be few enough of these to always print cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " @@ -447,7 +445,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto clean_new_insn = full_insn & ~(mask19 << 5); const auto new_insn = clean_new_insn | ((new_imm19_ext & mask19)<<5); // put the new instruction in the output - ms.PlopBytes(from_insn_location, (const char*)&new_insn, insn_bytes_len); + ms.plopBytes(from_insn_location, (const char*)&new_insn, insn_bytes_len); if (m_verbose) { cout << "Relocating a ldrsw pcrel relocation with orig_addr=" << hex @@ -475,7 +473,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto tramp_range=ms.getFreeRange(tramp_size); const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. - ms.SplitFreeRange({tramp_start,tramp_start+12}); + ms.splitFreeRange({tramp_start,tramp_start+12}); const auto FA=from_insn_location; @@ -496,15 +494,15 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // put an uncond branch at where the adr was. // and make it point at L0 - ms.PlopBytes(FA,branch_bytes.c_str(),4); - zo->ApplyPatch(FA,L0); + ms.plopBytes(FA,branch_bytes.c_str(),4); + zo->applyPatch(FA,L0); // adrp: 1 imm2lo 1 0000 immhi19 Rd auto adrp_bytes=string("\x00\x00\x00\x90",4); auto adrp_word =*(int*)adrp_bytes.c_str(); adrp_word|=destreg<<0; adrp_word |= ((relocd_immlo2&mask2) << 29) | ((relocd_immhi19&mask19)<<5); - ms.PlopBytes(L0,(char*)&adrp_word,4); + ms.plopBytes(L0,(char*)&adrp_word,4); // convert: ldrsw x reg : 1001 1000 ---imm19--- Rt // to : ldrsw x reg : 1011 1001 10 imm12 Rn Rt @@ -515,12 +513,12 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re new_ldr_word|=destreg<<0; // Rt new_ldr_word|=destreg<<5; // Rn new_ldr_word|=scaled_page_offset << 10 ; // imm12 - ms.PlopBytes(L1,(char*)&new_ldr_word,4); + ms.plopBytes(L1,(char*)&new_ldr_word,4); // put an uncond branch the end of the trampoline // and make it jump at FT - ms.PlopBytes(L2,branch_bytes.c_str(),4); - zo->ApplyPatch(L2,FT); + ms.plopBytes(L2,branch_bytes.c_str(),4); + zo->applyPatch(L2,FT); // should be few enough of these to always print cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " diff --git a/unpin_x86.cpp b/unpin_x86.cpp index b3c3426..c5c694a 100644 --- a/unpin_x86.cpp +++ b/unpin_x86.cpp @@ -29,7 +29,6 @@ **************************************************************************/ -#include <zipr_sdk.h> #include <string> #include <algorithm> #include "unpin.h" @@ -40,7 +39,6 @@ using namespace IRDB_SDK; using namespace std; using namespace Zipr_SDK; -using namespace ELFIO; #define ALLOF(a) begin(a),end(a) @@ -61,7 +59,8 @@ void UnpinX86_t::HandleRetAddrReloc(Instruction_t* from_insn, Relocation_t* relo auto from_insn_location=locMap[from_insn]; // 32-bit code and main executables just push a full 32-bit addr. - if(zo->getELFIO()->get_type()==ET_EXEC) + // if(zo->getELFIO()->get_type()==ET_EXEC) + if(zo->getFileIR()->getArchitecture()->getFileType()==adftELFEXE) { // not handled in push64_relocs which is disabled for shared objects. // expecting a 32-bit push, length=5 @@ -139,7 +138,7 @@ void UnpinX86_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* reloc) const auto new_disp=(int)(rel_addr1 + to_addr - from_insn->getDataBits().size()-from_insn_location); const auto newbits=from_insn->getDataBits().replace(disp_offset, disp_size, (char*)&new_disp, disp_size); from_insn->setDataBits(newbits); - ms.PlopBytes(from_insn_location, newbits.c_str(), newbits.size()); + ms.plopBytes(from_insn_location, newbits.c_str(), newbits.size()); const auto disasm2=DecodedInstruction_t::factory(from_insn); cout<<"unpin:pcrel:new_disp="<<hex<<new_disp<<endl; cout<<"unpin:pcrel:new_insn_addr="<<hex<<from_insn_location<<endl; @@ -237,7 +236,7 @@ void UnpinX86_t::HandleCallbackReloc(Instruction_t* from_insn, Relocation_t* rel */ at = call_addr + 1; at = call_addr + from_insn->getDataBits().length(); - ms.PlopBytes(at, bytes, sizeof(bytes)); + ms.plopBytes(at, bytes, sizeof(bytes)); /* * Turn off the following flags so that this -- GitLab