relocInfo_x86.cpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1998, 2008, 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 "incls/_precompiled.incl"
26# include "incls/_relocInfo_x86.cpp.incl"
27
28
29void 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 imm, call32, narrow oop
34  assert(which == Assembler::disp32_operand ||
35         which == Assembler::narrow_oop_operand ||
36         which == Assembler::imm_operand, "format unpacks ok");
37  if (which == Assembler::imm_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
55address 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
78void 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
85    // Unresolved jumps are recognized by a destination of -1
86    // However 64bit can't actually produce such an address
87    // and encodes a jump to self but jump_destination will
88    // return a -1 as the signal. We must not relocate this
89    // jmp or the ic code will not see it as unresolved.
90
91    if (nj->jump_destination() == (address) -1) {
92      x = addr(); // jump to self
93    }
94    nj->set_jump_destination(x);
95  } else if (ni->is_cond_jump()) {
96    // %%%% kludge this, for now, until we get a jump_destination method
97    address old_dest = nativeGeneralJump_at(addr())->jump_destination();
98    address disp = Assembler::locate_operand(addr(), Assembler::call32_operand);
99    *(jint*)disp += (x - old_dest);
100  } else if (ni->is_mov_literal64()) {
101    ((NativeMovConstReg*)ni)->set_data((intptr_t)x);
102  } else {
103    ShouldNotReachHere();
104  }
105}
106
107
108address* Relocation::pd_address_in_code() {
109  // All embedded Intel addresses are stored in 32-bit words.
110  // Since the addr points at the start of the instruction,
111  // we must parse the instruction a bit to find the embedded word.
112  assert(is_data(), "must be a DataRelocation");
113  typedef Assembler::WhichOperand WhichOperand;
114  WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32
115#ifdef AMD64
116  assert(which == Assembler::disp32_operand ||
117         which == Assembler::call32_operand ||
118         which == Assembler::imm_operand, "format unpacks ok");
119  if (which != Assembler::imm_operand) {
120    // The "address" in the code is a displacement can't return it as
121    // and address* since it is really a jint*
122    ShouldNotReachHere();
123    return NULL;
124  }
125#else
126  assert(which == Assembler::disp32_operand || which == Assembler::imm_operand, "format unpacks ok");
127#endif // AMD64
128  return (address*) Assembler::locate_operand(addr(), which);
129}
130
131
132address Relocation::pd_get_address_from_code() {
133#ifdef AMD64
134  // All embedded Intel addresses are stored in 32-bit words.
135  // Since the addr points at the start of the instruction,
136  // we must parse the instruction a bit to find the embedded word.
137  assert(is_data(), "must be a DataRelocation");
138  typedef Assembler::WhichOperand WhichOperand;
139  WhichOperand which = (WhichOperand) format(); // that is, disp32 or imm/imm32
140  assert(which == Assembler::disp32_operand ||
141         which == Assembler::call32_operand ||
142         which == Assembler::imm_operand, "format unpacks ok");
143  if (which != Assembler::imm_operand) {
144    address ip = addr();
145    address disp = Assembler::locate_operand(ip, which);
146    address next_ip = Assembler::locate_next_instruction(ip);
147    address a = next_ip + *(int32_t*) disp;
148    return a;
149  }
150#endif // AMD64
151  return *pd_address_in_code();
152}
153
154int Relocation::pd_breakpoint_size() {
155  // minimum breakpoint size, in short words
156  return NativeIllegalInstruction::instruction_size / sizeof(short);
157}
158
159void Relocation::pd_swap_in_breakpoint(address x, short* instrs, int instrlen) {
160  Untested("pd_swap_in_breakpoint");
161  if (instrs != NULL) {
162    assert(instrlen * sizeof(short) == NativeIllegalInstruction::instruction_size, "enough instrlen in reloc. data");
163    for (int i = 0; i < instrlen; i++) {
164      instrs[i] = ((short*)x)[i];
165    }
166  }
167  NativeIllegalInstruction::insert(x);
168}
169
170
171void Relocation::pd_swap_out_breakpoint(address x, short* instrs, int instrlen) {
172  Untested("pd_swap_out_breakpoint");
173  assert(NativeIllegalInstruction::instruction_size == sizeof(short), "right address unit for update");
174  NativeInstruction* ni = nativeInstruction_at(x);
175  *(short*)ni->addr_at(0) = instrs[0];
176}
177
178void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
179#ifdef _LP64
180  typedef Assembler::WhichOperand WhichOperand;
181  WhichOperand which = (WhichOperand) format();
182  // This format is imm but it is really disp32
183  which = Assembler::disp32_operand;
184  address orig_addr = old_addr_for(addr(), src, dest);
185  NativeInstruction* oni = nativeInstruction_at(orig_addr);
186  int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which);
187  // This poll_addr is incorrect by the size of the instruction it is irrelevant
188  intptr_t poll_addr = (intptr_t)oni + *orig_disp;
189
190  NativeInstruction* ni = nativeInstruction_at(addr());
191  intptr_t new_disp = poll_addr - (intptr_t) ni;
192
193  int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which);
194  * disp = (int32_t)new_disp;
195
196#endif // _LP64
197}
198
199void poll_return_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
200#ifdef _LP64
201  typedef Assembler::WhichOperand WhichOperand;
202  WhichOperand which = (WhichOperand) format();
203  // This format is imm but it is really disp32
204  which = Assembler::disp32_operand;
205  address orig_addr = old_addr_for(addr(), src, dest);
206  NativeInstruction* oni = nativeInstruction_at(orig_addr);
207  int32_t* orig_disp = (int32_t*) Assembler::locate_operand(orig_addr, which);
208  // This poll_addr is incorrect by the size of the instruction it is irrelevant
209  intptr_t poll_addr = (intptr_t)oni + *orig_disp;
210
211  NativeInstruction* ni = nativeInstruction_at(addr());
212  intptr_t new_disp = poll_addr - (intptr_t) ni;
213
214  int32_t* disp = (int32_t*) Assembler::locate_operand(addr(), which);
215  * disp = (int32_t)new_disp;
216#endif // _LP64
217}
218