duke@435: /* duke@435: * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_relocInfo_x86.cpp.incl" duke@435: duke@435: duke@435: void Relocation::pd_set_data_value(address x, intptr_t o) { duke@435: #ifdef AMD64 duke@435: x += o; duke@435: typedef Assembler::WhichOperand WhichOperand; duke@435: WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm64, call32 duke@435: assert(which == Assembler::disp32_operand || duke@435: which == Assembler::imm64_operand, "format unpacks ok"); duke@435: if (which == Assembler::imm64_operand) { duke@435: *pd_address_in_code() = x; duke@435: } else { duke@435: // Note: Use runtime_call_type relocations for call32_operand. duke@435: address ip = addr(); duke@435: address disp = Assembler::locate_operand(ip, which); duke@435: address next_ip = Assembler::locate_next_instruction(ip); duke@435: *(int32_t*) disp = x - next_ip; duke@435: } duke@435: #else duke@435: *pd_address_in_code() = x + o; duke@435: #endif // AMD64 duke@435: } duke@435: duke@435: duke@435: address Relocation::pd_call_destination(address orig_addr) { duke@435: intptr_t adj = 0; duke@435: if (orig_addr != NULL) { duke@435: // We just moved this call instruction from orig_addr to addr(). duke@435: // This means its target will appear to have grown by addr() - orig_addr. duke@435: adj = -( addr() - orig_addr ); duke@435: } duke@435: NativeInstruction* ni = nativeInstruction_at(addr()); duke@435: if (ni->is_call()) { duke@435: return nativeCall_at(addr())->destination() + adj; duke@435: } else if (ni->is_jump()) { duke@435: return nativeJump_at(addr())->jump_destination() + adj; duke@435: } else if (ni->is_cond_jump()) { duke@435: return nativeGeneralJump_at(addr())->jump_destination() + adj; duke@435: } else if (ni->is_mov_literal64()) { duke@435: return (address) ((NativeMovConstReg*)ni)->data(); duke@435: } else { duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: } duke@435: duke@435: duke@435: void Relocation::pd_set_call_destination(address x) { duke@435: NativeInstruction* ni = nativeInstruction_at(addr()); duke@435: if (ni->is_call()) { duke@435: nativeCall_at(addr())->set_destination(x); duke@435: } else if (ni->is_jump()) { duke@435: NativeJump* nj = nativeJump_at(addr()); duke@435: #ifdef AMD64 duke@435: if (nj->jump_destination() == (address) -1) { duke@435: x = (address) -1; // retain jump to self duke@435: } duke@435: #endif // AMD64 duke@435: nj->set_jump_destination(x); duke@435: } else if (ni->is_cond_jump()) { duke@435: // %%%% kludge this, for now, until we get a jump_destination method duke@435: address old_dest = nativeGeneralJump_at(addr())->jump_destination(); duke@435: address disp = Assembler::locate_operand(addr(), Assembler::call32_operand); duke@435: *(jint*)disp += (x - old_dest); duke@435: } else if (ni->is_mov_literal64()) { duke@435: ((NativeMovConstReg*)ni)->set_data((intptr_t)x); duke@435: } else { duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: duke@435: duke@435: address* Relocation::pd_address_in_code() { duke@435: // All embedded Intel addresses are stored in 32-bit words. duke@435: // Since the addr points at the start of the instruction, duke@435: // we must parse the instruction a bit to find the embedded word. duke@435: assert(is_data(), "must be a DataRelocation"); duke@435: typedef Assembler::WhichOperand WhichOperand; duke@435: WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm64/imm32 duke@435: #ifdef AMD64 duke@435: assert(which == Assembler::disp32_operand || duke@435: which == Assembler::call32_operand || duke@435: which == Assembler::imm64_operand, "format unpacks ok"); duke@435: if (which != Assembler::imm64_operand) { duke@435: // The "address" in the code is a displacement can't return it as duke@435: // and address* since it is really a jint* duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: #else duke@435: assert(which == Assembler::disp32_operand || which == Assembler::imm32_operand, "format unpacks ok"); duke@435: #endif // AMD64 duke@435: return (address*) Assembler::locate_operand(addr(), which); duke@435: } duke@435: duke@435: duke@435: address Relocation::pd_get_address_from_code() { duke@435: #ifdef AMD64 duke@435: // All embedded Intel addresses are stored in 32-bit words. duke@435: // Since the addr points at the start of the instruction, duke@435: // we must parse the instruction a bit to find the embedded word. duke@435: assert(is_data(), "must be a DataRelocation"); duke@435: typedef Assembler::WhichOperand WhichOperand; duke@435: WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm64/imm32 duke@435: assert(which == Assembler::disp32_operand || duke@435: which == Assembler::call32_operand || duke@435: which == Assembler::imm64_operand, "format unpacks ok"); duke@435: if (which != Assembler::imm64_operand) { duke@435: address ip = addr(); duke@435: address disp = Assembler::locate_operand(ip, which); duke@435: address next_ip = Assembler::locate_next_instruction(ip); duke@435: address a = next_ip + *(int32_t*) disp; duke@435: return a; duke@435: } duke@435: #endif // AMD64 duke@435: return *pd_address_in_code(); duke@435: } duke@435: duke@435: int Relocation::pd_breakpoint_size() { duke@435: // minimum breakpoint size, in short words duke@435: return NativeIllegalInstruction::instruction_size / sizeof(short); duke@435: } duke@435: duke@435: void Relocation::pd_swap_in_breakpoint(address x, short* instrs, int instrlen) { duke@435: Untested("pd_swap_in_breakpoint"); duke@435: if (instrs != NULL) { duke@435: assert(instrlen * sizeof(short) == NativeIllegalInstruction::instruction_size, "enough instrlen in reloc. data"); duke@435: for (int i = 0; i < instrlen; i++) { duke@435: instrs[i] = ((short*)x)[i]; duke@435: } duke@435: } duke@435: NativeIllegalInstruction::insert(x); duke@435: } duke@435: duke@435: duke@435: void Relocation::pd_swap_out_breakpoint(address x, short* instrs, int instrlen) { duke@435: Untested("pd_swap_out_breakpoint"); duke@435: assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update"); duke@435: NativeInstruction* ni = nativeInstruction_at(x); duke@435: *(short*)ni->addr_at(0) = instrs[0]; duke@435: }