/// <summary>
        /// 备份原始方法头
        /// </summary>
        protected virtual void BackupHeader()
        {
            byte *pTarget = (byte *)_targetPtr.ToPointer();

            _backupArray = new byte[_headSize];
            for (int i = 0; i < _headSize; i++)
            {
                if (_proxyPtr != null)
                {
                    _proxyPtr[i] = pTarget[i];
                }
                _backupArray[i] = pTarget[i];
            }

            if (_proxyPtr != null)
            {
                int index = 0;
                if (LDasm.CheckShortCall(_proxyPtr, s_jmpBuff.Length, out index))
                {
                    if (!NativeAPI.IsAndroidARM())
                    {
                        // 目标地址 = 偏移 + 5 + 指令地址
                        int  oldOffsetAddr = *((int *)(_proxyPtr + index + 1));
                        long targetAddr    = oldOffsetAddr + 5 + (long)_targetPtr + index;
                        // 因为指令地址发生了改变,所以要重新计算偏移 公式: 偏移 = 目标地址 - 指令地址 - 5
                        int newOffsetAddr = (int)(targetAddr - ((long)_proxyPtr + index) - 5);
                        *((int *)(_proxyPtr + index + 1)) = newOffsetAddr;
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 备份原始方法头
        /// </summary>
        protected virtual void BackupHeader()
        {
            byte *pTarget = (byte *)_targetPtr.ToPointer();

            _backupArray = new byte[_headSize];
            for (int i = 0; i < _headSize; i++)
            {
                if (_proxyPtr != null)
                {
                    _proxyPtr[i] = pTarget[i];
                }
                _backupArray[i] = pTarget[i];
            }

            if (_proxyPtr != null)
            {
                int index = 0;
                if (LDasm.CheckShortCall(_proxyPtr, s_jmpBuff.Length, out index))
                {
                    if (IntPtr.Size == 8)
                    {
                        // 目标地址 = 偏移 + 5 + 指令地址
                        int  oldOffsetAddr = *((int *)(_proxyPtr + index + 1));
                        long targetAddr    = oldOffsetAddr + 5 + (long)_targetPtr + index;
                        fixed(byte *p = &s_jmpBuff[s_addrOffset])
                        {
                            IntPtr *ptr = (IntPtr *)p;

                            *ptr = (IntPtr)targetAddr;
                        }

                        // 原来的跳转指令长度为5,现在为 14所以把_headsize 拓宽
                        _headSize = _headSize - 5 + 14;
                        for (int i = index; i < 14 + index; i++)
                        {
                            _proxyPtr[i] = s_jmpBuff[i - index];
                        }
                    }
                    else
                    {
                        // 目标地址 = 偏移 + 5 + 指令地址
                        int  oldOffsetAddr = *((int *)(_proxyPtr + index + 1));
                        long targetAddr    = oldOffsetAddr + 5 + (long)_targetPtr + index;
                        // 因为指令地址发生了改变,所以要重新计算偏移 公式: 偏移 = 目标地址 - 指令地址 - 5
                        int newOffsetAddr = (int)(targetAddr - ((long)_proxyPtr + index) - 5);
                        *((int *)(_proxyPtr + index + 1)) = newOffsetAddr;
                    }
                }
            }
        }