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