relocInfo_x86.cpp 8.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
#include "precompiled.hpp"
#include "asm/assembler.inline.hpp"
#include "assembler_x86.inline.hpp"
#include "code/relocInfo.hpp"
#include "nativeInst_x86.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/safepoint.hpp"
D
duke 已提交
32 33


34
void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) {
D
duke 已提交
35 36 37
#ifdef AMD64
  x += o;
  typedef Assembler::WhichOperand WhichOperand;
38
  WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm, call32, narrow oop
D
duke 已提交
39
  assert(which == Assembler::disp32_operand ||
40
         which == Assembler::narrow_oop_operand ||
41 42
         which == Assembler::imm_operand, "format unpacks ok");
  if (which == Assembler::imm_operand) {
43 44 45 46 47
    if (verify_only) {
      assert(*pd_address_in_code() == x, "instructions must match");
    } else {
      *pd_address_in_code() = x;
    }
48 49
  } else if (which == Assembler::narrow_oop_operand) {
    address disp = Assembler::locate_operand(addr(), which);
50 51 52 53 54
    if (verify_only) {
      assert(*(uint32_t*) disp == oopDesc::encode_heap_oop((oop)x), "instructions must match");
    } else {
      *(int32_t*) disp = oopDesc::encode_heap_oop((oop)x);
    }
D
duke 已提交
55 56 57 58 59
  } else {
    // Note:  Use runtime_call_type relocations for call32_operand.
    address ip = addr();
    address disp = Assembler::locate_operand(ip, which);
    address next_ip = Assembler::locate_next_instruction(ip);
60 61 62 63 64
    if (verify_only) {
      assert(*(int32_t*) disp == (x - next_ip), "instructions must match");
    } else {
      *(int32_t*) disp = x - next_ip;
    }
D
duke 已提交
65 66
  }
#else
67 68 69 70 71
  if (verify_only) {
    assert(*pd_address_in_code() == (x + o), "instructions must match");
  } else {
    *pd_address_in_code() = x + o;
  }
D
duke 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#endif // AMD64
}


address Relocation::pd_call_destination(address orig_addr) {
  intptr_t adj = 0;
  if (orig_addr != NULL) {
    // We just moved this call instruction from orig_addr to addr().
    // This means its target will appear to have grown by addr() - orig_addr.
    adj = -( addr() - orig_addr );
  }
  NativeInstruction* ni = nativeInstruction_at(addr());
  if (ni->is_call()) {
    return nativeCall_at(addr())->destination() + adj;
  } else if (ni->is_jump()) {
    return nativeJump_at(addr())->jump_destination() + adj;
  } else if (ni->is_cond_jump()) {
    return nativeGeneralJump_at(addr())->jump_destination() + adj;
  } else if (ni->is_mov_literal64()) {
    return (address) ((NativeMovConstReg*)ni)->data();
  } else {
    ShouldNotReachHere();
    return NULL;
  }
}


void Relocation::pd_set_call_destination(address x) {
  NativeInstruction* ni = nativeInstruction_at(addr());
  if (ni->is_call()) {
    nativeCall_at(addr())->set_destination(x);
  } else if (ni->is_jump()) {
    NativeJump* nj = nativeJump_at(addr());
105 106 107 108 109 110 111

    // Unresolved jumps are recognized by a destination of -1
    // However 64bit can't actually produce such an address
    // and encodes a jump to self but jump_destination will
    // return a -1 as the signal. We must not relocate this
    // jmp or the ic code will not see it as unresolved.

D
duke 已提交
112
    if (nj->jump_destination() == (address) -1) {
113
      x = addr(); // jump to self
D
duke 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }
    nj->set_jump_destination(x);
  } else if (ni->is_cond_jump()) {
    // %%%% kludge this, for now, until we get a jump_destination method
    address old_dest = nativeGeneralJump_at(addr())->jump_destination();
    address disp = Assembler::locate_operand(addr(), Assembler::call32_operand);
    *(jint*)disp += (x - old_dest);
  } else if (ni->is_mov_literal64()) {
    ((NativeMovConstReg*)ni)->set_data((intptr_t)x);
  } else {
    ShouldNotReachHere();
  }
}


