1 /*
   2  * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_relocInfo_x86.cpp.incl"
  27 
  28 
  29 void Relocation::pd_set_data_value(address x, intptr_t o) {
  30 #ifdef AMD64
  31   x += o;
  32   typedef Assembler::WhichOperand WhichOperand;
  33   WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm64, call32, narrow oop
  34   assert(which == Assembler::disp32_operand ||
  35          which == Assembler::narrow_oop_operand ||
  36          which == Assembler::imm64_operand, "format unpacks ok");
  37   if (which == Assembler::imm64_operand) {
  38     *pd_address_in_code() = x;
  39   } else if (which == Assembler::narrow_oop_operand) {
  40     address disp = Assembler::locate_operand(addr(), which);
  41     *(int32_t*) disp = oopDesc::encode_heap_oop((oop)x);
  42   } else {
  43     // Note:  Use runtime_call_type relocations for call32_operand.
  44     address ip = addr();
  45     address disp = Assembler::locate_operand(ip, which);
  46     address next_ip = Assembler::locate_next_instruction(ip);
  47     *(int32_t*) disp = x - next_ip;
  48   }
  49 #else
  50   *pd_address_in_code() = x + o;
  51 #endif // AMD64
  52 }
  53 
  54 
  55 address Relocation::pd_call_destination(address orig_addr) {
  56   intptr_t adj = 0;
  57   if (orig_addr != NULL) {
  58     // We just moved this call instruction from orig_addr to addr().
  59     // This means its target will appear to have grown by addr() - orig_addr.
  60     adj = -( addr() - orig_addr );
  61   }
  62   NativeInstruction* ni = nativeInstruction_at(addr());
  63   if (ni->is_call()) {
  64     return nativeCall_at(addr())->destination() + adj;
  65   } else if (ni->is_jump()) {
  66     return nativeJump_at(addr())->jump_destination() + adj;
  67   } else if (ni->is_cond_jump()) {
  68     return nativeGeneralJump_at(addr())->jump_destination() + adj;
  69   } else if (ni->is_mov_literal64()) {
  70     return (address) ((NativeMovConstReg*)ni)->data();
  71   } else {
  72     ShouldNotReachHere();
  73     return NULL;
  74   }
  75 }
  76 
  77 
  78 void Relocation::pd_set_call_destination(address x) {
  79   NativeInstruction* ni = nativeInstruction_at(addr());
  80   if (ni->is_call()) {
  81     nativeCall_at(addr())->set_destination(x);
  82   } else if (ni->is_jump()) {
  83     NativeJump* nj = nativeJump_at(addr());
  84 #ifdef AMD64
  85     if (nj->jump_destination() == (address) -1) {
  86       x = (address) -1; // retain jump to self
  87     }
  88 #endif // AMD64
  89     nj->set_jump_destination(x);
  90   } else if (ni->is_cond_jump()) {
  91     // %%%% kludge this, for now, until we get a jump_destination method
  92     address old_dest = nativeGeneralJump_at(addr())->jump_destination();
  93     address disp = Assembler::locate_operand(addr(), Assembler::call32_operand);
  94     *(jint*)disp += (x - old_dest);
  95   } else if (ni->is_mov_literal64()) {
  96     ((NativeMovConstReg*)ni)->set_data((intptr_t)x);
  97   } else {
  98     ShouldNotReachHere();
  99   }
 100 }
 101 
 102 
 103 address* Relocation::pd_address_in_code() {
 104   // All embedded Intel addresses are stored in 32-bit words.
 105   // Since the addr points at the start of the instruction,
 106   // we must parse the instruction a bit to find the embedded word.
 107   assert(is_data(), "must be a DataRelocation");
 108   typedef Assembler::WhichOperand WhichOperand;
 109   WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm64/imm32
 110 #ifdef AMD64
 111   assert(which == Assembler::disp32_operand ||
 112          which == Assembler::call32_operand ||
 113          which == Assembler::imm64_operand, "format unpacks ok");
 114   if (which != Assembler::imm64_operand) {
 115     // The "address" in the code is a displacement can't return it as
 116     // and address* since it is really a jint*
 117     ShouldNotReachHere();
 118     return NULL;
 119   }
 120 #else
 121   assert(which == Assembler::disp32_operand || which == Assembler::imm32_operand, "format unpacks ok");
 122 #endif // AMD64
 123   return (address*) Assembler::locate_operand(addr(), which);
 124 }
 125 
 126 
 127 address Relocation::pd_get_address_from_code() {
 128 #ifdef AMD64
 129   // All embedded Intel addresses are stored in 32-bit words.
 130   // Since the addr points at the start of the instruction,
 131   // we must parse the instruction a bit to find the embedded word.
 132   assert(is_data(), "must be a DataRelocation");
 133   typedef Assembler::WhichOperand WhichOperand;
 134   WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm64/imm32
 135   assert(which == Assembler::disp32_operand ||
 136          which == Assembler::call32_operand ||
 137          which == Assembler::imm64_operand, "format unpacks ok");
 138   if (which != Assembler::imm64_operand) {
 139     address ip = addr();
 140     address disp = Assembler::locate_operand(ip, which);
 141     address next_ip = Assembler::locate_next_instruction(ip);
 142     address a = next_ip + *(int32_t*) disp;
 143     return a;
 144   }
 145 #endif // AMD64
 146   return *pd_address_in_code();
 147 }
 148 
 149 int Relocation::pd_breakpoint_size() {
 150   // minimum breakpoint size, in short words
 151   return NativeIllegalInstruction::instruction_size / sizeof(short);
 152 }
 153 
 154 void Relocation::pd_swap_in_breakpoint(address x, short* instrs, int instrlen) {
 155   Untested("pd_swap_in_breakpoint");
 156   if (instrs != NULL) {
 157     assert(instrlen * sizeof(short) == NativeIllegalInstruction::instruction_size, "enough instrlen in reloc. data");
 158     for (int i = 0; i < instrlen; i++) {
 159       instrs[i] = ((short*)x)[i];
 160     }
 161   }
 162   NativeIllegalInstruction::insert(x);
 163 }
 164 
 165 
 166 void Relocation::pd_swap_out_breakpoint(address x, short* instrs, int instrlen) {
 167   Untested("pd_swap_out_breakpoint");
 168   assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
 169   NativeInstruction* ni = nativeInstruction_at(x);
 170   *(short*)ni->addr_at(0) = instrs[0];
 171 }