duke@435: /* coleenp@4037: * Copyright (c) 1997, 2012, 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: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "code/compiledIC.hpp" stefank@2314: #include "code/nmethod.hpp" stefank@2314: #include "code/relocInfo.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "runtime/stubCodeGenerator.hpp" stefank@2314: #include "utilities/copy.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "assembler_x86.inline.hpp" stefank@2314: # include "nativeInst_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "assembler_sparc.inline.hpp" stefank@2314: # include "nativeInst_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "assembler_zero.inline.hpp" stefank@2314: # include "nativeInst_zero.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "assembler_arm.inline.hpp" bobv@2508: # include "nativeInst_arm.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "assembler_ppc.inline.hpp" bobv@2508: # include "nativeInst_ppc.hpp" bobv@2508: #endif duke@435: duke@435: duke@435: const RelocationHolder RelocationHolder::none; // its type is relocInfo::none duke@435: duke@435: duke@435: // Implementation of relocInfo duke@435: duke@435: #ifdef ASSERT duke@435: relocInfo::relocInfo(relocType t, int off, int f) { duke@435: assert(t != data_prefix_tag, "cannot build a prefix this way"); duke@435: assert((t & type_mask) == t, "wrong type"); duke@435: assert((f & format_mask) == f, "wrong format"); duke@435: assert(off >= 0 && off < offset_limit(), "offset out off bounds"); duke@435: assert((off & (offset_unit-1)) == 0, "misaligned offset"); duke@435: (*this) = relocInfo(t, RAW_BITS, off, f); duke@435: } duke@435: #endif duke@435: duke@435: void relocInfo::initialize(CodeSection* dest, Relocation* reloc) { duke@435: relocInfo* data = this+1; // here's where the data might go duke@435: dest->set_locs_end(data); // sync end: the next call may read dest.locs_end duke@435: reloc->pack_data_to(dest); // maybe write data into locs, advancing locs_end duke@435: relocInfo* data_limit = dest->locs_end(); duke@435: if (data_limit > data) { duke@435: relocInfo suffix = (*this); duke@435: data_limit = this->finish_prefix((short*) data_limit); duke@435: // Finish up with the suffix. (Hack note: pack_data_to might edit this.) duke@435: *data_limit = suffix; duke@435: dest->set_locs_end(data_limit+1); duke@435: } duke@435: } duke@435: duke@435: relocInfo* relocInfo::finish_prefix(short* prefix_limit) { duke@435: assert(sizeof(relocInfo) == sizeof(short), "change this code"); duke@435: short* p = (short*)(this+1); duke@435: assert(prefix_limit >= p, "must be a valid span of data"); duke@435: int plen = prefix_limit - p; duke@435: if (plen == 0) { duke@435: debug_only(_value = 0xFFFF); duke@435: return this; // no data: remove self completely duke@435: } duke@435: if (plen == 1 && fits_into_immediate(p[0])) { duke@435: (*this) = immediate_relocInfo(p[0]); // move data inside self duke@435: return this+1; duke@435: } duke@435: // cannot compact, so just update the count and return the limit pointer duke@435: (*this) = prefix_relocInfo(plen); // write new datalen duke@435: assert(data() + datalen() == prefix_limit, "pointers must line up"); duke@435: return (relocInfo*)prefix_limit; duke@435: } duke@435: duke@435: duke@435: void relocInfo::set_type(relocType t) { duke@435: int old_offset = addr_offset(); duke@435: int old_format = format(); duke@435: (*this) = relocInfo(t, old_offset, old_format); duke@435: assert(type()==(int)t, "sanity check"); duke@435: assert(addr_offset()==old_offset, "sanity check"); duke@435: assert(format()==old_format, "sanity check"); duke@435: } duke@435: duke@435: duke@435: void relocInfo::set_format(int f) { duke@435: int old_offset = addr_offset(); duke@435: assert((f & format_mask) == f, "wrong format"); duke@435: _value = (_value & ~(format_mask << offset_width)) | (f << offset_width); duke@435: assert(addr_offset()==old_offset, "sanity check"); duke@435: } duke@435: duke@435: duke@435: void relocInfo::change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type) { duke@435: bool found = false; duke@435: while (itr->next() && !found) { duke@435: if (itr->addr() == pc) { duke@435: assert(itr->type()==old_type, "wrong relocInfo type found"); duke@435: itr->current()->set_type(new_type); duke@435: found=true; duke@435: } duke@435: } duke@435: assert(found, "no relocInfo found for pc"); duke@435: } duke@435: duke@435: duke@435: void relocInfo::remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type) { duke@435: change_reloc_info_for_address(itr, pc, old_type, none); duke@435: } duke@435: duke@435: duke@435: // ---------------------------------------------------------------------------------------------------- duke@435: // Implementation of RelocIterator duke@435: twisti@1918: void RelocIterator::initialize(nmethod* nm, address begin, address limit) { duke@435: initialize_misc(); duke@435: twisti@1918: if (nm == NULL && begin != NULL) { twisti@1918: // allow nmethod to be deduced from beginning address twisti@1918: CodeBlob* cb = CodeCache::find_blob(begin); twisti@1918: nm = cb->as_nmethod_or_null(); duke@435: } twisti@1918: assert(nm != NULL, "must be able to deduce nmethod from other arguments"); duke@435: twisti@1918: _code = nm; twisti@1918: _current = nm->relocation_begin() - 1; twisti@1918: _end = nm->relocation_end(); twisti@2117: _addr = nm->content_begin(); twisti@2117: twisti@2117: // Initialize code sections. twisti@2117: _section_start[CodeBuffer::SECT_CONSTS] = nm->consts_begin(); twisti@2117: _section_start[CodeBuffer::SECT_INSTS ] = nm->insts_begin() ; twisti@2117: _section_start[CodeBuffer::SECT_STUBS ] = nm->stub_begin() ; twisti@2117: twisti@2117: _section_end [CodeBuffer::SECT_CONSTS] = nm->consts_end() ; twisti@2117: _section_end [CodeBuffer::SECT_INSTS ] = nm->insts_end() ; twisti@2117: _section_end [CodeBuffer::SECT_STUBS ] = nm->stub_end() ; duke@435: duke@435: assert(!has_current(), "just checking"); twisti@2103: assert(begin == NULL || begin >= nm->code_begin(), "in bounds"); twisti@2103: assert(limit == NULL || limit <= nm->code_end(), "in bounds"); duke@435: set_limits(begin, limit); duke@435: } duke@435: duke@435: duke@435: RelocIterator::RelocIterator(CodeSection* cs, address begin, address limit) { duke@435: initialize_misc(); duke@435: duke@435: _current = cs->locs_start()-1; duke@435: _end = cs->locs_end(); duke@435: _addr = cs->start(); duke@435: _code = NULL; // Not cb->blob(); duke@435: duke@435: CodeBuffer* cb = cs->outer(); twisti@2117: assert((int) SECT_LIMIT == CodeBuffer::SECT_LIMIT, "my copy must be equal"); twisti@2117: for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) { twisti@2117: CodeSection* cs = cb->code_section(n); twisti@2117: _section_start[n] = cs->start(); twisti@2117: _section_end [n] = cs->end(); duke@435: } duke@435: duke@435: assert(!has_current(), "just checking"); duke@435: duke@435: assert(begin == NULL || begin >= cs->start(), "in bounds"); duke@435: assert(limit == NULL || limit <= cs->end(), "in bounds"); duke@435: set_limits(begin, limit); duke@435: } duke@435: duke@435: duke@435: enum { indexCardSize = 128 }; duke@435: struct RelocIndexEntry { duke@435: jint addr_offset; // offset from header_end of an addr() duke@435: jint reloc_offset; // offset from header_end of a relocInfo (prefix) duke@435: }; duke@435: duke@435: twisti@2117: bool RelocIterator::addr_in_const() const { twisti@2117: const int n = CodeBuffer::SECT_CONSTS; twisti@2117: return section_start(n) <= addr() && addr() < section_end(n); twisti@2117: } twisti@2117: twisti@2117: duke@435: static inline int num_cards(int code_size) { duke@435: return (code_size-1) / indexCardSize; duke@435: } duke@435: duke@435: duke@435: int RelocIterator::locs_and_index_size(int code_size, int locs_size) { duke@435: if (!UseRelocIndex) return locs_size; // no index duke@435: code_size = round_to(code_size, oopSize); duke@435: locs_size = round_to(locs_size, oopSize); duke@435: int index_size = num_cards(code_size) * sizeof(RelocIndexEntry); duke@435: // format of indexed relocs: duke@435: // relocation_begin: relocInfo ... duke@435: // index: (addr,reloc#) ... duke@435: // indexSize :relocation_end duke@435: return locs_size + index_size + BytesPerInt; duke@435: } duke@435: duke@435: duke@435: void RelocIterator::create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end) { duke@435: address relocation_begin = (address)dest_begin; duke@435: address relocation_end = (address)dest_end; duke@435: int total_size = relocation_end - relocation_begin; duke@435: int locs_size = dest_count * sizeof(relocInfo); duke@435: if (!UseRelocIndex) { duke@435: Copy::fill_to_bytes(relocation_begin + locs_size, total_size-locs_size, 0); duke@435: return; duke@435: } duke@435: int index_size = total_size - locs_size - BytesPerInt; // find out how much space is left duke@435: int ncards = index_size / sizeof(RelocIndexEntry); duke@435: assert(total_size == locs_size + index_size + BytesPerInt, "checkin'"); duke@435: assert(index_size >= 0 && index_size % sizeof(RelocIndexEntry) == 0, "checkin'"); duke@435: jint* index_size_addr = (jint*)relocation_end - 1; duke@435: duke@435: assert(sizeof(jint) == BytesPerInt, "change this code"); duke@435: duke@435: *index_size_addr = index_size; duke@435: if (index_size != 0) { duke@435: assert(index_size > 0, "checkin'"); duke@435: duke@435: RelocIndexEntry* index = (RelocIndexEntry *)(relocation_begin + locs_size); duke@435: assert(index == (RelocIndexEntry*)index_size_addr - ncards, "checkin'"); duke@435: duke@435: // walk over the relocations, and fill in index entries as we go duke@435: RelocIterator iter; duke@435: const address initial_addr = NULL; duke@435: relocInfo* const initial_current = dest_begin - 1; // biased by -1 like elsewhere duke@435: duke@435: iter._code = NULL; duke@435: iter._addr = initial_addr; duke@435: iter._limit = (address)(intptr_t)(ncards * indexCardSize); duke@435: iter._current = initial_current; duke@435: iter._end = dest_begin + dest_count; duke@435: duke@435: int i = 0; duke@435: address next_card_addr = (address)indexCardSize; duke@435: int addr_offset = 0; duke@435: int reloc_offset = 0; duke@435: while (true) { duke@435: // Checkpoint the iterator before advancing it. duke@435: addr_offset = iter._addr - initial_addr; duke@435: reloc_offset = iter._current - initial_current; duke@435: if (!iter.next()) break; duke@435: while (iter.addr() >= next_card_addr) { duke@435: index[i].addr_offset = addr_offset; duke@435: index[i].reloc_offset = reloc_offset; duke@435: i++; duke@435: next_card_addr += indexCardSize; duke@435: } duke@435: } duke@435: while (i < ncards) { duke@435: index[i].addr_offset = addr_offset; duke@435: index[i].reloc_offset = reloc_offset; duke@435: i++; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void RelocIterator::set_limits(address begin, address limit) { duke@435: int index_size = 0; duke@435: if (UseRelocIndex && _code != NULL) { duke@435: index_size = ((jint*)_end)[-1]; duke@435: _end = (relocInfo*)( (address)_end - index_size - BytesPerInt ); duke@435: } duke@435: duke@435: _limit = limit; duke@435: duke@435: // the limit affects this next stuff: duke@435: if (begin != NULL) { duke@435: #ifdef ASSERT duke@435: // In ASSERT mode we do not actually use the index, but simply duke@435: // check that its contents would have led us to the right answer. duke@435: address addrCheck = _addr; duke@435: relocInfo* infoCheck = _current; duke@435: #endif // ASSERT duke@435: if (index_size > 0) { duke@435: // skip ahead duke@435: RelocIndexEntry* index = (RelocIndexEntry*)_end; duke@435: RelocIndexEntry* index_limit = (RelocIndexEntry*)((address)index + index_size); twisti@2103: assert(_addr == _code->code_begin(), "_addr must be unadjusted"); duke@435: int card = (begin - _addr) / indexCardSize; duke@435: if (card > 0) { duke@435: if (index+card-1 < index_limit) index += card-1; duke@435: else index = index_limit - 1; duke@435: #ifdef ASSERT duke@435: addrCheck = _addr + index->addr_offset; duke@435: infoCheck = _current + index->reloc_offset; duke@435: #else duke@435: // Advance the iterator immediately to the last valid state duke@435: // for the previous card. Calling "next" will then advance duke@435: // it to the first item on the required card. duke@435: _addr += index->addr_offset; duke@435: _current += index->reloc_offset; duke@435: #endif // ASSERT duke@435: } duke@435: } duke@435: duke@435: relocInfo* backup; duke@435: address backup_addr; duke@435: while (true) { duke@435: backup = _current; duke@435: backup_addr = _addr; duke@435: #ifdef ASSERT duke@435: if (backup == infoCheck) { duke@435: assert(backup_addr == addrCheck, "must match"); addrCheck = NULL; infoCheck = NULL; duke@435: } else { duke@435: assert(addrCheck == NULL || backup_addr <= addrCheck, "must not pass addrCheck"); duke@435: } duke@435: #endif // ASSERT duke@435: if (!next() || addr() >= begin) break; duke@435: } duke@435: assert(addrCheck == NULL || addrCheck == backup_addr, "must have matched addrCheck"); duke@435: assert(infoCheck == NULL || infoCheck == backup, "must have matched infoCheck"); duke@435: // At this point, either we are at the first matching record, duke@435: // or else there is no such record, and !has_current(). duke@435: // In either case, revert to the immediatly preceding state. duke@435: _current = backup; duke@435: _addr = backup_addr; duke@435: set_has_current(false); duke@435: } duke@435: } duke@435: duke@435: duke@435: void RelocIterator::set_limit(address limit) { duke@435: address code_end = (address)code() + code()->size(); duke@435: assert(limit == NULL || limit <= code_end, "in bounds"); duke@435: _limit = limit; duke@435: } duke@435: duke@435: duke@435: void PatchingRelocIterator:: prepass() { duke@435: // turn breakpoints off during patching duke@435: _init_state = (*this); // save cursor duke@435: while (next()) { duke@435: if (type() == relocInfo::breakpoint_type) { duke@435: breakpoint_reloc()->set_active(false); duke@435: } duke@435: } duke@435: (RelocIterator&)(*this) = _init_state; // reset cursor for client duke@435: } duke@435: duke@435: duke@435: void PatchingRelocIterator:: postpass() { duke@435: // turn breakpoints back on after patching duke@435: (RelocIterator&)(*this) = _init_state; // reset cursor again duke@435: while (next()) { duke@435: if (type() == relocInfo::breakpoint_type) { duke@435: breakpoint_Relocation* bpt = breakpoint_reloc(); duke@435: bpt->set_active(bpt->enabled()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // All the strange bit-encodings are in here. duke@435: // The idea is to encode relocation data which are small integers duke@435: // very efficiently (a single extra halfword). Larger chunks of duke@435: // relocation data need a halfword header to hold their size. duke@435: void RelocIterator::advance_over_prefix() { duke@435: if (_current->is_datalen()) { duke@435: _data = (short*) _current->data(); duke@435: _datalen = _current->datalen(); duke@435: _current += _datalen + 1; // skip the embedded data & header duke@435: } else { duke@435: _databuf = _current->immediate(); duke@435: _data = &_databuf; duke@435: _datalen = 1; duke@435: _current++; // skip the header duke@435: } duke@435: // The client will see the following relocInfo, whatever that is. duke@435: // It is the reloc to which the preceding data applies. duke@435: } duke@435: duke@435: twisti@2117: void RelocIterator::initialize_misc() { twisti@2117: set_has_current(false); twisti@2117: for (int i = (int) CodeBuffer::SECT_FIRST; i < (int) CodeBuffer::SECT_LIMIT; i++) { twisti@2117: _section_start[i] = NULL; // these will be lazily computed, if needed twisti@2117: _section_end [i] = NULL; duke@435: } duke@435: } duke@435: duke@435: duke@435: Relocation* RelocIterator::reloc() { duke@435: // (take the "switch" out-of-line) duke@435: relocInfo::relocType t = type(); duke@435: if (false) {} duke@435: #define EACH_TYPE(name) \ duke@435: else if (t == relocInfo::name##_type) { \ duke@435: return name##_reloc(); \ duke@435: } duke@435: APPLY_TO_RELOCATIONS(EACH_TYPE); duke@435: #undef EACH_TYPE duke@435: assert(t == relocInfo::none, "must be padding"); duke@435: return new(_rh) Relocation(); duke@435: } duke@435: duke@435: duke@435: //////// Methods for flyweight Relocation types duke@435: duke@435: duke@435: RelocationHolder RelocationHolder::plus(int offset) const { duke@435: if (offset != 0) { duke@435: switch (type()) { duke@435: case relocInfo::none: duke@435: break; duke@435: case relocInfo::oop_type: duke@435: { duke@435: oop_Relocation* r = (oop_Relocation*)reloc(); duke@435: return oop_Relocation::spec(r->oop_index(), r->offset() + offset); duke@435: } coleenp@4037: case relocInfo::metadata_type: coleenp@4037: { coleenp@4037: metadata_Relocation* r = (metadata_Relocation*)reloc(); coleenp@4037: return metadata_Relocation::spec(r->metadata_index(), r->offset() + offset); coleenp@4037: } duke@435: default: duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: return (*this); duke@435: } duke@435: duke@435: duke@435: void Relocation::guarantee_size() { duke@435: guarantee(false, "Make _relocbuf bigger!"); duke@435: } duke@435: duke@435: // some relocations can compute their own values duke@435: address Relocation::value() { duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: void Relocation::set_value(address x) { duke@435: ShouldNotReachHere(); duke@435: } duke@435: duke@435: duke@435: RelocationHolder Relocation::spec_simple(relocInfo::relocType rtype) { duke@435: if (rtype == relocInfo::none) return RelocationHolder::none; duke@435: relocInfo ri = relocInfo(rtype, 0); duke@435: RelocIterator itr; duke@435: itr.set_current(ri); duke@435: itr.reloc(); duke@435: return itr._rh; duke@435: } duke@435: duke@435: int32_t Relocation::runtime_address_to_index(address runtime_address) { never@2737: assert(!is_reloc_index((intptr_t)runtime_address), "must not look like an index"); duke@435: duke@435: if (runtime_address == NULL) return 0; duke@435: duke@435: StubCodeDesc* p = StubCodeDesc::desc_for(runtime_address); duke@435: if (p != NULL && p->begin() == runtime_address) { never@2737: assert(is_reloc_index(p->index()), "there must not be too many stubs"); duke@435: return (int32_t)p->index(); duke@435: } else { duke@435: // Known "miscellaneous" non-stub pointers: duke@435: // os::get_polling_page(), SafepointSynchronize::address_of_state() duke@435: if (PrintRelocations) { duke@435: tty->print_cr("random unregistered address in relocInfo: " INTPTR_FORMAT, runtime_address); duke@435: } duke@435: #ifndef _LP64 duke@435: return (int32_t) (intptr_t)runtime_address; duke@435: #else duke@435: // didn't fit return non-index duke@435: return -1; duke@435: #endif /* _LP64 */ duke@435: } duke@435: } duke@435: duke@435: duke@435: address Relocation::index_to_runtime_address(int32_t index) { duke@435: if (index == 0) return NULL; duke@435: never@2737: if (is_reloc_index(index)) { duke@435: StubCodeDesc* p = StubCodeDesc::desc_for_index(index); duke@435: assert(p != NULL, "there must be a stub for this index"); duke@435: return p->begin(); duke@435: } else { duke@435: #ifndef _LP64 duke@435: // this only works on 32bit machines duke@435: return (address) ((intptr_t) index); duke@435: #else duke@435: fatal("Relocation::index_to_runtime_address, int32_t not pointer sized"); duke@435: return NULL; duke@435: #endif /* _LP64 */ duke@435: } duke@435: } duke@435: duke@435: address Relocation::old_addr_for(address newa, duke@435: const CodeBuffer* src, CodeBuffer* dest) { duke@435: int sect = dest->section_index_of(newa); duke@435: guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); duke@435: address ostart = src->code_section(sect)->start(); duke@435: address nstart = dest->code_section(sect)->start(); duke@435: return ostart + (newa - nstart); duke@435: } duke@435: duke@435: address Relocation::new_addr_for(address olda, duke@435: const CodeBuffer* src, CodeBuffer* dest) { duke@435: debug_only(const CodeBuffer* src0 = src); duke@435: int sect = CodeBuffer::SECT_NONE; duke@435: // Look for olda in the source buffer, and all previous incarnations duke@435: // if the source buffer has been expanded. duke@435: for (; src != NULL; src = src->before_expand()) { duke@435: sect = src->section_index_of(olda); duke@435: if (sect != CodeBuffer::SECT_NONE) break; duke@435: } duke@435: guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); duke@435: address ostart = src->code_section(sect)->start(); duke@435: address nstart = dest->code_section(sect)->start(); duke@435: return nstart + (olda - ostart); duke@435: } duke@435: duke@435: void Relocation::normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections) { duke@435: address addr0 = addr; duke@435: if (addr0 == NULL || dest->allocates2(addr0)) return; duke@435: CodeBuffer* cb = dest->outer(); duke@435: addr = new_addr_for(addr0, cb, cb); duke@435: assert(allow_other_sections || dest->contains2(addr), duke@435: "addr must be in required section"); duke@435: } duke@435: duke@435: duke@435: void CallRelocation::set_destination(address x) { duke@435: pd_set_call_destination(x); duke@435: } duke@435: duke@435: void CallRelocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { duke@435: // Usually a self-relative reference to an external routine. duke@435: // On some platforms, the reference is absolute (not self-relative). duke@435: // The enhanced use of pd_call_destination sorts this all out. duke@435: address orig_addr = old_addr_for(addr(), src, dest); duke@435: address callee = pd_call_destination(orig_addr); duke@435: // Reassert the callee address, this time in the new copy of the code. duke@435: pd_set_call_destination(callee); duke@435: } duke@435: duke@435: duke@435: //// pack/unpack methods duke@435: duke@435: void oop_Relocation::pack_data_to(CodeSection* dest) { duke@435: short* p = (short*) dest->locs_end(); duke@435: p = pack_2_ints_to(p, _oop_index, _offset); duke@435: dest->set_locs_end((relocInfo*) p); duke@435: } duke@435: duke@435: duke@435: void oop_Relocation::unpack_data() { duke@435: unpack_2_ints(_oop_index, _offset); duke@435: } duke@435: coleenp@4037: void metadata_Relocation::pack_data_to(CodeSection* dest) { coleenp@4037: short* p = (short*) dest->locs_end(); coleenp@4037: p = pack_2_ints_to(p, _metadata_index, _offset); coleenp@4037: dest->set_locs_end((relocInfo*) p); coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: void metadata_Relocation::unpack_data() { coleenp@4037: unpack_2_ints(_metadata_index, _offset); coleenp@4037: } coleenp@4037: duke@435: duke@435: void virtual_call_Relocation::pack_data_to(CodeSection* dest) { duke@435: short* p = (short*) dest->locs_end(); duke@435: address point = dest->locs_point(); duke@435: coleenp@4037: normalize_address(_cached_value, dest); coleenp@4037: jint x0 = scaled_offset_null_special(_cached_value, point); coleenp@4037: p = pack_1_int_to(p, x0); duke@435: dest->set_locs_end((relocInfo*) p); duke@435: } duke@435: duke@435: duke@435: void virtual_call_Relocation::unpack_data() { coleenp@4037: jint x0 = unpack_1_int(); duke@435: address point = addr(); coleenp@4037: _cached_value = x0==0? NULL: address_from_scaled_offset(x0, point); duke@435: } duke@435: duke@435: duke@435: void static_stub_Relocation::pack_data_to(CodeSection* dest) { duke@435: short* p = (short*) dest->locs_end(); duke@435: CodeSection* insts = dest->outer()->insts(); duke@435: normalize_address(_static_call, insts); duke@435: p = pack_1_int_to(p, scaled_offset(_static_call, insts->start())); duke@435: dest->set_locs_end((relocInfo*) p); duke@435: } duke@435: duke@435: void static_stub_Relocation::unpack_data() { duke@435: address base = binding()->section_start(CodeBuffer::SECT_INSTS); duke@435: _static_call = address_from_scaled_offset(unpack_1_int(), base); duke@435: } duke@435: duke@435: duke@435: void external_word_Relocation::pack_data_to(CodeSection* dest) { duke@435: short* p = (short*) dest->locs_end(); duke@435: int32_t index = runtime_address_to_index(_target); duke@435: #ifndef _LP64 duke@435: p = pack_1_int_to(p, index); duke@435: #else never@2737: if (is_reloc_index(index)) { duke@435: p = pack_2_ints_to(p, index, 0); duke@435: } else { duke@435: jlong t = (jlong) _target; duke@435: int32_t lo = low(t); duke@435: int32_t hi = high(t); duke@435: p = pack_2_ints_to(p, lo, hi); duke@435: DEBUG_ONLY(jlong t1 = jlong_from(hi, lo)); never@2737: assert(!is_reloc_index(t1) && (address) t1 == _target, "not symmetric"); duke@435: } duke@435: #endif /* _LP64 */ duke@435: dest->set_locs_end((relocInfo*) p); duke@435: } duke@435: duke@435: duke@435: void external_word_Relocation::unpack_data() { duke@435: #ifndef _LP64 duke@435: _target = index_to_runtime_address(unpack_1_int()); duke@435: #else duke@435: int32_t lo, hi; duke@435: unpack_2_ints(lo, hi); duke@435: jlong t = jlong_from(hi, lo);; never@2737: if (is_reloc_index(t)) { duke@435: _target = index_to_runtime_address(t); duke@435: } else { duke@435: _target = (address) t; duke@435: } duke@435: #endif /* _LP64 */ duke@435: } duke@435: duke@435: duke@435: void internal_word_Relocation::pack_data_to(CodeSection* dest) { duke@435: short* p = (short*) dest->locs_end(); duke@435: normalize_address(_target, dest, true); duke@435: duke@435: // Check whether my target address is valid within this section. duke@435: // If not, strengthen the relocation type to point to another section. duke@435: int sindex = _section; duke@435: if (sindex == CodeBuffer::SECT_NONE && _target != NULL duke@435: && (!dest->allocates(_target) || _target == dest->locs_point())) { duke@435: sindex = dest->outer()->section_index_of(_target); duke@435: guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere"); duke@435: relocInfo* base = dest->locs_end() - 1; duke@435: assert(base->type() == this->type(), "sanity"); duke@435: // Change the written type, to be section_word_type instead. duke@435: base->set_type(relocInfo::section_word_type); duke@435: } duke@435: duke@435: // Note: An internal_word relocation cannot refer to its own instruction, duke@435: // because we reserve "0" to mean that the pointer itself is embedded duke@435: // in the code stream. We use a section_word relocation for such cases. duke@435: duke@435: if (sindex == CodeBuffer::SECT_NONE) { duke@435: assert(type() == relocInfo::internal_word_type, "must be base class"); duke@435: guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section"); duke@435: jint x0 = scaled_offset_null_special(_target, dest->locs_point()); duke@435: assert(!(x0 == 0 && _target != NULL), "correct encoding of null target"); duke@435: p = pack_1_int_to(p, x0); duke@435: } else { duke@435: assert(_target != NULL, "sanity"); duke@435: CodeSection* sect = dest->outer()->code_section(sindex); duke@435: guarantee(sect->allocates2(_target), "must be in correct section"); duke@435: address base = sect->start(); duke@435: jint offset = scaled_offset(_target, base); duke@435: assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity"); duke@435: assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++"); duke@435: p = pack_1_int_to(p, (offset << section_width) | sindex); duke@435: } duke@435: duke@435: dest->set_locs_end((relocInfo*) p); duke@435: } duke@435: duke@435: duke@435: void internal_word_Relocation::unpack_data() { duke@435: jint x0 = unpack_1_int(); duke@435: _target = x0==0? NULL: address_from_scaled_offset(x0, addr()); duke@435: _section = CodeBuffer::SECT_NONE; duke@435: } duke@435: duke@435: duke@435: void section_word_Relocation::unpack_data() { duke@435: jint x = unpack_1_int(); duke@435: jint offset = (x >> section_width); duke@435: int sindex = (x & ((1<section_start(sindex); duke@435: duke@435: _section = sindex; duke@435: _target = address_from_scaled_offset(offset, base); duke@435: } duke@435: duke@435: duke@435: void breakpoint_Relocation::pack_data_to(CodeSection* dest) { duke@435: short* p = (short*) dest->locs_end(); duke@435: address point = dest->locs_point(); duke@435: duke@435: *p++ = _bits; duke@435: duke@435: assert(_target != NULL, "sanity"); duke@435: duke@435: if (internal()) normalize_address(_target, dest); duke@435: duke@435: jint target_bits = duke@435: (jint)( internal() ? scaled_offset (_target, point) duke@435: : runtime_address_to_index(_target) ); duke@435: if (settable()) { duke@435: // save space for set_target later duke@435: p = add_jint(p, target_bits); duke@435: } else { duke@435: p = add_var_int(p, target_bits); duke@435: } duke@435: duke@435: for (int i = 0; i < instrlen(); i++) { duke@435: // put placeholder words until bytes can be saved duke@435: p = add_short(p, (short)0x7777); duke@435: } duke@435: duke@435: dest->set_locs_end((relocInfo*) p); duke@435: } duke@435: duke@435: duke@435: void breakpoint_Relocation::unpack_data() { duke@435: _bits = live_bits(); duke@435: duke@435: int targetlen = datalen() - 1 - instrlen(); duke@435: jint target_bits = 0; duke@435: if (targetlen == 0) target_bits = 0; duke@435: else if (targetlen == 1) target_bits = *(data()+1); duke@435: else if (targetlen == 2) target_bits = relocInfo::jint_from_data(data()+1); duke@435: else { ShouldNotReachHere(); } duke@435: duke@435: _target = internal() ? address_from_scaled_offset(target_bits, addr()) duke@435: : index_to_runtime_address (target_bits); duke@435: } duke@435: duke@435: duke@435: //// miscellaneous methods duke@435: oop* oop_Relocation::oop_addr() { duke@435: int n = _oop_index; duke@435: if (n == 0) { duke@435: // oop is stored in the code stream duke@435: return (oop*) pd_address_in_code(); duke@435: } else { twisti@1918: // oop is stored in table at nmethod::oops_begin duke@435: return code()->oop_addr_at(n); duke@435: } duke@435: } duke@435: duke@435: duke@435: oop oop_Relocation::oop_value() { duke@435: oop v = *oop_addr(); duke@435: // clean inline caches store a special pseudo-null duke@435: if (v == (oop)Universe::non_oop_word()) v = NULL; duke@435: return v; duke@435: } duke@435: duke@435: duke@435: void oop_Relocation::fix_oop_relocation() { duke@435: if (!oop_is_immediate()) { duke@435: // get the oop from the pool, and re-insert it into the instruction: duke@435: set_value(value()); duke@435: } duke@435: } duke@435: duke@435: never@2657: void oop_Relocation::verify_oop_relocation() { never@2657: if (!oop_is_immediate()) { never@2657: // get the oop from the pool, and re-insert it into the instruction: never@2657: verify_value(value()); never@2657: } never@2657: } never@2657: coleenp@4037: // meta data versions coleenp@4037: Metadata** metadata_Relocation::metadata_addr() { coleenp@4037: int n = _metadata_index; coleenp@4037: if (n == 0) { coleenp@4037: // metadata is stored in the code stream coleenp@4037: return (Metadata**) pd_address_in_code(); duke@435: } else { coleenp@4037: // metadata is stored in table at nmethod::metadatas_begin coleenp@4037: return code()->metadata_addr_at(n); duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: Metadata* metadata_Relocation::metadata_value() { coleenp@4037: Metadata* v = *metadata_addr(); coleenp@4037: // clean inline caches store a special pseudo-null coleenp@4037: if (v == (Metadata*)Universe::non_oop_word()) v = NULL; coleenp@4037: return v; duke@435: } duke@435: coleenp@4037: coleenp@4037: void metadata_Relocation::fix_metadata_relocation() { coleenp@4037: if (!metadata_is_immediate()) { coleenp@4037: // get the metadata from the pool, and re-insert it into the instruction: coleenp@4037: pd_fix_value(value()); coleenp@4037: } duke@435: } duke@435: duke@435: coleenp@4037: void metadata_Relocation::verify_metadata_relocation() { coleenp@4037: if (!metadata_is_immediate()) { coleenp@4037: // get the metadata from the pool, and re-insert it into the instruction: coleenp@4037: verify_value(value()); coleenp@4037: } duke@435: } duke@435: coleenp@4037: address virtual_call_Relocation::cached_value() { coleenp@4037: assert(_cached_value != NULL && _cached_value < addr(), "must precede ic_call"); coleenp@4037: return _cached_value; duke@435: } duke@435: duke@435: duke@435: void virtual_call_Relocation::clear_inline_cache() { duke@435: // No stubs for ICs duke@435: // Clean IC duke@435: ResourceMark rm; duke@435: CompiledIC* icache = CompiledIC_at(this); duke@435: icache->set_to_clean(); duke@435: } duke@435: duke@435: duke@435: void opt_virtual_call_Relocation::clear_inline_cache() { duke@435: // No stubs for ICs duke@435: // Clean IC duke@435: ResourceMark rm; duke@435: CompiledIC* icache = CompiledIC_at(this); duke@435: icache->set_to_clean(); duke@435: } duke@435: duke@435: duke@435: address opt_virtual_call_Relocation::static_stub() { duke@435: // search for the static stub who points back to this static call duke@435: address static_call_addr = addr(); duke@435: RelocIterator iter(code()); duke@435: while (iter.next()) { duke@435: if (iter.type() == relocInfo::static_stub_type) { duke@435: if (iter.static_stub_reloc()->static_call() == static_call_addr) { duke@435: return iter.addr(); duke@435: } duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: void static_call_Relocation::clear_inline_cache() { duke@435: // Safe call site info duke@435: CompiledStaticCall* handler = compiledStaticCall_at(this); duke@435: handler->set_to_clean(); duke@435: } duke@435: duke@435: duke@435: address static_call_Relocation::static_stub() { duke@435: // search for the static stub who points back to this static call duke@435: address static_call_addr = addr(); duke@435: RelocIterator iter(code()); duke@435: while (iter.next()) { duke@435: if (iter.type() == relocInfo::static_stub_type) { duke@435: if (iter.static_stub_reloc()->static_call() == static_call_addr) { duke@435: return iter.addr(); duke@435: } duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: void static_stub_Relocation::clear_inline_cache() { duke@435: // Call stub is only used when calling the interpreted code. duke@435: // It does not really need to be cleared, except that we want to clean out the methodoop. duke@435: CompiledStaticCall::set_stub_to_clean(this); duke@435: } duke@435: duke@435: duke@435: void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { duke@435: address target = _target; duke@435: if (target == NULL) { duke@435: // An absolute embedded reference to an external location, duke@435: // which means there is nothing to fix here. duke@435: return; duke@435: } duke@435: // Probably this reference is absolute, not relative, so the duke@435: // following is probably a no-op. duke@435: assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity"); duke@435: set_value(target); duke@435: } duke@435: duke@435: duke@435: address external_word_Relocation::target() { duke@435: address target = _target; duke@435: if (target == NULL) { duke@435: target = pd_get_address_from_code(); duke@435: } duke@435: return target; duke@435: } duke@435: duke@435: duke@435: void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { duke@435: address target = _target; duke@435: if (target == NULL) { duke@435: if (addr_in_const()) { duke@435: target = new_addr_for(*(address*)addr(), src, dest); duke@435: } else { duke@435: target = new_addr_for(pd_get_address_from_code(), src, dest); duke@435: } duke@435: } duke@435: set_value(target); duke@435: } duke@435: duke@435: duke@435: address internal_word_Relocation::target() { duke@435: address target = _target; duke@435: if (target == NULL) { duke@435: target = pd_get_address_from_code(); duke@435: } duke@435: return target; duke@435: } duke@435: duke@435: duke@435: breakpoint_Relocation::breakpoint_Relocation(int kind, address target, bool internal) { duke@435: bool active = false; duke@435: bool enabled = (kind == initialization); duke@435: bool removable = (kind != safepoint); duke@435: bool settable = (target == NULL); duke@435: duke@435: int bits = kind; duke@435: if (enabled) bits |= enabled_state; duke@435: if (internal) bits |= internal_attr; duke@435: if (removable) bits |= removable_attr; duke@435: if (settable) bits |= settable_attr; duke@435: duke@435: _bits = bits | high_bit; duke@435: _target = target; duke@435: duke@435: assert(this->kind() == kind, "kind encoded"); duke@435: assert(this->enabled() == enabled, "enabled encoded"); duke@435: assert(this->active() == active, "active encoded"); duke@435: assert(this->internal() == internal, "internal encoded"); duke@435: assert(this->removable() == removable, "removable encoded"); duke@435: assert(this->settable() == settable, "settable encoded"); duke@435: } duke@435: duke@435: duke@435: address breakpoint_Relocation::target() const { duke@435: return _target; duke@435: } duke@435: duke@435: duke@435: void breakpoint_Relocation::set_target(address x) { duke@435: assert(settable(), "must be settable"); duke@435: jint target_bits = duke@435: (jint)(internal() ? scaled_offset (x, addr()) duke@435: : runtime_address_to_index(x)); duke@435: short* p = &live_bits() + 1; duke@435: p = add_jint(p, target_bits); duke@435: assert(p == instrs(), "new target must fit"); duke@435: _target = x; duke@435: } duke@435: duke@435: duke@435: void breakpoint_Relocation::set_enabled(bool b) { duke@435: if (enabled() == b) return; duke@435: duke@435: if (b) { duke@435: set_bits(bits() | enabled_state); duke@435: } else { duke@435: set_active(false); // remove the actual breakpoint insn, if any duke@435: set_bits(bits() & ~enabled_state); duke@435: } duke@435: } duke@435: duke@435: duke@435: void breakpoint_Relocation::set_active(bool b) { duke@435: assert(!b || enabled(), "cannot activate a disabled breakpoint"); duke@435: duke@435: if (active() == b) return; duke@435: duke@435: // %%% should probably seize a lock here (might not be the right lock) duke@435: //MutexLockerEx ml_patch(Patching_lock, true); duke@435: //if (active() == b) return; // recheck state after locking duke@435: duke@435: if (b) { duke@435: set_bits(bits() | active_state); duke@435: if (instrlen() == 0) duke@435: fatal("breakpoints in original code must be undoable"); duke@435: pd_swap_in_breakpoint (addr(), instrs(), instrlen()); duke@435: } else { duke@435: set_bits(bits() & ~active_state); duke@435: pd_swap_out_breakpoint(addr(), instrs(), instrlen()); duke@435: } duke@435: } duke@435: duke@435: duke@435: //--------------------------------------------------------------------------------- duke@435: // Non-product code duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: static const char* reloc_type_string(relocInfo::relocType t) { duke@435: switch (t) { duke@435: #define EACH_CASE(name) \ duke@435: case relocInfo::name##_type: \ duke@435: return #name; duke@435: duke@435: APPLY_TO_RELOCATIONS(EACH_CASE); duke@435: #undef EACH_CASE duke@435: duke@435: case relocInfo::none: duke@435: return "none"; duke@435: case relocInfo::data_prefix_tag: duke@435: return "prefix"; duke@435: default: duke@435: return "UNKNOWN RELOC TYPE"; duke@435: } duke@435: } duke@435: duke@435: duke@435: void RelocIterator::print_current() { duke@435: if (!has_current()) { duke@435: tty->print_cr("(no relocs)"); duke@435: return; duke@435: } iveresov@2344: tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d", iveresov@2344: _current, type(), reloc_type_string((relocInfo::relocType) type()), _addr, _current->addr_offset()); duke@435: if (current()->format() != 0) duke@435: tty->print(" format=%d", current()->format()); duke@435: if (datalen() == 1) { duke@435: tty->print(" data=%d", data()[0]); duke@435: } else if (datalen() > 0) { duke@435: tty->print(" data={"); duke@435: for (int i = 0; i < datalen(); i++) { duke@435: tty->print("%04x", data()[i] & 0xFFFF); duke@435: } duke@435: tty->print("}"); duke@435: } duke@435: tty->print("]"); duke@435: switch (type()) { duke@435: case relocInfo::oop_type: duke@435: { duke@435: oop_Relocation* r = oop_reloc(); duke@435: oop* oop_addr = NULL; duke@435: oop raw_oop = NULL; duke@435: oop oop_value = NULL; duke@435: if (code() != NULL || r->oop_is_immediate()) { duke@435: oop_addr = r->oop_addr(); duke@435: raw_oop = *oop_addr; duke@435: oop_value = r->oop_value(); duke@435: } duke@435: tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", duke@435: oop_addr, (address)raw_oop, r->offset()); duke@435: // Do not print the oop by default--we want this routine to duke@435: // work even during GC or other inconvenient times. duke@435: if (WizardMode && oop_value != NULL) { duke@435: tty->print("oop_value=" INTPTR_FORMAT ": ", (address)oop_value); duke@435: oop_value->print_value_on(tty); duke@435: } duke@435: break; duke@435: } coleenp@4037: case relocInfo::metadata_type: coleenp@4037: { coleenp@4037: metadata_Relocation* r = metadata_reloc(); coleenp@4037: Metadata** metadata_addr = NULL; coleenp@4037: Metadata* raw_metadata = NULL; coleenp@4037: Metadata* metadata_value = NULL; coleenp@4037: if (code() != NULL || r->metadata_is_immediate()) { coleenp@4037: metadata_addr = r->metadata_addr(); coleenp@4037: raw_metadata = *metadata_addr; coleenp@4037: metadata_value = r->metadata_value(); coleenp@4037: } coleenp@4037: tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", coleenp@4037: metadata_addr, (address)raw_metadata, r->offset()); coleenp@4037: if (metadata_value != NULL) { coleenp@4037: tty->print("metadata_value=" INTPTR_FORMAT ": ", (address)metadata_value); coleenp@4037: metadata_value->print_value_on(tty); coleenp@4037: } coleenp@4037: break; coleenp@4037: } duke@435: case relocInfo::external_word_type: duke@435: case relocInfo::internal_word_type: duke@435: case relocInfo::section_word_type: duke@435: { duke@435: DataRelocation* r = (DataRelocation*) reloc(); duke@435: tty->print(" | [target=" INTPTR_FORMAT "]", r->value()); //value==target duke@435: break; duke@435: } duke@435: case relocInfo::static_call_type: duke@435: case relocInfo::runtime_call_type: duke@435: { duke@435: CallRelocation* r = (CallRelocation*) reloc(); duke@435: tty->print(" | [destination=" INTPTR_FORMAT "]", r->destination()); duke@435: break; duke@435: } duke@435: case relocInfo::virtual_call_type: duke@435: { duke@435: virtual_call_Relocation* r = (virtual_call_Relocation*) reloc(); coleenp@4037: tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT "]", coleenp@4037: r->destination(), r->cached_value()); duke@435: break; duke@435: } duke@435: case relocInfo::static_stub_type: duke@435: { duke@435: static_stub_Relocation* r = (static_stub_Relocation*) reloc(); duke@435: tty->print(" | [static_call=" INTPTR_FORMAT "]", r->static_call()); duke@435: break; duke@435: } duke@435: } duke@435: tty->cr(); duke@435: } duke@435: duke@435: duke@435: void RelocIterator::print() { duke@435: RelocIterator save_this = (*this); duke@435: relocInfo* scan = _current; duke@435: if (!has_current()) scan += 1; // nothing to scan here! duke@435: duke@435: bool skip_next = has_current(); duke@435: bool got_next; duke@435: while (true) { duke@435: got_next = (skip_next || next()); duke@435: skip_next = false; duke@435: duke@435: tty->print(" @" INTPTR_FORMAT ": ", scan); duke@435: relocInfo* newscan = _current+1; duke@435: if (!has_current()) newscan -= 1; // nothing to scan here! duke@435: while (scan < newscan) { duke@435: tty->print("%04x", *(short*)scan & 0xFFFF); duke@435: scan++; duke@435: } duke@435: tty->cr(); duke@435: duke@435: if (!got_next) break; duke@435: print_current(); duke@435: } duke@435: duke@435: (*this) = save_this; duke@435: } duke@435: duke@435: // For the debugger: duke@435: extern "C" twisti@1918: void print_blob_locs(nmethod* nm) { twisti@1918: nm->print(); twisti@1918: RelocIterator iter(nm); duke@435: iter.print(); duke@435: } duke@435: extern "C" duke@435: void print_buf_locs(CodeBuffer* cb) { duke@435: FlagSetting fs(PrintRelocations, true); duke@435: cb->print(); duke@435: } duke@435: #endif // !PRODUCT