/*******************************************************************************1* Copyright (c) 2001, 2014 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21#if defined(WIN32) || defined (WIN64)22#include <windows.h>23#endif2425#include "j9.h"26#include "dfix.h"27#include "dfix_internal.h"28#include "ut_j9dfix.h"2930void31fixup(void* returnAddress, void* resolved)32{33U_8* callsite = ((U_8*)returnAddress) - 5;34IDATA oldDisplacement, newDisplacement, *displacementPtr;35DWORD oldProtectionBits = 0;36BOOL rcProtect = FALSE;3738/* Mark the code pages read/write */39rcProtect = VirtualProtect(callsite, 5, PAGE_EXECUTE_READWRITE, &oldProtectionBits);40if (!rcProtect) {41Assert_DFIX_FixupFailed();42return;43}4445/**46On x86 the instruction looks like:4700c01454 e847ffffff call dfixtest!zero_memory (00c013a0)4800c01459 83c408 add esp, <-- returnAddress points here49*/5051displacementPtr = (IDATA*)(callsite+1);52oldDisplacement = *displacementPtr;53newDisplacement = ((U_8*)resolved) - callsite - 5 /* instructions in this sequence*/ ;54*displacementPtr = newDisplacement;5556/* Restore the original protection bits */57rcProtect = VirtualProtect(callsite, 5, oldProtectionBits, &oldProtectionBits);58if (!rcProtect) {59Assert_DFIX_FixupFailed();60return;61}6263printf(64"fixup(returnAddress=0x%p, callsite=0x%p (opcode=%x) resolved=0x%p)\n",65returnAddress, callsite, *callsite, resolved);6667}6869#if defined(WIN32) || defined (WIN64)70BOOL WINAPI71DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)72{73if (DLL_PROCESS_ATTACH == fdwReason) {74if (!resolve_dfix_zero_memory()) {75return FALSE;76}77if (!resolve_dfix_memcpy()) {78return FALSE;79}80}81return TRUE;82}83#endif84858687