1 /*
   2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "code/relocInfo.hpp"
  28 #include "nativeInst_x86.hpp"
  29 #include "oops/klass.inline.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/safepoint.hpp"
  32 #include "runtime/safepointMechanism.hpp"
  33 
  34 
  35 void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) {
  36 #ifdef AMD64
  37   x += o;
  38   typedef Assembler::WhichOperand WhichOperand;
  39   WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm, call32, narrow oop
  40   assert(which == Assembler::disp32_operand ||
  41          which == Assembler::narrow_oop_operand ||
  42          which == Assembler::imm_operand, "format unpacks ok");
  43   if (which == Assembler::imm_operand) {
  44     if (verify_only) {
  45       guarantee(*pd_address_in_code() == x, "instructions must match");
  46     } else {
  47       *pd_address_in_code() = x;
  48     }
  49   } else if (which == Assembler::narrow_oop_operand) {
  50     address disp = Assembler::locate_operand(addr(), which);
  51     // both compressed oops and compressed classes look the same
  52     if (Universe::heap()->is_in_reserved((oop)x)) {
  53     if (verify_only) {
  54       guarantee(*(uint32_t*) disp == oopDesc::encode_heap_oop((oop)x), "instructions must match");
  55     } else {
  56       *(int32_t*) disp = oopDesc::encode_heap_oop((oop)x);
  57     }
  58   } else {
  59       if (verify_only) {
  60         guarantee(*(uint32_t*) disp == Klass::encode_klass((Klass*)x), "instructions must match");
  61       } else {
  62         *(int32_t*) disp = Klass::encode_klass((Klass*)x);
  63       }
  64     }
  65   } else {
  66     // Note:  Use runtime_call_type relocations for call32_operand.
  67     address ip = addr();
  68     address disp = Assembler::locate_operand(ip, which);
  69     address next_ip = Assembler::locate_next_instruction(ip);
  70     if (verify_only) {
  71       guarantee(*(int32_t*) disp == (x - next_ip), "instructions must match");
  72     } else {
  73       *(int32_t*) disp = x - next_ip;
  74     }
  75   }
  76 #else
  77   if (verify_only) {
  78     guarantee(*pd_address_in_code() == (x + o), "instructions must match");
  79   } else {
  80     *pd_address_in_code() = x + o;
  81   }
  82 #endif // AMD64
  83 }
  84 
  85 
  86 address Relocation::pd_call_destination(address orig_addr) {
  87   intptr_t adj = 0;
  88   if (orig_addr != NULL) {
  89     // We just moved this call instruction from orig_addr to addr().
  90     // This means its target will appear to have grown by addr() - orig_addr.
  91     adj = -( addr() - orig_addr );
  92   }
  93   NativeInstruction* ni = nativeInstruction_at(addr());
  94   if (ni->is_call()) {
  95     return nativeCall_at(addr())->destination() + adj;
  96   } else if (ni->is_jump()) {
  97     return nativeJump_at(addr())->jump_destination() + adj;
  98   } else if (ni->is_cond_jump()) {
  99     return nativeGeneralJump_at(addr())->jump_destination() + adj;
 100   } else if (ni->is_mov_literal64()) {
 101     return (address) ((NativeMovConstReg*)ni)->data();
 102   } else {
 103     ShouldNotReachHere();
 104     return NULL;
 105   }
 106 }
 107 
 108 
 109 void Relocation::pd_set_call_destination(address x) {
 110   NativeInstruction* ni = nativeInstruction_at(addr());
 111   if (ni->is_call()) {
 112     nativeCall_at(addr())->set_destination(x);
 113   } else if (ni->is_jump()) {
 114     NativeJump* nj = nativeJump_at(addr());
 115 
 116     // Unresolved jumps are recognized by a destination of -1
 117     // However 64bit can't actually produce such an address
 118     // and encodes a jump to self but jump_destination will
 119     // return a -1 as the signal. We must not relocate this
 120     // jmp or the ic code will not see it as unresolved.
 121 
 122     if (nj->jump_destination() == (address) -1) {
 123       x = addr(); // jump to self
 124     }
 125     nj->set_jump_destination(x);
 126   } else if (ni->is_cond_jump()) {
 127     // %%%% kludge this, for now, until we get a jump_destination method
 128     address old_dest = nativeGeneralJump_at(addr())->jump_destination();
 129     address disp = Assembler::locate_operand(addr(), Assembler::call32_operand);
 130     *(jint*)disp += (x - old_dest);
 131   } else if (ni->is_mov_literal64()) {
 132     ((NativeMovConstReg*)ni)->set_data((intptr_t)x);
 133   } else {
 134     ShouldNotReachHere();
 135   }
 136 }
 137 
 138 
 139 address* Relocation::pd_address_in_code() {
 140   // All embedded Intel addresses are stored in 32-bit words.
 141   // Since the addr points at the start of the instruction,
 142   // we must parse the instruction a bit to find the embedded word.
 143   assert(is_data(), "must be a DataRelocation");
 144   typedef Assembler::WhichOperand WhichOperand;
 145   WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32
 146 #ifdef AMD64
 147   assert(which == Assembler::disp32_operand ||
 148          which == Assembler::call32_operand ||
 149          which == Assembler::imm_operand, "format unpacks ok");
 150   // The "address" in the code is a displacement can't return it as
 151   // and address* since it is really a jint*
 152   guarantee(which == Assembler::imm_operand, "must be immediate operand");
 153 #else
 154   assert(which == Assembler::disp32_operand || which == Assembler::imm_operand, "format unpacks ok");
 155 #endif // AMD64
 156   return (address*) Assembler::locate_operand(addr(), which);
 157 }
 158 
 159 
 160 address Relocation::pd_get_address_from_code() {
 161 #ifdef AMD64
 162   // All embedded Intel addresses are stored in 32-bit words.
 163   // Since the addr points at the start of the instruction,
 164   // we must parse the instruction a bit to find the embedded word.
 165   assert(is_data(), "must be a DataRelocation");
 166   typedef Assembler::WhichOperand WhichOperand;
 167   WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32
 168   assert(which == Assembler::disp32_operand ||
 169          which == Assembler::call32_operand ||
 170          which == Assembler::imm_operand, "format unpacks ok");
 171   if (which != Assembler::imm_operand) {
 172     address ip = addr();
 173     address disp = Assembler::locate_operand(ip, which);
 174     address next_ip = Assembler::locate_next_instruction(ip);
 175     address a = next_ip + *(int32_t*) disp;
 176     return a;
 177   }
 178 #endif // AMD64
 179   return *pd_address_in_code();
 180 }
 181 
 182 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 183 #ifdef _LP64
 184   typedef Assembler::WhichOperand WhichOperand;
 185   WhichOperand which = (WhichOperand) format();
 186 #if !INCLUDE_JVMCI
 187   if (SafepointMechanism::uses_global_page_poll()) {
 188     assert((which == Assembler::disp32_operand) == !Assembler::is_polling_page_far(), "format not set correctly");
 189   }
 190 #endif
 191   if (which == Assembler::disp32_operand) {
 192     assert(SafepointMechanism::uses_global_page_poll(), "should only have generated such a poll if global polling enabled");
 193     address orig_addr = old_addr_for(addr(), src, dest);
 194     NativeInstruction* oni = nativeInstruction_at(orig_addr);
 195     int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which);
 196     // This poll_addr is incorrect by the size of the instruction it is irrelevant
 197     intptr_t poll_addr = (intptr_t)oni + *orig_disp;
 198     NativeInstruction* ni = nativeInstruction_at(addr());
 199     intptr_t new_disp = poll_addr - (intptr_t) ni;
 200 
 201     int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which);
 202     * disp = (int32_t)new_disp;
 203   }
 204 #endif // _LP64
 205 }
 206 
 207 void metadata_Relocation::pd_fix_value(address x) {
 208 }