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