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