aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "code/compiledIC.hpp" aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "code/relocInfo.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "runtime/stubCodeGenerator.hpp" aoqi@0: #include "utilities/copy.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: const RelocationHolder RelocationHolder::none; // its type is relocInfo::none aoqi@0: aoqi@0: aoqi@0: // Implementation of relocInfo aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: relocInfo::relocInfo(relocType t, int off, int f) { aoqi@0: assert(t != data_prefix_tag, "cannot build a prefix this way"); aoqi@0: assert((t & type_mask) == t, "wrong type"); aoqi@0: assert((f & format_mask) == f, "wrong format"); aoqi@0: assert(off >= 0 && off < offset_limit(), "offset out off bounds"); aoqi@0: assert((off & (offset_unit-1)) == 0, "misaligned offset"); aoqi@0: (*this) = relocInfo(t, RAW_BITS, off, f); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: void relocInfo::initialize(CodeSection* dest, Relocation* reloc) { aoqi@0: relocInfo* data = this+1; // here's where the data might go aoqi@0: dest->set_locs_end(data); // sync end: the next call may read dest.locs_end aoqi@0: reloc->pack_data_to(dest); // maybe write data into locs, advancing locs_end aoqi@0: relocInfo* data_limit = dest->locs_end(); aoqi@0: if (data_limit > data) { aoqi@0: relocInfo suffix = (*this); aoqi@0: data_limit = this->finish_prefix((short*) data_limit); aoqi@0: // Finish up with the suffix. (Hack note: pack_data_to might edit this.) aoqi@0: *data_limit = suffix; aoqi@0: dest->set_locs_end(data_limit+1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: relocInfo* relocInfo::finish_prefix(short* prefix_limit) { aoqi@0: assert(sizeof(relocInfo) == sizeof(short), "change this code"); aoqi@0: short* p = (short*)(this+1); aoqi@0: assert(prefix_limit >= p, "must be a valid span of data"); aoqi@0: int plen = prefix_limit - p; aoqi@0: if (plen == 0) { aoqi@0: debug_only(_value = 0xFFFF); aoqi@0: return this; // no data: remove self completely aoqi@0: } aoqi@0: if (plen == 1 && fits_into_immediate(p[0])) { aoqi@0: (*this) = immediate_relocInfo(p[0]); // move data inside self aoqi@0: return this+1; aoqi@0: } aoqi@0: // cannot compact, so just update the count and return the limit pointer aoqi@0: (*this) = prefix_relocInfo(plen); // write new datalen aoqi@0: assert(data() + datalen() == prefix_limit, "pointers must line up"); aoqi@0: return (relocInfo*)prefix_limit; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void relocInfo::set_type(relocType t) { aoqi@0: int old_offset = addr_offset(); aoqi@0: int old_format = format(); aoqi@0: (*this) = relocInfo(t, old_offset, old_format); aoqi@0: assert(type()==(int)t, "sanity check"); aoqi@0: assert(addr_offset()==old_offset, "sanity check"); aoqi@0: assert(format()==old_format, "sanity check"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void relocInfo::set_format(int f) { aoqi@0: int old_offset = addr_offset(); aoqi@0: assert((f & format_mask) == f, "wrong format"); aoqi@0: _value = (_value & ~(format_mask << offset_width)) | (f << offset_width); aoqi@0: assert(addr_offset()==old_offset, "sanity check"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void relocInfo::change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type) { aoqi@0: bool found = false; aoqi@0: while (itr->next() && !found) { aoqi@0: if (itr->addr() == pc) { aoqi@0: assert(itr->type()==old_type, "wrong relocInfo type found"); aoqi@0: itr->current()->set_type(new_type); aoqi@0: found=true; aoqi@0: } aoqi@0: } aoqi@0: assert(found, "no relocInfo found for pc"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void relocInfo::remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type) { aoqi@0: change_reloc_info_for_address(itr, pc, old_type, none); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ---------------------------------------------------------------------------------------------------- aoqi@0: // Implementation of RelocIterator aoqi@0: aoqi@0: void RelocIterator::initialize(nmethod* nm, address begin, address limit) { aoqi@0: initialize_misc(); aoqi@0: aoqi@0: if (nm == NULL && begin != NULL) { aoqi@0: // allow nmethod to be deduced from beginning address aoqi@0: CodeBlob* cb = CodeCache::find_blob(begin); aoqi@0: nm = cb->as_nmethod_or_null(); aoqi@0: } aoqi@0: assert(nm != NULL, "must be able to deduce nmethod from other arguments"); aoqi@0: aoqi@0: _code = nm; aoqi@0: _current = nm->relocation_begin() - 1; aoqi@0: _end = nm->relocation_end(); aoqi@0: _addr = nm->content_begin(); aoqi@0: aoqi@0: // Initialize code sections. aoqi@0: _section_start[CodeBuffer::SECT_CONSTS] = nm->consts_begin(); aoqi@0: _section_start[CodeBuffer::SECT_INSTS ] = nm->insts_begin() ; aoqi@0: _section_start[CodeBuffer::SECT_STUBS ] = nm->stub_begin() ; aoqi@0: aoqi@0: _section_end [CodeBuffer::SECT_CONSTS] = nm->consts_end() ; aoqi@0: _section_end [CodeBuffer::SECT_INSTS ] = nm->insts_end() ; aoqi@0: _section_end [CodeBuffer::SECT_STUBS ] = nm->stub_end() ; aoqi@0: aoqi@0: assert(!has_current(), "just checking"); aoqi@0: assert(begin == NULL || begin >= nm->code_begin(), "in bounds"); aoqi@0: assert(limit == NULL || limit <= nm->code_end(), "in bounds"); aoqi@0: set_limits(begin, limit); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: RelocIterator::RelocIterator(CodeSection* cs, address begin, address limit) { aoqi@0: initialize_misc(); aoqi@0: aoqi@0: _current = cs->locs_start()-1; aoqi@0: _end = cs->locs_end(); aoqi@0: _addr = cs->start(); aoqi@0: _code = NULL; // Not cb->blob(); aoqi@0: aoqi@0: CodeBuffer* cb = cs->outer(); aoqi@0: assert((int) SECT_LIMIT == CodeBuffer::SECT_LIMIT, "my copy must be equal"); aoqi@0: for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) { aoqi@0: CodeSection* cs = cb->code_section(n); aoqi@0: _section_start[n] = cs->start(); aoqi@0: _section_end [n] = cs->end(); aoqi@0: } aoqi@0: aoqi@0: assert(!has_current(), "just checking"); aoqi@0: aoqi@0: assert(begin == NULL || begin >= cs->start(), "in bounds"); aoqi@0: assert(limit == NULL || limit <= cs->end(), "in bounds"); aoqi@0: set_limits(begin, limit); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: enum { indexCardSize = 128 }; aoqi@0: struct RelocIndexEntry { aoqi@0: jint addr_offset; // offset from header_end of an addr() aoqi@0: jint reloc_offset; // offset from header_end of a relocInfo (prefix) aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: bool RelocIterator::addr_in_const() const { aoqi@0: const int n = CodeBuffer::SECT_CONSTS; aoqi@0: return section_start(n) <= addr() && addr() < section_end(n); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static inline int num_cards(int code_size) { aoqi@0: return (code_size-1) / indexCardSize; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int RelocIterator::locs_and_index_size(int code_size, int locs_size) { aoqi@0: if (!UseRelocIndex) return locs_size; // no index aoqi@0: code_size = round_to(code_size, oopSize); aoqi@0: locs_size = round_to(locs_size, oopSize); aoqi@0: int index_size = num_cards(code_size) * sizeof(RelocIndexEntry); aoqi@0: // format of indexed relocs: aoqi@0: // relocation_begin: relocInfo ... aoqi@0: // index: (addr,reloc#) ... aoqi@0: // indexSize :relocation_end aoqi@0: return locs_size + index_size + BytesPerInt; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void RelocIterator::create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end) { aoqi@0: address relocation_begin = (address)dest_begin; aoqi@0: address relocation_end = (address)dest_end; aoqi@0: int total_size = relocation_end - relocation_begin; aoqi@0: int locs_size = dest_count * sizeof(relocInfo); aoqi@0: if (!UseRelocIndex) { aoqi@0: Copy::fill_to_bytes(relocation_begin + locs_size, total_size-locs_size, 0); aoqi@0: return; aoqi@0: } aoqi@0: int index_size = total_size - locs_size - BytesPerInt; // find out how much space is left aoqi@0: int ncards = index_size / sizeof(RelocIndexEntry); aoqi@0: assert(total_size == locs_size + index_size + BytesPerInt, "checkin'"); aoqi@0: assert(index_size >= 0 && index_size % sizeof(RelocIndexEntry) == 0, "checkin'"); aoqi@0: jint* index_size_addr = (jint*)relocation_end - 1; aoqi@0: aoqi@0: assert(sizeof(jint) == BytesPerInt, "change this code"); aoqi@0: aoqi@0: *index_size_addr = index_size; aoqi@0: if (index_size != 0) { aoqi@0: assert(index_size > 0, "checkin'"); aoqi@0: aoqi@0: RelocIndexEntry* index = (RelocIndexEntry *)(relocation_begin + locs_size); aoqi@0: assert(index == (RelocIndexEntry*)index_size_addr - ncards, "checkin'"); aoqi@0: aoqi@0: // walk over the relocations, and fill in index entries as we go aoqi@0: RelocIterator iter; aoqi@0: const address initial_addr = NULL; aoqi@0: relocInfo* const initial_current = dest_begin - 1; // biased by -1 like elsewhere aoqi@0: aoqi@0: iter._code = NULL; aoqi@0: iter._addr = initial_addr; aoqi@0: iter._limit = (address)(intptr_t)(ncards * indexCardSize); aoqi@0: iter._current = initial_current; aoqi@0: iter._end = dest_begin + dest_count; aoqi@0: aoqi@0: int i = 0; aoqi@0: address next_card_addr = (address)indexCardSize; aoqi@0: int addr_offset = 0; aoqi@0: int reloc_offset = 0; aoqi@0: while (true) { aoqi@0: // Checkpoint the iterator before advancing it. aoqi@0: addr_offset = iter._addr - initial_addr; aoqi@0: reloc_offset = iter._current - initial_current; aoqi@0: if (!iter.next()) break; aoqi@0: while (iter.addr() >= next_card_addr) { aoqi@0: index[i].addr_offset = addr_offset; aoqi@0: index[i].reloc_offset = reloc_offset; aoqi@0: i++; aoqi@0: next_card_addr += indexCardSize; aoqi@0: } aoqi@0: } aoqi@0: while (i < ncards) { aoqi@0: index[i].addr_offset = addr_offset; aoqi@0: index[i].reloc_offset = reloc_offset; aoqi@0: i++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void RelocIterator::set_limits(address begin, address limit) { aoqi@0: int index_size = 0; aoqi@0: if (UseRelocIndex && _code != NULL) { aoqi@0: index_size = ((jint*)_end)[-1]; aoqi@0: _end = (relocInfo*)( (address)_end - index_size - BytesPerInt ); aoqi@0: } aoqi@0: aoqi@0: _limit = limit; aoqi@0: aoqi@0: // the limit affects this next stuff: aoqi@0: if (begin != NULL) { aoqi@0: #ifdef ASSERT aoqi@0: // In ASSERT mode we do not actually use the index, but simply aoqi@0: // check that its contents would have led us to the right answer. aoqi@0: address addrCheck = _addr; aoqi@0: relocInfo* infoCheck = _current; aoqi@0: #endif // ASSERT aoqi@0: if (index_size > 0) { aoqi@0: // skip ahead aoqi@0: RelocIndexEntry* index = (RelocIndexEntry*)_end; aoqi@0: RelocIndexEntry* index_limit = (RelocIndexEntry*)((address)index + index_size); aoqi@0: assert(_addr == _code->code_begin(), "_addr must be unadjusted"); aoqi@0: int card = (begin - _addr) / indexCardSize; aoqi@0: if (card > 0) { aoqi@0: if (index+card-1 < index_limit) index += card-1; aoqi@0: else index = index_limit - 1; aoqi@0: #ifdef ASSERT aoqi@0: addrCheck = _addr + index->addr_offset; aoqi@0: infoCheck = _current + index->reloc_offset; aoqi@0: #else aoqi@0: // Advance the iterator immediately to the last valid state aoqi@0: // for the previous card. Calling "next" will then advance aoqi@0: // it to the first item on the required card. aoqi@0: _addr += index->addr_offset; aoqi@0: _current += index->reloc_offset; aoqi@0: #endif // ASSERT aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: relocInfo* backup; aoqi@0: address backup_addr; aoqi@0: while (true) { aoqi@0: backup = _current; aoqi@0: backup_addr = _addr; aoqi@0: #ifdef ASSERT aoqi@0: if (backup == infoCheck) { aoqi@0: assert(backup_addr == addrCheck, "must match"); addrCheck = NULL; infoCheck = NULL; aoqi@0: } else { aoqi@0: assert(addrCheck == NULL || backup_addr <= addrCheck, "must not pass addrCheck"); aoqi@0: } aoqi@0: #endif // ASSERT aoqi@0: if (!next() || addr() >= begin) break; aoqi@0: } aoqi@0: assert(addrCheck == NULL || addrCheck == backup_addr, "must have matched addrCheck"); aoqi@0: assert(infoCheck == NULL || infoCheck == backup, "must have matched infoCheck"); aoqi@0: // At this point, either we are at the first matching record, aoqi@0: // or else there is no such record, and !has_current(). aoqi@0: // In either case, revert to the immediatly preceding state. aoqi@0: _current = backup; aoqi@0: _addr = backup_addr; aoqi@0: set_has_current(false); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void RelocIterator::set_limit(address limit) { aoqi@0: address code_end = (address)code() + code()->size(); aoqi@0: assert(limit == NULL || limit <= code_end, "in bounds"); aoqi@0: _limit = limit; aoqi@0: } aoqi@0: aoqi@0: // All the strange bit-encodings are in here. aoqi@0: // The idea is to encode relocation data which are small integers aoqi@0: // very efficiently (a single extra halfword). Larger chunks of aoqi@0: // relocation data need a halfword header to hold their size. aoqi@0: void RelocIterator::advance_over_prefix() { aoqi@0: if (_current->is_datalen()) { aoqi@0: _data = (short*) _current->data(); aoqi@0: _datalen = _current->datalen(); aoqi@0: _current += _datalen + 1; // skip the embedded data & header aoqi@0: } else { aoqi@0: _databuf = _current->immediate(); aoqi@0: _data = &_databuf; aoqi@0: _datalen = 1; aoqi@0: _current++; // skip the header aoqi@0: } aoqi@0: // The client will see the following relocInfo, whatever that is. aoqi@0: // It is the reloc to which the preceding data applies. aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void RelocIterator::initialize_misc() { aoqi@0: set_has_current(false); aoqi@0: for (int i = (int) CodeBuffer::SECT_FIRST; i < (int) CodeBuffer::SECT_LIMIT; i++) { aoqi@0: _section_start[i] = NULL; // these will be lazily computed, if needed aoqi@0: _section_end [i] = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Relocation* RelocIterator::reloc() { aoqi@0: // (take the "switch" out-of-line) aoqi@0: relocInfo::relocType t = type(); aoqi@0: if (false) {} aoqi@0: #define EACH_TYPE(name) \ aoqi@0: else if (t == relocInfo::name##_type) { \ aoqi@0: return name##_reloc(); \ aoqi@0: } aoqi@0: APPLY_TO_RELOCATIONS(EACH_TYPE); aoqi@0: #undef EACH_TYPE aoqi@0: assert(t == relocInfo::none, "must be padding"); aoqi@0: return new(_rh) Relocation(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //////// Methods for flyweight Relocation types aoqi@0: aoqi@0: aoqi@0: RelocationHolder RelocationHolder::plus(int offset) const { aoqi@0: if (offset != 0) { aoqi@0: switch (type()) { aoqi@0: case relocInfo::none: aoqi@0: break; aoqi@0: case relocInfo::oop_type: aoqi@0: { aoqi@0: oop_Relocation* r = (oop_Relocation*)reloc(); aoqi@0: return oop_Relocation::spec(r->oop_index(), r->offset() + offset); aoqi@0: } aoqi@0: case relocInfo::metadata_type: aoqi@0: { aoqi@0: metadata_Relocation* r = (metadata_Relocation*)reloc(); aoqi@0: return metadata_Relocation::spec(r->metadata_index(), r->offset() + offset); aoqi@0: } aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: return (*this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Relocation::guarantee_size() { aoqi@0: guarantee(false, "Make _relocbuf bigger!"); aoqi@0: } aoqi@0: aoqi@0: // some relocations can compute their own values aoqi@0: address Relocation::value() { aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Relocation::set_value(address x) { aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: RelocationHolder Relocation::spec_simple(relocInfo::relocType rtype) { aoqi@0: if (rtype == relocInfo::none) return RelocationHolder::none; aoqi@0: relocInfo ri = relocInfo(rtype, 0); aoqi@0: RelocIterator itr; aoqi@0: itr.set_current(ri); aoqi@0: itr.reloc(); aoqi@0: return itr._rh; aoqi@0: } aoqi@0: aoqi@0: int32_t Relocation::runtime_address_to_index(address runtime_address) { aoqi@0: assert(!is_reloc_index((intptr_t)runtime_address), "must not look like an index"); aoqi@0: aoqi@0: if (runtime_address == NULL) return 0; aoqi@0: aoqi@0: StubCodeDesc* p = StubCodeDesc::desc_for(runtime_address); aoqi@0: if (p != NULL && p->begin() == runtime_address) { aoqi@0: assert(is_reloc_index(p->index()), "there must not be too many stubs"); aoqi@0: return (int32_t)p->index(); aoqi@0: } else { aoqi@0: // Known "miscellaneous" non-stub pointers: aoqi@0: // os::get_polling_page(), SafepointSynchronize::address_of_state() aoqi@0: if (PrintRelocations) { aoqi@0: tty->print_cr("random unregistered address in relocInfo: " INTPTR_FORMAT, runtime_address); aoqi@0: } aoqi@0: #ifndef _LP64 aoqi@0: return (int32_t) (intptr_t)runtime_address; aoqi@0: #else aoqi@0: // didn't fit return non-index aoqi@0: return -1; aoqi@0: #endif /* _LP64 */ aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address Relocation::index_to_runtime_address(int32_t index) { aoqi@0: if (index == 0) return NULL; aoqi@0: aoqi@0: if (is_reloc_index(index)) { aoqi@0: StubCodeDesc* p = StubCodeDesc::desc_for_index(index); aoqi@0: assert(p != NULL, "there must be a stub for this index"); aoqi@0: return p->begin(); aoqi@0: } else { aoqi@0: #ifndef _LP64 aoqi@0: // this only works on 32bit machines aoqi@0: return (address) ((intptr_t) index); aoqi@0: #else aoqi@0: fatal("Relocation::index_to_runtime_address, int32_t not pointer sized"); aoqi@0: return NULL; aoqi@0: #endif /* _LP64 */ aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: address Relocation::old_addr_for(address newa, aoqi@0: const CodeBuffer* src, CodeBuffer* dest) { aoqi@0: int sect = dest->section_index_of(newa); aoqi@0: guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); aoqi@0: address ostart = src->code_section(sect)->start(); aoqi@0: address nstart = dest->code_section(sect)->start(); aoqi@0: return ostart + (newa - nstart); aoqi@0: } aoqi@0: aoqi@0: address Relocation::new_addr_for(address olda, aoqi@0: const CodeBuffer* src, CodeBuffer* dest) { aoqi@0: debug_only(const CodeBuffer* src0 = src); aoqi@0: int sect = CodeBuffer::SECT_NONE; aoqi@0: // Look for olda in the source buffer, and all previous incarnations aoqi@0: // if the source buffer has been expanded. aoqi@0: for (; src != NULL; src = src->before_expand()) { aoqi@0: sect = src->section_index_of(olda); aoqi@0: if (sect != CodeBuffer::SECT_NONE) break; aoqi@0: } aoqi@0: guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); aoqi@0: address ostart = src->code_section(sect)->start(); aoqi@0: address nstart = dest->code_section(sect)->start(); aoqi@0: return nstart + (olda - ostart); aoqi@0: } aoqi@0: aoqi@0: void Relocation::normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections) { aoqi@0: address addr0 = addr; aoqi@0: if (addr0 == NULL || dest->allocates2(addr0)) return; aoqi@0: CodeBuffer* cb = dest->outer(); aoqi@0: addr = new_addr_for(addr0, cb, cb); aoqi@0: assert(allow_other_sections || dest->contains2(addr), aoqi@0: "addr must be in required section"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CallRelocation::set_destination(address x) { aoqi@0: pd_set_call_destination(x); aoqi@0: } aoqi@0: aoqi@0: void CallRelocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { aoqi@0: // Usually a self-relative reference to an external routine. aoqi@0: // On some platforms, the reference is absolute (not self-relative). aoqi@0: // The enhanced use of pd_call_destination sorts this all out. aoqi@0: address orig_addr = old_addr_for(addr(), src, dest); aoqi@0: address callee = pd_call_destination(orig_addr); aoqi@0: // Reassert the callee address, this time in the new copy of the code. aoqi@0: pd_set_call_destination(callee); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //// pack/unpack methods aoqi@0: aoqi@0: void oop_Relocation::pack_data_to(CodeSection* dest) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: p = pack_2_ints_to(p, _oop_index, _offset); aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void oop_Relocation::unpack_data() { aoqi@0: unpack_2_ints(_oop_index, _offset); aoqi@0: } aoqi@0: aoqi@0: void metadata_Relocation::pack_data_to(CodeSection* dest) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: p = pack_2_ints_to(p, _metadata_index, _offset); aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void metadata_Relocation::unpack_data() { aoqi@0: unpack_2_ints(_metadata_index, _offset); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void virtual_call_Relocation::pack_data_to(CodeSection* dest) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: address point = dest->locs_point(); aoqi@0: aoqi@0: normalize_address(_cached_value, dest); aoqi@0: jint x0 = scaled_offset_null_special(_cached_value, point); aoqi@0: p = pack_1_int_to(p, x0); aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void virtual_call_Relocation::unpack_data() { aoqi@0: jint x0 = unpack_1_int(); aoqi@0: address point = addr(); aoqi@0: _cached_value = x0==0? NULL: address_from_scaled_offset(x0, point); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void static_stub_Relocation::pack_data_to(CodeSection* dest) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: CodeSection* insts = dest->outer()->insts(); aoqi@0: normalize_address(_static_call, insts); aoqi@0: p = pack_1_int_to(p, scaled_offset(_static_call, insts->start())); aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: void static_stub_Relocation::unpack_data() { aoqi@0: address base = binding()->section_start(CodeBuffer::SECT_INSTS); aoqi@0: _static_call = address_from_scaled_offset(unpack_1_int(), base); aoqi@0: } aoqi@0: aoqi@0: void trampoline_stub_Relocation::pack_data_to(CodeSection* dest ) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: CodeSection* insts = dest->outer()->insts(); aoqi@0: normalize_address(_owner, insts); aoqi@0: p = pack_1_int_to(p, scaled_offset(_owner, insts->start())); aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: void trampoline_stub_Relocation::unpack_data() { aoqi@0: address base = binding()->section_start(CodeBuffer::SECT_INSTS); aoqi@0: _owner = address_from_scaled_offset(unpack_1_int(), base); aoqi@0: } aoqi@0: aoqi@0: void external_word_Relocation::pack_data_to(CodeSection* dest) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: int32_t index = runtime_address_to_index(_target); aoqi@0: #ifndef _LP64 aoqi@0: p = pack_1_int_to(p, index); aoqi@0: #else aoqi@0: if (is_reloc_index(index)) { aoqi@0: p = pack_2_ints_to(p, index, 0); aoqi@0: } else { aoqi@0: jlong t = (jlong) _target; aoqi@0: int32_t lo = low(t); aoqi@0: int32_t hi = high(t); aoqi@0: p = pack_2_ints_to(p, lo, hi); aoqi@0: DEBUG_ONLY(jlong t1 = jlong_from(hi, lo)); aoqi@0: assert(!is_reloc_index(t1) && (address) t1 == _target, "not symmetric"); aoqi@0: } aoqi@0: #endif /* _LP64 */ aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void external_word_Relocation::unpack_data() { aoqi@0: #ifndef _LP64 aoqi@0: _target = index_to_runtime_address(unpack_1_int()); aoqi@0: #else aoqi@0: int32_t lo, hi; aoqi@0: unpack_2_ints(lo, hi); aoqi@0: jlong t = jlong_from(hi, lo);; aoqi@0: if (is_reloc_index(t)) { aoqi@0: _target = index_to_runtime_address(t); aoqi@0: } else { aoqi@0: _target = (address) t; aoqi@0: } aoqi@0: #endif /* _LP64 */ aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void internal_word_Relocation::pack_data_to(CodeSection* dest) { aoqi@0: short* p = (short*) dest->locs_end(); aoqi@0: normalize_address(_target, dest, true); aoqi@0: aoqi@0: // Check whether my target address is valid within this section. aoqi@0: // If not, strengthen the relocation type to point to another section. aoqi@0: int sindex = _section; aoqi@0: if (sindex == CodeBuffer::SECT_NONE && _target != NULL aoqi@0: && (!dest->allocates(_target) || _target == dest->locs_point())) { aoqi@0: sindex = dest->outer()->section_index_of(_target); aoqi@0: guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere"); aoqi@0: relocInfo* base = dest->locs_end() - 1; aoqi@0: assert(base->type() == this->type(), "sanity"); aoqi@0: // Change the written type, to be section_word_type instead. aoqi@0: base->set_type(relocInfo::section_word_type); aoqi@0: } aoqi@0: aoqi@0: // Note: An internal_word relocation cannot refer to its own instruction, aoqi@0: // because we reserve "0" to mean that the pointer itself is embedded aoqi@0: // in the code stream. We use a section_word relocation for such cases. aoqi@0: aoqi@0: if (sindex == CodeBuffer::SECT_NONE) { aoqi@0: assert(type() == relocInfo::internal_word_type, "must be base class"); aoqi@0: guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section"); aoqi@0: jint x0 = scaled_offset_null_special(_target, dest->locs_point()); aoqi@0: assert(!(x0 == 0 && _target != NULL), "correct encoding of null target"); aoqi@0: p = pack_1_int_to(p, x0); aoqi@0: } else { aoqi@0: assert(_target != NULL, "sanity"); aoqi@0: CodeSection* sect = dest->outer()->code_section(sindex); aoqi@0: guarantee(sect->allocates2(_target), "must be in correct section"); aoqi@0: address base = sect->start(); aoqi@0: jint offset = scaled_offset(_target, base); aoqi@0: assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity"); aoqi@0: assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++"); aoqi@0: p = pack_1_int_to(p, (offset << section_width) | sindex); aoqi@0: } aoqi@0: aoqi@0: dest->set_locs_end((relocInfo*) p); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void internal_word_Relocation::unpack_data() { aoqi@0: jint x0 = unpack_1_int(); aoqi@0: _target = x0==0? NULL: address_from_scaled_offset(x0, addr()); aoqi@0: _section = CodeBuffer::SECT_NONE; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void section_word_Relocation::unpack_data() { aoqi@0: jint x = unpack_1_int(); aoqi@0: jint offset = (x >> section_width); aoqi@0: int sindex = (x & ((1<section_start(sindex); aoqi@0: aoqi@0: _section = sindex; aoqi@0: _target = address_from_scaled_offset(offset, base); aoqi@0: } aoqi@0: aoqi@0: //// miscellaneous methods aoqi@0: oop* oop_Relocation::oop_addr() { aoqi@0: int n = _oop_index; aoqi@0: if (n == 0) { aoqi@0: // oop is stored in the code stream aoqi@0: return (oop*) pd_address_in_code(); aoqi@0: } else { aoqi@0: // oop is stored in table at nmethod::oops_begin aoqi@0: return code()->oop_addr_at(n); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: oop oop_Relocation::oop_value() { aoqi@0: oop v = *oop_addr(); aoqi@0: // clean inline caches store a special pseudo-null aoqi@0: if (v == (oop)Universe::non_oop_word()) v = NULL; aoqi@0: return v; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void oop_Relocation::fix_oop_relocation() { aoqi@0: if (!oop_is_immediate()) { aoqi@0: // get the oop from the pool, and re-insert it into the instruction: aoqi@0: set_value(value()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void oop_Relocation::verify_oop_relocation() { aoqi@0: if (!oop_is_immediate()) { aoqi@0: // get the oop from the pool, and re-insert it into the instruction: aoqi@0: verify_value(value()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // meta data versions aoqi@0: Metadata** metadata_Relocation::metadata_addr() { aoqi@0: int n = _metadata_index; aoqi@0: if (n == 0) { aoqi@0: // metadata is stored in the code stream aoqi@0: return (Metadata**) pd_address_in_code(); aoqi@0: } else { aoqi@0: // metadata is stored in table at nmethod::metadatas_begin aoqi@0: return code()->metadata_addr_at(n); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Metadata* metadata_Relocation::metadata_value() { aoqi@0: Metadata* v = *metadata_addr(); aoqi@0: // clean inline caches store a special pseudo-null aoqi@0: if (v == (Metadata*)Universe::non_oop_word()) v = NULL; aoqi@0: return v; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void metadata_Relocation::fix_metadata_relocation() { aoqi@0: if (!metadata_is_immediate()) { aoqi@0: // get the metadata from the pool, and re-insert it into the instruction: aoqi@0: pd_fix_value(value()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void metadata_Relocation::verify_metadata_relocation() { aoqi@0: if (!metadata_is_immediate()) { aoqi@0: // get the metadata from the pool, and re-insert it into the instruction: aoqi@0: verify_value(value()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: address virtual_call_Relocation::cached_value() { aoqi@0: assert(_cached_value != NULL && _cached_value < addr(), "must precede ic_call"); aoqi@0: return _cached_value; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void virtual_call_Relocation::clear_inline_cache() { aoqi@0: // No stubs for ICs aoqi@0: // Clean IC aoqi@0: ResourceMark rm; aoqi@0: CompiledIC* icache = CompiledIC_at(this); aoqi@0: icache->set_to_clean(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void opt_virtual_call_Relocation::clear_inline_cache() { aoqi@0: // No stubs for ICs aoqi@0: // Clean IC aoqi@0: ResourceMark rm; aoqi@0: CompiledIC* icache = CompiledIC_at(this); aoqi@0: icache->set_to_clean(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address opt_virtual_call_Relocation::static_stub() { aoqi@0: // search for the static stub who points back to this static call aoqi@0: address static_call_addr = addr(); aoqi@0: RelocIterator iter(code()); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::static_stub_type) { aoqi@0: if (iter.static_stub_reloc()->static_call() == static_call_addr) { aoqi@0: return iter.addr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void static_call_Relocation::clear_inline_cache() { aoqi@0: // Safe call site info aoqi@0: CompiledStaticCall* handler = compiledStaticCall_at(this); aoqi@0: handler->set_to_clean(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address static_call_Relocation::static_stub() { aoqi@0: // search for the static stub who points back to this static call aoqi@0: address static_call_addr = addr(); aoqi@0: RelocIterator iter(code()); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::static_stub_type) { aoqi@0: if (iter.static_stub_reloc()->static_call() == static_call_addr) { aoqi@0: return iter.addr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Finds the trampoline address for a call. If no trampoline stub is aoqi@0: // found NULL is returned which can be handled by the caller. aoqi@0: address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) { aoqi@0: // There are no relocations available when the code gets relocated aoqi@0: // because of CodeBuffer expansion. aoqi@0: if (code->relocation_size() == 0) aoqi@0: return NULL; aoqi@0: aoqi@0: RelocIterator iter(code, call); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::trampoline_stub_type) { aoqi@0: if (iter.trampoline_stub_reloc()->owner() == call) { aoqi@0: return iter.addr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: void static_stub_Relocation::clear_inline_cache() { aoqi@0: // Call stub is only used when calling the interpreted code. aoqi@0: // It does not really need to be cleared, except that we want to clean out the methodoop. aoqi@0: CompiledStaticCall::set_stub_to_clean(this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { aoqi@0: address target = _target; aoqi@0: if (target == NULL) { aoqi@0: // An absolute embedded reference to an external location, aoqi@0: // which means there is nothing to fix here. aoqi@0: return; aoqi@0: } aoqi@0: // Probably this reference is absolute, not relative, so the aoqi@0: // following is probably a no-op. aoqi@0: assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity"); aoqi@0: set_value(target); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address external_word_Relocation::target() { aoqi@0: address target = _target; aoqi@0: if (target == NULL) { aoqi@0: target = pd_get_address_from_code(); aoqi@0: } aoqi@0: return target; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { aoqi@0: address target = _target; aoqi@0: if (target == NULL) { aoqi@0: if (addr_in_const()) { aoqi@0: target = new_addr_for(*(address*)addr(), src, dest); aoqi@0: } else { aoqi@0: target = new_addr_for(pd_get_address_from_code(), src, dest); aoqi@0: } aoqi@0: } aoqi@0: set_value(target); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address internal_word_Relocation::target() { aoqi@0: address target = _target; aoqi@0: if (target == NULL) { aoqi@0: target = pd_get_address_from_code(); aoqi@0: } aoqi@0: return target; aoqi@0: } aoqi@0: aoqi@0: //--------------------------------------------------------------------------------- aoqi@0: // Non-product code aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: static const char* reloc_type_string(relocInfo::relocType t) { aoqi@0: switch (t) { aoqi@0: #define EACH_CASE(name) \ aoqi@0: case relocInfo::name##_type: \ aoqi@0: return #name; aoqi@0: aoqi@0: APPLY_TO_RELOCATIONS(EACH_CASE); aoqi@0: #undef EACH_CASE aoqi@0: aoqi@0: case relocInfo::none: aoqi@0: return "none"; aoqi@0: case relocInfo::data_prefix_tag: aoqi@0: return "prefix"; aoqi@0: default: aoqi@0: return "UNKNOWN RELOC TYPE"; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void RelocIterator::print_current() { aoqi@0: if (!has_current()) { aoqi@0: tty->print_cr("(no relocs)"); aoqi@0: return; aoqi@0: } aoqi@0: tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d", aoqi@0: _current, type(), reloc_type_string((relocInfo::relocType) type()), _addr, _current->addr_offset()); aoqi@0: if (current()->format() != 0) aoqi@0: tty->print(" format=%d", current()->format()); aoqi@0: if (datalen() == 1) { aoqi@0: tty->print(" data=%d", data()[0]); aoqi@0: } else if (datalen() > 0) { aoqi@0: tty->print(" data={"); aoqi@0: for (int i = 0; i < datalen(); i++) { aoqi@0: tty->print("%04x", data()[i] & 0xFFFF); aoqi@0: } aoqi@0: tty->print("}"); aoqi@0: } aoqi@0: tty->print("]"); aoqi@0: switch (type()) { aoqi@0: case relocInfo::oop_type: aoqi@0: { aoqi@0: oop_Relocation* r = oop_reloc(); aoqi@0: oop* oop_addr = NULL; aoqi@0: oop raw_oop = NULL; aoqi@0: oop oop_value = NULL; aoqi@0: if (code() != NULL || r->oop_is_immediate()) { aoqi@0: oop_addr = r->oop_addr(); aoqi@0: raw_oop = *oop_addr; aoqi@0: oop_value = r->oop_value(); aoqi@0: } aoqi@0: tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", aoqi@0: oop_addr, (address)raw_oop, r->offset()); aoqi@0: // Do not print the oop by default--we want this routine to aoqi@0: // work even during GC or other inconvenient times. aoqi@0: if (WizardMode && oop_value != NULL) { aoqi@0: tty->print("oop_value=" INTPTR_FORMAT ": ", (address)oop_value); aoqi@0: oop_value->print_value_on(tty); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::metadata_type: aoqi@0: { aoqi@0: metadata_Relocation* r = metadata_reloc(); aoqi@0: Metadata** metadata_addr = NULL; aoqi@0: Metadata* raw_metadata = NULL; aoqi@0: Metadata* metadata_value = NULL; aoqi@0: if (code() != NULL || r->metadata_is_immediate()) { aoqi@0: metadata_addr = r->metadata_addr(); aoqi@0: raw_metadata = *metadata_addr; aoqi@0: metadata_value = r->metadata_value(); aoqi@0: } aoqi@0: tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", aoqi@0: metadata_addr, (address)raw_metadata, r->offset()); aoqi@0: if (metadata_value != NULL) { aoqi@0: tty->print("metadata_value=" INTPTR_FORMAT ": ", (address)metadata_value); aoqi@0: metadata_value->print_value_on(tty); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::external_word_type: aoqi@0: case relocInfo::internal_word_type: aoqi@0: case relocInfo::section_word_type: aoqi@0: { aoqi@0: DataRelocation* r = (DataRelocation*) reloc(); aoqi@0: tty->print(" | [target=" INTPTR_FORMAT "]", r->value()); //value==target aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::static_call_type: aoqi@0: case relocInfo::runtime_call_type: aoqi@0: { aoqi@0: CallRelocation* r = (CallRelocation*) reloc(); aoqi@0: tty->print(" | [destination=" INTPTR_FORMAT "]", r->destination()); aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::virtual_call_type: aoqi@0: { aoqi@0: virtual_call_Relocation* r = (virtual_call_Relocation*) reloc(); aoqi@0: tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT "]", aoqi@0: r->destination(), r->cached_value()); aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::static_stub_type: aoqi@0: { aoqi@0: static_stub_Relocation* r = (static_stub_Relocation*) reloc(); aoqi@0: tty->print(" | [static_call=" INTPTR_FORMAT "]", r->static_call()); aoqi@0: break; aoqi@0: } aoqi@0: case relocInfo::trampoline_stub_type: aoqi@0: { aoqi@0: trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc(); aoqi@0: tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", r->owner()); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void RelocIterator::print() { aoqi@0: RelocIterator save_this = (*this); aoqi@0: relocInfo* scan = _current; aoqi@0: if (!has_current()) scan += 1; // nothing to scan here! aoqi@0: aoqi@0: bool skip_next = has_current(); aoqi@0: bool got_next; aoqi@0: while (true) { aoqi@0: got_next = (skip_next || next()); aoqi@0: skip_next = false; aoqi@0: aoqi@0: tty->print(" @" INTPTR_FORMAT ": ", scan); aoqi@0: relocInfo* newscan = _current+1; aoqi@0: if (!has_current()) newscan -= 1; // nothing to scan here! aoqi@0: while (scan < newscan) { aoqi@0: tty->print("%04x", *(short*)scan & 0xFFFF); aoqi@0: scan++; aoqi@0: } aoqi@0: tty->cr(); aoqi@0: aoqi@0: if (!got_next) break; aoqi@0: print_current(); aoqi@0: } aoqi@0: aoqi@0: (*this) = save_this; aoqi@0: } aoqi@0: aoqi@0: // For the debugger: aoqi@0: extern "C" aoqi@0: void print_blob_locs(nmethod* nm) { aoqi@0: nm->print(); aoqi@0: RelocIterator iter(nm); aoqi@0: iter.print(); aoqi@0: } aoqi@0: extern "C" aoqi@0: void print_buf_locs(CodeBuffer* cb) { aoqi@0: FlagSetting fs(PrintRelocations, true); aoqi@0: cb->print(); aoqi@0: } aoqi@0: #endif // !PRODUCT