aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@1: /* aoqi@1: * This file has been modified by Loongson Technology in 2015. These aoqi@1: * modifications are Copyright (c) 2015 Loongson Technology, and are made aoqi@1: * available on the same license terms set forth above. aoqi@1: */ aoqi@1: aoqi@0: #ifndef SHARE_VM_CODE_RELOCINFO_HPP aoqi@0: #define SHARE_VM_CODE_RELOCINFO_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: aoqi@0: class NativeMovConstReg; aoqi@0: aoqi@0: // Types in this file: aoqi@0: // relocInfo aoqi@0: // One element of an array of halfwords encoding compressed relocations. aoqi@0: // Also, the source of relocation types (relocInfo::oop_type, ...). aoqi@0: // Relocation aoqi@0: // A flyweight object representing a single relocation. aoqi@0: // It is fully unpacked from the compressed relocation array. aoqi@0: // metadata_Relocation, ... (subclasses of Relocation) aoqi@0: // The location of some type-specific operations (metadata_addr, ...). aoqi@0: // Also, the source of relocation specs (metadata_Relocation::spec, ...). aoqi@0: // oop_Relocation, ... (subclasses of Relocation) aoqi@0: // oops in the code stream (strings, class loaders) aoqi@0: // Also, the source of relocation specs (oop_Relocation::spec, ...). aoqi@0: // RelocationHolder aoqi@0: // A ValueObj type which acts as a union holding a Relocation object. aoqi@0: // Represents a relocation spec passed into a CodeBuffer during assembly. aoqi@0: // RelocIterator aoqi@0: // A StackObj which iterates over the relocations associated with aoqi@0: // a range of code addresses. Can be used to operate a copy of code. aoqi@0: // BoundRelocation aoqi@0: // An _internal_ type shared by packers and unpackers of relocations. aoqi@0: // It pastes together a RelocationHolder with some pointers into aoqi@0: // code and relocInfo streams. aoqi@0: aoqi@0: aoqi@0: // Notes on relocType: aoqi@0: // aoqi@0: // These hold enough information to read or write a value embedded in aoqi@0: // the instructions of an CodeBlob. They're used to update: aoqi@0: // aoqi@0: // 1) embedded oops (isOop() == true) aoqi@0: // 2) inline caches (isIC() == true) aoqi@0: // 3) runtime calls (isRuntimeCall() == true) aoqi@0: // 4) internal word ref (isInternalWord() == true) aoqi@0: // 5) external word ref (isExternalWord() == true) aoqi@0: // aoqi@0: // when objects move (GC) or if code moves (compacting the code heap). aoqi@0: // They are also used to patch the code (if a call site must change) aoqi@0: // aoqi@0: // A relocInfo is represented in 16 bits: aoqi@0: // 4 bits indicating the relocation type aoqi@0: // 12 bits indicating the offset from the previous relocInfo address aoqi@0: // aoqi@0: // The offsets accumulate along the relocInfo stream to encode the aoqi@0: // address within the CodeBlob, which is named RelocIterator::addr(). aoqi@0: // The address of a particular relocInfo always points to the first aoqi@0: // byte of the relevant instruction (and not to any of its subfields aoqi@0: // or embedded immediate constants). aoqi@0: // aoqi@0: // The offset value is scaled appropriately for the target machine. aoqi@0: // (See relocInfo_.hpp for the offset scaling.) aoqi@0: // aoqi@0: // On some machines, there may also be a "format" field which may provide aoqi@0: // additional information about the format of the instruction stream aoqi@0: // at the corresponding code address. The format value is usually zero. aoqi@0: // Any machine (such as Intel) whose instructions can sometimes contain aoqi@0: // more than one relocatable constant needs format codes to distinguish aoqi@0: // which operand goes with a given relocation. aoqi@0: // aoqi@0: // If the target machine needs N format bits, the offset has 12-N bits, aoqi@0: // the format is encoded between the offset and the type, and the aoqi@0: // relocInfo_.hpp file has manifest constants for the format codes. aoqi@0: // aoqi@0: // If the type is "data_prefix_tag" then the offset bits are further encoded, aoqi@0: // and in fact represent not a code-stream offset but some inline data. aoqi@0: // The data takes the form of a counted sequence of halfwords, which aoqi@0: // precedes the actual relocation record. (Clients never see it directly.) aoqi@0: // The interpetation of this extra data depends on the relocation type. aoqi@0: // aoqi@0: // On machines that have 32-bit immediate fields, there is usually aoqi@0: // little need for relocation "prefix" data, because the instruction stream aoqi@0: // is a perfectly reasonable place to store the value. On machines in aoqi@0: // which 32-bit values must be "split" across instructions, the relocation aoqi@0: // data is the "true" specification of the value, which is then applied aoqi@0: // to some field of the instruction (22 or 13 bits, on SPARC). aoqi@0: // aoqi@0: // Whenever the location of the CodeBlob changes, any PC-relative aoqi@0: // relocations, and any internal_word_type relocations, must be reapplied. aoqi@0: // After the GC runs, oop_type relocations must be reapplied. aoqi@0: // aoqi@0: // aoqi@0: // Here are meanings of the types: aoqi@0: // aoqi@0: // relocInfo::none -- a filler record aoqi@0: // Value: none aoqi@0: // Instruction: The corresponding code address is ignored aoqi@0: // Data: Any data prefix and format code are ignored aoqi@0: // (This means that any relocInfo can be disabled by setting aoqi@0: // its type to none. See relocInfo::remove.) aoqi@0: // aoqi@0: // relocInfo::oop_type, relocInfo::metadata_type -- a reference to an oop or meta data aoqi@0: // Value: an oop, or else the address (handle) of an oop aoqi@0: // Instruction types: memory (load), set (load address) aoqi@0: // Data: [] an oop stored in 4 bytes of instruction aoqi@0: // [n] n is the index of an oop in the CodeBlob's oop pool aoqi@0: // [[N]n l] and l is a byte offset to be applied to the oop aoqi@0: // [Nn Ll] both index and offset may be 32 bits if necessary aoqi@0: // Here is a special hack, used only by the old compiler: aoqi@0: // [[N]n 00] the value is the __address__ of the nth oop in the pool aoqi@0: // (Note that the offset allows optimal references to class variables.) aoqi@0: // aoqi@0: // relocInfo::internal_word_type -- an address within the same CodeBlob aoqi@0: // relocInfo::section_word_type -- same, but can refer to another section aoqi@0: // Value: an address in the CodeBlob's code or constants section aoqi@0: // Instruction types: memory (load), set (load address) aoqi@0: // Data: [] stored in 4 bytes of instruction aoqi@0: // [[L]l] a relative offset (see [About Offsets] below) aoqi@0: // In the case of section_word_type, the offset is relative to a section aoqi@0: // base address, and the section number (e.g., SECT_INSTS) is encoded aoqi@0: // into the low two bits of the offset L. aoqi@0: // aoqi@0: // relocInfo::external_word_type -- a fixed address in the runtime system aoqi@0: // Value: an address aoqi@0: // Instruction types: memory (load), set (load address) aoqi@0: // Data: [] stored in 4 bytes of instruction aoqi@0: // [n] the index of a "well-known" stub (usual case on RISC) aoqi@0: // [Ll] a 32-bit address aoqi@0: // aoqi@0: // relocInfo::runtime_call_type -- a fixed subroutine in the runtime system aoqi@0: // Value: an address aoqi@0: // Instruction types: PC-relative call (or a PC-relative branch) aoqi@0: // Data: [] stored in 4 bytes of instruction aoqi@0: // aoqi@0: // relocInfo::static_call_type -- a static call aoqi@0: // Value: an CodeBlob, a stub, or a fixup routine aoqi@0: // Instruction types: a call aoqi@0: // Data: [] aoqi@0: // The identity of the callee is extracted from debugging information. aoqi@0: // //%note reloc_3 aoqi@0: // aoqi@0: // relocInfo::virtual_call_type -- a virtual call site (which includes an inline aoqi@0: // cache) aoqi@0: // Value: an CodeBlob, a stub, the interpreter, or a fixup routine aoqi@0: // Instruction types: a call, plus some associated set-oop instructions aoqi@0: // Data: [] the associated set-oops are adjacent to the call aoqi@0: // [n] n is a relative offset to the first set-oop aoqi@0: // [[N]n l] and l is a limit within which the set-oops occur aoqi@0: // [Nn Ll] both n and l may be 32 bits if necessary aoqi@0: // The identity of the callee is extracted from debugging information. aoqi@0: // aoqi@0: // relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound aoqi@0: // aoqi@0: // Same info as a static_call_type. We use a special type, so the handling of aoqi@0: // virtuals and statics are separated. aoqi@0: // aoqi@0: // aoqi@0: // The offset n points to the first set-oop. (See [About Offsets] below.) aoqi@0: // In turn, the set-oop instruction specifies or contains an oop cell devoted aoqi@0: // exclusively to the IC call, which can be patched along with the call. aoqi@0: // aoqi@0: // The locations of any other set-oops are found by searching the relocation aoqi@0: // information starting at the first set-oop, and continuing until all aoqi@0: // relocations up through l have been inspected. The value l is another aoqi@0: // relative offset. (Both n and l are relative to the call's first byte.) aoqi@0: // aoqi@0: // The limit l of the search is exclusive. However, if it points within aoqi@0: // the call (e.g., offset zero), it is adjusted to point after the call and aoqi@0: // any associated machine-specific delay slot. aoqi@0: // aoqi@0: // Since the offsets could be as wide as 32-bits, these conventions aoqi@0: // put no restrictions whatever upon code reorganization. aoqi@0: // aoqi@0: // The compiler is responsible for ensuring that transition from a clean aoqi@0: // state to a monomorphic compiled state is MP-safe. This implies that aoqi@0: // the system must respond well to intermediate states where a random aoqi@0: // subset of the set-oops has been correctly from the clean state aoqi@0: // upon entry to the VEP of the compiled method. In the case of a aoqi@0: // machine (Intel) with a single set-oop instruction, the 32-bit aoqi@0: // immediate field must not straddle a unit of memory coherence. aoqi@0: // //%note reloc_3 aoqi@0: // aoqi@0: // relocInfo::static_stub_type -- an extra stub for each static_call_type aoqi@0: // Value: none aoqi@0: // Instruction types: a virtual call: { set_oop; jump; } aoqi@0: // Data: [[N]n] the offset of the associated static_call reloc aoqi@0: // This stub becomes the target of a static call which must be upgraded aoqi@0: // to a virtual call (because the callee is interpreted). aoqi@0: // See [About Offsets] below. aoqi@0: // //%note reloc_2 aoqi@0: // aoqi@0: // For example: aoqi@0: // aoqi@0: // INSTRUCTIONS RELOC: TYPE PREFIX DATA aoqi@0: // ------------ ---- ----------- aoqi@0: // sethi %hi(myObject), R oop_type [n(myObject)] aoqi@0: // ld [R+%lo(myObject)+fldOffset], R2 oop_type [n(myObject) fldOffset] aoqi@0: // add R2, 1, R2 aoqi@0: // st R2, [R+%lo(myObject)+fldOffset] oop_type [n(myObject) fldOffset] aoqi@0: //%note reloc_1 aoqi@0: // aoqi@0: // This uses 4 instruction words, 8 relocation halfwords, aoqi@0: // and an entry (which is sharable) in the CodeBlob's oop pool, aoqi@0: // for a total of 36 bytes. aoqi@0: // aoqi@0: // Note that the compiler is responsible for ensuring the "fldOffset" when aoqi@0: // added to "%lo(myObject)" does not overflow the immediate fields of the aoqi@0: // memory instructions. aoqi@0: // aoqi@0: // aoqi@0: // [About Offsets] Relative offsets are supplied to this module as aoqi@0: // positive byte offsets, but they may be internally stored scaled aoqi@0: // and/or negated, depending on what is most compact for the target aoqi@0: // system. Since the object pointed to by the offset typically aoqi@0: // precedes the relocation address, it is profitable to store aoqi@0: // these negative offsets as positive numbers, but this decision aoqi@0: // is internal to the relocation information abstractions. aoqi@0: // aoqi@0: aoqi@0: class Relocation; aoqi@0: class CodeBuffer; aoqi@0: class CodeSection; aoqi@0: class RelocIterator; aoqi@0: aoqi@0: class relocInfo VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class RelocIterator; aoqi@0: public: aoqi@0: enum relocType { aoqi@0: none = 0, // Used when no relocation should be generated aoqi@0: oop_type = 1, // embedded oop aoqi@0: virtual_call_type = 2, // a standard inline cache call for a virtual send aoqi@0: opt_virtual_call_type = 3, // a virtual call that has been statically bound (i.e., no IC cache) aoqi@0: static_call_type = 4, // a static send aoqi@0: static_stub_type = 5, // stub-entry for static send (takes care of interpreter case) aoqi@0: runtime_call_type = 6, // call to fixed external routine aoqi@0: external_word_type = 7, // reference to fixed external address aoqi@0: internal_word_type = 8, // reference within the current code blob aoqi@0: section_word_type = 9, // internal, but a cross-section reference aoqi@0: poll_type = 10, // polling instruction for safepoints aoqi@0: poll_return_type = 11, // polling instruction for safepoints at return aoqi@0: metadata_type = 12, // metadata that used to be oops aoqi@0: trampoline_stub_type = 13, // stub-entry for trampoline aoqi@1: #ifndef MIPS64 aoqi@0: yet_unused_type_1 = 14, // Still unused aoqi@1: #else aoqi@1: internal_pc_type = 14, // tag for internal data,?? aoqi@1: #endif aoqi@0: data_prefix_tag = 15, // tag for a prefix (carries data arguments) aoqi@0: type_mask = 15 // A mask which selects only the above values aoqi@0: }; aoqi@0: aoqi@0: protected: aoqi@0: unsigned short _value; aoqi@0: aoqi@0: enum RawBitsToken { RAW_BITS }; aoqi@0: relocInfo(relocType type, RawBitsToken ignore, int bits) aoqi@0: : _value((type << nontype_width) + bits) { } aoqi@0: aoqi@0: relocInfo(relocType type, RawBitsToken ignore, int off, int f) aoqi@0: : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { } aoqi@0: aoqi@0: public: aoqi@0: // constructor aoqi@0: relocInfo(relocType type, int offset, int format = 0) aoqi@0: #ifndef ASSERT aoqi@0: { aoqi@0: (*this) = relocInfo(type, RAW_BITS, offset, format); aoqi@0: } aoqi@0: #else aoqi@0: // Put a bunch of assertions out-of-line. aoqi@0: ; aoqi@0: #endif aoqi@0: aoqi@0: #define APPLY_TO_RELOCATIONS(visitor) \ aoqi@0: visitor(oop) \ aoqi@0: visitor(metadata) \ aoqi@0: visitor(virtual_call) \ aoqi@0: visitor(opt_virtual_call) \ aoqi@0: visitor(static_call) \ aoqi@0: visitor(static_stub) \ aoqi@0: visitor(runtime_call) \ aoqi@0: visitor(external_word) \ aoqi@0: visitor(internal_word) \ aoqi@0: visitor(poll) \ aoqi@0: visitor(poll_return) \ aoqi@0: visitor(trampoline_stub) \ aoqi@1: NOT_MIPS64(visitor(section_word)) MIPS64_ONLY(visitor(internal_pc)) \ aoqi@0: aoqi@0: aoqi@0: public: aoqi@0: enum { aoqi@0: value_width = sizeof(unsigned short) * BitsPerByte, aoqi@0: type_width = 4, // == log2(type_mask+1) aoqi@0: nontype_width = value_width - type_width, aoqi@0: datalen_width = nontype_width-1, aoqi@0: datalen_tag = 1 << datalen_width, // or-ed into _value aoqi@0: datalen_limit = 1 << datalen_width, aoqi@0: datalen_mask = (1 << datalen_width)-1 aoqi@0: }; aoqi@0: aoqi@0: // accessors aoqi@0: public: aoqi@0: relocType type() const { return (relocType)((unsigned)_value >> nontype_width); } aoqi@0: int format() const { return format_mask==0? 0: format_mask & aoqi@0: ((unsigned)_value >> offset_width); } aoqi@0: int addr_offset() const { assert(!is_prefix(), "must have offset"); aoqi@0: return (_value & offset_mask)*offset_unit; } aoqi@0: aoqi@0: protected: aoqi@0: const short* data() const { assert(is_datalen(), "must have data"); aoqi@0: return (const short*)(this + 1); } aoqi@0: int datalen() const { assert(is_datalen(), "must have data"); aoqi@0: return (_value & datalen_mask); } aoqi@0: int immediate() const { assert(is_immediate(), "must have immed"); aoqi@0: return (_value & datalen_mask); } aoqi@0: public: aoqi@0: static int addr_unit() { return offset_unit; } aoqi@0: static int offset_limit() { return (1 << offset_width) * offset_unit; } aoqi@0: aoqi@0: void set_type(relocType type); aoqi@0: void set_format(int format); aoqi@0: aoqi@0: void remove() { set_type(none); } aoqi@0: aoqi@0: protected: aoqi@0: bool is_none() const { return type() == none; } aoqi@0: bool is_prefix() const { return type() == data_prefix_tag; } aoqi@0: bool is_datalen() const { assert(is_prefix(), "must be prefix"); aoqi@0: return (_value & datalen_tag) != 0; } aoqi@0: bool is_immediate() const { assert(is_prefix(), "must be prefix"); aoqi@0: return (_value & datalen_tag) == 0; } aoqi@0: aoqi@0: public: aoqi@0: // Occasionally records of type relocInfo::none will appear in the stream. aoqi@0: // We do not bother to filter these out, but clients should ignore them. aoqi@0: // These records serve as "filler" in three ways: aoqi@0: // - to skip large spans of unrelocated code (this is rare) aoqi@0: // - to pad out the relocInfo array to the required oop alignment aoqi@0: // - to disable old relocation information which is no longer applicable aoqi@0: aoqi@0: inline friend relocInfo filler_relocInfo(); aoqi@0: aoqi@0: // Every non-prefix relocation may be preceded by at most one prefix, aoqi@0: // which supplies 1 or more halfwords of associated data. Conventionally, aoqi@0: // an int is represented by 0, 1, or 2 halfwords, depending on how aoqi@0: // many bits are required to represent the value. (In addition, aoqi@0: // if the sole halfword is a 10-bit unsigned number, it is made aoqi@0: // "immediate" in the prefix header word itself. This optimization aoqi@0: // is invisible outside this module.) aoqi@0: aoqi@0: inline friend relocInfo prefix_relocInfo(int datalen); aoqi@0: aoqi@0: protected: aoqi@0: // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value aoqi@0: static relocInfo immediate_relocInfo(int data0) { aoqi@0: assert(fits_into_immediate(data0), "data0 in limits"); aoqi@0: return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0); aoqi@0: } aoqi@0: static bool fits_into_immediate(int data0) { aoqi@0: return (data0 >= 0 && data0 < datalen_limit); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: // Support routines for compilers. aoqi@0: aoqi@0: // This routine takes an infant relocInfo (unprefixed) and aoqi@0: // edits in its prefix, if any. It also updates dest.locs_end. aoqi@0: void initialize(CodeSection* dest, Relocation* reloc); aoqi@0: aoqi@0: // This routine updates a prefix and returns the limit pointer. aoqi@0: // It tries to compress the prefix from 32 to 16 bits, and if aoqi@0: // successful returns a reduced "prefix_limit" pointer. aoqi@0: relocInfo* finish_prefix(short* prefix_limit); aoqi@0: aoqi@0: // bit-packers for the data array: aoqi@0: aoqi@0: // As it happens, the bytes within the shorts are ordered natively, aoqi@0: // but the shorts within the word are ordered big-endian. aoqi@0: // This is an arbitrary choice, made this way mainly to ease debugging. aoqi@0: static int data0_from_int(jint x) { return x >> value_width; } aoqi@0: static int data1_from_int(jint x) { return (short)x; } aoqi@0: static jint jint_from_data(short* data) { aoqi@0: return (data[0] << value_width) + (unsigned short)data[1]; aoqi@0: } aoqi@0: aoqi@0: static jint short_data_at(int n, short* data, int datalen) { aoqi@0: return datalen > n ? data[n] : 0; aoqi@0: } aoqi@0: aoqi@0: static jint jint_data_at(int n, short* data, int datalen) { aoqi@0: return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen); aoqi@0: } aoqi@0: aoqi@0: // Update methods for relocation information aoqi@0: // (since code is dynamically patched, we also need to dynamically update the relocation info) aoqi@0: // Both methods takes old_type, so it is able to performe sanity checks on the information removed. aoqi@0: static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type); aoqi@0: static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type); aoqi@0: aoqi@0: // Machine dependent stuff aoqi@0: #ifdef TARGET_ARCH_x86 aoqi@0: # include "relocInfo_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_sparc aoqi@0: # include "relocInfo_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_zero aoqi@0: # include "relocInfo_zero.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_arm aoqi@0: # include "relocInfo_arm.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_ppc aoqi@0: # include "relocInfo_ppc.hpp" aoqi@0: #endif aoqi@1: #ifdef TARGET_ARCH_mips aoqi@1: # include "relocInfo_mips.hpp" aoqi@1: #endif aoqi@0: aoqi@0: aoqi@0: protected: aoqi@0: // Derived constant, based on format_width which is PD: aoqi@0: enum { aoqi@0: offset_width = nontype_width - format_width, aoqi@0: offset_mask = (1< 0 aoqi@0: }; aoqi@0: }; aoqi@0: aoqi@0: #define FORWARD_DECLARE_EACH_CLASS(name) \ aoqi@0: class name##_Relocation; aoqi@0: APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS) aoqi@0: #undef FORWARD_DECLARE_EACH_CLASS aoqi@0: aoqi@0: aoqi@0: aoqi@0: inline relocInfo filler_relocInfo() { aoqi@0: return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit); aoqi@0: } aoqi@0: aoqi@0: inline relocInfo prefix_relocInfo(int datalen = 0) { aoqi@0: assert(relocInfo::fits_into_immediate(datalen), "datalen in limits"); aoqi@0: return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Holder for flyweight relocation objects. aoqi@0: // Although the flyweight subclasses are of varying sizes, aoqi@0: // the holder is "one size fits all". aoqi@0: class RelocationHolder VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class Relocation; aoqi@0: friend class CodeSection; aoqi@0: aoqi@0: private: aoqi@0: // this preallocated memory must accommodate all subclasses of Relocation aoqi@0: // (this number is assertion-checked in Relocation::operator new) aoqi@0: enum { _relocbuf_size = 5 }; aoqi@0: void* _relocbuf[ _relocbuf_size ]; aoqi@0: aoqi@0: public: aoqi@0: Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; } aoqi@0: inline relocInfo::relocType type() const; aoqi@0: aoqi@0: // Add a constant offset to a relocation. Helper for class Address. aoqi@0: RelocationHolder plus(int offset) const; aoqi@0: aoqi@0: inline RelocationHolder(); // initializes type to none aoqi@0: aoqi@0: inline RelocationHolder(Relocation* r); // make a copy aoqi@0: aoqi@0: static const RelocationHolder none; aoqi@0: }; aoqi@0: aoqi@0: // A RelocIterator iterates through the relocation information of a CodeBlob. aoqi@0: // It is a variable BoundRelocation which is able to take on successive aoqi@0: // values as it is advanced through a code stream. aoqi@0: // Usage: aoqi@0: // RelocIterator iter(nm); aoqi@0: // while (iter.next()) { aoqi@0: // iter.reloc()->some_operation(); aoqi@0: // } aoqi@0: // or: aoqi@0: // RelocIterator iter(nm); aoqi@0: // while (iter.next()) { aoqi@0: // switch (iter.type()) { aoqi@0: // case relocInfo::oop_type : aoqi@0: // case relocInfo::ic_type : aoqi@0: // case relocInfo::prim_type : aoqi@0: // case relocInfo::uncommon_type : aoqi@0: // case relocInfo::runtime_call_type : aoqi@0: // case relocInfo::internal_word_type: aoqi@0: // case relocInfo::external_word_type: aoqi@0: // ... aoqi@0: // } aoqi@0: // } aoqi@0: aoqi@0: class RelocIterator : public StackObj { aoqi@0: enum { SECT_LIMIT = 3 }; // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor aoqi@0: friend class Relocation; aoqi@0: friend class relocInfo; // for change_reloc_info_for_address only aoqi@0: typedef relocInfo::relocType relocType; aoqi@0: aoqi@0: private: aoqi@0: address _limit; // stop producing relocations after this _addr aoqi@0: relocInfo* _current; // the current relocation information aoqi@0: relocInfo* _end; // end marker; we're done iterating when _current == _end aoqi@0: nmethod* _code; // compiled method containing _addr aoqi@0: address _addr; // instruction to which the relocation applies aoqi@0: short _databuf; // spare buffer for compressed data aoqi@0: short* _data; // pointer to the relocation's data aoqi@0: short _datalen; // number of halfwords in _data aoqi@0: char _format; // position within the instruction aoqi@0: aoqi@0: // Base addresses needed to compute targets of section_word_type relocs. aoqi@0: address _section_start[SECT_LIMIT]; aoqi@0: address _section_end [SECT_LIMIT]; aoqi@0: aoqi@0: void set_has_current(bool b) { aoqi@0: _datalen = !b ? -1 : 0; aoqi@0: debug_only(_data = NULL); aoqi@0: } aoqi@0: void set_current(relocInfo& ri) { aoqi@0: _current = &ri; aoqi@0: set_has_current(true); aoqi@0: } aoqi@0: aoqi@0: RelocationHolder _rh; // where the current relocation is allocated aoqi@0: aoqi@0: relocInfo* current() const { assert(has_current(), "must have current"); aoqi@0: return _current; } aoqi@0: aoqi@0: void set_limits(address begin, address limit); aoqi@0: aoqi@0: void advance_over_prefix(); // helper method aoqi@0: aoqi@0: void initialize_misc(); aoqi@0: aoqi@0: void initialize(nmethod* nm, address begin, address limit); aoqi@0: aoqi@0: RelocIterator() { initialize_misc(); } aoqi@0: aoqi@0: public: aoqi@0: // constructor aoqi@0: RelocIterator(nmethod* nm, address begin = NULL, address limit = NULL); aoqi@0: RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL); aoqi@0: aoqi@0: // get next reloc info, return !eos aoqi@0: bool next() { aoqi@0: _current++; aoqi@0: assert(_current <= _end, "must not overrun relocInfo"); aoqi@0: if (_current == _end) { aoqi@0: set_has_current(false); aoqi@0: return false; aoqi@0: } aoqi@0: set_has_current(true); aoqi@0: aoqi@0: if (_current->is_prefix()) { aoqi@0: advance_over_prefix(); aoqi@0: assert(!current()->is_prefix(), "only one prefix at a time"); aoqi@0: } aoqi@0: aoqi@0: _addr += _current->addr_offset(); aoqi@0: aoqi@0: if (_limit != NULL && _addr >= _limit) { aoqi@0: set_has_current(false); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: if (relocInfo::have_format) _format = current()->format(); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // accessors aoqi@0: address limit() const { return _limit; } aoqi@0: void set_limit(address x); aoqi@0: relocType type() const { return current()->type(); } aoqi@0: int format() const { return (relocInfo::have_format) ? current()->format() : 0; } aoqi@0: address addr() const { return _addr; } aoqi@0: nmethod* code() const { return _code; } aoqi@0: short* data() const { return _data; } aoqi@0: int datalen() const { return _datalen; } aoqi@0: bool has_current() const { return _datalen >= 0; } aoqi@0: aoqi@0: void set_addr(address addr) { _addr = addr; } aoqi@0: bool addr_in_const() const; aoqi@0: aoqi@0: address section_start(int n) const { aoqi@0: assert(_section_start[n], "must be initialized"); aoqi@0: return _section_start[n]; aoqi@0: } aoqi@0: address section_end(int n) const { aoqi@0: assert(_section_end[n], "must be initialized"); aoqi@0: return _section_end[n]; aoqi@0: } aoqi@0: aoqi@0: // The address points to the affected displacement part of the instruction. aoqi@0: // For RISC, this is just the whole instruction. aoqi@0: // For Intel, this is an unaligned 32-bit word. aoqi@0: aoqi@0: // type-specific relocation accessors: oop_Relocation* oop_reloc(), etc. aoqi@0: #define EACH_TYPE(name) \ aoqi@0: inline name##_Relocation* name##_reloc(); aoqi@0: APPLY_TO_RELOCATIONS(EACH_TYPE) aoqi@0: #undef EACH_TYPE aoqi@0: // generic relocation accessor; switches on type to call the above aoqi@0: Relocation* reloc(); aoqi@0: aoqi@0: // CodeBlob's have relocation indexes for faster random access: aoqi@0: static int locs_and_index_size(int code_size, int locs_size); aoqi@0: // Store an index into [dest_start+dest_count..dest_end). aoqi@0: // At dest_start[0..dest_count] is the actual relocation information. aoqi@0: // Everything else up to dest_end is free space for the index. aoqi@0: static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: public: aoqi@0: void print(); aoqi@0: void print_current(); aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // A Relocation is a flyweight object allocated within a RelocationHolder. aoqi@0: // It represents the relocation data of relocation record. aoqi@0: // So, the RelocIterator unpacks relocInfos into Relocations. aoqi@0: aoqi@0: class Relocation VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class RelocationHolder; aoqi@0: friend class RelocIterator; aoqi@0: aoqi@0: private: aoqi@0: static void guarantee_size(); aoqi@0: aoqi@0: // When a relocation has been created by a RelocIterator, aoqi@0: // this field is non-null. It allows the relocation to know aoqi@0: // its context, such as the address to which it applies. aoqi@0: RelocIterator* _binding; aoqi@0: aoqi@0: protected: aoqi@0: RelocIterator* binding() const { aoqi@0: assert(_binding != NULL, "must be bound"); aoqi@0: return _binding; aoqi@0: } aoqi@0: void set_binding(RelocIterator* b) { aoqi@0: assert(_binding == NULL, "must be unbound"); aoqi@0: _binding = b; aoqi@0: assert(_binding != NULL, "must now be bound"); aoqi@0: } aoqi@0: aoqi@0: Relocation() { aoqi@0: _binding = NULL; aoqi@0: } aoqi@0: aoqi@0: static RelocationHolder newHolder() { aoqi@0: return RelocationHolder(); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: void* operator new(size_t size, const RelocationHolder& holder) throw() { aoqi@0: if (size > sizeof(holder._relocbuf)) guarantee_size(); aoqi@0: assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree"); aoqi@0: return holder.reloc(); aoqi@0: } aoqi@0: aoqi@0: // make a generic relocation for a given type (if possible) aoqi@0: static RelocationHolder spec_simple(relocInfo::relocType rtype); aoqi@0: aoqi@0: // here is the type-specific hook which writes relocation data: aoqi@0: virtual void pack_data_to(CodeSection* dest) { } aoqi@0: aoqi@0: // here is the type-specific hook which reads (unpacks) relocation data: aoqi@0: virtual void unpack_data() { aoqi@0: assert(datalen()==0 || type()==relocInfo::none, "no data here"); aoqi@0: } aoqi@0: aoqi@0: static bool is_reloc_index(intptr_t index) { aoqi@0: return 0 < index && index < os::vm_page_size(); aoqi@0: } aoqi@0: aoqi@0: protected: aoqi@0: // Helper functions for pack_data_to() and unpack_data(). aoqi@0: aoqi@0: // Most of the compression logic is confined here. aoqi@0: // (The "immediate data" mechanism of relocInfo works independently aoqi@0: // of this stuff, and acts to further compress most 1-word data prefixes.) aoqi@0: aoqi@0: // A variable-width int is encoded as a short if it will fit in 16 bits. aoqi@0: // The decoder looks at datalen to decide whether to unpack short or jint. aoqi@0: // Most relocation records are quite simple, containing at most two ints. aoqi@0: aoqi@0: static bool is_short(jint x) { return x == (short)x; } aoqi@0: static short* add_short(short* p, int x) { *p++ = x; return p; } aoqi@0: static short* add_jint (short* p, jint x) { aoqi@0: *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x); aoqi@0: return p; aoqi@0: } aoqi@0: static short* add_var_int(short* p, jint x) { // add a variable-width int aoqi@0: if (is_short(x)) p = add_short(p, x); aoqi@0: else p = add_jint (p, x); aoqi@0: return p; aoqi@0: } aoqi@0: aoqi@0: static short* pack_1_int_to(short* p, jint x0) { aoqi@0: // Format is one of: [] [x] [Xx] aoqi@0: if (x0 != 0) p = add_var_int(p, x0); aoqi@0: return p; aoqi@0: } aoqi@0: int unpack_1_int() { aoqi@0: assert(datalen() <= 2, "too much data"); aoqi@0: return relocInfo::jint_data_at(0, data(), datalen()); aoqi@0: } aoqi@0: aoqi@0: // With two ints, the short form is used only if both ints are short. aoqi@0: short* pack_2_ints_to(short* p, jint x0, jint x1) { aoqi@0: // Format is one of: [] [x y?] [Xx Y?y] aoqi@0: if (x0 == 0 && x1 == 0) { aoqi@0: // no halfwords needed to store zeroes aoqi@0: } else if (is_short(x0) && is_short(x1)) { aoqi@0: // 1-2 halfwords needed to store shorts aoqi@0: p = add_short(p, x0); if (x1!=0) p = add_short(p, x1); aoqi@0: } else { aoqi@0: // 3-4 halfwords needed to store jints aoqi@0: p = add_jint(p, x0); p = add_var_int(p, x1); aoqi@0: } aoqi@0: return p; aoqi@0: } aoqi@0: void unpack_2_ints(jint& x0, jint& x1) { aoqi@0: int dlen = datalen(); aoqi@0: short* dp = data(); aoqi@0: if (dlen <= 2) { aoqi@0: x0 = relocInfo::short_data_at(0, dp, dlen); aoqi@0: x1 = relocInfo::short_data_at(1, dp, dlen); aoqi@0: } else { aoqi@0: assert(dlen <= 4, "too much data"); aoqi@0: x0 = relocInfo::jint_data_at(0, dp, dlen); aoqi@0: x1 = relocInfo::jint_data_at(2, dp, dlen); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected: aoqi@0: // platform-dependent utilities for decoding and patching instructions aoqi@0: void pd_set_data_value (address x, intptr_t off, bool verify_only = false); // a set or mem-ref aoqi@0: void pd_verify_data_value (address x, intptr_t off) { pd_set_data_value(x, off, true); } aoqi@0: address pd_call_destination (address orig_addr = NULL); aoqi@0: void pd_set_call_destination (address x); aoqi@0: aoqi@0: // this extracts the address of an address in the code stream instead of the reloc data aoqi@0: address* pd_address_in_code (); aoqi@0: aoqi@0: // this extracts an address from the code stream instead of the reloc data aoqi@0: address pd_get_address_from_code (); aoqi@0: aoqi@0: // these convert from byte offsets, to scaled offsets, to addresses aoqi@0: static jint scaled_offset(address x, address base) { aoqi@0: int byte_offset = x - base; aoqi@0: int offset = -byte_offset / relocInfo::addr_unit(); aoqi@0: assert(address_from_scaled_offset(offset, base) == x, "just checkin'"); aoqi@0: return offset; aoqi@0: } aoqi@0: static jint scaled_offset_null_special(address x, address base) { aoqi@0: // Some relocations treat offset=0 as meaning NULL. aoqi@0: // Handle this extra convention carefully. aoqi@0: if (x == NULL) return 0; aoqi@0: assert(x != base, "offset must not be zero"); aoqi@0: return scaled_offset(x, base); aoqi@0: } aoqi@0: static address address_from_scaled_offset(jint offset, address base) { aoqi@0: int byte_offset = -( offset * relocInfo::addr_unit() ); aoqi@0: return base + byte_offset; aoqi@0: } aoqi@0: aoqi@0: // these convert between indexes and addresses in the runtime system aoqi@0: static int32_t runtime_address_to_index(address runtime_address); aoqi@0: static address index_to_runtime_address(int32_t index); aoqi@0: aoqi@0: // helpers for mapping between old and new addresses after a move or resize aoqi@0: address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest); aoqi@0: address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest); aoqi@0: void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false); aoqi@0: aoqi@0: public: aoqi@0: // accessors which only make sense for a bound Relocation aoqi@0: address addr() const { return binding()->addr(); } aoqi@0: nmethod* code() const { return binding()->code(); } aoqi@0: bool addr_in_const() const { return binding()->addr_in_const(); } aoqi@0: protected: aoqi@0: short* data() const { return binding()->data(); } aoqi@0: int datalen() const { return binding()->datalen(); } aoqi@0: int format() const { return binding()->format(); } aoqi@0: aoqi@0: public: aoqi@0: virtual relocInfo::relocType type() { return relocInfo::none; } aoqi@0: aoqi@0: // is it a call instruction? aoqi@0: virtual bool is_call() { return false; } aoqi@0: aoqi@0: // is it a data movement instruction? aoqi@0: virtual bool is_data() { return false; } aoqi@0: aoqi@0: // some relocations can compute their own values aoqi@0: virtual address value(); aoqi@0: aoqi@0: // all relocations are able to reassert their values aoqi@0: virtual void set_value(address x); aoqi@0: aoqi@0: virtual void clear_inline_cache() { } aoqi@0: aoqi@0: // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and aoqi@0: // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is aoqi@0: // probably a reasonable assumption, since empty caches simplifies code reloacation. aoqi@0: virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { } aoqi@0: aoqi@0: void print(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // certain inlines must be deferred until class Relocation is defined: aoqi@0: aoqi@0: inline RelocationHolder::RelocationHolder() { aoqi@0: // initialize the vtbl, just to keep things type-safe aoqi@0: new(*this) Relocation(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: inline RelocationHolder::RelocationHolder(Relocation* r) { aoqi@0: // wordwise copy from r (ok if it copies garbage after r) aoqi@0: for (int i = 0; i < _relocbuf_size; i++) { aoqi@0: _relocbuf[i] = ((void**)r)[i]; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: relocInfo::relocType RelocationHolder::type() const { aoqi@0: return reloc()->type(); aoqi@0: } aoqi@0: aoqi@0: // A DataRelocation always points at a memory or load-constant instruction.. aoqi@0: // It is absolute on most machines, and the constant is split on RISCs. aoqi@0: // The specific subtypes are oop, external_word, and internal_word. aoqi@0: // By convention, the "value" does not include a separately reckoned "offset". aoqi@0: class DataRelocation : public Relocation { aoqi@0: public: aoqi@0: bool is_data() { return true; } aoqi@0: aoqi@0: // both target and offset must be computed somehow from relocation data aoqi@0: virtual int offset() { return 0; } aoqi@0: address value() = 0; aoqi@0: void set_value(address x) { set_value(x, offset()); } aoqi@0: void set_value(address x, intptr_t o) { aoqi@0: if (addr_in_const()) aoqi@0: *(address*)addr() = x; aoqi@0: else aoqi@0: pd_set_data_value(x, o); aoqi@0: } aoqi@0: void verify_value(address x) { aoqi@0: if (addr_in_const()) aoqi@0: assert(*(address*)addr() == x, "must agree"); aoqi@0: else aoqi@0: pd_verify_data_value(x, offset()); aoqi@0: } aoqi@0: aoqi@0: // The "o" (displacement) argument is relevant only to split relocations aoqi@0: // on RISC machines. In some CPUs (SPARC), the set-hi and set-lo ins'ns aoqi@0: // can encode more than 32 bits between them. This allows compilers to aoqi@0: // share set-hi instructions between addresses that differ by a small aoqi@0: // offset (e.g., different static variables in the same class). aoqi@0: // On such machines, the "x" argument to set_value on all set-lo aoqi@0: // instructions must be the same as the "x" argument for the aoqi@0: // corresponding set-hi instructions. The "o" arguments for the aoqi@0: // set-hi instructions are ignored, and must not affect the high-half aoqi@0: // immediate constant. The "o" arguments for the set-lo instructions are aoqi@0: // added into the low-half immediate constant, and must not overflow it. aoqi@0: }; aoqi@0: aoqi@0: // A CallRelocation always points at a call instruction. aoqi@0: // It is PC-relative on most machines. aoqi@0: class CallRelocation : public Relocation { aoqi@0: public: aoqi@0: bool is_call() { return true; } aoqi@0: aoqi@0: address destination() { return pd_call_destination(); } aoqi@0: void set_destination(address x); // pd_set_call_destination aoqi@0: aoqi@0: void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest); aoqi@0: address value() { return destination(); } aoqi@0: void set_value(address x) { set_destination(x); } aoqi@0: }; aoqi@0: aoqi@0: class oop_Relocation : public DataRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::oop_type; } aoqi@0: aoqi@0: public: aoqi@0: // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll] aoqi@0: // an oop in the CodeBlob's oop pool aoqi@0: static RelocationHolder spec(int oop_index, int offset = 0) { aoqi@0: assert(oop_index > 0, "must be a pool-resident oop"); aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) oop_Relocation(oop_index, offset); aoqi@0: return rh; aoqi@0: } aoqi@0: // an oop in the instruction stream aoqi@0: static RelocationHolder spec_for_immediate() { aoqi@0: const int oop_index = 0; aoqi@0: const int offset = 0; // if you want an offset, use the oop pool aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) oop_Relocation(oop_index, offset); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: jint _oop_index; // if > 0, index into CodeBlob::oop_at aoqi@0: jint _offset; // byte offset to apply to the oop itself aoqi@0: aoqi@0: oop_Relocation(int oop_index, int offset) { aoqi@0: _oop_index = oop_index; _offset = offset; aoqi@0: } aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: oop_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: int oop_index() { return _oop_index; } aoqi@0: int offset() { return _offset; } aoqi@0: aoqi@0: // data is packed in "2_ints" format: [i o] or [Ii Oo] aoqi@0: void pack_data_to(CodeSection* dest); aoqi@0: void unpack_data(); aoqi@0: aoqi@0: void fix_oop_relocation(); // reasserts oop value aoqi@0: aoqi@0: void verify_oop_relocation(); aoqi@0: aoqi@0: address value() { return (address) *oop_addr(); } aoqi@0: aoqi@0: bool oop_is_immediate() { return oop_index() == 0; } aoqi@0: aoqi@0: oop* oop_addr(); // addr or &pool[jint_data] aoqi@0: oop oop_value(); // *oop_addr aoqi@0: // Note: oop_value transparently converts Universe::non_oop_word to NULL. aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // copy of oop_Relocation for now but may delete stuff in both/either aoqi@0: class metadata_Relocation : public DataRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::metadata_type; } aoqi@0: aoqi@0: public: aoqi@0: // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll] aoqi@0: // an metadata in the CodeBlob's metadata pool aoqi@0: static RelocationHolder spec(int metadata_index, int offset = 0) { aoqi@0: assert(metadata_index > 0, "must be a pool-resident metadata"); aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) metadata_Relocation(metadata_index, offset); aoqi@0: return rh; aoqi@0: } aoqi@0: // an metadata in the instruction stream aoqi@0: static RelocationHolder spec_for_immediate() { aoqi@0: const int metadata_index = 0; aoqi@0: const int offset = 0; // if you want an offset, use the metadata pool aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) metadata_Relocation(metadata_index, offset); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: jint _metadata_index; // if > 0, index into nmethod::metadata_at aoqi@0: jint _offset; // byte offset to apply to the metadata itself aoqi@0: aoqi@0: metadata_Relocation(int metadata_index, int offset) { aoqi@0: _metadata_index = metadata_index; _offset = offset; aoqi@0: } aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: metadata_Relocation() { } aoqi@0: aoqi@0: // Fixes a Metadata pointer in the code. Most platforms embeds the aoqi@0: // Metadata pointer in the code at compile time so this is empty aoqi@0: // for them. aoqi@0: void pd_fix_value(address x); aoqi@0: aoqi@0: public: aoqi@0: int metadata_index() { return _metadata_index; } aoqi@0: int offset() { return _offset; } aoqi@0: aoqi@0: // data is packed in "2_ints" format: [i o] or [Ii Oo] aoqi@0: void pack_data_to(CodeSection* dest); aoqi@0: void unpack_data(); aoqi@0: aoqi@0: void fix_metadata_relocation(); // reasserts metadata value aoqi@0: aoqi@0: void verify_metadata_relocation(); aoqi@0: aoqi@0: address value() { return (address) *metadata_addr(); } aoqi@0: aoqi@0: bool metadata_is_immediate() { return metadata_index() == 0; } aoqi@0: aoqi@0: Metadata** metadata_addr(); // addr or &pool[jint_data] aoqi@0: Metadata* metadata_value(); // *metadata_addr aoqi@0: // Note: metadata_value transparently converts Universe::non_metadata_word to NULL. aoqi@0: }; aoqi@0: aoqi@1: #ifdef MIPS64 aoqi@1: // to handle the set_last_java_frame pc aoqi@1: class internal_pc_Relocation : public Relocation { aoqi@1: relocInfo::relocType type() { return relocInfo::internal_pc_type; } aoqi@1: public: aoqi@1: address pc() {pd_get_address_from_code();} aoqi@1: //void fix_relocation_at_move(intptr_t delta); aoqi@1: void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest); aoqi@1: }; aoqi@1: #endif aoqi@0: aoqi@0: class virtual_call_Relocation : public CallRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::virtual_call_type; } aoqi@0: aoqi@0: public: aoqi@0: // "cached_value" points to the first associated set-oop. aoqi@0: // The oop_limit helps find the last associated set-oop. aoqi@0: // (See comments at the top of this file.) aoqi@0: static RelocationHolder spec(address cached_value) { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) virtual_call_Relocation(cached_value); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: virtual_call_Relocation(address cached_value) { aoqi@0: _cached_value = cached_value; aoqi@0: assert(cached_value != NULL, "first oop address must be specified"); aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: address _cached_value; // location of set-value instruction aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: virtual_call_Relocation() { } aoqi@0: aoqi@0: aoqi@0: public: aoqi@0: address cached_value(); aoqi@0: aoqi@0: // data is packed as scaled offsets in "2_ints" format: [f l] or [Ff Ll] aoqi@0: // oop_limit is set to 0 if the limit falls somewhere within the call. aoqi@0: // When unpacking, a zero oop_limit is taken to refer to the end of the call. aoqi@0: // (This has the effect of bringing in the call's delay slot on SPARC.) aoqi@0: void pack_data_to(CodeSection* dest); aoqi@0: void unpack_data(); aoqi@0: aoqi@0: void clear_inline_cache(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class opt_virtual_call_Relocation : public CallRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec() { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) opt_virtual_call_Relocation(); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: friend class RelocIterator; aoqi@0: opt_virtual_call_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: void clear_inline_cache(); aoqi@0: aoqi@0: // find the matching static_stub aoqi@0: address static_stub(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class static_call_Relocation : public CallRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::static_call_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec() { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) static_call_Relocation(); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: friend class RelocIterator; aoqi@0: static_call_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: void clear_inline_cache(); aoqi@0: aoqi@0: // find the matching static_stub aoqi@0: address static_stub(); aoqi@0: }; aoqi@0: aoqi@0: class static_stub_Relocation : public Relocation { aoqi@0: relocInfo::relocType type() { return relocInfo::static_stub_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec(address static_call) { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) static_stub_Relocation(static_call); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: address _static_call; // location of corresponding static_call aoqi@0: aoqi@0: static_stub_Relocation(address static_call) { aoqi@0: _static_call = static_call; aoqi@0: } aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: static_stub_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: void clear_inline_cache(); aoqi@0: aoqi@0: address static_call() { return _static_call; } aoqi@0: aoqi@0: // data is packed as a scaled offset in "1_int" format: [c] or [Cc] aoqi@0: void pack_data_to(CodeSection* dest); aoqi@0: void unpack_data(); aoqi@0: }; aoqi@0: aoqi@0: class runtime_call_Relocation : public CallRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::runtime_call_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec() { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) runtime_call_Relocation(); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: friend class RelocIterator; aoqi@0: runtime_call_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: }; aoqi@0: aoqi@0: // Trampoline Relocations. aoqi@0: // A trampoline allows to encode a small branch in the code, even if there aoqi@0: // is the chance that this branch can not reach all possible code locations. aoqi@0: // If the relocation finds that a branch is too far for the instruction aoqi@0: // in the code, it can patch it to jump to the trampoline where is aoqi@0: // sufficient space for a far branch. Needed on PPC. aoqi@0: class trampoline_stub_Relocation : public Relocation { aoqi@0: relocInfo::relocType type() { return relocInfo::trampoline_stub_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec(address static_call) { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: return (new (rh) trampoline_stub_Relocation(static_call)); aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: address _owner; // Address of the NativeCall that owns the trampoline. aoqi@0: aoqi@0: trampoline_stub_Relocation(address owner) { aoqi@0: _owner = owner; aoqi@0: } aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: trampoline_stub_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // Return the address of the NativeCall that owns the trampoline. aoqi@0: address owner() { return _owner; } aoqi@0: aoqi@0: void pack_data_to(CodeSection * dest); aoqi@0: void unpack_data(); aoqi@0: aoqi@0: // Find the trampoline stub for a call. aoqi@0: static address get_trampoline_for(address call, nmethod* code); aoqi@0: }; aoqi@0: aoqi@0: class external_word_Relocation : public DataRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::external_word_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec(address target) { aoqi@0: assert(target != NULL, "must not be null"); aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) external_word_Relocation(target); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: // Use this one where all 32/64 bits of the target live in the code stream. aoqi@0: // The target must be an intptr_t, and must be absolute (not relative). aoqi@0: static RelocationHolder spec_for_immediate() { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) external_word_Relocation(NULL); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: // Some address looking values aren't safe to treat as relocations aoqi@0: // and should just be treated as constants. aoqi@0: static bool can_be_relocated(address target) { aoqi@0: return target != NULL && !is_reloc_index((intptr_t)target); aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: address _target; // address in runtime aoqi@0: aoqi@0: external_word_Relocation(address target) { aoqi@0: _target = target; aoqi@0: } aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: external_word_Relocation() { } aoqi@0: aoqi@0: public: aoqi@0: // data is packed as a well-known address in "1_int" format: [a] or [Aa] aoqi@0: // The function runtime_address_to_index is used to turn full addresses aoqi@0: // to short indexes, if they are pre-registered by the stub mechanism. aoqi@0: // If the "a" value is 0 (i.e., _target is NULL), the address is stored aoqi@0: // in the code stream. See external_word_Relocation::target(). aoqi@0: void pack_data_to(CodeSection* dest); aoqi@0: void unpack_data(); aoqi@0: aoqi@0: void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest); aoqi@0: address target(); // if _target==NULL, fetch addr from code stream aoqi@0: address value() { return target(); } aoqi@0: }; aoqi@0: aoqi@0: class internal_word_Relocation : public DataRelocation { aoqi@0: relocInfo::relocType type() { return relocInfo::internal_word_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec(address target) { aoqi@0: assert(target != NULL, "must not be null"); aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) internal_word_Relocation(target); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: // use this one where all the bits of the target can fit in the code stream: aoqi@0: static RelocationHolder spec_for_immediate() { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) internal_word_Relocation(NULL); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: internal_word_Relocation(address target) { aoqi@0: _target = target; aoqi@0: _section = -1; // self-relative aoqi@0: } aoqi@0: aoqi@0: protected: aoqi@0: address _target; // address in CodeBlob aoqi@0: int _section; // section providing base address, if any aoqi@0: aoqi@0: friend class RelocIterator; aoqi@0: internal_word_Relocation() { } aoqi@0: aoqi@0: // bit-width of LSB field in packed offset, if section >= 0 aoqi@0: enum { section_width = 2 }; // must equal CodeBuffer::sect_bits aoqi@0: aoqi@0: public: aoqi@0: // data is packed as a scaled offset in "1_int" format: [o] or [Oo] aoqi@0: // If the "o" value is 0 (i.e., _target is NULL), the offset is stored aoqi@0: // in the code stream. See internal_word_Relocation::target(). aoqi@0: // If _section is not -1, it is appended to the low bits of the offset. aoqi@0: void pack_data_to(CodeSection* dest); aoqi@0: void unpack_data(); aoqi@0: aoqi@0: void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest); aoqi@0: address target(); // if _target==NULL, fetch addr from code stream aoqi@0: int section() { return _section; } aoqi@0: address value() { return target(); } aoqi@0: }; aoqi@0: aoqi@0: class section_word_Relocation : public internal_word_Relocation { aoqi@0: relocInfo::relocType type() { return relocInfo::section_word_type; } aoqi@0: aoqi@0: public: aoqi@0: static RelocationHolder spec(address target, int section) { aoqi@0: RelocationHolder rh = newHolder(); aoqi@0: new(rh) section_word_Relocation(target, section); aoqi@0: return rh; aoqi@0: } aoqi@0: aoqi@0: section_word_Relocation(address target, int section) { aoqi@0: assert(target != NULL, "must not be null"); aoqi@0: assert(section >= 0, "must be a valid section"); aoqi@0: _target = target; aoqi@0: _section = section; aoqi@0: } aoqi@0: aoqi@0: //void pack_data_to -- inherited aoqi@0: void unpack_data(); aoqi@0: aoqi@0: private: aoqi@0: friend class RelocIterator; aoqi@0: section_word_Relocation() { } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class poll_Relocation : public Relocation { aoqi@0: bool is_data() { return true; } aoqi@0: relocInfo::relocType type() { return relocInfo::poll_type; } aoqi@0: void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest); aoqi@0: }; aoqi@0: aoqi@0: class poll_return_Relocation : public Relocation { aoqi@0: bool is_data() { return true; } aoqi@0: relocInfo::relocType type() { return relocInfo::poll_return_type; } aoqi@0: void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest); aoqi@0: }; aoqi@0: aoqi@0: // We know all the xxx_Relocation classes, so now we can define these: aoqi@0: #define EACH_CASE(name) \ aoqi@0: inline name##_Relocation* RelocIterator::name##_reloc() { \ aoqi@0: assert(type() == relocInfo::name##_type, "type must agree"); \ aoqi@0: /* The purpose of the placed "new" is to re-use the same */ \ aoqi@0: /* stack storage for each new iteration. */ \ aoqi@0: name##_Relocation* r = new(_rh) name##_Relocation(); \ aoqi@0: r->set_binding(this); \ aoqi@0: r->name##_Relocation::unpack_data(); \ aoqi@0: return r; \ aoqi@0: } aoqi@0: APPLY_TO_RELOCATIONS(EACH_CASE); aoqi@0: #undef EACH_CASE aoqi@0: aoqi@0: inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) { aoqi@0: initialize(nm, begin, limit); aoqi@0: } aoqi@0: aoqi@0: #endif // SHARE_VM_CODE_RELOCINFO_HPP