address* Relocation::pd_address_in_code() {
  // All embedded Intel addresses are stored in 32-bit words.
  // Since the addr points at the start of the instruction,
  // we must parse the instruction a bit to find the embedded word.
  assert(is_data(), "must be a DataRelocation");
  typedef Assembler::WhichOperand WhichOperand;
135
  WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32
D
duke 已提交
136 137 138
#ifdef AMD64
  assert(which == Assembler::disp32_operand ||
         which == Assembler::call32_operand ||
139 140
         which == Assembler::imm_operand, "format unpacks ok");
  if (which != Assembler::imm_operand) {
D
duke 已提交
141 142 143 144 145 146
    // The "address" in the code is a displacement can't return it as
    // and address* since it is really a jint*
    ShouldNotReachHere();
    return NULL;
  }
#else
147
  assert(which == Assembler::disp32_operand || which == Assembler::imm_operand, "format unpacks ok");
D
duke 已提交
148 149 150 151 152 153 154 155 156 157 158 159
#endif // AMD64
  return (address*) Assembler::locate_operand(addr(), which);
}


address Relocation::pd_get_address_from_code() {
#ifdef AMD64
  // All embedded Intel addresses are stored in 32-bit words.
  // Since the addr points at the start of the instruction,
  // we must parse the instruction a bit to find the embedded word.
  assert(is_data(), "must be a DataRelocation");
  typedef Assembler::WhichOperand WhichOperand;
160
  WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32
D
duke 已提交
161 162
  assert(which == Assembler::disp32_operand ||
         which == Assembler::call32_operand ||
163 164
         which == Assembler::imm_operand, "format unpacks ok");
  if (which != Assembler::imm_operand) {
D
duke 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    address ip = addr();
    address disp = Assembler::locate_operand(ip, which);
    address next_ip = Assembler::locate_next_instruction(ip);
    address a = next_ip + *(int32_t*) disp;
    return a;
  }
#endif // AMD64
  return *pd_address_in_code();
}

int Relocation::pd_breakpoint_size() {
  // minimum breakpoint size, in short words
  return NativeIllegalInstruction::instruction_size / sizeof(short);
}

void Relocation::pd_swap_in_breakpoint(address x, short* instrs, int instrlen) {
  Untested("pd_swap_in_breakpoint");
  if (instrs != NULL) {
    assert(instrlen * sizeof(short) == NativeIllegalInstruction::instruction_size, "enough instrlen in reloc. data");
    for (int i = 0; i < instrlen; i++) {
      instrs[i] = ((short*)x)[i];
    }
  }
  NativeIllegalInstruction::insert(x);
}


void Relocation::pd_swap_out_breakpoint(address x, short* instrs, int instrlen) {
  Untested("pd_swap_out_breakpoint");
  assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
  NativeInstruction* ni = nativeInstruction_at(x);
  *(short*)ni->addr_at(0) = instrs[0];
}
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
#ifdef _LP64
  typedef Assembler::WhichOperand WhichOperand;
  WhichOperand which = (WhichOperand) format();
  // This format is imm but it is really disp32
  which = Assembler::disp32_operand;
  address orig_addr = old_addr_for(addr(), src, dest);
  NativeInstruction* oni = nativeInstruction_at(orig_addr);
  int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which);
  // This poll_addr is incorrect by the size of the instruction it is irrelevant
  intptr_t poll_addr = (intptr_t)oni + *orig_disp;

  NativeInstruction* ni = nativeInstruction_at(addr());
  intptr_t new_disp = poll_addr - (intptr_t) ni;

  int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which);
  * disp = (int32_t)new_disp;

#endif // _LP64
}

void poll_return_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
#ifdef _LP64
  typedef Assembler::WhichOperand WhichOperand;
  WhichOperand which = (WhichOperand) format();
  // This format is imm but it is really disp32
  which = Assembler::disp32_operand;
  address orig_addr = old_addr_for(addr(), src, dest);
  NativeInstruction* oni = nativeInstruction_at(orig_addr);
  int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which);
  // This poll_addr is incorrect by the size of the instruction it is irrelevant
  intptr_t poll_addr = (intptr_t)oni + *orig_disp;

  NativeInstruction* ni = nativeInstruction_at(addr());
  intptr_t new_disp = poll_addr - (intptr_t) ni;

  int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which);
  * disp = (int32_t)new_disp;
#endif // _LP64
}