2362N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 829N/A * This code is free software; you can redistribute it and/or modify it 829N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. 2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT 829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 829N/A * version 2 for more details (a copy is included in the LICENSE file that 829N/A * accompanied this code). 829N/A * You should have received a copy of the GNU General Public License version 829N/A * 2 along with this work; if not, write to the Free Software Foundation, 829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 829N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 829N/A// One element of an array of halfwords encoding compressed relocations. 829N/A// Also, the source of relocation types (relocInfo::oop_type, ...). 829N/A// A flyweight object representing a single relocation. 829N/A// It is fully unpacked from the compressed relocation array. 829N/A// oop_Relocation, ... (subclasses of Relocation) 829N/A// The location of some type-specific operations (oop_addr, ...). 829N/A// Also, the source of relocation specs (oop_Relocation::spec, ...). 829N/A// A ValueObj type which acts as a union holding a Relocation object. 829N/A// Represents a relocation spec passed into a CodeBuffer during assembly. 829N/A// A StackObj which iterates over the relocations associated with 829N/A// a range of code addresses. Can be used to operate a copy of code. 829N/A// PatchingRelocIterator 829N/A// Specialized subtype of RelocIterator which removes breakpoints 829N/A// temporarily during iteration, then restores them. 829N/A// An _internal_ type shared by packers and unpackers of relocations. 829N/A// It pastes together a RelocationHolder with some pointers into 829N/A// code and relocInfo streams. 829N/A// These hold enough information to read or write a value embedded in 829N/A// the instructions of an CodeBlob. They're used to update: 829N/A// 1) embedded oops (isOop() == true) 829N/A// 2) inline caches (isIC() == true) 829N/A// 3) runtime calls (isRuntimeCall() == true) 829N/A// 4) internal word ref (isInternalWord() == true) 829N/A// 5) external word ref (isExternalWord() == true) 829N/A// when objects move (GC) or if code moves (compacting the code heap). 829N/A// They are also used to patch the code (if a call site must change) 829N/A// A relocInfo is represented in 16 bits: 829N/A// 4 bits indicating the relocation type 829N/A// 12 bits indicating the offset from the previous relocInfo address 829N/A// The offsets accumulate along the relocInfo stream to encode the 829N/A// address within the CodeBlob, which is named RelocIterator::addr(). 829N/A// The address of a particular relocInfo always points to the first 829N/A// byte of the relevant instruction (and not to any of its subfields 829N/A// or embedded immediate constants). 829N/A// The offset value is scaled appropriately for the target machine. 829N/A// (See relocInfo_<arch>.hpp for the offset scaling.) 829N/A// On some machines, there may also be a "format" field which may provide 829N/A// additional information about the format of the instruction stream 829N/A// at the corresponding code address. The format value is usually zero. 829N/A// Any machine (such as Intel) whose instructions can sometimes contain 829N/A// more than one relocatable constant needs format codes to distinguish 829N/A// which operand goes with a given relocation. 829N/A// If the target machine needs N format bits, the offset has 12-N bits, 829N/A// the format is encoded between the offset and the type, and the 829N/A// relocInfo_<arch>.hpp file has manifest constants for the format codes. 829N/A// If the type is "data_prefix_tag" then the offset bits are further encoded, 829N/A// and in fact represent not a code-stream offset but some inline data. 829N/A// The data takes the form of a counted sequence of halfwords, which 829N/A// precedes the actual relocation record. (Clients never see it directly.) 829N/A// The interpetation of this extra data depends on the relocation type. 829N/A// On machines that have 32-bit immediate fields, there is usually 829N/A// little need for relocation "prefix" data, because the instruction stream 829N/A// is a perfectly reasonable place to store the value. On machines in 829N/A// which 32-bit values must be "split" across instructions, the relocation 829N/A// data is the "true" specification of the value, which is then applied 829N/A// to some field of the instruction (22 or 13 bits, on SPARC). 829N/A// Whenever the location of the CodeBlob changes, any PC-relative 829N/A// relocations, and any internal_word_type relocations, must be reapplied. 829N/A// After the GC runs, oop_type relocations must be reapplied. // Here are meanings of the types: // relocInfo::none -- a filler record // Instruction: The corresponding code address is ignored // Data: Any data prefix and format code are ignored // (This means that any relocInfo can be disabled by setting // its type to none. See relocInfo::remove.) // relocInfo::oop_type -- a reference to an oop // Value: an oop, or else the address (handle) of an oop // Instruction types: memory (load), set (load address) // Data: [] an oop stored in 4 bytes of instruction // [n] n is the index of an oop in the CodeBlob's oop pool // [[N]n l] and l is a byte offset to be applied to the oop // [Nn Ll] both index and offset may be 32 bits if necessary // Here is a special hack, used only by the old compiler: // [[N]n 00] the value is the __address__ of the nth oop in the pool // (Note that the offset allows optimal references to class variables.) // relocInfo::internal_word_type -- an address within the same CodeBlob // relocInfo::section_word_type -- same, but can refer to another section // Value: an address in the CodeBlob's code or constants section // Instruction types: memory (load), set (load address) // Data: [] stored in 4 bytes of instruction // [[L]l] a relative offset (see [About Offsets] below) // In the case of section_word_type, the offset is relative to a section // base address, and the section number (e.g., SECT_INSTS) is encoded // into the low two bits of the offset L. // relocInfo::external_word_type -- a fixed address in the runtime system // Instruction types: memory (load), set (load address) // Data: [] stored in 4 bytes of instruction // [n] the index of a "well-known" stub (usual case on RISC) // relocInfo::runtime_call_type -- a fixed subroutine in the runtime system // Instruction types: PC-relative call (or a PC-relative branch) // Data: [] stored in 4 bytes of instruction // relocInfo::static_call_type -- a static call // Value: an CodeBlob, a stub, or a fixup routine // Instruction types: a call // The identity of the callee is extracted from debugging information. // relocInfo::virtual_call_type -- a virtual call site (which includes an inline // Value: an CodeBlob, a stub, the interpreter, or a fixup routine // Instruction types: a call, plus some associated set-oop instructions // Data: [] the associated set-oops are adjacent to the call // [n] n is a relative offset to the first set-oop // [[N]n l] and l is a limit within which the set-oops occur // [Nn Ll] both n and l may be 32 bits if necessary // The identity of the callee is extracted from debugging information. // relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound // Same info as a static_call_type. We use a special type, so the handling of // virtuals and statics are separated. // The offset n points to the first set-oop. (See [About Offsets] below.) // In turn, the set-oop instruction specifies or contains an oop cell devoted // exclusively to the IC call, which can be patched along with the call. // The locations of any other set-oops are found by searching the relocation // information starting at the first set-oop, and continuing until all // relocations up through l have been inspected. The value l is another // relative offset. (Both n and l are relative to the call's first byte.) // The limit l of the search is exclusive. However, if it points within // the call (e.g., offset zero), it is adjusted to point after the call and // any associated machine-specific delay slot. // Since the offsets could be as wide as 32-bits, these conventions // put no restrictions whatever upon code reorganization. // The compiler is responsible for ensuring that transition from a clean // state to a monomorphic compiled state is MP-safe. This implies that // the system must respond well to intermediate states where a random // subset of the set-oops has been correctly from the clean state // upon entry to the VEP of the compiled method. In the case of a // machine (Intel) with a single set-oop instruction, the 32-bit // immediate field must not straddle a unit of memory coherence. // relocInfo::breakpoint_type -- a conditional breakpoint in the code // Instruction types: any whatsoever // The b is a bit-packed word representing the breakpoint's attributes. // The t is a target address which the breakpoint calls (when it is enabled). // The i... is a place to store one or two instruction words overwritten // by a trap, so that the breakpoint may be subsequently removed. // relocInfo::static_stub_type -- an extra stub for each static_call_type // Instruction types: a virtual call: { set_oop; jump; } // Data: [[N]n] the offset of the associated static_call reloc // This stub becomes the target of a static call which must be upgraded // to a virtual call (because the callee is interpreted). // See [About Offsets] below. // INSTRUCTIONS RELOC: TYPE PREFIX DATA // ------------ ---- ----------- // sethi %hi(myObject), R oop_type [n(myObject)] // ld [R+%lo(myObject)+fldOffset], R2 oop_type [n(myObject) fldOffset] // st R2, [R+%lo(myObject)+fldOffset] oop_type [n(myObject) fldOffset] // This uses 4 instruction words, 8 relocation halfwords, // and an entry (which is sharable) in the CodeBlob's oop pool, // for a total of 36 bytes. // Note that the compiler is responsible for ensuring the "fldOffset" when // added to "%lo(myObject)" does not overflow the immediate fields of the // [About Offsets] Relative offsets are supplied to this module as // positive byte offsets, but they may be internally stored scaled // and/or negated, depending on what is most compact for the target // system. Since the object pointed to by the offset typically // precedes the relocation address, it is profitable to store // these negative offsets as positive numbers, but this decision // is internal to the relocation information abstractions. none = 0,
// Used when no relocation should be generated static_stub_type =
5,
// stub-entry for static send (takes care of interpreter case) poll_type =
10,
// polling instruction for safepoints type_mask =
15 // A mask which selects only the above values // Put a bunch of assertions out-of-line. return (
const short*)(
this +
1); }
// Occasionally records of type relocInfo::none will appear in the stream. // We do not bother to filter these out, but clients should ignore them. // These records serve as "filler" in three ways: // - to skip large spans of unrelocated code (this is rare) // - to pad out the relocInfo array to the required oop alignment // - to disable old relocation information which is no longer applicable // Every non-prefix relocation may be preceded by at most one prefix, // which supplies 1 or more halfwords of associated data. Conventionally, // an int is represented by 0, 1, or 2 halfwords, depending on how // many bits are required to represent the value. (In addition, // if the sole halfword is a 10-bit unsigned number, it is made // "immediate" in the prefix header word itself. This optimization // is invisible outside this module.) // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value // Support routines for compilers. // This routine takes an infant relocInfo (unprefixed) and // edits in its prefix, if any. It also updates dest.locs_end. // This routine updates a prefix and returns the limit pointer. // It tries to compress the prefix from 32 to 16 bits, and if // successful returns a reduced "prefix_limit" pointer. // bit-packers for the data array: // As it happens, the bytes within the shorts are ordered natively, // but the shorts within the word are ordered big-endian. // This is an arbitrary choice, made this way mainly to ease debugging. // Update methods for relocation information // (since code is dynamically patched, we also need to dynamically update the relocation info) // Both methods takes old_type, so it is able to performe sanity checks on the information removed. // Machine dependent stuff // Derived constant, based on format_width which is PD: // Conservatively large estimate of maximum length (in shorts) // of any relocation record (probably breakpoints are largest). // Extended format is length prefix, data words, and tag/offset suffix. // Holder for flyweight relocation objects. // Although the flyweight subclasses are of varying sizes, // the holder is "one size fits all". // this preallocated memory must accommodate all subclasses of Relocation // (this number is assertion-checked in Relocation::operator new) // Add a constant offset to a relocation. Helper for class Address. // A RelocIterator iterates through the relocation information of a CodeBlob. // It is a variable BoundRelocation which is able to take on successive // values as it is advanced through a code stream. // RelocIterator iter(nm); // iter.reloc()->some_operation(); // RelocIterator iter(nm); // case relocInfo::oop_type : // case relocInfo::ic_type : // case relocInfo::prim_type : // case relocInfo::uncommon_type : // case relocInfo::runtime_call_type : // case relocInfo::internal_word_type: // case relocInfo::external_word_type: enum {
SECT_LIMIT =
3 };
// must be equal to CodeBuffer::SECT_LIMIT, checked in ctor friend class relocInfo;
// for change_reloc_info_for_address only relocInfo*
_end;
// end marker; we're done iterating when _current == _end address _addr;
// instruction to which the relocation applies short _databuf;
// spare buffer for compressed data short*
_data;
// pointer to the relocation's data short _datalen;
// number of halfwords in _data char _format;
// position within the instruction // Base addresses needed to compute targets of section_word_type relocs. // make an uninitialized one, for PatchingRelocIterator: // get next reloc info, return !eos // The address points to the affected displacement part of the instruction. // For RISC, this is just the whole instruction. // For Intel, this is an unaligned 32-bit word. // type-specific relocation accessors: oop_Relocation* oop_reloc(), etc. // generic relocation accessor; switches on type to call the above // CodeBlob's have relocation indexes for faster random access: // Store an index into [dest_start+dest_count..dest_end). // At dest_start[0..dest_count] is the actual relocation information. // Everything else up to dest_end is free space for the index. // A Relocation is a flyweight object allocated within a RelocationHolder. // It represents the relocation data of relocation record. // So, the RelocIterator unpacks relocInfos into Relocations. // When a relocation has been created by a RelocIterator, // this field is non-null. It allows the relocation to know // its context, such as the address to which it applies. // make a generic relocation for a given type (if possible) // here is the type-specific hook which writes relocation data: // here is the type-specific hook which reads (unpacks) relocation data: // Helper functions for pack_data_to() and unpack_data(). // Most of the compression logic is confined here. // (The "immediate data" mechanism of relocInfo works independently // of this stuff, and acts to further compress most 1-word data prefixes.) // A variable-width int is encoded as a short if it will fit in 16 bits. // The decoder looks at datalen to decide whether to unpack short or jint. // Most relocation records are quite simple, containing at most two ints. static short*
add_short(
short* p,
int x) { *p++ = x;
return p; }
// Format is one of: [] [x] [Xx] // With two ints, the short form is used only if both ints are short. // Format is one of: [] [x y?] [Xx Y?y] if (
x0 == 0 &&
x1 == 0) {
// no halfwords needed to store zeroes // 1-2 halfwords needed to store shorts // 3-4 halfwords needed to store jints // platform-dependent utilities for decoding and patching instructions // this extracts the address of an address in the code stream instead of the reloc data // this extracts an address from the code stream instead of the reloc data // these convert from byte offsets, to scaled offsets, to addresses // Some relocations treat offset=0 as meaning NULL. // Handle this extra convention carefully. // these convert between indexes and addresses in the runtime system // helpers for mapping between old and new addresses after a move or resize // accessors which only make sense for a bound Relocation // is it a call instruction? virtual bool is_call() {
return false; }
// is it a data movement instruction? virtual bool is_data() {
return false; }
// some relocations can compute their own values // all relocations are able to reassert their values // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is // probably a reasonable assumption, since empty caches simplifies code reloacation. // certain inlines must be deferred until class Relocation is defined: // initialize the vtbl, just to keep things type-safe // wordwise copy from r (ok if it copies garbage after r) // A DataRelocation always points at a memory or load-constant instruction.. // It is absolute on most machines, and the constant is split on RISCs. // The specific subtypes are oop, external_word, and internal_word. // By convention, the "value" does not include a separately reckoned "offset". // both target and offset must be computed somehow from relocation data virtual int offset() {
return 0; }
// The "o" (displacement) argument is relevant only to split relocations // on RISC machines. In some CPUs (SPARC), the set-hi and set-lo ins'ns // can encode more than 32 bits between them. This allows compilers to // share set-hi instructions between addresses that differ by a small // offset (e.g., different static variables in the same class). // On such machines, the "x" argument to set_value on all set-lo // instructions must be the same as the "x" argument for the // corresponding set-hi instructions. The "o" arguments for the // set-hi instructions are ignored, and must not affect the high-half // immediate constant. The "o" arguments for the set-lo instructions are // added into the low-half immediate constant, and must not overflow it. // A CallRelocation always points at a call instruction. // It is PC-relative on most machines. // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll] // an oop in the CodeBlob's oop pool // an oop in the instruction stream const int offset = 0;
// if you want an offset, use the oop pool jint _offset;
// byte offset to apply to the oop itself // data is packed in "2_ints" format: [i o] or [Ii Oo] // Note: oop_value transparently converts Universe::non_oop_word to NULL. // "first_oop" points to the first associated set-oop. // The oop_limit helps find the last associated set-oop. // (See comments at the top of this file.) // data is packed as scaled offsets in "2_ints" format: [f l] or [Ff Ll] // oop_limit is set to 0 if the limit falls somewhere within the call. // When unpacking, a zero oop_limit is taken to refer to the end of the call. // (This has the effect of bringing in the call's delay slot on SPARC.) // Figure out where an ic_call is hiding, given a set-oop or call. // Either ic_call or first_oop must be non-null; the other is deduced. // Code if non-NULL must be the nmethod, else it is deduced. // The address of the patchable oop is also deduced. // The returned iterator will enumerate over the oops and the ic_call, // as well as any other relocations that happen to be in that span of code. // Recognize relevant set_oops with: oop_reloc()->oop_addr() == oop_addr. // find the matching static_stub // find the matching static_stub // data is packed as a scaled offset in "1_int" format: [c] or [Cc] // Use this one where all 32/64 bits of the target live in the code stream. // The target must be an intptr_t, and must be absolute (not relative). // Some address looking values aren't safe to treat as relocations // and should just be treated as constants. // data is packed as a well-known address in "1_int" format: [a] or [Aa] // The function runtime_address_to_index is used to turn full addresses // to short indexes, if they are pre-registered by the stub mechanism. // If the "a" value is 0 (i.e., _target is NULL), the address is stored // in the code stream. See external_word_Relocation::target(). address target();
// if _target==NULL, fetch addr from code stream // use this one where all the bits of the target can fit in the code stream: int _section;
// section providing base address, if any // bit-width of LSB field in packed offset, if section >= 0 // data is packed as a scaled offset in "1_int" format: [o] or [Oo] // If the "o" value is 0 (i.e., _target is NULL), the offset is stored // in the code stream. See internal_word_Relocation::target(). // If _section is not -1, it is appended to the low bits of the offset. address target();
// if _target==NULL, fetch addr from code stream //void pack_data_to -- inherited // attributes which affect the interpretation of the data: internal_attr =
0x0020,
// the target is an internal addr (local stub) // states which can change over time: enabled_state =
0x0100,
// breakpoint must be active in running code active_state =
0x0200,
// breakpoint instruction actually in code kind_mask =
0x000F,
// mask for extracting kind high_bit =
0x4000 // extra bit which is always set // If target is NULL, 32 bits are reserved for a later set_target(). // We require every bits value to NOT to fit into relocInfo::datalen_width, // because we are going to actually store state in the reloc, and so // cannot allow it to be compressed (and hence copied by the iterator). short _bits;
// bit-encoded kind, attrs, & state void set_enabled(
bool b);
// to activate, you must also say set_active void set_active(
bool b);
// actually inserts bpt (must be enabled 1st) // data is packed as 16 bits, followed by the target (1 or 2 words), followed // if necessary by empty storage for saving away original instruction bytes. // during certain operations, breakpoints must be out of the way: assert(!
active(),
"cannot perform relocation on enabled breakpoints");
// We know all the xxx_Relocation classes, so now we can define these: /* The purpose of the placed "new" is to re-use the same */ \
/* stack storage for each new iteration. */ \
// if you are going to patch code, you should use this subclass of void prepass();
// deactivates all breakpoints void postpass();
// reactivates all enabled breakpoints // do not copy these puppies; it would have unpredictable side effects // these are private and have no bodies defined because they should not be called #
endif // SHARE_VM_CODE_RELOCINFO_HPP