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