src/share/vm/code/relocInfo.hpp

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
parent 2750
6c97c830fb6f
child 5314
7875ea94bea5
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

duke@435 1 /*
coleenp@4037 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_CODE_RELOCINFO_HPP
stefank@2314 26 #define SHARE_VM_CODE_RELOCINFO_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "utilities/top.hpp"
stefank@2314 30
coleenp@4037 31 class NativeMovConstReg;
coleenp@4037 32
duke@435 33 // Types in this file:
duke@435 34 // relocInfo
duke@435 35 // One element of an array of halfwords encoding compressed relocations.
duke@435 36 // Also, the source of relocation types (relocInfo::oop_type, ...).
duke@435 37 // Relocation
duke@435 38 // A flyweight object representing a single relocation.
duke@435 39 // It is fully unpacked from the compressed relocation array.
coleenp@4037 40 // metadata_Relocation, ... (subclasses of Relocation)
coleenp@4037 41 // The location of some type-specific operations (metadata_addr, ...).
coleenp@4037 42 // Also, the source of relocation specs (metadata_Relocation::spec, ...).
duke@435 43 // oop_Relocation, ... (subclasses of Relocation)
coleenp@4037 44 // oops in the code stream (strings, class loaders)
duke@435 45 // Also, the source of relocation specs (oop_Relocation::spec, ...).
duke@435 46 // RelocationHolder
duke@435 47 // A ValueObj type which acts as a union holding a Relocation object.
duke@435 48 // Represents a relocation spec passed into a CodeBuffer during assembly.
duke@435 49 // RelocIterator
duke@435 50 // A StackObj which iterates over the relocations associated with
duke@435 51 // a range of code addresses. Can be used to operate a copy of code.
duke@435 52 // PatchingRelocIterator
duke@435 53 // Specialized subtype of RelocIterator which removes breakpoints
duke@435 54 // temporarily during iteration, then restores them.
duke@435 55 // BoundRelocation
duke@435 56 // An _internal_ type shared by packers and unpackers of relocations.
duke@435 57 // It pastes together a RelocationHolder with some pointers into
duke@435 58 // code and relocInfo streams.
duke@435 59
duke@435 60
duke@435 61 // Notes on relocType:
duke@435 62 //
duke@435 63 // These hold enough information to read or write a value embedded in
duke@435 64 // the instructions of an CodeBlob. They're used to update:
duke@435 65 //
duke@435 66 // 1) embedded oops (isOop() == true)
duke@435 67 // 2) inline caches (isIC() == true)
duke@435 68 // 3) runtime calls (isRuntimeCall() == true)
duke@435 69 // 4) internal word ref (isInternalWord() == true)
duke@435 70 // 5) external word ref (isExternalWord() == true)
duke@435 71 //
duke@435 72 // when objects move (GC) or if code moves (compacting the code heap).
duke@435 73 // They are also used to patch the code (if a call site must change)
duke@435 74 //
duke@435 75 // A relocInfo is represented in 16 bits:
duke@435 76 // 4 bits indicating the relocation type
duke@435 77 // 12 bits indicating the offset from the previous relocInfo address
duke@435 78 //
duke@435 79 // The offsets accumulate along the relocInfo stream to encode the
duke@435 80 // address within the CodeBlob, which is named RelocIterator::addr().
duke@435 81 // The address of a particular relocInfo always points to the first
duke@435 82 // byte of the relevant instruction (and not to any of its subfields
duke@435 83 // or embedded immediate constants).
duke@435 84 //
duke@435 85 // The offset value is scaled appropriately for the target machine.
duke@435 86 // (See relocInfo_<arch>.hpp for the offset scaling.)
duke@435 87 //
duke@435 88 // On some machines, there may also be a "format" field which may provide
duke@435 89 // additional information about the format of the instruction stream
duke@435 90 // at the corresponding code address. The format value is usually zero.
duke@435 91 // Any machine (such as Intel) whose instructions can sometimes contain
duke@435 92 // more than one relocatable constant needs format codes to distinguish
duke@435 93 // which operand goes with a given relocation.
duke@435 94 //
duke@435 95 // If the target machine needs N format bits, the offset has 12-N bits,
duke@435 96 // the format is encoded between the offset and the type, and the
duke@435 97 // relocInfo_<arch>.hpp file has manifest constants for the format codes.
duke@435 98 //
duke@435 99 // If the type is "data_prefix_tag" then the offset bits are further encoded,
duke@435 100 // and in fact represent not a code-stream offset but some inline data.
duke@435 101 // The data takes the form of a counted sequence of halfwords, which
duke@435 102 // precedes the actual relocation record. (Clients never see it directly.)
duke@435 103 // The interpetation of this extra data depends on the relocation type.
duke@435 104 //
duke@435 105 // On machines that have 32-bit immediate fields, there is usually
duke@435 106 // little need for relocation "prefix" data, because the instruction stream
duke@435 107 // is a perfectly reasonable place to store the value. On machines in
duke@435 108 // which 32-bit values must be "split" across instructions, the relocation
duke@435 109 // data is the "true" specification of the value, which is then applied
duke@435 110 // to some field of the instruction (22 or 13 bits, on SPARC).
duke@435 111 //
duke@435 112 // Whenever the location of the CodeBlob changes, any PC-relative
duke@435 113 // relocations, and any internal_word_type relocations, must be reapplied.
duke@435 114 // After the GC runs, oop_type relocations must be reapplied.
duke@435 115 //
duke@435 116 //
duke@435 117 // Here are meanings of the types:
duke@435 118 //
duke@435 119 // relocInfo::none -- a filler record
duke@435 120 // Value: none
duke@435 121 // Instruction: The corresponding code address is ignored
duke@435 122 // Data: Any data prefix and format code are ignored
duke@435 123 // (This means that any relocInfo can be disabled by setting
duke@435 124 // its type to none. See relocInfo::remove.)
duke@435 125 //
coleenp@4037 126 // relocInfo::oop_type, relocInfo::metadata_type -- a reference to an oop or meta data
duke@435 127 // Value: an oop, or else the address (handle) of an oop
duke@435 128 // Instruction types: memory (load), set (load address)
duke@435 129 // Data: [] an oop stored in 4 bytes of instruction
duke@435 130 // [n] n is the index of an oop in the CodeBlob's oop pool
duke@435 131 // [[N]n l] and l is a byte offset to be applied to the oop
duke@435 132 // [Nn Ll] both index and offset may be 32 bits if necessary
duke@435 133 // Here is a special hack, used only by the old compiler:
duke@435 134 // [[N]n 00] the value is the __address__ of the nth oop in the pool
duke@435 135 // (Note that the offset allows optimal references to class variables.)
duke@435 136 //
duke@435 137 // relocInfo::internal_word_type -- an address within the same CodeBlob
duke@435 138 // relocInfo::section_word_type -- same, but can refer to another section
duke@435 139 // Value: an address in the CodeBlob's code or constants section
duke@435 140 // Instruction types: memory (load), set (load address)
duke@435 141 // Data: [] stored in 4 bytes of instruction
duke@435 142 // [[L]l] a relative offset (see [About Offsets] below)
duke@435 143 // In the case of section_word_type, the offset is relative to a section
duke@435 144 // base address, and the section number (e.g., SECT_INSTS) is encoded
duke@435 145 // into the low two bits of the offset L.
duke@435 146 //
duke@435 147 // relocInfo::external_word_type -- a fixed address in the runtime system
duke@435 148 // Value: an address
duke@435 149 // Instruction types: memory (load), set (load address)
duke@435 150 // Data: [] stored in 4 bytes of instruction
duke@435 151 // [n] the index of a "well-known" stub (usual case on RISC)
duke@435 152 // [Ll] a 32-bit address
duke@435 153 //
duke@435 154 // relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
duke@435 155 // Value: an address
duke@435 156 // Instruction types: PC-relative call (or a PC-relative branch)
duke@435 157 // Data: [] stored in 4 bytes of instruction
duke@435 158 //
duke@435 159 // relocInfo::static_call_type -- a static call
duke@435 160 // Value: an CodeBlob, a stub, or a fixup routine
duke@435 161 // Instruction types: a call
duke@435 162 // Data: []
duke@435 163 // The identity of the callee is extracted from debugging information.
duke@435 164 // //%note reloc_3
duke@435 165 //
duke@435 166 // relocInfo::virtual_call_type -- a virtual call site (which includes an inline
duke@435 167 // cache)
duke@435 168 // Value: an CodeBlob, a stub, the interpreter, or a fixup routine
duke@435 169 // Instruction types: a call, plus some associated set-oop instructions
duke@435 170 // Data: [] the associated set-oops are adjacent to the call
duke@435 171 // [n] n is a relative offset to the first set-oop
duke@435 172 // [[N]n l] and l is a limit within which the set-oops occur
duke@435 173 // [Nn Ll] both n and l may be 32 bits if necessary
duke@435 174 // The identity of the callee is extracted from debugging information.
duke@435 175 //
duke@435 176 // relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
duke@435 177 //
duke@435 178 // Same info as a static_call_type. We use a special type, so the handling of
duke@435 179 // virtuals and statics are separated.
duke@435 180 //
duke@435 181 //
duke@435 182 // The offset n points to the first set-oop. (See [About Offsets] below.)
duke@435 183 // In turn, the set-oop instruction specifies or contains an oop cell devoted
duke@435 184 // exclusively to the IC call, which can be patched along with the call.
duke@435 185 //
duke@435 186 // The locations of any other set-oops are found by searching the relocation
duke@435 187 // information starting at the first set-oop, and continuing until all
duke@435 188 // relocations up through l have been inspected. The value l is another
duke@435 189 // relative offset. (Both n and l are relative to the call's first byte.)
duke@435 190 //
duke@435 191 // The limit l of the search is exclusive. However, if it points within
duke@435 192 // the call (e.g., offset zero), it is adjusted to point after the call and
duke@435 193 // any associated machine-specific delay slot.
duke@435 194 //
duke@435 195 // Since the offsets could be as wide as 32-bits, these conventions
duke@435 196 // put no restrictions whatever upon code reorganization.
duke@435 197 //
duke@435 198 // The compiler is responsible for ensuring that transition from a clean
duke@435 199 // state to a monomorphic compiled state is MP-safe. This implies that
duke@435 200 // the system must respond well to intermediate states where a random
duke@435 201 // subset of the set-oops has been correctly from the clean state
duke@435 202 // upon entry to the VEP of the compiled method. In the case of a
duke@435 203 // machine (Intel) with a single set-oop instruction, the 32-bit
duke@435 204 // immediate field must not straddle a unit of memory coherence.
duke@435 205 // //%note reloc_3
duke@435 206 //
duke@435 207 // relocInfo::breakpoint_type -- a conditional breakpoint in the code
duke@435 208 // Value: none
duke@435 209 // Instruction types: any whatsoever
duke@435 210 // Data: [b [T]t i...]
duke@435 211 // The b is a bit-packed word representing the breakpoint's attributes.
duke@435 212 // The t is a target address which the breakpoint calls (when it is enabled).
duke@435 213 // The i... is a place to store one or two instruction words overwritten
duke@435 214 // by a trap, so that the breakpoint may be subsequently removed.
duke@435 215 //
duke@435 216 // relocInfo::static_stub_type -- an extra stub for each static_call_type
duke@435 217 // Value: none
duke@435 218 // Instruction types: a virtual call: { set_oop; jump; }
duke@435 219 // Data: [[N]n] the offset of the associated static_call reloc
duke@435 220 // This stub becomes the target of a static call which must be upgraded
duke@435 221 // to a virtual call (because the callee is interpreted).
duke@435 222 // See [About Offsets] below.
duke@435 223 // //%note reloc_2
duke@435 224 //
duke@435 225 // For example:
duke@435 226 //
duke@435 227 // INSTRUCTIONS RELOC: TYPE PREFIX DATA
duke@435 228 // ------------ ---- -----------
duke@435 229 // sethi %hi(myObject), R oop_type [n(myObject)]
duke@435 230 // ld [R+%lo(myObject)+fldOffset], R2 oop_type [n(myObject) fldOffset]
duke@435 231 // add R2, 1, R2
duke@435 232 // st R2, [R+%lo(myObject)+fldOffset] oop_type [n(myObject) fldOffset]
duke@435 233 //%note reloc_1
duke@435 234 //
duke@435 235 // This uses 4 instruction words, 8 relocation halfwords,
duke@435 236 // and an entry (which is sharable) in the CodeBlob's oop pool,
duke@435 237 // for a total of 36 bytes.
duke@435 238 //
duke@435 239 // Note that the compiler is responsible for ensuring the "fldOffset" when
duke@435 240 // added to "%lo(myObject)" does not overflow the immediate fields of the
duke@435 241 // memory instructions.
duke@435 242 //
duke@435 243 //
duke@435 244 // [About Offsets] Relative offsets are supplied to this module as
duke@435 245 // positive byte offsets, but they may be internally stored scaled
duke@435 246 // and/or negated, depending on what is most compact for the target
duke@435 247 // system. Since the object pointed to by the offset typically
duke@435 248 // precedes the relocation address, it is profitable to store
duke@435 249 // these negative offsets as positive numbers, but this decision
duke@435 250 // is internal to the relocation information abstractions.
duke@435 251 //
duke@435 252
duke@435 253 class Relocation;
duke@435 254 class CodeBuffer;
duke@435 255 class CodeSection;
duke@435 256 class RelocIterator;
duke@435 257
duke@435 258 class relocInfo VALUE_OBJ_CLASS_SPEC {
duke@435 259 friend class RelocIterator;
duke@435 260 public:
duke@435 261 enum relocType {
duke@435 262 none = 0, // Used when no relocation should be generated
duke@435 263 oop_type = 1, // embedded oop
duke@435 264 virtual_call_type = 2, // a standard inline cache call for a virtual send
duke@435 265 opt_virtual_call_type = 3, // a virtual call that has been statically bound (i.e., no IC cache)
duke@435 266 static_call_type = 4, // a static send
duke@435 267 static_stub_type = 5, // stub-entry for static send (takes care of interpreter case)
duke@435 268 runtime_call_type = 6, // call to fixed external routine
duke@435 269 external_word_type = 7, // reference to fixed external address
duke@435 270 internal_word_type = 8, // reference within the current code blob
duke@435 271 section_word_type = 9, // internal, but a cross-section reference
duke@435 272 poll_type = 10, // polling instruction for safepoints
duke@435 273 poll_return_type = 11, // polling instruction for safepoints at return
duke@435 274 breakpoint_type = 12, // an initialization barrier or safepoint
coleenp@4037 275 metadata_type = 13, // metadata that used to be oops
duke@435 276 yet_unused_type_2 = 14, // Still unused
duke@435 277 data_prefix_tag = 15, // tag for a prefix (carries data arguments)
duke@435 278 type_mask = 15 // A mask which selects only the above values
duke@435 279 };
duke@435 280
duke@435 281 protected:
duke@435 282 unsigned short _value;
duke@435 283
duke@435 284 enum RawBitsToken { RAW_BITS };
duke@435 285 relocInfo(relocType type, RawBitsToken ignore, int bits)
duke@435 286 : _value((type << nontype_width) + bits) { }
duke@435 287
duke@435 288 relocInfo(relocType type, RawBitsToken ignore, int off, int f)
duke@435 289 : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }
duke@435 290
duke@435 291 public:
duke@435 292 // constructor
duke@435 293 relocInfo(relocType type, int offset, int format = 0)
duke@435 294 #ifndef ASSERT
duke@435 295 {
duke@435 296 (*this) = relocInfo(type, RAW_BITS, offset, format);
duke@435 297 }
duke@435 298 #else
duke@435 299 // Put a bunch of assertions out-of-line.
duke@435 300 ;
duke@435 301 #endif
duke@435 302
duke@435 303 #define APPLY_TO_RELOCATIONS(visitor) \
duke@435 304 visitor(oop) \
coleenp@4037 305 visitor(metadata) \
duke@435 306 visitor(virtual_call) \
duke@435 307 visitor(opt_virtual_call) \
duke@435 308 visitor(static_call) \
duke@435 309 visitor(static_stub) \
duke@435 310 visitor(runtime_call) \
duke@435 311 visitor(external_word) \
duke@435 312 visitor(internal_word) \
duke@435 313 visitor(poll) \
duke@435 314 visitor(poll_return) \
duke@435 315 visitor(breakpoint) \
duke@435 316 visitor(section_word) \
duke@435 317
duke@435 318
duke@435 319 public:
duke@435 320 enum {
duke@435 321 value_width = sizeof(unsigned short) * BitsPerByte,
duke@435 322 type_width = 4, // == log2(type_mask+1)
duke@435 323 nontype_width = value_width - type_width,
duke@435 324 datalen_width = nontype_width-1,
duke@435 325 datalen_tag = 1 << datalen_width, // or-ed into _value
duke@435 326 datalen_limit = 1 << datalen_width,
duke@435 327 datalen_mask = (1 << datalen_width)-1
duke@435 328 };
duke@435 329
duke@435 330 // accessors
duke@435 331 public:
duke@435 332 relocType type() const { return (relocType)((unsigned)_value >> nontype_width); }
duke@435 333 int format() const { return format_mask==0? 0: format_mask &
duke@435 334 ((unsigned)_value >> offset_width); }
duke@435 335 int addr_offset() const { assert(!is_prefix(), "must have offset");
duke@435 336 return (_value & offset_mask)*offset_unit; }
duke@435 337
duke@435 338 protected:
duke@435 339 const short* data() const { assert(is_datalen(), "must have data");
duke@435 340 return (const short*)(this + 1); }
duke@435 341 int datalen() const { assert(is_datalen(), "must have data");
duke@435 342 return (_value & datalen_mask); }
duke@435 343 int immediate() const { assert(is_immediate(), "must have immed");
duke@435 344 return (_value & datalen_mask); }
duke@435 345 public:
duke@435 346 static int addr_unit() { return offset_unit; }
duke@435 347 static int offset_limit() { return (1 << offset_width) * offset_unit; }
duke@435 348
duke@435 349 void set_type(relocType type);
duke@435 350 void set_format(int format);
duke@435 351
duke@435 352 void remove() { set_type(none); }
duke@435 353
duke@435 354 protected:
duke@435 355 bool is_none() const { return type() == none; }
duke@435 356 bool is_prefix() const { return type() == data_prefix_tag; }
duke@435 357 bool is_datalen() const { assert(is_prefix(), "must be prefix");
duke@435 358 return (_value & datalen_tag) != 0; }
duke@435 359 bool is_immediate() const { assert(is_prefix(), "must be prefix");
duke@435 360 return (_value & datalen_tag) == 0; }
duke@435 361
duke@435 362 public:
duke@435 363 // Occasionally records of type relocInfo::none will appear in the stream.
duke@435 364 // We do not bother to filter these out, but clients should ignore them.
duke@435 365 // These records serve as "filler" in three ways:
duke@435 366 // - to skip large spans of unrelocated code (this is rare)
duke@435 367 // - to pad out the relocInfo array to the required oop alignment
duke@435 368 // - to disable old relocation information which is no longer applicable
duke@435 369
duke@435 370 inline friend relocInfo filler_relocInfo();
duke@435 371
duke@435 372 // Every non-prefix relocation may be preceded by at most one prefix,
duke@435 373 // which supplies 1 or more halfwords of associated data. Conventionally,
duke@435 374 // an int is represented by 0, 1, or 2 halfwords, depending on how
duke@435 375 // many bits are required to represent the value. (In addition,
duke@435 376 // if the sole halfword is a 10-bit unsigned number, it is made
duke@435 377 // "immediate" in the prefix header word itself. This optimization
duke@435 378 // is invisible outside this module.)
duke@435 379
duke@435 380 inline friend relocInfo prefix_relocInfo(int datalen = 0);
duke@435 381
duke@435 382 protected:
duke@435 383 // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
duke@435 384 static relocInfo immediate_relocInfo(int data0) {
duke@435 385 assert(fits_into_immediate(data0), "data0 in limits");
duke@435 386 return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
duke@435 387 }
duke@435 388 static bool fits_into_immediate(int data0) {
duke@435 389 return (data0 >= 0 && data0 < datalen_limit);
duke@435 390 }
duke@435 391
duke@435 392 public:
duke@435 393 // Support routines for compilers.
duke@435 394
duke@435 395 // This routine takes an infant relocInfo (unprefixed) and
duke@435 396 // edits in its prefix, if any. It also updates dest.locs_end.
duke@435 397 void initialize(CodeSection* dest, Relocation* reloc);
duke@435 398
duke@435 399 // This routine updates a prefix and returns the limit pointer.
duke@435 400 // It tries to compress the prefix from 32 to 16 bits, and if
duke@435 401 // successful returns a reduced "prefix_limit" pointer.
duke@435 402 relocInfo* finish_prefix(short* prefix_limit);
duke@435 403
duke@435 404 // bit-packers for the data array:
duke@435 405
duke@435 406 // As it happens, the bytes within the shorts are ordered natively,
duke@435 407 // but the shorts within the word are ordered big-endian.
duke@435 408 // This is an arbitrary choice, made this way mainly to ease debugging.
duke@435 409 static int data0_from_int(jint x) { return x >> value_width; }
duke@435 410 static int data1_from_int(jint x) { return (short)x; }
duke@435 411 static jint jint_from_data(short* data) {
duke@435 412 return (data[0] << value_width) + (unsigned short)data[1];
duke@435 413 }
duke@435 414
duke@435 415 static jint short_data_at(int n, short* data, int datalen) {
duke@435 416 return datalen > n ? data[n] : 0;
duke@435 417 }
duke@435 418
duke@435 419 static jint jint_data_at(int n, short* data, int datalen) {
duke@435 420 return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
duke@435 421 }
duke@435 422
duke@435 423 // Update methods for relocation information
duke@435 424 // (since code is dynamically patched, we also need to dynamically update the relocation info)
duke@435 425 // Both methods takes old_type, so it is able to performe sanity checks on the information removed.
duke@435 426 static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
duke@435 427 static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);
duke@435 428
duke@435 429 // Machine dependent stuff
stefank@2314 430 #ifdef TARGET_ARCH_x86
stefank@2314 431 # include "relocInfo_x86.hpp"
stefank@2314 432 #endif
stefank@2314 433 #ifdef TARGET_ARCH_sparc
stefank@2314 434 # include "relocInfo_sparc.hpp"
stefank@2314 435 #endif
stefank@2314 436 #ifdef TARGET_ARCH_zero
stefank@2314 437 # include "relocInfo_zero.hpp"
stefank@2314 438 #endif
bobv@2508 439 #ifdef TARGET_ARCH_arm
bobv@2508 440 # include "relocInfo_arm.hpp"
bobv@2508 441 #endif
bobv@2508 442 #ifdef TARGET_ARCH_ppc
bobv@2508 443 # include "relocInfo_ppc.hpp"
bobv@2508 444 #endif
stefank@2314 445
duke@435 446
duke@435 447 protected:
duke@435 448 // Derived constant, based on format_width which is PD:
duke@435 449 enum {
duke@435 450 offset_width = nontype_width - format_width,
duke@435 451 offset_mask = (1<<offset_width) - 1,
duke@435 452 format_mask = (1<<format_width) - 1
duke@435 453 };
duke@435 454 public:
duke@435 455 enum {
duke@435 456 // Conservatively large estimate of maximum length (in shorts)
duke@435 457 // of any relocation record (probably breakpoints are largest).
duke@435 458 // Extended format is length prefix, data words, and tag/offset suffix.
duke@435 459 length_limit = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
duke@435 460 have_format = format_width > 0
duke@435 461 };
duke@435 462 };
duke@435 463
duke@435 464 #define FORWARD_DECLARE_EACH_CLASS(name) \
duke@435 465 class name##_Relocation;
duke@435 466 APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
duke@435 467 #undef FORWARD_DECLARE_EACH_CLASS
duke@435 468
duke@435 469
duke@435 470
duke@435 471 inline relocInfo filler_relocInfo() {
duke@435 472 return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
duke@435 473 }
duke@435 474
duke@435 475 inline relocInfo prefix_relocInfo(int datalen) {
duke@435 476 assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
duke@435 477 return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
duke@435 478 }
duke@435 479
duke@435 480
duke@435 481 // Holder for flyweight relocation objects.
duke@435 482 // Although the flyweight subclasses are of varying sizes,
duke@435 483 // the holder is "one size fits all".
duke@435 484 class RelocationHolder VALUE_OBJ_CLASS_SPEC {
duke@435 485 friend class Relocation;
duke@435 486 friend class CodeSection;
duke@435 487
duke@435 488 private:
duke@435 489 // this preallocated memory must accommodate all subclasses of Relocation
duke@435 490 // (this number is assertion-checked in Relocation::operator new)
duke@435 491 enum { _relocbuf_size = 5 };
duke@435 492 void* _relocbuf[ _relocbuf_size ];
duke@435 493
duke@435 494 public:
duke@435 495 Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }
duke@435 496 inline relocInfo::relocType type() const;
duke@435 497
duke@435 498 // Add a constant offset to a relocation. Helper for class Address.
duke@435 499 RelocationHolder plus(int offset) const;
duke@435 500
duke@435 501 inline RelocationHolder(); // initializes type to none
duke@435 502
duke@435 503 inline RelocationHolder(Relocation* r); // make a copy
duke@435 504
duke@435 505 static const RelocationHolder none;
duke@435 506 };
duke@435 507
duke@435 508 // A RelocIterator iterates through the relocation information of a CodeBlob.
duke@435 509 // It is a variable BoundRelocation which is able to take on successive
duke@435 510 // values as it is advanced through a code stream.
duke@435 511 // Usage:
duke@435 512 // RelocIterator iter(nm);
duke@435 513 // while (iter.next()) {
duke@435 514 // iter.reloc()->some_operation();
duke@435 515 // }
duke@435 516 // or:
duke@435 517 // RelocIterator iter(nm);
duke@435 518 // while (iter.next()) {
duke@435 519 // switch (iter.type()) {
duke@435 520 // case relocInfo::oop_type :
duke@435 521 // case relocInfo::ic_type :
duke@435 522 // case relocInfo::prim_type :
duke@435 523 // case relocInfo::uncommon_type :
duke@435 524 // case relocInfo::runtime_call_type :
duke@435 525 // case relocInfo::internal_word_type:
duke@435 526 // case relocInfo::external_word_type:
duke@435 527 // ...
duke@435 528 // }
duke@435 529 // }
duke@435 530
duke@435 531 class RelocIterator : public StackObj {
twisti@2117 532 enum { SECT_LIMIT = 3 }; // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor
duke@435 533 friend class Relocation;
duke@435 534 friend class relocInfo; // for change_reloc_info_for_address only
duke@435 535 typedef relocInfo::relocType relocType;
duke@435 536
duke@435 537 private:
duke@435 538 address _limit; // stop producing relocations after this _addr
duke@435 539 relocInfo* _current; // the current relocation information
duke@435 540 relocInfo* _end; // end marker; we're done iterating when _current == _end
twisti@1918 541 nmethod* _code; // compiled method containing _addr
duke@435 542 address _addr; // instruction to which the relocation applies
duke@435 543 short _databuf; // spare buffer for compressed data
duke@435 544 short* _data; // pointer to the relocation's data
duke@435 545 short _datalen; // number of halfwords in _data
duke@435 546 char _format; // position within the instruction
duke@435 547
duke@435 548 // Base addresses needed to compute targets of section_word_type relocs.
duke@435 549 address _section_start[SECT_LIMIT];
twisti@2117 550 address _section_end [SECT_LIMIT];
duke@435 551
duke@435 552 void set_has_current(bool b) {
duke@435 553 _datalen = !b ? -1 : 0;
duke@435 554 debug_only(_data = NULL);
duke@435 555 }
duke@435 556 void set_current(relocInfo& ri) {
duke@435 557 _current = &ri;
duke@435 558 set_has_current(true);
duke@435 559 }
duke@435 560
duke@435 561 RelocationHolder _rh; // where the current relocation is allocated
duke@435 562
duke@435 563 relocInfo* current() const { assert(has_current(), "must have current");
duke@435 564 return _current; }
duke@435 565
duke@435 566 void set_limits(address begin, address limit);
duke@435 567
duke@435 568 void advance_over_prefix(); // helper method
duke@435 569
twisti@2117 570 void initialize_misc();
duke@435 571
twisti@1918 572 void initialize(nmethod* nm, address begin, address limit);
duke@435 573
duke@435 574 friend class PatchingRelocIterator;
duke@435 575 // make an uninitialized one, for PatchingRelocIterator:
duke@435 576 RelocIterator() { initialize_misc(); }
duke@435 577
duke@435 578 public:
duke@435 579 // constructor
twisti@1918 580 RelocIterator(nmethod* nm, address begin = NULL, address limit = NULL);
duke@435 581 RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);
duke@435 582
duke@435 583 // get next reloc info, return !eos
duke@435 584 bool next() {
duke@435 585 _current++;
duke@435 586 assert(_current <= _end, "must not overrun relocInfo");
duke@435 587 if (_current == _end) {
duke@435 588 set_has_current(false);
duke@435 589 return false;
duke@435 590 }
duke@435 591 set_has_current(true);
duke@435 592
duke@435 593 if (_current->is_prefix()) {
duke@435 594 advance_over_prefix();
duke@435 595 assert(!current()->is_prefix(), "only one prefix at a time");
duke@435 596 }
duke@435 597
duke@435 598 _addr += _current->addr_offset();
duke@435 599
duke@435 600 if (_limit != NULL && _addr >= _limit) {
duke@435 601 set_has_current(false);
duke@435 602 return false;
duke@435 603 }
duke@435 604
duke@435 605 if (relocInfo::have_format) _format = current()->format();
duke@435 606 return true;
duke@435 607 }
duke@435 608
duke@435 609 // accessors
duke@435 610 address limit() const { return _limit; }
duke@435 611 void set_limit(address x);
duke@435 612 relocType type() const { return current()->type(); }
duke@435 613 int format() const { return (relocInfo::have_format) ? current()->format() : 0; }
duke@435 614 address addr() const { return _addr; }
twisti@1918 615 nmethod* code() const { return _code; }
duke@435 616 short* data() const { return _data; }
duke@435 617 int datalen() const { return _datalen; }
duke@435 618 bool has_current() const { return _datalen >= 0; }
duke@435 619
duke@435 620 void set_addr(address addr) { _addr = addr; }
twisti@2117 621 bool addr_in_const() const;
duke@435 622
duke@435 623 address section_start(int n) const {
twisti@2117 624 assert(_section_start[n], "must be initialized");
twisti@2117 625 return _section_start[n];
twisti@2117 626 }
twisti@2117 627 address section_end(int n) const {
twisti@2117 628 assert(_section_end[n], "must be initialized");
twisti@2117 629 return _section_end[n];
duke@435 630 }
duke@435 631
duke@435 632 // The address points to the affected displacement part of the instruction.
duke@435 633 // For RISC, this is just the whole instruction.
duke@435 634 // For Intel, this is an unaligned 32-bit word.
duke@435 635
duke@435 636 // type-specific relocation accessors: oop_Relocation* oop_reloc(), etc.
duke@435 637 #define EACH_TYPE(name) \
duke@435 638 inline name##_Relocation* name##_reloc();
duke@435 639 APPLY_TO_RELOCATIONS(EACH_TYPE)
duke@435 640 #undef EACH_TYPE
duke@435 641 // generic relocation accessor; switches on type to call the above
duke@435 642 Relocation* reloc();
duke@435 643
duke@435 644 // CodeBlob's have relocation indexes for faster random access:
duke@435 645 static int locs_and_index_size(int code_size, int locs_size);
duke@435 646 // Store an index into [dest_start+dest_count..dest_end).
duke@435 647 // At dest_start[0..dest_count] is the actual relocation information.
duke@435 648 // Everything else up to dest_end is free space for the index.
duke@435 649 static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);
duke@435 650
duke@435 651 #ifndef PRODUCT
duke@435 652 public:
duke@435 653 void print();
duke@435 654 void print_current();
duke@435 655 #endif
duke@435 656 };
duke@435 657
duke@435 658
duke@435 659 // A Relocation is a flyweight object allocated within a RelocationHolder.
duke@435 660 // It represents the relocation data of relocation record.
duke@435 661 // So, the RelocIterator unpacks relocInfos into Relocations.
duke@435 662
duke@435 663 class Relocation VALUE_OBJ_CLASS_SPEC {
duke@435 664 friend class RelocationHolder;
duke@435 665 friend class RelocIterator;
duke@435 666
duke@435 667 private:
duke@435 668 static void guarantee_size();
duke@435 669
duke@435 670 // When a relocation has been created by a RelocIterator,
duke@435 671 // this field is non-null. It allows the relocation to know
duke@435 672 // its context, such as the address to which it applies.
duke@435 673 RelocIterator* _binding;
duke@435 674
duke@435 675 protected:
duke@435 676 RelocIterator* binding() const {
duke@435 677 assert(_binding != NULL, "must be bound");
duke@435 678 return _binding;
duke@435 679 }
duke@435 680 void set_binding(RelocIterator* b) {
duke@435 681 assert(_binding == NULL, "must be unbound");
duke@435 682 _binding = b;
duke@435 683 assert(_binding != NULL, "must now be bound");
duke@435 684 }
duke@435 685
duke@435 686 Relocation() {
duke@435 687 _binding = NULL;
duke@435 688 }
duke@435 689
duke@435 690 static RelocationHolder newHolder() {
duke@435 691 return RelocationHolder();
duke@435 692 }
duke@435 693
duke@435 694 public:
duke@435 695 void* operator new(size_t size, const RelocationHolder& holder) {
duke@435 696 if (size > sizeof(holder._relocbuf)) guarantee_size();
duke@435 697 assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");
duke@435 698 return holder.reloc();
duke@435 699 }
duke@435 700
duke@435 701 // make a generic relocation for a given type (if possible)
duke@435 702 static RelocationHolder spec_simple(relocInfo::relocType rtype);
duke@435 703
duke@435 704 // here is the type-specific hook which writes relocation data:
duke@435 705 virtual void pack_data_to(CodeSection* dest) { }
duke@435 706
duke@435 707 // here is the type-specific hook which reads (unpacks) relocation data:
duke@435 708 virtual void unpack_data() {
duke@435 709 assert(datalen()==0 || type()==relocInfo::none, "no data here");
duke@435 710 }
duke@435 711
never@2737 712 static bool is_reloc_index(intptr_t index) {
never@2737 713 return 0 < index && index < os::vm_page_size();
never@2737 714 }
never@2737 715
duke@435 716 protected:
duke@435 717 // Helper functions for pack_data_to() and unpack_data().
duke@435 718
duke@435 719 // Most of the compression logic is confined here.
duke@435 720 // (The "immediate data" mechanism of relocInfo works independently
duke@435 721 // of this stuff, and acts to further compress most 1-word data prefixes.)
duke@435 722
duke@435 723 // A variable-width int is encoded as a short if it will fit in 16 bits.
duke@435 724 // The decoder looks at datalen to decide whether to unpack short or jint.
duke@435 725 // Most relocation records are quite simple, containing at most two ints.
duke@435 726
duke@435 727 static bool is_short(jint x) { return x == (short)x; }
duke@435 728 static short* add_short(short* p, int x) { *p++ = x; return p; }
duke@435 729 static short* add_jint (short* p, jint x) {
duke@435 730 *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
duke@435 731 return p;
duke@435 732 }
duke@435 733 static short* add_var_int(short* p, jint x) { // add a variable-width int
duke@435 734 if (is_short(x)) p = add_short(p, x);
duke@435 735 else p = add_jint (p, x);
duke@435 736 return p;
duke@435 737 }
duke@435 738
duke@435 739 static short* pack_1_int_to(short* p, jint x0) {
duke@435 740 // Format is one of: [] [x] [Xx]
duke@435 741 if (x0 != 0) p = add_var_int(p, x0);
duke@435 742 return p;
duke@435 743 }
duke@435 744 int unpack_1_int() {
duke@435 745 assert(datalen() <= 2, "too much data");
duke@435 746 return relocInfo::jint_data_at(0, data(), datalen());
duke@435 747 }
duke@435 748
duke@435 749 // With two ints, the short form is used only if both ints are short.
duke@435 750 short* pack_2_ints_to(short* p, jint x0, jint x1) {
duke@435 751 // Format is one of: [] [x y?] [Xx Y?y]
duke@435 752 if (x0 == 0 && x1 == 0) {
duke@435 753 // no halfwords needed to store zeroes
duke@435 754 } else if (is_short(x0) && is_short(x1)) {
duke@435 755 // 1-2 halfwords needed to store shorts
duke@435 756 p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);
duke@435 757 } else {
duke@435 758 // 3-4 halfwords needed to store jints
duke@435 759 p = add_jint(p, x0); p = add_var_int(p, x1);
duke@435 760 }
duke@435 761 return p;
duke@435 762 }
duke@435 763 void unpack_2_ints(jint& x0, jint& x1) {
duke@435 764 int dlen = datalen();
duke@435 765 short* dp = data();
duke@435 766 if (dlen <= 2) {
duke@435 767 x0 = relocInfo::short_data_at(0, dp, dlen);
duke@435 768 x1 = relocInfo::short_data_at(1, dp, dlen);
duke@435 769 } else {
duke@435 770 assert(dlen <= 4, "too much data");
duke@435 771 x0 = relocInfo::jint_data_at(0, dp, dlen);
duke@435 772 x1 = relocInfo::jint_data_at(2, dp, dlen);
duke@435 773 }
duke@435 774 }
duke@435 775
duke@435 776 protected:
duke@435 777 // platform-dependent utilities for decoding and patching instructions
never@2657 778 void pd_set_data_value (address x, intptr_t off, bool verify_only = false); // a set or mem-ref
never@2657 779 void pd_verify_data_value (address x, intptr_t off) { pd_set_data_value(x, off, true); }
duke@435 780 address pd_call_destination (address orig_addr = NULL);
duke@435 781 void pd_set_call_destination (address x);
duke@435 782 void pd_swap_in_breakpoint (address x, short* instrs, int instrlen);
duke@435 783 void pd_swap_out_breakpoint (address x, short* instrs, int instrlen);
duke@435 784 static int pd_breakpoint_size ();
duke@435 785
duke@435 786 // this extracts the address of an address in the code stream instead of the reloc data
duke@435 787 address* pd_address_in_code ();
duke@435 788
duke@435 789 // this extracts an address from the code stream instead of the reloc data
duke@435 790 address pd_get_address_from_code ();
duke@435 791
duke@435 792 // these convert from byte offsets, to scaled offsets, to addresses
duke@435 793 static jint scaled_offset(address x, address base) {
duke@435 794 int byte_offset = x - base;
duke@435 795 int offset = -byte_offset / relocInfo::addr_unit();
duke@435 796 assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
duke@435 797 return offset;
duke@435 798 }
duke@435 799 static jint scaled_offset_null_special(address x, address base) {
duke@435 800 // Some relocations treat offset=0 as meaning NULL.
duke@435 801 // Handle this extra convention carefully.
duke@435 802 if (x == NULL) return 0;
duke@435 803 assert(x != base, "offset must not be zero");
duke@435 804 return scaled_offset(x, base);
duke@435 805 }
duke@435 806 static address address_from_scaled_offset(jint offset, address base) {
duke@435 807 int byte_offset = -( offset * relocInfo::addr_unit() );
duke@435 808 return base + byte_offset;
duke@435 809 }
duke@435 810
duke@435 811 // these convert between indexes and addresses in the runtime system
duke@435 812 static int32_t runtime_address_to_index(address runtime_address);
duke@435 813 static address index_to_runtime_address(int32_t index);
duke@435 814
duke@435 815 // helpers for mapping between old and new addresses after a move or resize
duke@435 816 address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
duke@435 817 address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
duke@435 818 void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);
duke@435 819
duke@435 820 public:
duke@435 821 // accessors which only make sense for a bound Relocation
twisti@1918 822 address addr() const { return binding()->addr(); }
twisti@1918 823 nmethod* code() const { return binding()->code(); }
twisti@1918 824 bool addr_in_const() const { return binding()->addr_in_const(); }
duke@435 825 protected:
duke@435 826 short* data() const { return binding()->data(); }
duke@435 827 int datalen() const { return binding()->datalen(); }
duke@435 828 int format() const { return binding()->format(); }
duke@435 829
duke@435 830 public:
duke@435 831 virtual relocInfo::relocType type() { return relocInfo::none; }
duke@435 832
duke@435 833 // is it a call instruction?
duke@435 834 virtual bool is_call() { return false; }
duke@435 835
duke@435 836 // is it a data movement instruction?
duke@435 837 virtual bool is_data() { return false; }
duke@435 838
duke@435 839 // some relocations can compute their own values
duke@435 840 virtual address value();
duke@435 841
duke@435 842 // all relocations are able to reassert their values
duke@435 843 virtual void set_value(address x);
duke@435 844
duke@435 845 virtual void clear_inline_cache() { }
duke@435 846
duke@435 847 // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
duke@435 848 // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is
duke@435 849 // probably a reasonable assumption, since empty caches simplifies code reloacation.
duke@435 850 virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }
duke@435 851
duke@435 852 void print();
duke@435 853 };
duke@435 854
duke@435 855
duke@435 856 // certain inlines must be deferred until class Relocation is defined:
duke@435 857
duke@435 858 inline RelocationHolder::RelocationHolder() {
duke@435 859 // initialize the vtbl, just to keep things type-safe
duke@435 860 new(*this) Relocation();
duke@435 861 }
duke@435 862
duke@435 863
duke@435 864 inline RelocationHolder::RelocationHolder(Relocation* r) {
duke@435 865 // wordwise copy from r (ok if it copies garbage after r)
duke@435 866 for (int i = 0; i < _relocbuf_size; i++) {
duke@435 867 _relocbuf[i] = ((void**)r)[i];
duke@435 868 }
duke@435 869 }
duke@435 870
duke@435 871
duke@435 872 relocInfo::relocType RelocationHolder::type() const {
duke@435 873 return reloc()->type();
duke@435 874 }
duke@435 875
duke@435 876 // A DataRelocation always points at a memory or load-constant instruction..
duke@435 877 // It is absolute on most machines, and the constant is split on RISCs.
duke@435 878 // The specific subtypes are oop, external_word, and internal_word.
duke@435 879 // By convention, the "value" does not include a separately reckoned "offset".
duke@435 880 class DataRelocation : public Relocation {
duke@435 881 public:
duke@435 882 bool is_data() { return true; }
duke@435 883
duke@435 884 // both target and offset must be computed somehow from relocation data
duke@435 885 virtual int offset() { return 0; }
duke@435 886 address value() = 0;
duke@435 887 void set_value(address x) { set_value(x, offset()); }
duke@435 888 void set_value(address x, intptr_t o) {
duke@435 889 if (addr_in_const())
duke@435 890 *(address*)addr() = x;
duke@435 891 else
duke@435 892 pd_set_data_value(x, o);
duke@435 893 }
never@2657 894 void verify_value(address x) {
never@2657 895 if (addr_in_const())
never@2657 896 assert(*(address*)addr() == x, "must agree");
never@2657 897 else
never@2657 898 pd_verify_data_value(x, offset());
never@2657 899 }
duke@435 900
duke@435 901 // The "o" (displacement) argument is relevant only to split relocations
duke@435 902 // on RISC machines. In some CPUs (SPARC), the set-hi and set-lo ins'ns
duke@435 903 // can encode more than 32 bits between them. This allows compilers to
duke@435 904 // share set-hi instructions between addresses that differ by a small
duke@435 905 // offset (e.g., different static variables in the same class).
duke@435 906 // On such machines, the "x" argument to set_value on all set-lo
duke@435 907 // instructions must be the same as the "x" argument for the
duke@435 908 // corresponding set-hi instructions. The "o" arguments for the
duke@435 909 // set-hi instructions are ignored, and must not affect the high-half
duke@435 910 // immediate constant. The "o" arguments for the set-lo instructions are
duke@435 911 // added into the low-half immediate constant, and must not overflow it.
duke@435 912 };
duke@435 913
duke@435 914 // A CallRelocation always points at a call instruction.
duke@435 915 // It is PC-relative on most machines.
duke@435 916 class CallRelocation : public Relocation {
duke@435 917 public:
duke@435 918 bool is_call() { return true; }
duke@435 919
duke@435 920 address destination() { return pd_call_destination(); }
duke@435 921 void set_destination(address x); // pd_set_call_destination
duke@435 922
duke@435 923 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
duke@435 924 address value() { return destination(); }
duke@435 925 void set_value(address x) { set_destination(x); }
duke@435 926 };
duke@435 927
duke@435 928 class oop_Relocation : public DataRelocation {
duke@435 929 relocInfo::relocType type() { return relocInfo::oop_type; }
duke@435 930
duke@435 931 public:
duke@435 932 // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll]
duke@435 933 // an oop in the CodeBlob's oop pool
duke@435 934 static RelocationHolder spec(int oop_index, int offset = 0) {
duke@435 935 assert(oop_index > 0, "must be a pool-resident oop");
duke@435 936 RelocationHolder rh = newHolder();
duke@435 937 new(rh) oop_Relocation(oop_index, offset);
duke@435 938 return rh;
duke@435 939 }
duke@435 940 // an oop in the instruction stream
duke@435 941 static RelocationHolder spec_for_immediate() {
duke@435 942 const int oop_index = 0;
duke@435 943 const int offset = 0; // if you want an offset, use the oop pool
duke@435 944 RelocationHolder rh = newHolder();
duke@435 945 new(rh) oop_Relocation(oop_index, offset);
duke@435 946 return rh;
duke@435 947 }
duke@435 948
duke@435 949 private:
duke@435 950 jint _oop_index; // if > 0, index into CodeBlob::oop_at
duke@435 951 jint _offset; // byte offset to apply to the oop itself
duke@435 952
duke@435 953 oop_Relocation(int oop_index, int offset) {
duke@435 954 _oop_index = oop_index; _offset = offset;
duke@435 955 }
duke@435 956
duke@435 957 friend class RelocIterator;
duke@435 958 oop_Relocation() { }
duke@435 959
duke@435 960 public:
duke@435 961 int oop_index() { return _oop_index; }
duke@435 962 int offset() { return _offset; }
duke@435 963
duke@435 964 // data is packed in "2_ints" format: [i o] or [Ii Oo]
duke@435 965 void pack_data_to(CodeSection* dest);
duke@435 966 void unpack_data();
duke@435 967
duke@435 968 void fix_oop_relocation(); // reasserts oop value
duke@435 969
never@2657 970 void verify_oop_relocation();
never@2657 971
duke@435 972 address value() { return (address) *oop_addr(); }
duke@435 973
duke@435 974 bool oop_is_immediate() { return oop_index() == 0; }
duke@435 975
duke@435 976 oop* oop_addr(); // addr or &pool[jint_data]
duke@435 977 oop oop_value(); // *oop_addr
duke@435 978 // Note: oop_value transparently converts Universe::non_oop_word to NULL.
duke@435 979 };
duke@435 980
coleenp@4037 981
coleenp@4037 982 // copy of oop_Relocation for now but may delete stuff in both/either
coleenp@4037 983 class metadata_Relocation : public DataRelocation {
coleenp@4037 984 relocInfo::relocType type() { return relocInfo::metadata_type; }
coleenp@4037 985
coleenp@4037 986 public:
coleenp@4037 987 // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll]
coleenp@4037 988 // an metadata in the CodeBlob's metadata pool
coleenp@4037 989 static RelocationHolder spec(int metadata_index, int offset = 0) {
coleenp@4037 990 assert(metadata_index > 0, "must be a pool-resident metadata");
coleenp@4037 991 RelocationHolder rh = newHolder();
coleenp@4037 992 new(rh) metadata_Relocation(metadata_index, offset);
coleenp@4037 993 return rh;
coleenp@4037 994 }
coleenp@4037 995 // an metadata in the instruction stream
coleenp@4037 996 static RelocationHolder spec_for_immediate() {
coleenp@4037 997 const int metadata_index = 0;
coleenp@4037 998 const int offset = 0; // if you want an offset, use the metadata pool
coleenp@4037 999 RelocationHolder rh = newHolder();
coleenp@4037 1000 new(rh) metadata_Relocation(metadata_index, offset);
coleenp@4037 1001 return rh;
coleenp@4037 1002 }
coleenp@4037 1003
coleenp@4037 1004 private:
coleenp@4037 1005 jint _metadata_index; // if > 0, index into nmethod::metadata_at
coleenp@4037 1006 jint _offset; // byte offset to apply to the metadata itself
coleenp@4037 1007
coleenp@4037 1008 metadata_Relocation(int metadata_index, int offset) {
coleenp@4037 1009 _metadata_index = metadata_index; _offset = offset;
coleenp@4037 1010 }
coleenp@4037 1011
coleenp@4037 1012 friend class RelocIterator;
coleenp@4037 1013 metadata_Relocation() { }
coleenp@4037 1014
coleenp@4037 1015 // Fixes a Metadata pointer in the code. Most platforms embeds the
coleenp@4037 1016 // Metadata pointer in the code at compile time so this is empty
coleenp@4037 1017 // for them.
coleenp@4037 1018 void pd_fix_value(address x);
coleenp@4037 1019
coleenp@4037 1020 public:
coleenp@4037 1021 int metadata_index() { return _metadata_index; }
coleenp@4037 1022 int offset() { return _offset; }
coleenp@4037 1023
coleenp@4037 1024 // data is packed in "2_ints" format: [i o] or [Ii Oo]
coleenp@4037 1025 void pack_data_to(CodeSection* dest);
coleenp@4037 1026 void unpack_data();
coleenp@4037 1027
coleenp@4037 1028 void fix_metadata_relocation(); // reasserts metadata value
coleenp@4037 1029
coleenp@4037 1030 void verify_metadata_relocation();
coleenp@4037 1031
coleenp@4037 1032 address value() { return (address) *metadata_addr(); }
coleenp@4037 1033
coleenp@4037 1034 bool metadata_is_immediate() { return metadata_index() == 0; }
coleenp@4037 1035
coleenp@4037 1036 Metadata** metadata_addr(); // addr or &pool[jint_data]
coleenp@4037 1037 Metadata* metadata_value(); // *metadata_addr
coleenp@4037 1038 // Note: metadata_value transparently converts Universe::non_metadata_word to NULL.
coleenp@4037 1039 };
coleenp@4037 1040
coleenp@4037 1041
duke@435 1042 class virtual_call_Relocation : public CallRelocation {
duke@435 1043 relocInfo::relocType type() { return relocInfo::virtual_call_type; }
duke@435 1044
duke@435 1045 public:
coleenp@4037 1046 // "cached_value" points to the first associated set-oop.
duke@435 1047 // The oop_limit helps find the last associated set-oop.
duke@435 1048 // (See comments at the top of this file.)
coleenp@4037 1049 static RelocationHolder spec(address cached_value) {
duke@435 1050 RelocationHolder rh = newHolder();
coleenp@4037 1051 new(rh) virtual_call_Relocation(cached_value);
duke@435 1052 return rh;
duke@435 1053 }
duke@435 1054
coleenp@4037 1055 virtual_call_Relocation(address cached_value) {
coleenp@4037 1056 _cached_value = cached_value;
coleenp@4037 1057 assert(cached_value != NULL, "first oop address must be specified");
duke@435 1058 }
duke@435 1059
duke@435 1060 private:
coleenp@4037 1061 address _cached_value; // location of set-value instruction
duke@435 1062
duke@435 1063 friend class RelocIterator;
duke@435 1064 virtual_call_Relocation() { }
duke@435 1065
duke@435 1066
duke@435 1067 public:
coleenp@4037 1068 address cached_value();
duke@435 1069
duke@435 1070 // data is packed as scaled offsets in "2_ints" format: [f l] or [Ff Ll]
duke@435 1071 // oop_limit is set to 0 if the limit falls somewhere within the call.
duke@435 1072 // When unpacking, a zero oop_limit is taken to refer to the end of the call.
duke@435 1073 // (This has the effect of bringing in the call's delay slot on SPARC.)
duke@435 1074 void pack_data_to(CodeSection* dest);
duke@435 1075 void unpack_data();
duke@435 1076
duke@435 1077 void clear_inline_cache();
duke@435 1078 };
duke@435 1079
duke@435 1080
duke@435 1081 class opt_virtual_call_Relocation : public CallRelocation {
duke@435 1082 relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }
duke@435 1083
duke@435 1084 public:
duke@435 1085 static RelocationHolder spec() {
duke@435 1086 RelocationHolder rh = newHolder();
duke@435 1087 new(rh) opt_virtual_call_Relocation();
duke@435 1088 return rh;
duke@435 1089 }
duke@435 1090
duke@435 1091 private:
duke@435 1092 friend class RelocIterator;
duke@435 1093 opt_virtual_call_Relocation() { }
duke@435 1094
duke@435 1095 public:
duke@435 1096 void clear_inline_cache();
duke@435 1097
duke@435 1098 // find the matching static_stub
duke@435 1099 address static_stub();
duke@435 1100 };
duke@435 1101
duke@435 1102
duke@435 1103 class static_call_Relocation : public CallRelocation {
duke@435 1104 relocInfo::relocType type() { return relocInfo::static_call_type; }
duke@435 1105
duke@435 1106 public:
duke@435 1107 static RelocationHolder spec() {
duke@435 1108 RelocationHolder rh = newHolder();
duke@435 1109 new(rh) static_call_Relocation();
duke@435 1110 return rh;
duke@435 1111 }
duke@435 1112
duke@435 1113 private:
duke@435 1114 friend class RelocIterator;
duke@435 1115 static_call_Relocation() { }
duke@435 1116
duke@435 1117 public:
duke@435 1118 void clear_inline_cache();
duke@435 1119
duke@435 1120 // find the matching static_stub
duke@435 1121 address static_stub();
duke@435 1122 };
duke@435 1123
duke@435 1124 class static_stub_Relocation : public Relocation {
duke@435 1125 relocInfo::relocType type() { return relocInfo::static_stub_type; }
duke@435 1126
duke@435 1127 public:
duke@435 1128 static RelocationHolder spec(address static_call) {
duke@435 1129 RelocationHolder rh = newHolder();
duke@435 1130 new(rh) static_stub_Relocation(static_call);
duke@435 1131 return rh;
duke@435 1132 }
duke@435 1133
duke@435 1134 private:
duke@435 1135 address _static_call; // location of corresponding static_call
duke@435 1136
duke@435 1137 static_stub_Relocation(address static_call) {
duke@435 1138 _static_call = static_call;
duke@435 1139 }
duke@435 1140
duke@435 1141 friend class RelocIterator;
duke@435 1142 static_stub_Relocation() { }
duke@435 1143
duke@435 1144 public:
duke@435 1145 void clear_inline_cache();
duke@435 1146
duke@435 1147 address static_call() { return _static_call; }
duke@435 1148
duke@435 1149 // data is packed as a scaled offset in "1_int" format: [c] or [Cc]
duke@435 1150 void pack_data_to(CodeSection* dest);
duke@435 1151 void unpack_data();
duke@435 1152 };
duke@435 1153
duke@435 1154 class runtime_call_Relocation : public CallRelocation {
duke@435 1155 relocInfo::relocType type() { return relocInfo::runtime_call_type; }
duke@435 1156
duke@435 1157 public:
duke@435 1158 static RelocationHolder spec() {
duke@435 1159 RelocationHolder rh = newHolder();
duke@435 1160 new(rh) runtime_call_Relocation();
duke@435 1161 return rh;
duke@435 1162 }
duke@435 1163
duke@435 1164 private:
duke@435 1165 friend class RelocIterator;
duke@435 1166 runtime_call_Relocation() { }
duke@435 1167
duke@435 1168 public:
duke@435 1169 };
duke@435 1170
duke@435 1171 class external_word_Relocation : public DataRelocation {
duke@435 1172 relocInfo::relocType type() { return relocInfo::external_word_type; }
duke@435 1173
duke@435 1174 public:
duke@435 1175 static RelocationHolder spec(address target) {
duke@435 1176 assert(target != NULL, "must not be null");
duke@435 1177 RelocationHolder rh = newHolder();
duke@435 1178 new(rh) external_word_Relocation(target);
duke@435 1179 return rh;
duke@435 1180 }
duke@435 1181
duke@435 1182 // Use this one where all 32/64 bits of the target live in the code stream.
duke@435 1183 // The target must be an intptr_t, and must be absolute (not relative).
duke@435 1184 static RelocationHolder spec_for_immediate() {
duke@435 1185 RelocationHolder rh = newHolder();
duke@435 1186 new(rh) external_word_Relocation(NULL);
duke@435 1187 return rh;
duke@435 1188 }
duke@435 1189
never@2737 1190 // Some address looking values aren't safe to treat as relocations
never@2737 1191 // and should just be treated as constants.
never@2737 1192 static bool can_be_relocated(address target) {
never@2737 1193 return target != NULL && !is_reloc_index((intptr_t)target);
never@2737 1194 }
never@2737 1195
duke@435 1196 private:
duke@435 1197 address _target; // address in runtime
duke@435 1198
duke@435 1199 external_word_Relocation(address target) {
duke@435 1200 _target = target;
duke@435 1201 }
duke@435 1202
duke@435 1203 friend class RelocIterator;
duke@435 1204 external_word_Relocation() { }
duke@435 1205
duke@435 1206 public:
duke@435 1207 // data is packed as a well-known address in "1_int" format: [a] or [Aa]
duke@435 1208 // The function runtime_address_to_index is used to turn full addresses
duke@435 1209 // to short indexes, if they are pre-registered by the stub mechanism.
duke@435 1210 // If the "a" value is 0 (i.e., _target is NULL), the address is stored
duke@435 1211 // in the code stream. See external_word_Relocation::target().
duke@435 1212 void pack_data_to(CodeSection* dest);
duke@435 1213 void unpack_data();
duke@435 1214
duke@435 1215 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
duke@435 1216 address target(); // if _target==NULL, fetch addr from code stream
duke@435 1217 address value() { return target(); }
duke@435 1218 };
duke@435 1219
duke@435 1220 class internal_word_Relocation : public DataRelocation {
duke@435 1221 relocInfo::relocType type() { return relocInfo::internal_word_type; }
duke@435 1222
duke@435 1223 public:
duke@435 1224 static RelocationHolder spec(address target) {
duke@435 1225 assert(target != NULL, "must not be null");
duke@435 1226 RelocationHolder rh = newHolder();
duke@435 1227 new(rh) internal_word_Relocation(target);
duke@435 1228 return rh;
duke@435 1229 }
duke@435 1230
duke@435 1231 // use this one where all the bits of the target can fit in the code stream:
duke@435 1232 static RelocationHolder spec_for_immediate() {
duke@435 1233 RelocationHolder rh = newHolder();
duke@435 1234 new(rh) internal_word_Relocation(NULL);
duke@435 1235 return rh;
duke@435 1236 }
duke@435 1237
duke@435 1238 internal_word_Relocation(address target) {
duke@435 1239 _target = target;
duke@435 1240 _section = -1; // self-relative
duke@435 1241 }
duke@435 1242
duke@435 1243 protected:
duke@435 1244 address _target; // address in CodeBlob
duke@435 1245 int _section; // section providing base address, if any
duke@435 1246
duke@435 1247 friend class RelocIterator;
duke@435 1248 internal_word_Relocation() { }
duke@435 1249
duke@435 1250 // bit-width of LSB field in packed offset, if section >= 0
duke@435 1251 enum { section_width = 2 }; // must equal CodeBuffer::sect_bits
duke@435 1252
duke@435 1253 public:
duke@435 1254 // data is packed as a scaled offset in "1_int" format: [o] or [Oo]
duke@435 1255 // If the "o" value is 0 (i.e., _target is NULL), the offset is stored
duke@435 1256 // in the code stream. See internal_word_Relocation::target().
duke@435 1257 // If _section is not -1, it is appended to the low bits of the offset.
duke@435 1258 void pack_data_to(CodeSection* dest);
duke@435 1259 void unpack_data();
duke@435 1260
duke@435 1261 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
duke@435 1262 address target(); // if _target==NULL, fetch addr from code stream
duke@435 1263 int section() { return _section; }
duke@435 1264 address value() { return target(); }
duke@435 1265 };
duke@435 1266
duke@435 1267 class section_word_Relocation : public internal_word_Relocation {
duke@435 1268 relocInfo::relocType type() { return relocInfo::section_word_type; }
duke@435 1269
duke@435 1270 public:
duke@435 1271 static RelocationHolder spec(address target, int section) {
duke@435 1272 RelocationHolder rh = newHolder();
duke@435 1273 new(rh) section_word_Relocation(target, section);
duke@435 1274 return rh;
duke@435 1275 }
duke@435 1276
duke@435 1277 section_word_Relocation(address target, int section) {
duke@435 1278 assert(target != NULL, "must not be null");
duke@435 1279 assert(section >= 0, "must be a valid section");
duke@435 1280 _target = target;
duke@435 1281 _section = section;
duke@435 1282 }
duke@435 1283
duke@435 1284 //void pack_data_to -- inherited
duke@435 1285 void unpack_data();
duke@435 1286
duke@435 1287 private:
duke@435 1288 friend class RelocIterator;
duke@435 1289 section_word_Relocation() { }
duke@435 1290 };
duke@435 1291
duke@435 1292
duke@435 1293 class poll_Relocation : public Relocation {
duke@435 1294 bool is_data() { return true; }
duke@435 1295 relocInfo::relocType type() { return relocInfo::poll_type; }
never@739 1296 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
duke@435 1297 };
duke@435 1298
duke@435 1299 class poll_return_Relocation : public Relocation {
duke@435 1300 bool is_data() { return true; }
duke@435 1301 relocInfo::relocType type() { return relocInfo::poll_return_type; }
never@739 1302 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
duke@435 1303 };
duke@435 1304
duke@435 1305
duke@435 1306 class breakpoint_Relocation : public Relocation {
duke@435 1307 relocInfo::relocType type() { return relocInfo::breakpoint_type; }
duke@435 1308
duke@435 1309 enum {
duke@435 1310 // attributes which affect the interpretation of the data:
duke@435 1311 removable_attr = 0x0010, // buffer [i...] allows for undoing the trap
duke@435 1312 internal_attr = 0x0020, // the target is an internal addr (local stub)
duke@435 1313 settable_attr = 0x0040, // the target is settable
duke@435 1314
duke@435 1315 // states which can change over time:
duke@435 1316 enabled_state = 0x0100, // breakpoint must be active in running code
duke@435 1317 active_state = 0x0200, // breakpoint instruction actually in code
duke@435 1318
duke@435 1319 kind_mask = 0x000F, // mask for extracting kind
duke@435 1320 high_bit = 0x4000 // extra bit which is always set
duke@435 1321 };
duke@435 1322
duke@435 1323 public:
duke@435 1324 enum {
duke@435 1325 // kinds:
duke@435 1326 initialization = 1,
duke@435 1327 safepoint = 2
duke@435 1328 };
duke@435 1329
duke@435 1330 // If target is NULL, 32 bits are reserved for a later set_target().
duke@435 1331 static RelocationHolder spec(int kind, address target = NULL, bool internal_target = false) {
duke@435 1332 RelocationHolder rh = newHolder();
duke@435 1333 new(rh) breakpoint_Relocation(kind, target, internal_target);
duke@435 1334 return rh;
duke@435 1335 }
duke@435 1336
duke@435 1337 private:
duke@435 1338 // We require every bits value to NOT to fit into relocInfo::datalen_width,
duke@435 1339 // because we are going to actually store state in the reloc, and so
duke@435 1340 // cannot allow it to be compressed (and hence copied by the iterator).
duke@435 1341
duke@435 1342 short _bits; // bit-encoded kind, attrs, & state
duke@435 1343 address _target;
duke@435 1344
duke@435 1345 breakpoint_Relocation(int kind, address target, bool internal_target);
duke@435 1346
duke@435 1347 friend class RelocIterator;
duke@435 1348 breakpoint_Relocation() { }
duke@435 1349
duke@435 1350 short bits() const { return _bits; }
duke@435 1351 short& live_bits() const { return data()[0]; }
duke@435 1352 short* instrs() const { return data() + datalen() - instrlen(); }
duke@435 1353 int instrlen() const { return removable() ? pd_breakpoint_size() : 0; }
duke@435 1354
duke@435 1355 void set_bits(short x) {
duke@435 1356 assert(live_bits() == _bits, "must be the only mutator of reloc info");
duke@435 1357 live_bits() = _bits = x;
duke@435 1358 }
duke@435 1359
duke@435 1360 public:
duke@435 1361 address target() const;
duke@435 1362 void set_target(address x);
duke@435 1363
duke@435 1364 int kind() const { return bits() & kind_mask; }
duke@435 1365 bool enabled() const { return (bits() & enabled_state) != 0; }
duke@435 1366 bool active() const { return (bits() & active_state) != 0; }
duke@435 1367 bool internal() const { return (bits() & internal_attr) != 0; }
duke@435 1368 bool removable() const { return (bits() & removable_attr) != 0; }
duke@435 1369 bool settable() const { return (bits() & settable_attr) != 0; }
duke@435 1370
duke@435 1371 void set_enabled(bool b); // to activate, you must also say set_active
duke@435 1372 void set_active(bool b); // actually inserts bpt (must be enabled 1st)
duke@435 1373
duke@435 1374 // data is packed as 16 bits, followed by the target (1 or 2 words), followed
duke@435 1375 // if necessary by empty storage for saving away original instruction bytes.
duke@435 1376 void pack_data_to(CodeSection* dest);
duke@435 1377 void unpack_data();
duke@435 1378
duke@435 1379 // during certain operations, breakpoints must be out of the way:
duke@435 1380 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
duke@435 1381 assert(!active(), "cannot perform relocation on enabled breakpoints");
duke@435 1382 }
duke@435 1383 };
duke@435 1384
duke@435 1385
duke@435 1386 // We know all the xxx_Relocation classes, so now we can define these:
duke@435 1387 #define EACH_CASE(name) \
duke@435 1388 inline name##_Relocation* RelocIterator::name##_reloc() { \
duke@435 1389 assert(type() == relocInfo::name##_type, "type must agree"); \
duke@435 1390 /* The purpose of the placed "new" is to re-use the same */ \
duke@435 1391 /* stack storage for each new iteration. */ \
duke@435 1392 name##_Relocation* r = new(_rh) name##_Relocation(); \
duke@435 1393 r->set_binding(this); \
duke@435 1394 r->name##_Relocation::unpack_data(); \
duke@435 1395 return r; \
duke@435 1396 }
duke@435 1397 APPLY_TO_RELOCATIONS(EACH_CASE);
duke@435 1398 #undef EACH_CASE
duke@435 1399
twisti@1918 1400 inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {
twisti@1918 1401 initialize(nm, begin, limit);
duke@435 1402 }
duke@435 1403
duke@435 1404 // if you are going to patch code, you should use this subclass of
duke@435 1405 // RelocIterator
duke@435 1406 class PatchingRelocIterator : public RelocIterator {
duke@435 1407 private:
duke@435 1408 RelocIterator _init_state;
duke@435 1409
duke@435 1410 void prepass(); // deactivates all breakpoints
duke@435 1411 void postpass(); // reactivates all enabled breakpoints
duke@435 1412
duke@435 1413 // do not copy these puppies; it would have unpredictable side effects
duke@435 1414 // these are private and have no bodies defined because they should not be called
duke@435 1415 PatchingRelocIterator(const RelocIterator&);
duke@435 1416 void operator=(const RelocIterator&);
duke@435 1417
duke@435 1418 public:
twisti@1918 1419 PatchingRelocIterator(nmethod* nm, address begin = NULL, address limit = NULL)
twisti@1918 1420 : RelocIterator(nm, begin, limit) { prepass(); }
duke@435 1421
duke@435 1422 ~PatchingRelocIterator() { postpass(); }
duke@435 1423 };
stefank@2314 1424
stefank@2314 1425 #endif // SHARE_VM_CODE_RELOCINFO_HPP

mercurial