Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/relocInfo.hpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_CODE_RELOCINFO_HPP25#define SHARE_VM_CODE_RELOCINFO_HPP2627#include "memory/allocation.hpp"28#include "utilities/top.hpp"2930class NativeMovConstReg;3132// Types in this file:33// relocInfo34// One element of an array of halfwords encoding compressed relocations.35// Also, the source of relocation types (relocInfo::oop_type, ...).36// Relocation37// A flyweight object representing a single relocation.38// It is fully unpacked from the compressed relocation array.39// metadata_Relocation, ... (subclasses of Relocation)40// The location of some type-specific operations (metadata_addr, ...).41// Also, the source of relocation specs (metadata_Relocation::spec, ...).42// oop_Relocation, ... (subclasses of Relocation)43// oops in the code stream (strings, class loaders)44// Also, the source of relocation specs (oop_Relocation::spec, ...).45// RelocationHolder46// A ValueObj type which acts as a union holding a Relocation object.47// Represents a relocation spec passed into a CodeBuffer during assembly.48// RelocIterator49// A StackObj which iterates over the relocations associated with50// a range of code addresses. Can be used to operate a copy of code.51// BoundRelocation52// An _internal_ type shared by packers and unpackers of relocations.53// It pastes together a RelocationHolder with some pointers into54// code and relocInfo streams.555657// Notes on relocType:58//59// These hold enough information to read or write a value embedded in60// the instructions of an CodeBlob. They're used to update:61//62// 1) embedded oops (isOop() == true)63// 2) inline caches (isIC() == true)64// 3) runtime calls (isRuntimeCall() == true)65// 4) internal word ref (isInternalWord() == true)66// 5) external word ref (isExternalWord() == true)67//68// when objects move (GC) or if code moves (compacting the code heap).69// They are also used to patch the code (if a call site must change)70//71// A relocInfo is represented in 16 bits:72// 4 bits indicating the relocation type73// 12 bits indicating the offset from the previous relocInfo address74//75// The offsets accumulate along the relocInfo stream to encode the76// address within the CodeBlob, which is named RelocIterator::addr().77// The address of a particular relocInfo always points to the first78// byte of the relevant instruction (and not to any of its subfields79// or embedded immediate constants).80//81// The offset value is scaled appropriately for the target machine.82// (See relocInfo_<arch>.hpp for the offset scaling.)83//84// On some machines, there may also be a "format" field which may provide85// additional information about the format of the instruction stream86// at the corresponding code address. The format value is usually zero.87// Any machine (such as Intel) whose instructions can sometimes contain88// more than one relocatable constant needs format codes to distinguish89// which operand goes with a given relocation.90//91// If the target machine needs N format bits, the offset has 12-N bits,92// the format is encoded between the offset and the type, and the93// relocInfo_<arch>.hpp file has manifest constants for the format codes.94//95// If the type is "data_prefix_tag" then the offset bits are further encoded,96// and in fact represent not a code-stream offset but some inline data.97// The data takes the form of a counted sequence of halfwords, which98// precedes the actual relocation record. (Clients never see it directly.)99// The interpetation of this extra data depends on the relocation type.100//101// On machines that have 32-bit immediate fields, there is usually102// little need for relocation "prefix" data, because the instruction stream103// is a perfectly reasonable place to store the value. On machines in104// which 32-bit values must be "split" across instructions, the relocation105// data is the "true" specification of the value, which is then applied106// to some field of the instruction (22 or 13 bits, on SPARC).107//108// Whenever the location of the CodeBlob changes, any PC-relative109// relocations, and any internal_word_type relocations, must be reapplied.110// After the GC runs, oop_type relocations must be reapplied.111//112//113// Here are meanings of the types:114//115// relocInfo::none -- a filler record116// Value: none117// Instruction: The corresponding code address is ignored118// Data: Any data prefix and format code are ignored119// (This means that any relocInfo can be disabled by setting120// its type to none. See relocInfo::remove.)121//122// relocInfo::oop_type, relocInfo::metadata_type -- a reference to an oop or meta data123// Value: an oop, or else the address (handle) of an oop124// Instruction types: memory (load), set (load address)125// Data: [] an oop stored in 4 bytes of instruction126// [n] n is the index of an oop in the CodeBlob's oop pool127// [[N]n l] and l is a byte offset to be applied to the oop128// [Nn Ll] both index and offset may be 32 bits if necessary129// Here is a special hack, used only by the old compiler:130// [[N]n 00] the value is the __address__ of the nth oop in the pool131// (Note that the offset allows optimal references to class variables.)132//133// relocInfo::internal_word_type -- an address within the same CodeBlob134// relocInfo::section_word_type -- same, but can refer to another section135// Value: an address in the CodeBlob's code or constants section136// Instruction types: memory (load), set (load address)137// Data: [] stored in 4 bytes of instruction138// [[L]l] a relative offset (see [About Offsets] below)139// In the case of section_word_type, the offset is relative to a section140// base address, and the section number (e.g., SECT_INSTS) is encoded141// into the low two bits of the offset L.142//143// relocInfo::external_word_type -- a fixed address in the runtime system144// Value: an address145// Instruction types: memory (load), set (load address)146// Data: [] stored in 4 bytes of instruction147// [n] the index of a "well-known" stub (usual case on RISC)148// [Ll] a 32-bit address149//150// relocInfo::runtime_call_type -- a fixed subroutine in the runtime system151// Value: an address152// Instruction types: PC-relative call (or a PC-relative branch)153// Data: [] stored in 4 bytes of instruction154//155// relocInfo::static_call_type -- a static call156// Value: an CodeBlob, a stub, or a fixup routine157// Instruction types: a call158// Data: []159// The identity of the callee is extracted from debugging information.160// //%note reloc_3161//162// relocInfo::virtual_call_type -- a virtual call site (which includes an inline163// cache)164// Value: an CodeBlob, a stub, the interpreter, or a fixup routine165// Instruction types: a call, plus some associated set-oop instructions166// Data: [] the associated set-oops are adjacent to the call167// [n] n is a relative offset to the first set-oop168// [[N]n l] and l is a limit within which the set-oops occur169// [Nn Ll] both n and l may be 32 bits if necessary170// The identity of the callee is extracted from debugging information.171//172// relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound173//174// Same info as a static_call_type. We use a special type, so the handling of175// virtuals and statics are separated.176//177//178// The offset n points to the first set-oop. (See [About Offsets] below.)179// In turn, the set-oop instruction specifies or contains an oop cell devoted180// exclusively to the IC call, which can be patched along with the call.181//182// The locations of any other set-oops are found by searching the relocation183// information starting at the first set-oop, and continuing until all184// relocations up through l have been inspected. The value l is another185// relative offset. (Both n and l are relative to the call's first byte.)186//187// The limit l of the search is exclusive. However, if it points within188// the call (e.g., offset zero), it is adjusted to point after the call and189// any associated machine-specific delay slot.190//191// Since the offsets could be as wide as 32-bits, these conventions192// put no restrictions whatever upon code reorganization.193//194// The compiler is responsible for ensuring that transition from a clean195// state to a monomorphic compiled state is MP-safe. This implies that196// the system must respond well to intermediate states where a random197// subset of the set-oops has been correctly from the clean state198// upon entry to the VEP of the compiled method. In the case of a199// machine (Intel) with a single set-oop instruction, the 32-bit200// immediate field must not straddle a unit of memory coherence.201// //%note reloc_3202//203// relocInfo::static_stub_type -- an extra stub for each static_call_type204// Value: none205// Instruction types: a virtual call: { set_oop; jump; }206// Data: [[N]n] the offset of the associated static_call reloc207// This stub becomes the target of a static call which must be upgraded208// to a virtual call (because the callee is interpreted).209// See [About Offsets] below.210// //%note reloc_2211//212// For example:213//214// INSTRUCTIONS RELOC: TYPE PREFIX DATA215// ------------ ---- -----------216// sethi %hi(myObject), R oop_type [n(myObject)]217// ld [R+%lo(myObject)+fldOffset], R2 oop_type [n(myObject) fldOffset]218// add R2, 1, R2219// st R2, [R+%lo(myObject)+fldOffset] oop_type [n(myObject) fldOffset]220//%note reloc_1221//222// This uses 4 instruction words, 8 relocation halfwords,223// and an entry (which is sharable) in the CodeBlob's oop pool,224// for a total of 36 bytes.225//226// Note that the compiler is responsible for ensuring the "fldOffset" when227// added to "%lo(myObject)" does not overflow the immediate fields of the228// memory instructions.229//230//231// [About Offsets] Relative offsets are supplied to this module as232// positive byte offsets, but they may be internally stored scaled233// and/or negated, depending on what is most compact for the target234// system. Since the object pointed to by the offset typically235// precedes the relocation address, it is profitable to store236// these negative offsets as positive numbers, but this decision237// is internal to the relocation information abstractions.238//239240class Relocation;241class CodeBuffer;242class CodeSection;243class RelocIterator;244245class relocInfo VALUE_OBJ_CLASS_SPEC {246friend class RelocIterator;247public:248enum relocType {249none = 0, // Used when no relocation should be generated250oop_type = 1, // embedded oop251virtual_call_type = 2, // a standard inline cache call for a virtual send252opt_virtual_call_type = 3, // a virtual call that has been statically bound (i.e., no IC cache)253static_call_type = 4, // a static send254static_stub_type = 5, // stub-entry for static send (takes care of interpreter case)255runtime_call_type = 6, // call to fixed external routine256external_word_type = 7, // reference to fixed external address257internal_word_type = 8, // reference within the current code blob258section_word_type = 9, // internal, but a cross-section reference259poll_type = 10, // polling instruction for safepoints260poll_return_type = 11, // polling instruction for safepoints at return261metadata_type = 12, // metadata that used to be oops262trampoline_stub_type = 13, // stub-entry for trampoline263yet_unused_type_1 = 14, // Still unused264data_prefix_tag = 15, // tag for a prefix (carries data arguments)265type_mask = 15 // A mask which selects only the above values266};267268protected:269unsigned short _value;270271enum RawBitsToken { RAW_BITS };272relocInfo(relocType type, RawBitsToken ignore, int bits)273: _value((type << nontype_width) + bits) { }274275relocInfo(relocType type, RawBitsToken ignore, int off, int f)276: _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }277278public:279// constructor280relocInfo(relocType type, int offset, int format = 0)281#ifndef ASSERT282{283(*this) = relocInfo(type, RAW_BITS, offset, format);284}285#else286// Put a bunch of assertions out-of-line.287;288#endif289290#define APPLY_TO_RELOCATIONS(visitor) \291visitor(oop) \292visitor(metadata) \293visitor(virtual_call) \294visitor(opt_virtual_call) \295visitor(static_call) \296visitor(static_stub) \297visitor(runtime_call) \298visitor(external_word) \299visitor(internal_word) \300visitor(poll) \301visitor(poll_return) \302visitor(section_word) \303visitor(trampoline_stub) \304305306public:307enum {308value_width = sizeof(unsigned short) * BitsPerByte,309type_width = 4, // == log2(type_mask+1)310nontype_width = value_width - type_width,311datalen_width = nontype_width-1,312datalen_tag = 1 << datalen_width, // or-ed into _value313datalen_limit = 1 << datalen_width,314datalen_mask = (1 << datalen_width)-1315};316317// accessors318public:319relocType type() const { return (relocType)((unsigned)_value >> nontype_width); }320int format() const { return format_mask==0? 0: format_mask &321((unsigned)_value >> offset_width); }322int addr_offset() const { assert(!is_prefix(), "must have offset");323return (_value & offset_mask)*offset_unit; }324325protected:326const short* data() const { assert(is_datalen(), "must have data");327return (const short*)(this + 1); }328int datalen() const { assert(is_datalen(), "must have data");329return (_value & datalen_mask); }330int immediate() const { assert(is_immediate(), "must have immed");331return (_value & datalen_mask); }332public:333static int addr_unit() { return offset_unit; }334static int offset_limit() { return (1 << offset_width) * offset_unit; }335336void set_type(relocType type);337void set_format(int format);338339void remove() { set_type(none); }340341protected:342bool is_none() const { return type() == none; }343bool is_prefix() const { return type() == data_prefix_tag; }344bool is_datalen() const { assert(is_prefix(), "must be prefix");345return (_value & datalen_tag) != 0; }346bool is_immediate() const { assert(is_prefix(), "must be prefix");347return (_value & datalen_tag) == 0; }348349public:350// Occasionally records of type relocInfo::none will appear in the stream.351// We do not bother to filter these out, but clients should ignore them.352// These records serve as "filler" in three ways:353// - to skip large spans of unrelocated code (this is rare)354// - to pad out the relocInfo array to the required oop alignment355// - to disable old relocation information which is no longer applicable356357inline friend relocInfo filler_relocInfo();358359// Every non-prefix relocation may be preceded by at most one prefix,360// which supplies 1 or more halfwords of associated data. Conventionally,361// an int is represented by 0, 1, or 2 halfwords, depending on how362// many bits are required to represent the value. (In addition,363// if the sole halfword is a 10-bit unsigned number, it is made364// "immediate" in the prefix header word itself. This optimization365// is invisible outside this module.)366367inline friend relocInfo prefix_relocInfo(int datalen);368369protected:370// an immediate relocInfo optimizes a prefix with one 10-bit unsigned value371static relocInfo immediate_relocInfo(int data0) {372assert(fits_into_immediate(data0), "data0 in limits");373return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);374}375static bool fits_into_immediate(int data0) {376return (data0 >= 0 && data0 < datalen_limit);377}378379public:380// Support routines for compilers.381382// This routine takes an infant relocInfo (unprefixed) and383// edits in its prefix, if any. It also updates dest.locs_end.384void initialize(CodeSection* dest, Relocation* reloc);385386// This routine updates a prefix and returns the limit pointer.387// It tries to compress the prefix from 32 to 16 bits, and if388// successful returns a reduced "prefix_limit" pointer.389relocInfo* finish_prefix(short* prefix_limit);390391// bit-packers for the data array:392393// As it happens, the bytes within the shorts are ordered natively,394// but the shorts within the word are ordered big-endian.395// This is an arbitrary choice, made this way mainly to ease debugging.396static int data0_from_int(jint x) { return x >> value_width; }397static int data1_from_int(jint x) { return (short)x; }398static jint jint_from_data(short* data) {399return (data[0] << value_width) + (unsigned short)data[1];400}401402static jint short_data_at(int n, short* data, int datalen) {403return datalen > n ? data[n] : 0;404}405406static jint jint_data_at(int n, short* data, int datalen) {407return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);408}409410// Update methods for relocation information411// (since code is dynamically patched, we also need to dynamically update the relocation info)412// Both methods takes old_type, so it is able to performe sanity checks on the information removed.413static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);414static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);415416// Machine dependent stuff417#ifdef TARGET_ARCH_x86418# include "relocInfo_x86.hpp"419#endif420#ifdef TARGET_ARCH_aarch32421# include "relocInfo_aarch32.hpp"422#endif423#ifdef TARGET_ARCH_aarch64424# include "relocInfo_aarch64.hpp"425#endif426#ifdef TARGET_ARCH_sparc427# include "relocInfo_sparc.hpp"428#endif429#ifdef TARGET_ARCH_zero430# include "relocInfo_zero.hpp"431#endif432#ifdef TARGET_ARCH_arm433# include "relocInfo_arm.hpp"434#endif435#ifdef TARGET_ARCH_ppc436# include "relocInfo_ppc.hpp"437#endif438439440protected:441// Derived constant, based on format_width which is PD:442enum {443offset_width = nontype_width - format_width,444offset_mask = (1<<offset_width) - 1,445format_mask = (1<<format_width) - 1446};447public:448enum {449// Conservatively large estimate of maximum length (in shorts)450// of any relocation record.451// Extended format is length prefix, data words, and tag/offset suffix.452length_limit = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,453have_format = format_width > 0454};455};456457#define FORWARD_DECLARE_EACH_CLASS(name) \458class name##_Relocation;459APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)460#undef FORWARD_DECLARE_EACH_CLASS461462463464inline relocInfo filler_relocInfo() {465return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);466}467468inline relocInfo prefix_relocInfo(int datalen = 0) {469assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");470return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);471}472473474// Holder for flyweight relocation objects.475// Although the flyweight subclasses are of varying sizes,476// the holder is "one size fits all".477class RelocationHolder VALUE_OBJ_CLASS_SPEC {478friend class Relocation;479friend class CodeSection;480481private:482// this preallocated memory must accommodate all subclasses of Relocation483// (this number is assertion-checked in Relocation::operator new)484enum { _relocbuf_size = 5 };485void* _relocbuf[ _relocbuf_size ];486487public:488Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }489inline relocInfo::relocType type() const;490491// Add a constant offset to a relocation. Helper for class Address.492RelocationHolder plus(int offset) const;493494inline RelocationHolder(); // initializes type to none495496inline RelocationHolder(Relocation* r); // make a copy497498static const RelocationHolder none;499};500501// A RelocIterator iterates through the relocation information of a CodeBlob.502// It is a variable BoundRelocation which is able to take on successive503// values as it is advanced through a code stream.504// Usage:505// RelocIterator iter(nm);506// while (iter.next()) {507// iter.reloc()->some_operation();508// }509// or:510// RelocIterator iter(nm);511// while (iter.next()) {512// switch (iter.type()) {513// case relocInfo::oop_type :514// case relocInfo::ic_type :515// case relocInfo::prim_type :516// case relocInfo::uncommon_type :517// case relocInfo::runtime_call_type :518// case relocInfo::internal_word_type:519// case relocInfo::external_word_type:520// ...521// }522// }523524class RelocIterator : public StackObj {525enum { SECT_LIMIT = 3 }; // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor526friend class Relocation;527friend class relocInfo; // for change_reloc_info_for_address only528typedef relocInfo::relocType relocType;529530private:531address _limit; // stop producing relocations after this _addr532relocInfo* _current; // the current relocation information533relocInfo* _end; // end marker; we're done iterating when _current == _end534nmethod* _code; // compiled method containing _addr535address _addr; // instruction to which the relocation applies536short _databuf; // spare buffer for compressed data537short* _data; // pointer to the relocation's data538short _datalen; // number of halfwords in _data539char _format; // position within the instruction540541// Base addresses needed to compute targets of section_word_type relocs.542address _section_start[SECT_LIMIT];543address _section_end [SECT_LIMIT];544545void set_has_current(bool b) {546_datalen = !b ? -1 : 0;547debug_only(_data = NULL);548}549void set_current(relocInfo& ri) {550_current = &ri;551set_has_current(true);552}553554RelocationHolder _rh; // where the current relocation is allocated555556relocInfo* current() const { assert(has_current(), "must have current");557return _current; }558559void set_limits(address begin, address limit);560561void advance_over_prefix(); // helper method562563void initialize_misc();564565void initialize(nmethod* nm, address begin, address limit);566567RelocIterator() { initialize_misc(); }568569public:570// constructor571RelocIterator(nmethod* nm, address begin = NULL, address limit = NULL);572RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);573574// get next reloc info, return !eos575bool next() {576_current++;577assert(_current <= _end, "must not overrun relocInfo");578if (_current == _end) {579set_has_current(false);580return false;581}582set_has_current(true);583584if (_current->is_prefix()) {585advance_over_prefix();586assert(!current()->is_prefix(), "only one prefix at a time");587}588589_addr += _current->addr_offset();590591if (_limit != NULL && _addr >= _limit) {592set_has_current(false);593return false;594}595596if (relocInfo::have_format) _format = current()->format();597return true;598}599600// accessors601address limit() const { return _limit; }602void set_limit(address x);603relocType type() const { return current()->type(); }604int format() const { return (relocInfo::have_format) ? current()->format() : 0; }605address addr() const { return _addr; }606nmethod* code() const { return _code; }607short* data() const { return _data; }608int datalen() const { return _datalen; }609bool has_current() const { return _datalen >= 0; }610611void set_addr(address addr) { _addr = addr; }612bool addr_in_const() const;613614address section_start(int n) const {615assert(_section_start[n], "must be initialized");616return _section_start[n];617}618address section_end(int n) const {619assert(_section_end[n], "must be initialized");620return _section_end[n];621}622623// The address points to the affected displacement part of the instruction.624// For RISC, this is just the whole instruction.625// For Intel, this is an unaligned 32-bit word.626627// type-specific relocation accessors: oop_Relocation* oop_reloc(), etc.628#define EACH_TYPE(name) \629inline name##_Relocation* name##_reloc();630APPLY_TO_RELOCATIONS(EACH_TYPE)631#undef EACH_TYPE632// generic relocation accessor; switches on type to call the above633Relocation* reloc();634635// CodeBlob's have relocation indexes for faster random access:636static int locs_and_index_size(int code_size, int locs_size);637// Store an index into [dest_start+dest_count..dest_end).638// At dest_start[0..dest_count] is the actual relocation information.639// Everything else up to dest_end is free space for the index.640static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);641642#ifndef PRODUCT643public:644void print();645void print_current();646#endif647};648649650// A Relocation is a flyweight object allocated within a RelocationHolder.651// It represents the relocation data of relocation record.652// So, the RelocIterator unpacks relocInfos into Relocations.653654class Relocation VALUE_OBJ_CLASS_SPEC {655friend class RelocationHolder;656friend class RelocIterator;657658private:659static void guarantee_size();660661// When a relocation has been created by a RelocIterator,662// this field is non-null. It allows the relocation to know663// its context, such as the address to which it applies.664RelocIterator* _binding;665666protected:667RelocIterator* binding() const {668assert(_binding != NULL, "must be bound");669return _binding;670}671void set_binding(RelocIterator* b) {672assert(_binding == NULL, "must be unbound");673_binding = b;674assert(_binding != NULL, "must now be bound");675}676677Relocation() {678_binding = NULL;679}680681static RelocationHolder newHolder() {682return RelocationHolder();683}684685public:686void* operator new(size_t size, const RelocationHolder& holder) throw() {687if (size > sizeof(holder._relocbuf)) guarantee_size();688assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");689return holder.reloc();690}691692// make a generic relocation for a given type (if possible)693static RelocationHolder spec_simple(relocInfo::relocType rtype);694695// here is the type-specific hook which writes relocation data:696virtual void pack_data_to(CodeSection* dest) { }697698// here is the type-specific hook which reads (unpacks) relocation data:699virtual void unpack_data() {700assert(datalen()==0 || type()==relocInfo::none, "no data here");701}702703static bool is_reloc_index(intptr_t index) {704return 0 < index && index < os::vm_page_size();705}706707protected:708// Helper functions for pack_data_to() and unpack_data().709710// Most of the compression logic is confined here.711// (The "immediate data" mechanism of relocInfo works independently712// of this stuff, and acts to further compress most 1-word data prefixes.)713714// A variable-width int is encoded as a short if it will fit in 16 bits.715// The decoder looks at datalen to decide whether to unpack short or jint.716// Most relocation records are quite simple, containing at most two ints.717718static bool is_short(jint x) { return x == (short)x; }719static short* add_short(short* p, int x) { *p++ = x; return p; }720static short* add_jint (short* p, jint x) {721*p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);722return p;723}724static short* add_var_int(short* p, jint x) { // add a variable-width int725if (is_short(x)) p = add_short(p, x);726else p = add_jint (p, x);727return p;728}729730static short* pack_1_int_to(short* p, jint x0) {731// Format is one of: [] [x] [Xx]732if (x0 != 0) p = add_var_int(p, x0);733return p;734}735int unpack_1_int() {736assert(datalen() <= 2, "too much data");737return relocInfo::jint_data_at(0, data(), datalen());738}739740// With two ints, the short form is used only if both ints are short.741short* pack_2_ints_to(short* p, jint x0, jint x1) {742// Format is one of: [] [x y?] [Xx Y?y]743if (x0 == 0 && x1 == 0) {744// no halfwords needed to store zeroes745} else if (is_short(x0) && is_short(x1)) {746// 1-2 halfwords needed to store shorts747p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);748} else {749// 3-4 halfwords needed to store jints750p = add_jint(p, x0); p = add_var_int(p, x1);751}752return p;753}754void unpack_2_ints(jint& x0, jint& x1) {755int dlen = datalen();756short* dp = data();757if (dlen <= 2) {758x0 = relocInfo::short_data_at(0, dp, dlen);759x1 = relocInfo::short_data_at(1, dp, dlen);760} else {761assert(dlen <= 4, "too much data");762x0 = relocInfo::jint_data_at(0, dp, dlen);763x1 = relocInfo::jint_data_at(2, dp, dlen);764}765}766767protected:768// platform-dependent utilities for decoding and patching instructions769void pd_set_data_value (address x, intptr_t off, bool verify_only = false); // a set or mem-ref770void pd_verify_data_value (address x, intptr_t off) { pd_set_data_value(x, off, true); }771address pd_call_destination (address orig_addr = NULL);772void pd_set_call_destination (address x);773774// this extracts the address of an address in the code stream instead of the reloc data775address* pd_address_in_code ();776777// this extracts an address from the code stream instead of the reloc data778address pd_get_address_from_code ();779780// these convert from byte offsets, to scaled offsets, to addresses781static jint scaled_offset(address x, address base) {782int byte_offset = x - base;783int offset = -byte_offset / relocInfo::addr_unit();784assert(address_from_scaled_offset(offset, base) == x, "just checkin'");785return offset;786}787static jint scaled_offset_null_special(address x, address base) {788// Some relocations treat offset=0 as meaning NULL.789// Handle this extra convention carefully.790if (x == NULL) return 0;791assert(x != base, "offset must not be zero");792return scaled_offset(x, base);793}794static address address_from_scaled_offset(jint offset, address base) {795int byte_offset = -( offset * relocInfo::addr_unit() );796return base + byte_offset;797}798799// these convert between indexes and addresses in the runtime system800static int32_t runtime_address_to_index(address runtime_address);801static address index_to_runtime_address(int32_t index);802803// helpers for mapping between old and new addresses after a move or resize804address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);805address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);806void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);807808public:809// accessors which only make sense for a bound Relocation810address addr() const { return binding()->addr(); }811nmethod* code() const { return binding()->code(); }812bool addr_in_const() const { return binding()->addr_in_const(); }813protected:814short* data() const { return binding()->data(); }815int datalen() const { return binding()->datalen(); }816int format() const { return binding()->format(); }817818public:819virtual relocInfo::relocType type() { return relocInfo::none; }820821// is it a call instruction?822virtual bool is_call() { return false; }823824// is it a data movement instruction?825virtual bool is_data() { return false; }826827// some relocations can compute their own values828virtual address value();829830// all relocations are able to reassert their values831virtual void set_value(address x);832833virtual void clear_inline_cache() { }834835// This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and836// ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is837// probably a reasonable assumption, since empty caches simplifies code reloacation.838virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }839840void print();841};842843844// certain inlines must be deferred until class Relocation is defined:845846inline RelocationHolder::RelocationHolder() {847// initialize the vtbl, just to keep things type-safe848new(*this) Relocation();849}850851852inline RelocationHolder::RelocationHolder(Relocation* r) {853// wordwise copy from r (ok if it copies garbage after r)854for (int i = 0; i < _relocbuf_size; i++) {855_relocbuf[i] = ((void**)r)[i];856}857}858859860relocInfo::relocType RelocationHolder::type() const {861return reloc()->type();862}863864// A DataRelocation always points at a memory or load-constant instruction..865// It is absolute on most machines, and the constant is split on RISCs.866// The specific subtypes are oop, external_word, and internal_word.867// By convention, the "value" does not include a separately reckoned "offset".868class DataRelocation : public Relocation {869public:870bool is_data() { return true; }871872// both target and offset must be computed somehow from relocation data873virtual int offset() { return 0; }874address value() = 0;875void set_value(address x) { set_value(x, offset()); }876void set_value(address x, intptr_t o) {877if (addr_in_const())878*(address*)addr() = x;879else880pd_set_data_value(x, o);881}882void verify_value(address x) {883if (addr_in_const())884assert(*(address*)addr() == x, "must agree");885else886pd_verify_data_value(x, offset());887}888889// The "o" (displacement) argument is relevant only to split relocations890// on RISC machines. In some CPUs (SPARC), the set-hi and set-lo ins'ns891// can encode more than 32 bits between them. This allows compilers to892// share set-hi instructions between addresses that differ by a small893// offset (e.g., different static variables in the same class).894// On such machines, the "x" argument to set_value on all set-lo895// instructions must be the same as the "x" argument for the896// corresponding set-hi instructions. The "o" arguments for the897// set-hi instructions are ignored, and must not affect the high-half898// immediate constant. The "o" arguments for the set-lo instructions are899// added into the low-half immediate constant, and must not overflow it.900};901902// A CallRelocation always points at a call instruction.903// It is PC-relative on most machines.904class CallRelocation : public Relocation {905public:906bool is_call() { return true; }907908address destination() { return pd_call_destination(); }909void set_destination(address x); // pd_set_call_destination910911void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);912address value() { return destination(); }913void set_value(address x) { set_destination(x); }914};915916class oop_Relocation : public DataRelocation {917relocInfo::relocType type() { return relocInfo::oop_type; }918919public:920// encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll]921// an oop in the CodeBlob's oop pool922static RelocationHolder spec(int oop_index, int offset = 0) {923assert(oop_index > 0, "must be a pool-resident oop");924RelocationHolder rh = newHolder();925new(rh) oop_Relocation(oop_index, offset);926return rh;927}928// an oop in the instruction stream929static RelocationHolder spec_for_immediate() {930const int oop_index = 0;931const int offset = 0; // if you want an offset, use the oop pool932RelocationHolder rh = newHolder();933new(rh) oop_Relocation(oop_index, offset);934return rh;935}936937private:938jint _oop_index; // if > 0, index into CodeBlob::oop_at939jint _offset; // byte offset to apply to the oop itself940941oop_Relocation(int oop_index, int offset) {942_oop_index = oop_index; _offset = offset;943}944945friend class RelocIterator;946oop_Relocation() { }947948public:949int oop_index() { return _oop_index; }950int offset() { return _offset; }951952// data is packed in "2_ints" format: [i o] or [Ii Oo]953void pack_data_to(CodeSection* dest);954void unpack_data();955956void fix_oop_relocation(); // reasserts oop value957958void verify_oop_relocation();959960address value() { return (address) *oop_addr(); }961962bool oop_is_immediate() { return oop_index() == 0; }963964oop* oop_addr(); // addr or &pool[jint_data]965oop oop_value(); // *oop_addr966// Note: oop_value transparently converts Universe::non_oop_word to NULL.967};968969970// copy of oop_Relocation for now but may delete stuff in both/either971class metadata_Relocation : public DataRelocation {972relocInfo::relocType type() { return relocInfo::metadata_type; }973974public:975// encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll]976// an metadata in the CodeBlob's metadata pool977static RelocationHolder spec(int metadata_index, int offset = 0) {978assert(metadata_index > 0, "must be a pool-resident metadata");979RelocationHolder rh = newHolder();980new(rh) metadata_Relocation(metadata_index, offset);981return rh;982}983// an metadata in the instruction stream984static RelocationHolder spec_for_immediate() {985const int metadata_index = 0;986const int offset = 0; // if you want an offset, use the metadata pool987RelocationHolder rh = newHolder();988new(rh) metadata_Relocation(metadata_index, offset);989return rh;990}991992private:993jint _metadata_index; // if > 0, index into nmethod::metadata_at994jint _offset; // byte offset to apply to the metadata itself995996metadata_Relocation(int metadata_index, int offset) {997_metadata_index = metadata_index; _offset = offset;998}9991000friend class RelocIterator;1001metadata_Relocation() { }10021003// Fixes a Metadata pointer in the code. Most platforms embeds the1004// Metadata pointer in the code at compile time so this is empty1005// for them.1006void pd_fix_value(address x);10071008public:1009int metadata_index() { return _metadata_index; }1010int offset() { return _offset; }10111012// data is packed in "2_ints" format: [i o] or [Ii Oo]1013void pack_data_to(CodeSection* dest);1014void unpack_data();10151016void fix_metadata_relocation(); // reasserts metadata value10171018void verify_metadata_relocation();10191020address value() { return (address) *metadata_addr(); }10211022bool metadata_is_immediate() { return metadata_index() == 0; }10231024Metadata** metadata_addr(); // addr or &pool[jint_data]1025Metadata* metadata_value(); // *metadata_addr1026// Note: metadata_value transparently converts Universe::non_metadata_word to NULL.1027};102810291030class virtual_call_Relocation : public CallRelocation {1031relocInfo::relocType type() { return relocInfo::virtual_call_type; }10321033public:1034// "cached_value" points to the first associated set-oop.1035// The oop_limit helps find the last associated set-oop.1036// (See comments at the top of this file.)1037static RelocationHolder spec(address cached_value) {1038RelocationHolder rh = newHolder();1039new(rh) virtual_call_Relocation(cached_value);1040return rh;1041}10421043virtual_call_Relocation(address cached_value) {1044_cached_value = cached_value;1045assert(cached_value != NULL, "first oop address must be specified");1046}10471048private:1049address _cached_value; // location of set-value instruction10501051friend class RelocIterator;1052virtual_call_Relocation() { }105310541055public:1056address cached_value();10571058// data is packed as scaled offsets in "2_ints" format: [f l] or [Ff Ll]1059// oop_limit is set to 0 if the limit falls somewhere within the call.1060// When unpacking, a zero oop_limit is taken to refer to the end of the call.1061// (This has the effect of bringing in the call's delay slot on SPARC.)1062void pack_data_to(CodeSection* dest);1063void unpack_data();10641065void clear_inline_cache();1066};106710681069class opt_virtual_call_Relocation : public CallRelocation {1070relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }10711072public:1073static RelocationHolder spec() {1074RelocationHolder rh = newHolder();1075new(rh) opt_virtual_call_Relocation();1076return rh;1077}10781079private:1080friend class RelocIterator;1081opt_virtual_call_Relocation() { }10821083public:1084void clear_inline_cache();10851086// find the matching static_stub1087address static_stub();1088};108910901091class static_call_Relocation : public CallRelocation {1092relocInfo::relocType type() { return relocInfo::static_call_type; }10931094public:1095static RelocationHolder spec() {1096RelocationHolder rh = newHolder();1097new(rh) static_call_Relocation();1098return rh;1099}11001101private:1102friend class RelocIterator;1103static_call_Relocation() { }11041105public:1106void clear_inline_cache();11071108// find the matching static_stub1109address static_stub();1110};11111112class static_stub_Relocation : public Relocation {1113relocInfo::relocType type() { return relocInfo::static_stub_type; }11141115public:1116static RelocationHolder spec(address static_call) {1117RelocationHolder rh = newHolder();1118new(rh) static_stub_Relocation(static_call);1119return rh;1120}11211122private:1123address _static_call; // location of corresponding static_call11241125static_stub_Relocation(address static_call) {1126_static_call = static_call;1127}11281129friend class RelocIterator;1130static_stub_Relocation() { }11311132public:1133void clear_inline_cache();11341135address static_call() { return _static_call; }11361137// data is packed as a scaled offset in "1_int" format: [c] or [Cc]1138void pack_data_to(CodeSection* dest);1139void unpack_data();1140};11411142class runtime_call_Relocation : public CallRelocation {1143relocInfo::relocType type() { return relocInfo::runtime_call_type; }11441145public:1146static RelocationHolder spec() {1147RelocationHolder rh = newHolder();1148new(rh) runtime_call_Relocation();1149return rh;1150}11511152private:1153friend class RelocIterator;1154runtime_call_Relocation() { }11551156public:1157};11581159// Trampoline Relocations.1160// A trampoline allows to encode a small branch in the code, even if there1161// is the chance that this branch can not reach all possible code locations.1162// If the relocation finds that a branch is too far for the instruction1163// in the code, it can patch it to jump to the trampoline where is1164// sufficient space for a far branch. Needed on PPC.1165class trampoline_stub_Relocation : public Relocation {1166relocInfo::relocType type() { return relocInfo::trampoline_stub_type; }11671168public:1169static RelocationHolder spec(address static_call) {1170RelocationHolder rh = newHolder();1171return (new (rh) trampoline_stub_Relocation(static_call));1172}11731174private:1175address _owner; // Address of the NativeCall that owns the trampoline.11761177trampoline_stub_Relocation(address owner) {1178_owner = owner;1179}11801181friend class RelocIterator;1182trampoline_stub_Relocation() { }11831184public:11851186// Return the address of the NativeCall that owns the trampoline.1187address owner() { return _owner; }11881189void pack_data_to(CodeSection * dest);1190void unpack_data();11911192// Find the trampoline stub for a call.1193static address get_trampoline_for(address call, nmethod* code);1194};11951196class external_word_Relocation : public DataRelocation {1197relocInfo::relocType type() { return relocInfo::external_word_type; }11981199public:1200static RelocationHolder spec(address target) {1201assert(target != NULL, "must not be null");1202RelocationHolder rh = newHolder();1203new(rh) external_word_Relocation(target);1204return rh;1205}12061207// Use this one where all 32/64 bits of the target live in the code stream.1208// The target must be an intptr_t, and must be absolute (not relative).1209static RelocationHolder spec_for_immediate() {1210RelocationHolder rh = newHolder();1211new(rh) external_word_Relocation(NULL);1212return rh;1213}12141215// Some address looking values aren't safe to treat as relocations1216// and should just be treated as constants.1217static bool can_be_relocated(address target) {1218return target != NULL && !is_reloc_index((intptr_t)target);1219}12201221private:1222address _target; // address in runtime12231224external_word_Relocation(address target) {1225_target = target;1226}12271228friend class RelocIterator;1229external_word_Relocation() { }12301231public:1232// data is packed as a well-known address in "1_int" format: [a] or [Aa]1233// The function runtime_address_to_index is used to turn full addresses1234// to short indexes, if they are pre-registered by the stub mechanism.1235// If the "a" value is 0 (i.e., _target is NULL), the address is stored1236// in the code stream. See external_word_Relocation::target().1237void pack_data_to(CodeSection* dest);1238void unpack_data();12391240void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);1241address target(); // if _target==NULL, fetch addr from code stream1242address value() { return target(); }1243};12441245class internal_word_Relocation : public DataRelocation {1246relocInfo::relocType type() { return relocInfo::internal_word_type; }12471248public:1249static RelocationHolder spec(address target) {1250assert(target != NULL, "must not be null");1251RelocationHolder rh = newHolder();1252new(rh) internal_word_Relocation(target);1253return rh;1254}12551256// use this one where all the bits of the target can fit in the code stream:1257static RelocationHolder spec_for_immediate() {1258RelocationHolder rh = newHolder();1259new(rh) internal_word_Relocation(NULL);1260return rh;1261}12621263internal_word_Relocation(address target) {1264_target = target;1265_section = -1; // self-relative1266}12671268protected:1269address _target; // address in CodeBlob1270int _section; // section providing base address, if any12711272friend class RelocIterator;1273internal_word_Relocation() { }12741275// bit-width of LSB field in packed offset, if section >= 01276enum { section_width = 2 }; // must equal CodeBuffer::sect_bits12771278public:1279// data is packed as a scaled offset in "1_int" format: [o] or [Oo]1280// If the "o" value is 0 (i.e., _target is NULL), the offset is stored1281// in the code stream. See internal_word_Relocation::target().1282// If _section is not -1, it is appended to the low bits of the offset.1283void pack_data_to(CodeSection* dest);1284void unpack_data();12851286void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);1287address target(); // if _target==NULL, fetch addr from code stream1288int section() { return _section; }1289address value() { return target(); }1290};12911292class section_word_Relocation : public internal_word_Relocation {1293relocInfo::relocType type() { return relocInfo::section_word_type; }12941295public:1296static RelocationHolder spec(address target, int section) {1297RelocationHolder rh = newHolder();1298new(rh) section_word_Relocation(target, section);1299return rh;1300}13011302section_word_Relocation(address target, int section) {1303assert(target != NULL, "must not be null");1304assert(section >= 0, "must be a valid section");1305_target = target;1306_section = section;1307}13081309//void pack_data_to -- inherited1310void unpack_data();13111312private:1313friend class RelocIterator;1314section_word_Relocation() { }1315};131613171318class poll_Relocation : public Relocation {1319bool is_data() { return true; }1320relocInfo::relocType type() { return relocInfo::poll_type; }1321void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);1322};13231324class poll_return_Relocation : public Relocation {1325bool is_data() { return true; }1326relocInfo::relocType type() { return relocInfo::poll_return_type; }1327void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);1328};13291330// We know all the xxx_Relocation classes, so now we can define these:1331#define EACH_CASE(name) \1332inline name##_Relocation* RelocIterator::name##_reloc() { \1333assert(type() == relocInfo::name##_type, "type must agree"); \1334/* The purpose of the placed "new" is to re-use the same */ \1335/* stack storage for each new iteration. */ \1336name##_Relocation* r = new(_rh) name##_Relocation(); \1337r->set_binding(this); \1338r->name##_Relocation::unpack_data(); \1339return r; \1340}1341APPLY_TO_RELOCATIONS(EACH_CASE);1342#undef EACH_CASE13431344inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {1345initialize(nm, begin, limit);1346}13471348#endif // SHARE_VM_CODE_RELOCINFO_HPP134913501351