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