src/share/vm/code/relocInfo.hpp

changeset 0
f90c822e73f8
child 1
2d8a650513c2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/code/relocInfo.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,1343 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_CODE_RELOCINFO_HPP
    1.29 +#define SHARE_VM_CODE_RELOCINFO_HPP
    1.30 +
    1.31 +#include "memory/allocation.hpp"
    1.32 +#include "utilities/top.hpp"
    1.33 +
    1.34 +class NativeMovConstReg;
    1.35 +
    1.36 +// Types in this file:
    1.37 +//    relocInfo
    1.38 +//      One element of an array of halfwords encoding compressed relocations.
    1.39 +//      Also, the source of relocation types (relocInfo::oop_type, ...).
    1.40 +//    Relocation
    1.41 +//      A flyweight object representing a single relocation.
    1.42 +//      It is fully unpacked from the compressed relocation array.
    1.43 +//    metadata_Relocation, ... (subclasses of Relocation)
    1.44 +//      The location of some type-specific operations (metadata_addr, ...).
    1.45 +//      Also, the source of relocation specs (metadata_Relocation::spec, ...).
    1.46 +//    oop_Relocation, ... (subclasses of Relocation)
    1.47 +//      oops in the code stream (strings, class loaders)
    1.48 +//      Also, the source of relocation specs (oop_Relocation::spec, ...).
    1.49 +//    RelocationHolder
    1.50 +//      A ValueObj type which acts as a union holding a Relocation object.
    1.51 +//      Represents a relocation spec passed into a CodeBuffer during assembly.
    1.52 +//    RelocIterator
    1.53 +//      A StackObj which iterates over the relocations associated with
    1.54 +//      a range of code addresses.  Can be used to operate a copy of code.
    1.55 +//    BoundRelocation
    1.56 +//      An _internal_ type shared by packers and unpackers of relocations.
    1.57 +//      It pastes together a RelocationHolder with some pointers into
    1.58 +//      code and relocInfo streams.
    1.59 +
    1.60 +
    1.61 +// Notes on relocType:
    1.62 +//
    1.63 +// These hold enough information to read or write a value embedded in
    1.64 +// the instructions of an CodeBlob.  They're used to update:
    1.65 +//
    1.66 +//   1) embedded oops     (isOop()          == true)
    1.67 +//   2) inline caches     (isIC()           == true)
    1.68 +//   3) runtime calls     (isRuntimeCall()  == true)
    1.69 +//   4) internal word ref (isInternalWord() == true)
    1.70 +//   5) external word ref (isExternalWord() == true)
    1.71 +//
    1.72 +// when objects move (GC) or if code moves (compacting the code heap).
    1.73 +// They are also used to patch the code (if a call site must change)
    1.74 +//
    1.75 +// A relocInfo is represented in 16 bits:
    1.76 +//   4 bits indicating the relocation type
    1.77 +//  12 bits indicating the offset from the previous relocInfo address
    1.78 +//
    1.79 +// The offsets accumulate along the relocInfo stream to encode the
    1.80 +// address within the CodeBlob, which is named RelocIterator::addr().
    1.81 +// The address of a particular relocInfo always points to the first
    1.82 +// byte of the relevant instruction (and not to any of its subfields
    1.83 +// or embedded immediate constants).
    1.84 +//
    1.85 +// The offset value is scaled appropriately for the target machine.
    1.86 +// (See relocInfo_<arch>.hpp for the offset scaling.)
    1.87 +//
    1.88 +// On some machines, there may also be a "format" field which may provide
    1.89 +// additional information about the format of the instruction stream
    1.90 +// at the corresponding code address.  The format value is usually zero.
    1.91 +// Any machine (such as Intel) whose instructions can sometimes contain
    1.92 +// more than one relocatable constant needs format codes to distinguish
    1.93 +// which operand goes with a given relocation.
    1.94 +//
    1.95 +// If the target machine needs N format bits, the offset has 12-N bits,
    1.96 +// the format is encoded between the offset and the type, and the
    1.97 +// relocInfo_<arch>.hpp file has manifest constants for the format codes.
    1.98 +//
    1.99 +// If the type is "data_prefix_tag" then the offset bits are further encoded,
   1.100 +// and in fact represent not a code-stream offset but some inline data.
   1.101 +// The data takes the form of a counted sequence of halfwords, which
   1.102 +// precedes the actual relocation record.  (Clients never see it directly.)
   1.103 +// The interpetation of this extra data depends on the relocation type.
   1.104 +//
   1.105 +// On machines that have 32-bit immediate fields, there is usually
   1.106 +// little need for relocation "prefix" data, because the instruction stream
   1.107 +// is a perfectly reasonable place to store the value.  On machines in
   1.108 +// which 32-bit values must be "split" across instructions, the relocation
   1.109 +// data is the "true" specification of the value, which is then applied
   1.110 +// to some field of the instruction (22 or 13 bits, on SPARC).
   1.111 +//
   1.112 +// Whenever the location of the CodeBlob changes, any PC-relative
   1.113 +// relocations, and any internal_word_type relocations, must be reapplied.
   1.114 +// After the GC runs, oop_type relocations must be reapplied.
   1.115 +//
   1.116 +//
   1.117 +// Here are meanings of the types:
   1.118 +//
   1.119 +// relocInfo::none -- a filler record
   1.120 +//   Value:  none
   1.121 +//   Instruction: The corresponding code address is ignored
   1.122 +//   Data:  Any data prefix and format code are ignored
   1.123 +//   (This means that any relocInfo can be disabled by setting
   1.124 +//   its type to none.  See relocInfo::remove.)
   1.125 +//
   1.126 +// relocInfo::oop_type, relocInfo::metadata_type -- a reference to an oop or meta data
   1.127 +//   Value:  an oop, or else the address (handle) of an oop
   1.128 +//   Instruction types: memory (load), set (load address)
   1.129 +//   Data:  []       an oop stored in 4 bytes of instruction
   1.130 +//          [n]      n is the index of an oop in the CodeBlob's oop pool
   1.131 +//          [[N]n l] and l is a byte offset to be applied to the oop
   1.132 +//          [Nn Ll]  both index and offset may be 32 bits if necessary
   1.133 +//   Here is a special hack, used only by the old compiler:
   1.134 +//          [[N]n 00] the value is the __address__ of the nth oop in the pool
   1.135 +//   (Note that the offset allows optimal references to class variables.)
   1.136 +//
   1.137 +// relocInfo::internal_word_type -- an address within the same CodeBlob
   1.138 +// relocInfo::section_word_type -- same, but can refer to another section
   1.139 +//   Value:  an address in the CodeBlob's code or constants section
   1.140 +//   Instruction types: memory (load), set (load address)
   1.141 +//   Data:  []     stored in 4 bytes of instruction
   1.142 +//          [[L]l] a relative offset (see [About Offsets] below)
   1.143 +//   In the case of section_word_type, the offset is relative to a section
   1.144 +//   base address, and the section number (e.g., SECT_INSTS) is encoded
   1.145 +//   into the low two bits of the offset L.
   1.146 +//
   1.147 +// relocInfo::external_word_type -- a fixed address in the runtime system
   1.148 +//   Value:  an address
   1.149 +//   Instruction types: memory (load), set (load address)
   1.150 +//   Data:  []   stored in 4 bytes of instruction
   1.151 +//          [n]  the index of a "well-known" stub (usual case on RISC)
   1.152 +//          [Ll] a 32-bit address
   1.153 +//
   1.154 +// relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
   1.155 +//   Value:  an address
   1.156 +//   Instruction types: PC-relative call (or a PC-relative branch)
   1.157 +//   Data:  []   stored in 4 bytes of instruction
   1.158 +//
   1.159 +// relocInfo::static_call_type -- a static call
   1.160 +//   Value:  an CodeBlob, a stub, or a fixup routine
   1.161 +//   Instruction types: a call
   1.162 +//   Data:  []
   1.163 +//   The identity of the callee is extracted from debugging information.
   1.164 +//   //%note reloc_3
   1.165 +//
   1.166 +// relocInfo::virtual_call_type -- a virtual call site (which includes an inline
   1.167 +//                                 cache)
   1.168 +//   Value:  an CodeBlob, a stub, the interpreter, or a fixup routine
   1.169 +//   Instruction types: a call, plus some associated set-oop instructions
   1.170 +//   Data:  []       the associated set-oops are adjacent to the call
   1.171 +//          [n]      n is a relative offset to the first set-oop
   1.172 +//          [[N]n l] and l is a limit within which the set-oops occur
   1.173 +//          [Nn Ll]  both n and l may be 32 bits if necessary
   1.174 +//   The identity of the callee is extracted from debugging information.
   1.175 +//
   1.176 +// relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
   1.177 +//
   1.178 +//    Same info as a static_call_type. We use a special type, so the handling of
   1.179 +//    virtuals and statics are separated.
   1.180 +//
   1.181 +//
   1.182 +//   The offset n points to the first set-oop.  (See [About Offsets] below.)
   1.183 +//   In turn, the set-oop instruction specifies or contains an oop cell devoted
   1.184 +//   exclusively to the IC call, which can be patched along with the call.
   1.185 +//
   1.186 +//   The locations of any other set-oops are found by searching the relocation
   1.187 +//   information starting at the first set-oop, and continuing until all
   1.188 +//   relocations up through l have been inspected.  The value l is another
   1.189 +//   relative offset.  (Both n and l are relative to the call's first byte.)
   1.190 +//
   1.191 +//   The limit l of the search is exclusive.  However, if it points within
   1.192 +//   the call (e.g., offset zero), it is adjusted to point after the call and
   1.193 +//   any associated machine-specific delay slot.
   1.194 +//
   1.195 +//   Since the offsets could be as wide as 32-bits, these conventions
   1.196 +//   put no restrictions whatever upon code reorganization.
   1.197 +//
   1.198 +//   The compiler is responsible for ensuring that transition from a clean
   1.199 +//   state to a monomorphic compiled state is MP-safe.  This implies that
   1.200 +//   the system must respond well to intermediate states where a random
   1.201 +//   subset of the set-oops has been correctly from the clean state
   1.202 +//   upon entry to the VEP of the compiled method.  In the case of a
   1.203 +//   machine (Intel) with a single set-oop instruction, the 32-bit
   1.204 +//   immediate field must not straddle a unit of memory coherence.
   1.205 +//   //%note reloc_3
   1.206 +//
   1.207 +// relocInfo::static_stub_type -- an extra stub for each static_call_type
   1.208 +//   Value:  none
   1.209 +//   Instruction types: a virtual call:  { set_oop; jump; }
   1.210 +//   Data:  [[N]n]  the offset of the associated static_call reloc
   1.211 +//   This stub becomes the target of a static call which must be upgraded
   1.212 +//   to a virtual call (because the callee is interpreted).
   1.213 +//   See [About Offsets] below.
   1.214 +//   //%note reloc_2
   1.215 +//
   1.216 +// For example:
   1.217 +//
   1.218 +//   INSTRUCTIONS                        RELOC: TYPE    PREFIX DATA
   1.219 +//   ------------                               ----    -----------
   1.220 +// sethi      %hi(myObject),  R               oop_type [n(myObject)]
   1.221 +// ld      [R+%lo(myObject)+fldOffset], R2    oop_type [n(myObject) fldOffset]
   1.222 +// add R2, 1, R2
   1.223 +// st  R2, [R+%lo(myObject)+fldOffset]        oop_type [n(myObject) fldOffset]
   1.224 +//%note reloc_1
   1.225 +//
   1.226 +// This uses 4 instruction words, 8 relocation halfwords,
   1.227 +// and an entry (which is sharable) in the CodeBlob's oop pool,
   1.228 +// for a total of 36 bytes.
   1.229 +//
   1.230 +// Note that the compiler is responsible for ensuring the "fldOffset" when
   1.231 +// added to "%lo(myObject)" does not overflow the immediate fields of the
   1.232 +// memory instructions.
   1.233 +//
   1.234 +//
   1.235 +// [About Offsets] Relative offsets are supplied to this module as
   1.236 +// positive byte offsets, but they may be internally stored scaled
   1.237 +// and/or negated, depending on what is most compact for the target
   1.238 +// system.  Since the object pointed to by the offset typically
   1.239 +// precedes the relocation address, it is profitable to store
   1.240 +// these negative offsets as positive numbers, but this decision
   1.241 +// is internal to the relocation information abstractions.
   1.242 +//
   1.243 +
   1.244 +class Relocation;
   1.245 +class CodeBuffer;
   1.246 +class CodeSection;
   1.247 +class RelocIterator;
   1.248 +
   1.249 +class relocInfo VALUE_OBJ_CLASS_SPEC {
   1.250 +  friend class RelocIterator;
   1.251 + public:
   1.252 +  enum relocType {
   1.253 +    none                    =  0, // Used when no relocation should be generated
   1.254 +    oop_type                =  1, // embedded oop
   1.255 +    virtual_call_type       =  2, // a standard inline cache call for a virtual send
   1.256 +    opt_virtual_call_type   =  3, // a virtual call that has been statically bound (i.e., no IC cache)
   1.257 +    static_call_type        =  4, // a static send
   1.258 +    static_stub_type        =  5, // stub-entry for static send  (takes care of interpreter case)
   1.259 +    runtime_call_type       =  6, // call to fixed external routine
   1.260 +    external_word_type      =  7, // reference to fixed external address
   1.261 +    internal_word_type      =  8, // reference within the current code blob
   1.262 +    section_word_type       =  9, // internal, but a cross-section reference
   1.263 +    poll_type               = 10, // polling instruction for safepoints
   1.264 +    poll_return_type        = 11, // polling instruction for safepoints at return
   1.265 +    metadata_type           = 12, // metadata that used to be oops
   1.266 +    trampoline_stub_type    = 13, // stub-entry for trampoline
   1.267 +    yet_unused_type_1       = 14, // Still unused
   1.268 +    data_prefix_tag         = 15, // tag for a prefix (carries data arguments)
   1.269 +    type_mask               = 15  // A mask which selects only the above values
   1.270 +  };
   1.271 +
   1.272 + protected:
   1.273 +  unsigned short _value;
   1.274 +
   1.275 +  enum RawBitsToken { RAW_BITS };
   1.276 +  relocInfo(relocType type, RawBitsToken ignore, int bits)
   1.277 +    : _value((type << nontype_width) + bits) { }
   1.278 +
   1.279 +  relocInfo(relocType type, RawBitsToken ignore, int off, int f)
   1.280 +    : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }
   1.281 +
   1.282 + public:
   1.283 +  // constructor
   1.284 +  relocInfo(relocType type, int offset, int format = 0)
   1.285 +#ifndef ASSERT
   1.286 +  {
   1.287 +    (*this) = relocInfo(type, RAW_BITS, offset, format);
   1.288 +  }
   1.289 +#else
   1.290 +  // Put a bunch of assertions out-of-line.
   1.291 +  ;
   1.292 +#endif
   1.293 +
   1.294 +  #define APPLY_TO_RELOCATIONS(visitor) \
   1.295 +    visitor(oop) \
   1.296 +    visitor(metadata) \
   1.297 +    visitor(virtual_call) \
   1.298 +    visitor(opt_virtual_call) \
   1.299 +    visitor(static_call) \
   1.300 +    visitor(static_stub) \
   1.301 +    visitor(runtime_call) \
   1.302 +    visitor(external_word) \
   1.303 +    visitor(internal_word) \
   1.304 +    visitor(poll) \
   1.305 +    visitor(poll_return) \
   1.306 +    visitor(section_word) \
   1.307 +    visitor(trampoline_stub) \
   1.308 +
   1.309 +
   1.310 + public:
   1.311 +  enum {
   1.312 +    value_width             = sizeof(unsigned short) * BitsPerByte,
   1.313 +    type_width              = 4,   // == log2(type_mask+1)
   1.314 +    nontype_width           = value_width - type_width,
   1.315 +    datalen_width           = nontype_width-1,
   1.316 +    datalen_tag             = 1 << datalen_width,  // or-ed into _value
   1.317 +    datalen_limit           = 1 << datalen_width,
   1.318 +    datalen_mask            = (1 << datalen_width)-1
   1.319 +  };
   1.320 +
   1.321 +  // accessors
   1.322 + public:
   1.323 +  relocType  type()       const { return (relocType)((unsigned)_value >> nontype_width); }
   1.324 +  int  format()           const { return format_mask==0? 0: format_mask &
   1.325 +                                         ((unsigned)_value >> offset_width); }
   1.326 +  int  addr_offset()      const { assert(!is_prefix(), "must have offset");
   1.327 +                                  return (_value & offset_mask)*offset_unit; }
   1.328 +
   1.329 + protected:
   1.330 +  const short* data()     const { assert(is_datalen(), "must have data");
   1.331 +                                  return (const short*)(this + 1); }
   1.332 +  int          datalen()  const { assert(is_datalen(), "must have data");
   1.333 +                                  return (_value & datalen_mask); }
   1.334 +  int         immediate() const { assert(is_immediate(), "must have immed");
   1.335 +                                  return (_value & datalen_mask); }
   1.336 + public:
   1.337 +  static int addr_unit()        { return offset_unit; }
   1.338 +  static int offset_limit()     { return (1 << offset_width) * offset_unit; }
   1.339 +
   1.340 +  void set_type(relocType type);
   1.341 +  void set_format(int format);
   1.342 +
   1.343 +  void remove() { set_type(none); }
   1.344 +
   1.345 + protected:
   1.346 +  bool is_none()                const { return type() == none; }
   1.347 +  bool is_prefix()              const { return type() == data_prefix_tag; }
   1.348 +  bool is_datalen()             const { assert(is_prefix(), "must be prefix");
   1.349 +                                        return (_value & datalen_tag) != 0; }
   1.350 +  bool is_immediate()           const { assert(is_prefix(), "must be prefix");
   1.351 +                                        return (_value & datalen_tag) == 0; }
   1.352 +
   1.353 + public:
   1.354 +  // Occasionally records of type relocInfo::none will appear in the stream.
   1.355 +  // We do not bother to filter these out, but clients should ignore them.
   1.356 +  // These records serve as "filler" in three ways:
   1.357 +  //  - to skip large spans of unrelocated code (this is rare)
   1.358 +  //  - to pad out the relocInfo array to the required oop alignment
   1.359 +  //  - to disable old relocation information which is no longer applicable
   1.360 +
   1.361 +  inline friend relocInfo filler_relocInfo();
   1.362 +
   1.363 +  // Every non-prefix relocation may be preceded by at most one prefix,
   1.364 +  // which supplies 1 or more halfwords of associated data.  Conventionally,
   1.365 +  // an int is represented by 0, 1, or 2 halfwords, depending on how
   1.366 +  // many bits are required to represent the value.  (In addition,
   1.367 +  // if the sole halfword is a 10-bit unsigned number, it is made
   1.368 +  // "immediate" in the prefix header word itself.  This optimization
   1.369 +  // is invisible outside this module.)
   1.370 +
   1.371 +  inline friend relocInfo prefix_relocInfo(int datalen);
   1.372 +
   1.373 + protected:
   1.374 +  // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
   1.375 +  static relocInfo immediate_relocInfo(int data0) {
   1.376 +    assert(fits_into_immediate(data0), "data0 in limits");
   1.377 +    return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
   1.378 +  }
   1.379 +  static bool fits_into_immediate(int data0) {
   1.380 +    return (data0 >= 0 && data0 < datalen_limit);
   1.381 +  }
   1.382 +
   1.383 + public:
   1.384 +  // Support routines for compilers.
   1.385 +
   1.386 +  // This routine takes an infant relocInfo (unprefixed) and
   1.387 +  // edits in its prefix, if any.  It also updates dest.locs_end.
   1.388 +  void initialize(CodeSection* dest, Relocation* reloc);
   1.389 +
   1.390 +  // This routine updates a prefix and returns the limit pointer.
   1.391 +  // It tries to compress the prefix from 32 to 16 bits, and if
   1.392 +  // successful returns a reduced "prefix_limit" pointer.
   1.393 +  relocInfo* finish_prefix(short* prefix_limit);
   1.394 +
   1.395 +  // bit-packers for the data array:
   1.396 +
   1.397 +  // As it happens, the bytes within the shorts are ordered natively,
   1.398 +  // but the shorts within the word are ordered big-endian.
   1.399 +  // This is an arbitrary choice, made this way mainly to ease debugging.
   1.400 +  static int data0_from_int(jint x)         { return x >> value_width; }
   1.401 +  static int data1_from_int(jint x)         { return (short)x; }
   1.402 +  static jint jint_from_data(short* data) {
   1.403 +    return (data[0] << value_width) + (unsigned short)data[1];
   1.404 +  }
   1.405 +
   1.406 +  static jint short_data_at(int n, short* data, int datalen) {
   1.407 +    return datalen > n ? data[n] : 0;
   1.408 +  }
   1.409 +
   1.410 +  static jint jint_data_at(int n, short* data, int datalen) {
   1.411 +    return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
   1.412 +  }
   1.413 +
   1.414 +  // Update methods for relocation information
   1.415 +  // (since code is dynamically patched, we also need to dynamically update the relocation info)
   1.416 +  // Both methods takes old_type, so it is able to performe sanity checks on the information removed.
   1.417 +  static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
   1.418 +  static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);
   1.419 +
   1.420 +  // Machine dependent stuff
   1.421 +#ifdef TARGET_ARCH_x86
   1.422 +# include "relocInfo_x86.hpp"
   1.423 +#endif
   1.424 +#ifdef TARGET_ARCH_sparc
   1.425 +# include "relocInfo_sparc.hpp"
   1.426 +#endif
   1.427 +#ifdef TARGET_ARCH_zero
   1.428 +# include "relocInfo_zero.hpp"
   1.429 +#endif
   1.430 +#ifdef TARGET_ARCH_arm
   1.431 +# include "relocInfo_arm.hpp"
   1.432 +#endif
   1.433 +#ifdef TARGET_ARCH_ppc
   1.434 +# include "relocInfo_ppc.hpp"
   1.435 +#endif
   1.436 +
   1.437 +
   1.438 + protected:
   1.439 +  // Derived constant, based on format_width which is PD:
   1.440 +  enum {
   1.441 +    offset_width       = nontype_width - format_width,
   1.442 +    offset_mask        = (1<<offset_width) - 1,
   1.443 +    format_mask        = (1<<format_width) - 1
   1.444 +  };
   1.445 + public:
   1.446 +  enum {
   1.447 +    // Conservatively large estimate of maximum length (in shorts)
   1.448 +    // of any relocation record.
   1.449 +    // Extended format is length prefix, data words, and tag/offset suffix.
   1.450 +    length_limit       = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
   1.451 +    have_format        = format_width > 0
   1.452 +  };
   1.453 +};
   1.454 +
   1.455 +#define FORWARD_DECLARE_EACH_CLASS(name)              \
   1.456 +class name##_Relocation;
   1.457 +APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
   1.458 +#undef FORWARD_DECLARE_EACH_CLASS
   1.459 +
   1.460 +
   1.461 +
   1.462 +inline relocInfo filler_relocInfo() {
   1.463 +  return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
   1.464 +}
   1.465 +
   1.466 +inline relocInfo prefix_relocInfo(int datalen = 0) {
   1.467 +  assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
   1.468 +  return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
   1.469 +}
   1.470 +
   1.471 +
   1.472 +// Holder for flyweight relocation objects.
   1.473 +// Although the flyweight subclasses are of varying sizes,
   1.474 +// the holder is "one size fits all".
   1.475 +class RelocationHolder VALUE_OBJ_CLASS_SPEC {
   1.476 +  friend class Relocation;
   1.477 +  friend class CodeSection;
   1.478 +
   1.479 + private:
   1.480 +  // this preallocated memory must accommodate all subclasses of Relocation
   1.481 +  // (this number is assertion-checked in Relocation::operator new)
   1.482 +  enum { _relocbuf_size = 5 };
   1.483 +  void* _relocbuf[ _relocbuf_size ];
   1.484 +
   1.485 + public:
   1.486 +  Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }
   1.487 +  inline relocInfo::relocType type() const;
   1.488 +
   1.489 +  // Add a constant offset to a relocation.  Helper for class Address.
   1.490 +  RelocationHolder plus(int offset) const;
   1.491 +
   1.492 +  inline RelocationHolder();                // initializes type to none
   1.493 +
   1.494 +  inline RelocationHolder(Relocation* r);   // make a copy
   1.495 +
   1.496 +  static const RelocationHolder none;
   1.497 +};
   1.498 +
   1.499 +// A RelocIterator iterates through the relocation information of a CodeBlob.
   1.500 +// It is a variable BoundRelocation which is able to take on successive
   1.501 +// values as it is advanced through a code stream.
   1.502 +// Usage:
   1.503 +//   RelocIterator iter(nm);
   1.504 +//   while (iter.next()) {
   1.505 +//     iter.reloc()->some_operation();
   1.506 +//   }
   1.507 +// or:
   1.508 +//   RelocIterator iter(nm);
   1.509 +//   while (iter.next()) {
   1.510 +//     switch (iter.type()) {
   1.511 +//      case relocInfo::oop_type          :
   1.512 +//      case relocInfo::ic_type           :
   1.513 +//      case relocInfo::prim_type         :
   1.514 +//      case relocInfo::uncommon_type     :
   1.515 +//      case relocInfo::runtime_call_type :
   1.516 +//      case relocInfo::internal_word_type:
   1.517 +//      case relocInfo::external_word_type:
   1.518 +//      ...
   1.519 +//     }
   1.520 +//   }
   1.521 +
   1.522 +class RelocIterator : public StackObj {
   1.523 +  enum { SECT_LIMIT = 3 };  // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor
   1.524 +  friend class Relocation;
   1.525 +  friend class relocInfo;       // for change_reloc_info_for_address only
   1.526 +  typedef relocInfo::relocType relocType;
   1.527 +
   1.528 + private:
   1.529 +  address    _limit;   // stop producing relocations after this _addr
   1.530 +  relocInfo* _current; // the current relocation information
   1.531 +  relocInfo* _end;     // end marker; we're done iterating when _current == _end
   1.532 +  nmethod*   _code;    // compiled method containing _addr
   1.533 +  address    _addr;    // instruction to which the relocation applies
   1.534 +  short      _databuf; // spare buffer for compressed data
   1.535 +  short*     _data;    // pointer to the relocation's data
   1.536 +  short      _datalen; // number of halfwords in _data
   1.537 +  char       _format;  // position within the instruction
   1.538 +
   1.539 +  // Base addresses needed to compute targets of section_word_type relocs.
   1.540 +  address    _section_start[SECT_LIMIT];
   1.541 +  address    _section_end  [SECT_LIMIT];
   1.542 +
   1.543 +  void set_has_current(bool b) {
   1.544 +    _datalen = !b ? -1 : 0;
   1.545 +    debug_only(_data = NULL);
   1.546 +  }
   1.547 +  void set_current(relocInfo& ri) {
   1.548 +    _current = &ri;
   1.549 +    set_has_current(true);
   1.550 +  }
   1.551 +
   1.552 +  RelocationHolder _rh; // where the current relocation is allocated
   1.553 +
   1.554 +  relocInfo* current() const { assert(has_current(), "must have current");
   1.555 +                               return _current; }
   1.556 +
   1.557 +  void set_limits(address begin, address limit);
   1.558 +
   1.559 +  void advance_over_prefix();    // helper method
   1.560 +
   1.561 +  void initialize_misc();
   1.562 +
   1.563 +  void initialize(nmethod* nm, address begin, address limit);
   1.564 +
   1.565 +  RelocIterator() { initialize_misc(); }
   1.566 +
   1.567 + public:
   1.568 +  // constructor
   1.569 +  RelocIterator(nmethod* nm,     address begin = NULL, address limit = NULL);
   1.570 +  RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);
   1.571 +
   1.572 +  // get next reloc info, return !eos
   1.573 +  bool next() {
   1.574 +    _current++;
   1.575 +    assert(_current <= _end, "must not overrun relocInfo");
   1.576 +    if (_current == _end) {
   1.577 +      set_has_current(false);
   1.578 +      return false;
   1.579 +    }
   1.580 +    set_has_current(true);
   1.581 +
   1.582 +    if (_current->is_prefix()) {
   1.583 +      advance_over_prefix();
   1.584 +      assert(!current()->is_prefix(), "only one prefix at a time");
   1.585 +    }
   1.586 +
   1.587 +    _addr += _current->addr_offset();
   1.588 +
   1.589 +    if (_limit != NULL && _addr >= _limit) {
   1.590 +      set_has_current(false);
   1.591 +      return false;
   1.592 +    }
   1.593 +
   1.594 +    if (relocInfo::have_format)  _format = current()->format();
   1.595 +    return true;
   1.596 +  }
   1.597 +
   1.598 +  // accessors
   1.599 +  address      limit()        const { return _limit; }
   1.600 +  void     set_limit(address x);
   1.601 +  relocType    type()         const { return current()->type(); }
   1.602 +  int          format()       const { return (relocInfo::have_format) ? current()->format() : 0; }
   1.603 +  address      addr()         const { return _addr; }
   1.604 +  nmethod*     code()         const { return _code; }
   1.605 +  short*       data()         const { return _data; }
   1.606 +  int          datalen()      const { return _datalen; }
   1.607 +  bool     has_current()      const { return _datalen >= 0; }
   1.608 +
   1.609 +  void       set_addr(address addr) { _addr = addr; }
   1.610 +  bool   addr_in_const()      const;
   1.611 +
   1.612 +  address section_start(int n) const {
   1.613 +    assert(_section_start[n], "must be initialized");
   1.614 +    return _section_start[n];
   1.615 +  }
   1.616 +  address section_end(int n) const {
   1.617 +    assert(_section_end[n], "must be initialized");
   1.618 +    return _section_end[n];
   1.619 +  }
   1.620 +
   1.621 +  // The address points to the affected displacement part of the instruction.
   1.622 +  // For RISC, this is just the whole instruction.
   1.623 +  // For Intel, this is an unaligned 32-bit word.
   1.624 +
   1.625 +  // type-specific relocation accessors:  oop_Relocation* oop_reloc(), etc.
   1.626 +  #define EACH_TYPE(name)                               \
   1.627 +  inline name##_Relocation* name##_reloc();
   1.628 +  APPLY_TO_RELOCATIONS(EACH_TYPE)
   1.629 +  #undef EACH_TYPE
   1.630 +  // generic relocation accessor; switches on type to call the above
   1.631 +  Relocation* reloc();
   1.632 +
   1.633 +  // CodeBlob's have relocation indexes for faster random access:
   1.634 +  static int locs_and_index_size(int code_size, int locs_size);
   1.635 +  // Store an index into [dest_start+dest_count..dest_end).
   1.636 +  // At dest_start[0..dest_count] is the actual relocation information.
   1.637 +  // Everything else up to dest_end is free space for the index.
   1.638 +  static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);
   1.639 +
   1.640 +#ifndef PRODUCT
   1.641 + public:
   1.642 +  void print();
   1.643 +  void print_current();
   1.644 +#endif
   1.645 +};
   1.646 +
   1.647 +
   1.648 +// A Relocation is a flyweight object allocated within a RelocationHolder.
   1.649 +// It represents the relocation data of relocation record.
   1.650 +// So, the RelocIterator unpacks relocInfos into Relocations.
   1.651 +
   1.652 +class Relocation VALUE_OBJ_CLASS_SPEC {
   1.653 +  friend class RelocationHolder;
   1.654 +  friend class RelocIterator;
   1.655 +
   1.656 + private:
   1.657 +  static void guarantee_size();
   1.658 +
   1.659 +  // When a relocation has been created by a RelocIterator,
   1.660 +  // this field is non-null.  It allows the relocation to know
   1.661 +  // its context, such as the address to which it applies.
   1.662 +  RelocIterator* _binding;
   1.663 +
   1.664 + protected:
   1.665 +  RelocIterator* binding() const {
   1.666 +    assert(_binding != NULL, "must be bound");
   1.667 +    return _binding;
   1.668 +  }
   1.669 +  void set_binding(RelocIterator* b) {
   1.670 +    assert(_binding == NULL, "must be unbound");
   1.671 +    _binding = b;
   1.672 +    assert(_binding != NULL, "must now be bound");
   1.673 +  }
   1.674 +
   1.675 +  Relocation() {
   1.676 +    _binding = NULL;
   1.677 +  }
   1.678 +
   1.679 +  static RelocationHolder newHolder() {
   1.680 +    return RelocationHolder();
   1.681 +  }
   1.682 +
   1.683 + public:
   1.684 +  void* operator new(size_t size, const RelocationHolder& holder) throw() {
   1.685 +    if (size > sizeof(holder._relocbuf)) guarantee_size();
   1.686 +    assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");
   1.687 +    return holder.reloc();
   1.688 +  }
   1.689 +
   1.690 +  // make a generic relocation for a given type (if possible)
   1.691 +  static RelocationHolder spec_simple(relocInfo::relocType rtype);
   1.692 +
   1.693 +  // here is the type-specific hook which writes relocation data:
   1.694 +  virtual void pack_data_to(CodeSection* dest) { }
   1.695 +
   1.696 +  // here is the type-specific hook which reads (unpacks) relocation data:
   1.697 +  virtual void unpack_data() {
   1.698 +    assert(datalen()==0 || type()==relocInfo::none, "no data here");
   1.699 +  }
   1.700 +
   1.701 +  static bool is_reloc_index(intptr_t index) {
   1.702 +    return 0 < index && index < os::vm_page_size();
   1.703 +  }
   1.704 +
   1.705 + protected:
   1.706 +  // Helper functions for pack_data_to() and unpack_data().
   1.707 +
   1.708 +  // Most of the compression logic is confined here.
   1.709 +  // (The "immediate data" mechanism of relocInfo works independently
   1.710 +  // of this stuff, and acts to further compress most 1-word data prefixes.)
   1.711 +
   1.712 +  // A variable-width int is encoded as a short if it will fit in 16 bits.
   1.713 +  // The decoder looks at datalen to decide whether to unpack short or jint.
   1.714 +  // Most relocation records are quite simple, containing at most two ints.
   1.715 +
   1.716 +  static bool is_short(jint x) { return x == (short)x; }
   1.717 +  static short* add_short(short* p, int x)  { *p++ = x; return p; }
   1.718 +  static short* add_jint (short* p, jint x) {
   1.719 +    *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
   1.720 +    return p;
   1.721 +  }
   1.722 +  static short* add_var_int(short* p, jint x) {   // add a variable-width int
   1.723 +    if (is_short(x))  p = add_short(p, x);
   1.724 +    else              p = add_jint (p, x);
   1.725 +    return p;
   1.726 +  }
   1.727 +
   1.728 +  static short* pack_1_int_to(short* p, jint x0) {
   1.729 +    // Format is one of:  [] [x] [Xx]
   1.730 +    if (x0 != 0)  p = add_var_int(p, x0);
   1.731 +    return p;
   1.732 +  }
   1.733 +  int unpack_1_int() {
   1.734 +    assert(datalen() <= 2, "too much data");
   1.735 +    return relocInfo::jint_data_at(0, data(), datalen());
   1.736 +  }
   1.737 +
   1.738 +  // With two ints, the short form is used only if both ints are short.
   1.739 +  short* pack_2_ints_to(short* p, jint x0, jint x1) {
   1.740 +    // Format is one of:  [] [x y?] [Xx Y?y]
   1.741 +    if (x0 == 0 && x1 == 0) {
   1.742 +      // no halfwords needed to store zeroes
   1.743 +    } else if (is_short(x0) && is_short(x1)) {
   1.744 +      // 1-2 halfwords needed to store shorts
   1.745 +      p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);
   1.746 +    } else {
   1.747 +      // 3-4 halfwords needed to store jints
   1.748 +      p = add_jint(p, x0);             p = add_var_int(p, x1);
   1.749 +    }
   1.750 +    return p;
   1.751 +  }
   1.752 +  void unpack_2_ints(jint& x0, jint& x1) {
   1.753 +    int    dlen = datalen();
   1.754 +    short* dp  = data();
   1.755 +    if (dlen <= 2) {
   1.756 +      x0 = relocInfo::short_data_at(0, dp, dlen);
   1.757 +      x1 = relocInfo::short_data_at(1, dp, dlen);
   1.758 +    } else {
   1.759 +      assert(dlen <= 4, "too much data");
   1.760 +      x0 = relocInfo::jint_data_at(0, dp, dlen);
   1.761 +      x1 = relocInfo::jint_data_at(2, dp, dlen);
   1.762 +    }
   1.763 +  }
   1.764 +
   1.765 + protected:
   1.766 +  // platform-dependent utilities for decoding and patching instructions
   1.767 +  void       pd_set_data_value       (address x, intptr_t off, bool verify_only = false); // a set or mem-ref
   1.768 +  void       pd_verify_data_value    (address x, intptr_t off) { pd_set_data_value(x, off, true); }
   1.769 +  address    pd_call_destination     (address orig_addr = NULL);
   1.770 +  void       pd_set_call_destination (address x);
   1.771 +
   1.772 +  // this extracts the address of an address in the code stream instead of the reloc data
   1.773 +  address* pd_address_in_code       ();
   1.774 +
   1.775 +  // this extracts an address from the code stream instead of the reloc data
   1.776 +  address  pd_get_address_from_code ();
   1.777 +
   1.778 +  // these convert from byte offsets, to scaled offsets, to addresses
   1.779 +  static jint scaled_offset(address x, address base) {
   1.780 +    int byte_offset = x - base;
   1.781 +    int offset = -byte_offset / relocInfo::addr_unit();
   1.782 +    assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
   1.783 +    return offset;
   1.784 +  }
   1.785 +  static jint scaled_offset_null_special(address x, address base) {
   1.786 +    // Some relocations treat offset=0 as meaning NULL.
   1.787 +    // Handle this extra convention carefully.
   1.788 +    if (x == NULL)  return 0;
   1.789 +    assert(x != base, "offset must not be zero");
   1.790 +    return scaled_offset(x, base);
   1.791 +  }
   1.792 +  static address address_from_scaled_offset(jint offset, address base) {
   1.793 +    int byte_offset = -( offset * relocInfo::addr_unit() );
   1.794 +    return base + byte_offset;
   1.795 +  }
   1.796 +
   1.797 +  // these convert between indexes and addresses in the runtime system
   1.798 +  static int32_t runtime_address_to_index(address runtime_address);
   1.799 +  static address index_to_runtime_address(int32_t index);
   1.800 +
   1.801 +  // helpers for mapping between old and new addresses after a move or resize
   1.802 +  address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
   1.803 +  address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
   1.804 +  void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);
   1.805 +
   1.806 + public:
   1.807 +  // accessors which only make sense for a bound Relocation
   1.808 +  address  addr()         const { return binding()->addr(); }
   1.809 +  nmethod* code()         const { return binding()->code(); }
   1.810 +  bool     addr_in_const() const { return binding()->addr_in_const(); }
   1.811 + protected:
   1.812 +  short*   data()         const { return binding()->data(); }
   1.813 +  int      datalen()      const { return binding()->datalen(); }
   1.814 +  int      format()       const { return binding()->format(); }
   1.815 +
   1.816 + public:
   1.817 +  virtual relocInfo::relocType type()            { return relocInfo::none; }
   1.818 +
   1.819 +  // is it a call instruction?
   1.820 +  virtual bool is_call()                         { return false; }
   1.821 +
   1.822 +  // is it a data movement instruction?
   1.823 +  virtual bool is_data()                         { return false; }
   1.824 +
   1.825 +  // some relocations can compute their own values
   1.826 +  virtual address  value();
   1.827 +
   1.828 +  // all relocations are able to reassert their values
   1.829 +  virtual void set_value(address x);
   1.830 +
   1.831 +  virtual void clear_inline_cache()              { }
   1.832 +
   1.833 +  // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
   1.834 +  // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is
   1.835 +  // probably a reasonable assumption, since empty caches simplifies code reloacation.
   1.836 +  virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }
   1.837 +
   1.838 +  void print();
   1.839 +};
   1.840 +
   1.841 +
   1.842 +// certain inlines must be deferred until class Relocation is defined:
   1.843 +
   1.844 +inline RelocationHolder::RelocationHolder() {
   1.845 +  // initialize the vtbl, just to keep things type-safe
   1.846 +  new(*this) Relocation();
   1.847 +}
   1.848 +
   1.849 +
   1.850 +inline RelocationHolder::RelocationHolder(Relocation* r) {
   1.851 +  // wordwise copy from r (ok if it copies garbage after r)
   1.852 +  for (int i = 0; i < _relocbuf_size; i++) {
   1.853 +    _relocbuf[i] = ((void**)r)[i];
   1.854 +  }
   1.855 +}
   1.856 +
   1.857 +
   1.858 +relocInfo::relocType RelocationHolder::type() const {
   1.859 +  return reloc()->type();
   1.860 +}
   1.861 +
   1.862 +// A DataRelocation always points at a memory or load-constant instruction..
   1.863 +// It is absolute on most machines, and the constant is split on RISCs.
   1.864 +// The specific subtypes are oop, external_word, and internal_word.
   1.865 +// By convention, the "value" does not include a separately reckoned "offset".
   1.866 +class DataRelocation : public Relocation {
   1.867 + public:
   1.868 +  bool          is_data()                      { return true; }
   1.869 +
   1.870 +  // both target and offset must be computed somehow from relocation data
   1.871 +  virtual int    offset()                      { return 0; }
   1.872 +  address         value()                      = 0;
   1.873 +  void        set_value(address x)             { set_value(x, offset()); }
   1.874 +  void        set_value(address x, intptr_t o) {
   1.875 +    if (addr_in_const())
   1.876 +      *(address*)addr() = x;
   1.877 +    else
   1.878 +      pd_set_data_value(x, o);
   1.879 +  }
   1.880 +  void        verify_value(address x) {
   1.881 +    if (addr_in_const())
   1.882 +      assert(*(address*)addr() == x, "must agree");
   1.883 +    else
   1.884 +      pd_verify_data_value(x, offset());
   1.885 +  }
   1.886 +
   1.887 +  // The "o" (displacement) argument is relevant only to split relocations
   1.888 +  // on RISC machines.  In some CPUs (SPARC), the set-hi and set-lo ins'ns
   1.889 +  // can encode more than 32 bits between them.  This allows compilers to
   1.890 +  // share set-hi instructions between addresses that differ by a small
   1.891 +  // offset (e.g., different static variables in the same class).
   1.892 +  // On such machines, the "x" argument to set_value on all set-lo
   1.893 +  // instructions must be the same as the "x" argument for the
   1.894 +  // corresponding set-hi instructions.  The "o" arguments for the
   1.895 +  // set-hi instructions are ignored, and must not affect the high-half
   1.896 +  // immediate constant.  The "o" arguments for the set-lo instructions are
   1.897 +  // added into the low-half immediate constant, and must not overflow it.
   1.898 +};
   1.899 +
   1.900 +// A CallRelocation always points at a call instruction.
   1.901 +// It is PC-relative on most machines.
   1.902 +class CallRelocation : public Relocation {
   1.903 + public:
   1.904 +  bool is_call() { return true; }
   1.905 +
   1.906 +  address  destination()                    { return pd_call_destination(); }
   1.907 +  void     set_destination(address x); // pd_set_call_destination
   1.908 +
   1.909 +  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
   1.910 +  address  value()                          { return destination();  }
   1.911 +  void     set_value(address x)             { set_destination(x); }
   1.912 +};
   1.913 +
   1.914 +class oop_Relocation : public DataRelocation {
   1.915 +  relocInfo::relocType type() { return relocInfo::oop_type; }
   1.916 +
   1.917 + public:
   1.918 +  // encode in one of these formats:  [] [n] [n l] [Nn l] [Nn Ll]
   1.919 +  // an oop in the CodeBlob's oop pool
   1.920 +  static RelocationHolder spec(int oop_index, int offset = 0) {
   1.921 +    assert(oop_index > 0, "must be a pool-resident oop");
   1.922 +    RelocationHolder rh = newHolder();
   1.923 +    new(rh) oop_Relocation(oop_index, offset);
   1.924 +    return rh;
   1.925 +  }
   1.926 +  // an oop in the instruction stream
   1.927 +  static RelocationHolder spec_for_immediate() {
   1.928 +    const int oop_index = 0;
   1.929 +    const int offset    = 0;    // if you want an offset, use the oop pool
   1.930 +    RelocationHolder rh = newHolder();
   1.931 +    new(rh) oop_Relocation(oop_index, offset);
   1.932 +    return rh;
   1.933 +  }
   1.934 +
   1.935 + private:
   1.936 +  jint _oop_index;                  // if > 0, index into CodeBlob::oop_at
   1.937 +  jint _offset;                     // byte offset to apply to the oop itself
   1.938 +
   1.939 +  oop_Relocation(int oop_index, int offset) {
   1.940 +    _oop_index = oop_index; _offset = offset;
   1.941 +  }
   1.942 +
   1.943 +  friend class RelocIterator;
   1.944 +  oop_Relocation() { }
   1.945 +
   1.946 + public:
   1.947 +  int oop_index() { return _oop_index; }
   1.948 +  int offset()    { return _offset; }
   1.949 +
   1.950 +  // data is packed in "2_ints" format:  [i o] or [Ii Oo]
   1.951 +  void pack_data_to(CodeSection* dest);
   1.952 +  void unpack_data();
   1.953 +
   1.954 +  void fix_oop_relocation();        // reasserts oop value
   1.955 +
   1.956 +  void verify_oop_relocation();
   1.957 +
   1.958 +  address value()  { return (address) *oop_addr(); }
   1.959 +
   1.960 +  bool oop_is_immediate()  { return oop_index() == 0; }
   1.961 +
   1.962 +  oop* oop_addr();                  // addr or &pool[jint_data]
   1.963 +  oop  oop_value();                 // *oop_addr
   1.964 +  // Note:  oop_value transparently converts Universe::non_oop_word to NULL.
   1.965 +};
   1.966 +
   1.967 +
   1.968 +// copy of oop_Relocation for now but may delete stuff in both/either
   1.969 +class metadata_Relocation : public DataRelocation {
   1.970 +  relocInfo::relocType type() { return relocInfo::metadata_type; }
   1.971 +
   1.972 + public:
   1.973 +  // encode in one of these formats:  [] [n] [n l] [Nn l] [Nn Ll]
   1.974 +  // an metadata in the CodeBlob's metadata pool
   1.975 +  static RelocationHolder spec(int metadata_index, int offset = 0) {
   1.976 +    assert(metadata_index > 0, "must be a pool-resident metadata");
   1.977 +    RelocationHolder rh = newHolder();
   1.978 +    new(rh) metadata_Relocation(metadata_index, offset);
   1.979 +    return rh;
   1.980 +  }
   1.981 +  // an metadata in the instruction stream
   1.982 +  static RelocationHolder spec_for_immediate() {
   1.983 +    const int metadata_index = 0;
   1.984 +    const int offset    = 0;    // if you want an offset, use the metadata pool
   1.985 +    RelocationHolder rh = newHolder();
   1.986 +    new(rh) metadata_Relocation(metadata_index, offset);
   1.987 +    return rh;
   1.988 +  }
   1.989 +
   1.990 + private:
   1.991 +  jint _metadata_index;            // if > 0, index into nmethod::metadata_at
   1.992 +  jint _offset;                     // byte offset to apply to the metadata itself
   1.993 +
   1.994 +  metadata_Relocation(int metadata_index, int offset) {
   1.995 +    _metadata_index = metadata_index; _offset = offset;
   1.996 +  }
   1.997 +
   1.998 +  friend class RelocIterator;
   1.999 +  metadata_Relocation() { }
  1.1000 +
  1.1001 +  // Fixes a Metadata pointer in the code. Most platforms embeds the
  1.1002 +  // Metadata pointer in the code at compile time so this is empty
  1.1003 +  // for them.
  1.1004 +  void pd_fix_value(address x);
  1.1005 +
  1.1006 + public:
  1.1007 +  int metadata_index() { return _metadata_index; }
  1.1008 +  int offset()    { return _offset; }
  1.1009 +
  1.1010 +  // data is packed in "2_ints" format:  [i o] or [Ii Oo]
  1.1011 +  void pack_data_to(CodeSection* dest);
  1.1012 +  void unpack_data();
  1.1013 +
  1.1014 +  void fix_metadata_relocation();        // reasserts metadata value
  1.1015 +
  1.1016 +  void verify_metadata_relocation();
  1.1017 +
  1.1018 +  address value()  { return (address) *metadata_addr(); }
  1.1019 +
  1.1020 +  bool metadata_is_immediate()  { return metadata_index() == 0; }
  1.1021 +
  1.1022 +  Metadata**   metadata_addr();                  // addr or &pool[jint_data]
  1.1023 +  Metadata*    metadata_value();                 // *metadata_addr
  1.1024 +  // Note:  metadata_value transparently converts Universe::non_metadata_word to NULL.
  1.1025 +};
  1.1026 +
  1.1027 +
  1.1028 +class virtual_call_Relocation : public CallRelocation {
  1.1029 +  relocInfo::relocType type() { return relocInfo::virtual_call_type; }
  1.1030 +
  1.1031 + public:
  1.1032 +  // "cached_value" points to the first associated set-oop.
  1.1033 +  // The oop_limit helps find the last associated set-oop.
  1.1034 +  // (See comments at the top of this file.)
  1.1035 +  static RelocationHolder spec(address cached_value) {
  1.1036 +    RelocationHolder rh = newHolder();
  1.1037 +    new(rh) virtual_call_Relocation(cached_value);
  1.1038 +    return rh;
  1.1039 +  }
  1.1040 +
  1.1041 +  virtual_call_Relocation(address cached_value) {
  1.1042 +    _cached_value = cached_value;
  1.1043 +    assert(cached_value != NULL, "first oop address must be specified");
  1.1044 +  }
  1.1045 +
  1.1046 + private:
  1.1047 +  address _cached_value;               // location of set-value instruction
  1.1048 +
  1.1049 +  friend class RelocIterator;
  1.1050 +  virtual_call_Relocation() { }
  1.1051 +
  1.1052 +
  1.1053 + public:
  1.1054 +  address cached_value();
  1.1055 +
  1.1056 +  // data is packed as scaled offsets in "2_ints" format:  [f l] or [Ff Ll]
  1.1057 +  // oop_limit is set to 0 if the limit falls somewhere within the call.
  1.1058 +  // When unpacking, a zero oop_limit is taken to refer to the end of the call.
  1.1059 +  // (This has the effect of bringing in the call's delay slot on SPARC.)
  1.1060 +  void pack_data_to(CodeSection* dest);
  1.1061 +  void unpack_data();
  1.1062 +
  1.1063 +  void clear_inline_cache();
  1.1064 +};
  1.1065 +
  1.1066 +
  1.1067 +class opt_virtual_call_Relocation : public CallRelocation {
  1.1068 +  relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }
  1.1069 +
  1.1070 + public:
  1.1071 +  static RelocationHolder spec() {
  1.1072 +    RelocationHolder rh = newHolder();
  1.1073 +    new(rh) opt_virtual_call_Relocation();
  1.1074 +    return rh;
  1.1075 +  }
  1.1076 +
  1.1077 + private:
  1.1078 +  friend class RelocIterator;
  1.1079 +  opt_virtual_call_Relocation() { }
  1.1080 +
  1.1081 + public:
  1.1082 +  void clear_inline_cache();
  1.1083 +
  1.1084 +  // find the matching static_stub
  1.1085 +  address static_stub();
  1.1086 +};
  1.1087 +
  1.1088 +
  1.1089 +class static_call_Relocation : public CallRelocation {
  1.1090 +  relocInfo::relocType type() { return relocInfo::static_call_type; }
  1.1091 +
  1.1092 + public:
  1.1093 +  static RelocationHolder spec() {
  1.1094 +    RelocationHolder rh = newHolder();
  1.1095 +    new(rh) static_call_Relocation();
  1.1096 +    return rh;
  1.1097 +  }
  1.1098 +
  1.1099 + private:
  1.1100 +  friend class RelocIterator;
  1.1101 +  static_call_Relocation() { }
  1.1102 +
  1.1103 + public:
  1.1104 +  void clear_inline_cache();
  1.1105 +
  1.1106 +  // find the matching static_stub
  1.1107 +  address static_stub();
  1.1108 +};
  1.1109 +
  1.1110 +class static_stub_Relocation : public Relocation {
  1.1111 +  relocInfo::relocType type() { return relocInfo::static_stub_type; }
  1.1112 +
  1.1113 + public:
  1.1114 +  static RelocationHolder spec(address static_call) {
  1.1115 +    RelocationHolder rh = newHolder();
  1.1116 +    new(rh) static_stub_Relocation(static_call);
  1.1117 +    return rh;
  1.1118 +  }
  1.1119 +
  1.1120 + private:
  1.1121 +  address _static_call;             // location of corresponding static_call
  1.1122 +
  1.1123 +  static_stub_Relocation(address static_call) {
  1.1124 +    _static_call = static_call;
  1.1125 +  }
  1.1126 +
  1.1127 +  friend class RelocIterator;
  1.1128 +  static_stub_Relocation() { }
  1.1129 +
  1.1130 + public:
  1.1131 +  void clear_inline_cache();
  1.1132 +
  1.1133 +  address static_call() { return _static_call; }
  1.1134 +
  1.1135 +  // data is packed as a scaled offset in "1_int" format:  [c] or [Cc]
  1.1136 +  void pack_data_to(CodeSection* dest);
  1.1137 +  void unpack_data();
  1.1138 +};
  1.1139 +
  1.1140 +class runtime_call_Relocation : public CallRelocation {
  1.1141 +  relocInfo::relocType type() { return relocInfo::runtime_call_type; }
  1.1142 +
  1.1143 + public:
  1.1144 +  static RelocationHolder spec() {
  1.1145 +    RelocationHolder rh = newHolder();
  1.1146 +    new(rh) runtime_call_Relocation();
  1.1147 +    return rh;
  1.1148 +  }
  1.1149 +
  1.1150 + private:
  1.1151 +  friend class RelocIterator;
  1.1152 +  runtime_call_Relocation() { }
  1.1153 +
  1.1154 + public:
  1.1155 +};
  1.1156 +
  1.1157 +// Trampoline Relocations.
  1.1158 +// A trampoline allows to encode a small branch in the code, even if there
  1.1159 +// is the chance that this branch can not reach all possible code locations.
  1.1160 +// If the relocation finds that a branch is too far for the instruction
  1.1161 +// in the code, it can patch it to jump to the trampoline where is
  1.1162 +// sufficient space for a far branch. Needed on PPC.
  1.1163 +class trampoline_stub_Relocation : public Relocation {
  1.1164 +  relocInfo::relocType type() { return relocInfo::trampoline_stub_type; }
  1.1165 +
  1.1166 + public:
  1.1167 +  static RelocationHolder spec(address static_call) {
  1.1168 +    RelocationHolder rh = newHolder();
  1.1169 +    return (new (rh) trampoline_stub_Relocation(static_call));
  1.1170 +  }
  1.1171 +
  1.1172 + private:
  1.1173 +  address _owner;    // Address of the NativeCall that owns the trampoline.
  1.1174 +
  1.1175 +  trampoline_stub_Relocation(address owner) {
  1.1176 +    _owner = owner;
  1.1177 +  }
  1.1178 +
  1.1179 +  friend class RelocIterator;
  1.1180 +  trampoline_stub_Relocation() { }
  1.1181 +
  1.1182 + public:
  1.1183 +
  1.1184 +  // Return the address of the NativeCall that owns the trampoline.
  1.1185 +  address owner() { return _owner; }
  1.1186 +
  1.1187 +  void pack_data_to(CodeSection * dest);
  1.1188 +  void unpack_data();
  1.1189 +
  1.1190 +  // Find the trampoline stub for a call.
  1.1191 +  static address get_trampoline_for(address call, nmethod* code);
  1.1192 +};
  1.1193 +
  1.1194 +class external_word_Relocation : public DataRelocation {
  1.1195 +  relocInfo::relocType type() { return relocInfo::external_word_type; }
  1.1196 +
  1.1197 + public:
  1.1198 +  static RelocationHolder spec(address target) {
  1.1199 +    assert(target != NULL, "must not be null");
  1.1200 +    RelocationHolder rh = newHolder();
  1.1201 +    new(rh) external_word_Relocation(target);
  1.1202 +    return rh;
  1.1203 +  }
  1.1204 +
  1.1205 +  // Use this one where all 32/64 bits of the target live in the code stream.
  1.1206 +  // The target must be an intptr_t, and must be absolute (not relative).
  1.1207 +  static RelocationHolder spec_for_immediate() {
  1.1208 +    RelocationHolder rh = newHolder();
  1.1209 +    new(rh) external_word_Relocation(NULL);
  1.1210 +    return rh;
  1.1211 +  }
  1.1212 +
  1.1213 +  // Some address looking values aren't safe to treat as relocations
  1.1214 +  // and should just be treated as constants.
  1.1215 +  static bool can_be_relocated(address target) {
  1.1216 +    return target != NULL && !is_reloc_index((intptr_t)target);
  1.1217 +  }
  1.1218 +
  1.1219 + private:
  1.1220 +  address _target;                  // address in runtime
  1.1221 +
  1.1222 +  external_word_Relocation(address target) {
  1.1223 +    _target = target;
  1.1224 +  }
  1.1225 +
  1.1226 +  friend class RelocIterator;
  1.1227 +  external_word_Relocation() { }
  1.1228 +
  1.1229 + public:
  1.1230 +  // data is packed as a well-known address in "1_int" format:  [a] or [Aa]
  1.1231 +  // The function runtime_address_to_index is used to turn full addresses
  1.1232 +  // to short indexes, if they are pre-registered by the stub mechanism.
  1.1233 +  // If the "a" value is 0 (i.e., _target is NULL), the address is stored
  1.1234 +  // in the code stream.  See external_word_Relocation::target().
  1.1235 +  void pack_data_to(CodeSection* dest);
  1.1236 +  void unpack_data();
  1.1237 +
  1.1238 +  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  1.1239 +  address  target();        // if _target==NULL, fetch addr from code stream
  1.1240 +  address  value()          { return target(); }
  1.1241 +};
  1.1242 +
  1.1243 +class internal_word_Relocation : public DataRelocation {
  1.1244 +  relocInfo::relocType type() { return relocInfo::internal_word_type; }
  1.1245 +
  1.1246 + public:
  1.1247 +  static RelocationHolder spec(address target) {
  1.1248 +    assert(target != NULL, "must not be null");
  1.1249 +    RelocationHolder rh = newHolder();
  1.1250 +    new(rh) internal_word_Relocation(target);
  1.1251 +    return rh;
  1.1252 +  }
  1.1253 +
  1.1254 +  // use this one where all the bits of the target can fit in the code stream:
  1.1255 +  static RelocationHolder spec_for_immediate() {
  1.1256 +    RelocationHolder rh = newHolder();
  1.1257 +    new(rh) internal_word_Relocation(NULL);
  1.1258 +    return rh;
  1.1259 +  }
  1.1260 +
  1.1261 +  internal_word_Relocation(address target) {
  1.1262 +    _target  = target;
  1.1263 +    _section = -1;  // self-relative
  1.1264 +  }
  1.1265 +
  1.1266 + protected:
  1.1267 +  address _target;                  // address in CodeBlob
  1.1268 +  int     _section;                 // section providing base address, if any
  1.1269 +
  1.1270 +  friend class RelocIterator;
  1.1271 +  internal_word_Relocation() { }
  1.1272 +
  1.1273 +  // bit-width of LSB field in packed offset, if section >= 0
  1.1274 +  enum { section_width = 2 }; // must equal CodeBuffer::sect_bits
  1.1275 +
  1.1276 + public:
  1.1277 +  // data is packed as a scaled offset in "1_int" format:  [o] or [Oo]
  1.1278 +  // If the "o" value is 0 (i.e., _target is NULL), the offset is stored
  1.1279 +  // in the code stream.  See internal_word_Relocation::target().
  1.1280 +  // If _section is not -1, it is appended to the low bits of the offset.
  1.1281 +  void pack_data_to(CodeSection* dest);
  1.1282 +  void unpack_data();
  1.1283 +
  1.1284 +  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  1.1285 +  address  target();        // if _target==NULL, fetch addr from code stream
  1.1286 +  int      section()        { return _section;   }
  1.1287 +  address  value()          { return target();   }
  1.1288 +};
  1.1289 +
  1.1290 +class section_word_Relocation : public internal_word_Relocation {
  1.1291 +  relocInfo::relocType type() { return relocInfo::section_word_type; }
  1.1292 +
  1.1293 + public:
  1.1294 +  static RelocationHolder spec(address target, int section) {
  1.1295 +    RelocationHolder rh = newHolder();
  1.1296 +    new(rh) section_word_Relocation(target, section);
  1.1297 +    return rh;
  1.1298 +  }
  1.1299 +
  1.1300 +  section_word_Relocation(address target, int section) {
  1.1301 +    assert(target != NULL, "must not be null");
  1.1302 +    assert(section >= 0, "must be a valid section");
  1.1303 +    _target  = target;
  1.1304 +    _section = section;
  1.1305 +  }
  1.1306 +
  1.1307 +  //void pack_data_to -- inherited
  1.1308 +  void unpack_data();
  1.1309 +
  1.1310 + private:
  1.1311 +  friend class RelocIterator;
  1.1312 +  section_word_Relocation() { }
  1.1313 +};
  1.1314 +
  1.1315 +
  1.1316 +class poll_Relocation : public Relocation {
  1.1317 +  bool          is_data()                      { return true; }
  1.1318 +  relocInfo::relocType type() { return relocInfo::poll_type; }
  1.1319 +  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  1.1320 +};
  1.1321 +
  1.1322 +class poll_return_Relocation : public Relocation {
  1.1323 +  bool          is_data()                      { return true; }
  1.1324 +  relocInfo::relocType type() { return relocInfo::poll_return_type; }
  1.1325 +  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  1.1326 +};
  1.1327 +
  1.1328 +// We know all the xxx_Relocation classes, so now we can define these:
  1.1329 +#define EACH_CASE(name)                                         \
  1.1330 +inline name##_Relocation* RelocIterator::name##_reloc() {       \
  1.1331 +  assert(type() == relocInfo::name##_type, "type must agree");  \
  1.1332 +  /* The purpose of the placed "new" is to re-use the same */   \
  1.1333 +  /* stack storage for each new iteration. */                   \
  1.1334 +  name##_Relocation* r = new(_rh) name##_Relocation();          \
  1.1335 +  r->set_binding(this);                                         \
  1.1336 +  r->name##_Relocation::unpack_data();                          \
  1.1337 +  return r;                                                     \
  1.1338 +}
  1.1339 +APPLY_TO_RELOCATIONS(EACH_CASE);
  1.1340 +#undef EACH_CASE
  1.1341 +
  1.1342 +inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {
  1.1343 +  initialize(nm, begin, limit);
  1.1344 +}
  1.1345 +
  1.1346 +#endif // SHARE_VM_CODE_RELOCINFO_HPP

mercurial