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