📄 vex_main.c
字号:
/*---------------------------------------------------------------*//*--- ---*//*--- This file (main/vex_main.c) is ---*//*--- Copyright (C) OpenWorks LLP. All rights reserved. ---*//*--- ---*//*---------------------------------------------------------------*//* This file is part of LibVEX, a library for dynamic binary instrumentation and translation. Copyright (C) 2004-2006 OpenWorks LLP. All rights reserved. This library is made available under a dual licensing scheme. If you link LibVEX against other code all of which is itself licensed under the GNU General Public License, version 2 dated June 1991 ("GPL v2"), then you may use LibVEX under the terms of the GPL v2, as appearing in the file LICENSE.GPL. If the file LICENSE.GPL is missing, you can obtain a copy of the GPL v2 from the Free Software Foundation Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. For any other uses of LibVEX, you must first obtain a commercial license from OpenWorks LLP. Please contact info@open-works.co.uk for information about commercial licensing. This software is provided by OpenWorks LLP "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall OpenWorks LLP be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. Neither the names of the U.S. Department of Energy nor the University of California nor the names of its contributors may be used to endorse or promote products derived from this software without prior written permission.*/#include "libvex.h"#include "libvex_emwarn.h"#include "libvex_guest_x86.h"#include "libvex_guest_amd64.h"#include "libvex_guest_arm.h"#include "libvex_guest_ppc32.h"#include "libvex_guest_ppc64.h"#include "main/vex_globals.h"#include "main/vex_util.h"#include "host-generic/h_generic_regs.h"#include "ir/iropt.h"#include "host-x86/hdefs.h"#include "host-amd64/hdefs.h"#include "host-ppc/hdefs.h"#include "guest-generic/bb_to_IR.h"#include "guest-x86/gdefs.h"#include "guest-amd64/gdefs.h"#include "guest-arm/gdefs.h"#include "guest-ppc/gdefs.h"/* This file contains the top level interface to the library. *//* --------- fwds ... --------- */static Bool are_valid_hwcaps ( VexArch arch, UInt hwcaps );static HChar* show_hwcaps ( VexArch arch, UInt hwcaps );/* --------- Initialise the library. --------- *//* Exported to library client. */const HChar* LibVEX_Version ( void ){return#include "main/vex_svnversion.h" ;}/* Exported to library client. */void LibVEX_default_VexControl ( /*OUT*/ VexControl* vcon ){ vcon->iropt_verbosity = 0; vcon->iropt_level = 2; vcon->iropt_precise_memory_exns = False; vcon->iropt_unroll_thresh = 120; vcon->guest_max_insns = 60; vcon->guest_chase_thresh = 10;}/* Exported to library client. */void LibVEX_Init ( /* failure exit function */ __attribute__ ((noreturn)) void (*failure_exit) ( void ), /* logging output function */ void (*log_bytes) ( HChar*, Int nbytes ), /* debug paranoia level */ Int debuglevel, /* Are we supporting valgrind checking? */ Bool valgrind_support, /* Control ... */ /*READONLY*/VexControl* vcon){ /* First off, do enough minimal setup so that the following assertions can fail in a sane fashion, if need be. */ vex_failure_exit = failure_exit; vex_log_bytes = log_bytes; /* Now it's safe to check parameters for sanity. */ vassert(!vex_initdone); vassert(failure_exit); vassert(log_bytes); vassert(debuglevel >= 0); vassert(vcon->iropt_verbosity >= 0); vassert(vcon->iropt_level >= 0); vassert(vcon->iropt_level <= 2); vassert(vcon->iropt_unroll_thresh >= 0); vassert(vcon->iropt_unroll_thresh <= 400); vassert(vcon->guest_max_insns >= 1); vassert(vcon->guest_max_insns <= 100); vassert(vcon->guest_chase_thresh >= 0); vassert(vcon->guest_chase_thresh < vcon->guest_max_insns); /* Check that Vex has been built with sizes of basic types as stated in priv/libvex_basictypes.h. Failure of any of these is a serious configuration error and should be corrected immediately. If any of these assertions fail you can fully expect Vex not to work properly, if at all. */ vassert(1 == sizeof(UChar)); vassert(1 == sizeof(Char)); vassert(2 == sizeof(UShort)); vassert(2 == sizeof(Short)); vassert(4 == sizeof(UInt)); vassert(4 == sizeof(Int)); vassert(8 == sizeof(ULong)); vassert(8 == sizeof(Long)); vassert(4 == sizeof(Float)); vassert(8 == sizeof(Double)); vassert(1 == sizeof(Bool)); vassert(4 == sizeof(Addr32)); vassert(8 == sizeof(Addr64)); vassert(16 == sizeof(U128)); vassert(sizeof(void*) == 4 || sizeof(void*) == 8); vassert(sizeof(void*) == sizeof(int*)); vassert(sizeof(void*) == sizeof(HWord)); vassert(VEX_HOST_WORDSIZE == sizeof(void*)); vassert(VEX_HOST_WORDSIZE == sizeof(HWord)); /* Really start up .. */ vex_debuglevel = debuglevel; vex_valgrind_support = valgrind_support; vex_control = *vcon; vex_initdone = True; vexSetAllocMode ( VexAllocModeTEMP );}/* --------- Make a translation. --------- *//* Exported to library client. */VexTranslateResult LibVEX_Translate ( VexTranslateArgs* vta ){ /* This the bundle of functions we need to do the back-end stuff (insn selection, reg-alloc, assembly) whilst being insulated from the target instruction set. */ HReg* available_real_regs; Int n_available_real_regs; Bool (*isMove) ( HInstr*, HReg*, HReg* ); void (*getRegUsage) ( HRegUsage*, HInstr*, Bool ); void (*mapRegs) ( HRegRemap*, HInstr*, Bool ); HInstr* (*genSpill) ( HReg, Int, Bool ); HInstr* (*genReload) ( HReg, Int, Bool ); void (*ppInstr) ( HInstr*, Bool ); void (*ppReg) ( HReg ); HInstrArray* (*iselBB) ( IRBB*, VexArch, VexArchInfo* ); Int (*emit) ( UChar*, Int, HInstr*, Bool, void* ); IRExpr* (*specHelper) ( HChar*, IRExpr** ); Bool (*preciseMemExnsFn) ( Int, Int ); DisOneInstrFn disInstrFn; VexGuestLayout* guest_layout; Bool host_is_bigendian = False; IRBB* irbb; HInstrArray* vcode; HInstrArray* rcode; Int i, j, k, out_used, guest_sizeB; Int offB_TISTART, offB_TILEN; UChar insn_bytes[32]; IRType guest_word_type; IRType host_word_type; Bool mode64; guest_layout = NULL; available_real_regs = NULL; n_available_real_regs = 0; isMove = NULL; getRegUsage = NULL; mapRegs = NULL; genSpill = NULL; genReload = NULL; ppInstr = NULL; ppReg = NULL; iselBB = NULL; emit = NULL; specHelper = NULL; preciseMemExnsFn = NULL; disInstrFn = NULL; guest_word_type = Ity_INVALID; host_word_type = Ity_INVALID; offB_TISTART = 0; offB_TILEN = 0; mode64 = False; vex_traceflags = vta->traceflags; vassert(vex_initdone); vexSetAllocModeTEMP_and_clear(); vexAllocSanityCheck(); /* First off, check that the guest and host insn sets are supported. */ switch (vta->arch_host) { case VexArchX86: mode64 = False; getAllocableRegs_X86 ( &n_available_real_regs, &available_real_regs ); isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr; getRegUsage = (void(*)(HRegUsage*,HInstr*, Bool)) getRegUsage_X86Instr; mapRegs = (void(*)(HRegRemap*,HInstr*, Bool)) mapRegs_X86Instr; genSpill = (HInstr*(*)(HReg,Int, Bool)) genSpill_X86; genReload = (HInstr*(*)(HReg,Int, Bool)) genReload_X86; ppInstr = (void(*)(HInstr*, Bool)) ppX86Instr; ppReg = (void(*)(HReg)) ppHRegX86; iselBB = iselBB_X86; emit = (Int(*)(UChar*,Int,HInstr*,Bool,void*)) emit_X86Instr; host_is_bigendian = False; host_word_type = Ity_I32; vassert(are_valid_hwcaps(VexArchX86, vta->archinfo_host.hwcaps)); vassert(vta->dispatch != NULL); /* jump-to-dispatcher scheme */ break; case VexArchAMD64: mode64 = True; getAllocableRegs_AMD64 ( &n_available_real_regs, &available_real_regs ); isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_AMD64Instr; getRegUsage = (void(*)(HRegUsage*,HInstr*, Bool)) getRegUsage_AMD64Instr; mapRegs = (void(*)(HRegRemap*,HInstr*, Bool)) mapRegs_AMD64Instr; genSpill = (HInstr*(*)(HReg,Int, Bool)) genSpill_AMD64; genReload = (HInstr*(*)(HReg,Int, Bool)) genReload_AMD64; ppInstr = (void(*)(HInstr*, Bool)) ppAMD64Instr; ppReg = (void(*)(HReg)) ppHRegAMD64; iselBB = iselBB_AMD64; emit = (Int(*)(UChar*,Int,HInstr*,Bool,void*)) emit_AMD64Instr; host_is_bigendian = False; host_word_type = Ity_I64; vassert(are_valid_hwcaps(VexArchAMD64, vta->archinfo_host.hwcaps)); vassert(vta->dispatch != NULL); /* jump-to-dispatcher scheme */ break; case VexArchPPC32: mode64 = False; getAllocableRegs_PPC ( &n_available_real_regs, &available_real_regs, mode64 ); isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_PPCInstr; getRegUsage = (void(*)(HRegUsage*,HInstr*,Bool)) getRegUsage_PPCInstr; mapRegs = (void(*)(HRegRemap*,HInstr*,Bool)) mapRegs_PPCInstr; genSpill = (HInstr*(*)(HReg,Int,Bool)) genSpill_PPC; genReload = (HInstr*(*)(HReg,Int,Bool)) genReload_PPC; ppInstr = (void(*)(HInstr*,Bool)) ppPPCInstr; ppReg = (void(*)(HReg)) ppHRegPPC; iselBB = iselBB_PPC; emit = (Int(*)(UChar*,Int,HInstr*,Bool,void*)) emit_PPCInstr; host_is_bigendian = True; host_word_type = Ity_I32; vassert(are_valid_hwcaps(VexArchPPC32, vta->archinfo_host.hwcaps)); vassert(vta->dispatch == NULL); /* return-to-dispatcher scheme */ break; case VexArchPPC64: mode64 = True; getAllocableRegs_PPC ( &n_available_real_regs, &available_real_regs, mode64 ); isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_PPCInstr; getRegUsage = (void(*)(HRegUsage*,HInstr*, Bool)) getRegUsage_PPCInstr; mapRegs = (void(*)(HRegRemap*,HInstr*, Bool)) mapRegs_PPCInstr; genSpill = (HInstr*(*)(HReg,Int, Bool)) genSpill_PPC; genReload = (HInstr*(*)(HReg,Int, Bool)) genReload_PPC; ppInstr = (void(*)(HInstr*, Bool)) ppPPCInstr; ppReg = (void(*)(HReg)) ppHRegPPC; iselBB = iselBB_PPC; emit = (Int(*)(UChar*,Int,HInstr*,Bool,void*)) emit_PPCInstr; host_is_bigendian = True; host_word_type = Ity_I64; vassert(are_valid_hwcaps(VexArchPPC64, vta->archinfo_host.hwcaps)); vassert(vta->dispatch == NULL); /* return-to-dispatcher scheme */ break; default: vpanic("LibVEX_Translate: unsupported target insn set"); } switch (vta->arch_guest) { case VexArchX86: preciseMemExnsFn = guest_x86_state_requires_precise_mem_exns; disInstrFn = disInstr_X86; specHelper = guest_x86_spechelper; guest_sizeB = sizeof(VexGuestX86State); guest_word_type = Ity_I32; guest_layout = &x86guest_layout; offB_TISTART = offsetof(VexGuestX86State,guest_TISTART); offB_TILEN = offsetof(VexGuestX86State,guest_TILEN); vassert(are_valid_hwcaps(VexArchX86, vta->archinfo_guest.hwcaps)); vassert(0 == sizeof(VexGuestX86State) % 8); vassert(sizeof( ((VexGuestX86State*)0)->guest_TISTART) == 4); vassert(sizeof( ((VexGuestX86State*)0)->guest_TILEN ) == 4); vassert(sizeof( ((VexGuestX86State*)0)->guest_NRADDR ) == 4); break; case VexArchAMD64: preciseMemExnsFn = guest_amd64_state_requires_precise_mem_exns; disInstrFn = disInstr_AMD64; specHelper = guest_amd64_spechelper; guest_sizeB = sizeof(VexGuestAMD64State); guest_word_type = Ity_I64; guest_layout = &amd64guest_layout; offB_TISTART = offsetof(VexGuestAMD64State,guest_TISTART); offB_TILEN = offsetof(VexGuestAMD64State,guest_TILEN); vassert(are_valid_hwcaps(VexArchAMD64, vta->archinfo_guest.hwcaps)); vassert(0 == sizeof(VexGuestAMD64State) % 8); vassert(sizeof( ((VexGuestAMD64State*)0)->guest_TISTART ) == 8); vassert(sizeof( ((VexGuestAMD64State*)0)->guest_TILEN ) == 8); vassert(sizeof( ((VexGuestAMD64State*)0)->guest_NRADDR ) == 8); break; case VexArchARM: preciseMemExnsFn = guest_arm_state_requires_precise_mem_exns; disInstrFn = NULL; /* HACK */ specHelper = guest_arm_spechelper; guest_sizeB = sizeof(VexGuestARMState); guest_word_type = Ity_I32; guest_layout = &armGuest_layout; offB_TISTART = 0; /* hack ... arm has bitrot */ offB_TILEN = 0; /* hack ... arm has bitrot */ vassert(are_valid_hwcaps(VexArchARM, vta->archinfo_guest.hwcaps)); break; case VexArchPPC32: preciseMemExnsFn = guest_ppc32_state_requires_precise_mem_exns; disInstrFn = disInstr_PPC; specHelper = guest_ppc32_spechelper; guest_sizeB = sizeof(VexGuestPPC32State); guest_word_type = Ity_I32; guest_layout = &ppc32Guest_layout; offB_TISTART = offsetof(VexGuestPPC32State,guest_TISTART); offB_TILEN = offsetof(VexGuestPPC32State,guest_TILEN); vassert(are_valid_hwcaps(VexArchPPC32, vta->archinfo_guest.hwcaps)); vassert(0 == sizeof(VexGuestPPC32State) % 8); vassert(sizeof( ((VexGuestPPC32State*)0)->guest_TISTART ) == 4); vassert(sizeof( ((VexGuestPPC32State*)0)->guest_TILEN ) == 4); vassert(sizeof( ((VexGuestPPC32State*)0)->guest_NRADDR ) == 4); break; case VexArchPPC64: preciseMemExnsFn = guest_ppc64_state_requires_precise_mem_exns; disInstrFn = disInstr_PPC; specHelper = guest_ppc64_spechelper; guest_sizeB = sizeof(VexGuestPPC64State); guest_word_type = Ity_I64; guest_layout = &ppc64Guest_layout; offB_TISTART = offsetof(VexGuestPPC64State,guest_TISTART); offB_TILEN = offsetof(VexGuestPPC64State,guest_TILEN); vassert(are_valid_hwcaps(VexArchPPC64, vta->archinfo_guest.hwcaps)); vassert(0 == sizeof(VexGuestPPC64State) % 16); vassert(sizeof( ((VexGuestPPC64State*)0)->guest_TISTART ) == 8); vassert(sizeof( ((VexGuestPPC64State*)0)->guest_TILEN ) == 8); vassert(sizeof( ((VexGuestPPC64State*)0)->guest_NRADDR ) == 8);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -