relocInfo.cpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25# include "incls/_precompiled.incl" 26# include "incls/_relocInfo.cpp.incl" 27 28 29const RelocationHolder RelocationHolder::none; // its type is relocInfo::none 30 31 32// Implementation of relocInfo 33 34#ifdef ASSERT 35relocInfo::relocInfo(relocType t, int off, int f) { 36 assert(t != data_prefix_tag, "cannot build a prefix this way"); 37 assert((t & type_mask) == t, "wrong type"); 38 assert((f & format_mask) == f, "wrong format"); 39 assert(off >= 0 && off < offset_limit(), "offset out off bounds"); 40 assert((off & (offset_unit-1)) == 0, "misaligned offset"); 41 (*this) = relocInfo(t, RAW_BITS, off, f); 42} 43#endif 44 45void relocInfo::initialize(CodeSection* dest, Relocation* reloc) { 46 relocInfo* data = this+1; // here's where the data might go 47 dest->set_locs_end(data); // sync end: the next call may read dest.locs_end 48 reloc->pack_data_to(dest); // maybe write data into locs, advancing locs_end 49 relocInfo* data_limit = dest->locs_end(); 50 if (data_limit > data) { 51 relocInfo suffix = (*this); 52 data_limit = this->finish_prefix((short*) data_limit); 53 // Finish up with the suffix. (Hack note: pack_data_to might edit this.) 54 *data_limit = suffix; 55 dest->set_locs_end(data_limit+1); 56 } 57} 58 59relocInfo* relocInfo::finish_prefix(short* prefix_limit) { 60 assert(sizeof(relocInfo) == sizeof(short), "change this code"); 61 short* p = (short*)(this+1); 62 assert(prefix_limit >= p, "must be a valid span of data"); 63 int plen = prefix_limit - p; 64 if (plen == 0) { 65 debug_only(_value = 0xFFFF); 66 return this; // no data: remove self completely 67 } 68 if (plen == 1 && fits_into_immediate(p[0])) { 69 (*this) = immediate_relocInfo(p[0]); // move data inside self 70 return this+1; 71 } 72 // cannot compact, so just update the count and return the limit pointer 73 (*this) = prefix_relocInfo(plen); // write new datalen 74 assert(data() + datalen() == prefix_limit, "pointers must line up"); 75 return (relocInfo*)prefix_limit; 76} 77 78 79void relocInfo::set_type(relocType t) { 80 int old_offset = addr_offset(); 81 int old_format = format(); 82 (*this) = relocInfo(t, old_offset, old_format); 83 assert(type()==(int)t, "sanity check"); 84 assert(addr_offset()==old_offset, "sanity check"); 85 assert(format()==old_format, "sanity check"); 86} 87 88 89void relocInfo::set_format(int f) { 90 int old_offset = addr_offset(); 91 assert((f & format_mask) == f, "wrong format"); 92 _value = (_value & ~(format_mask << offset_width)) | (f << offset_width); 93 assert(addr_offset()==old_offset, "sanity check"); 94} 95 96 97void relocInfo::change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type) { 98 bool found = false; 99 while (itr->next() && !found) { 100 if (itr->addr() == pc) { 101 assert(itr->type()==old_type, "wrong relocInfo type found"); 102 itr->current()->set_type(new_type); 103 found=true; 104 } 105 } 106 assert(found, "no relocInfo found for pc"); 107} 108 109 110void relocInfo::remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type) { 111 change_reloc_info_for_address(itr, pc, old_type, none); 112} 113 114 115// ---------------------------------------------------------------------------------------------------- 116// Implementation of RelocIterator 117 118void RelocIterator::initialize(CodeBlob* cb, address begin, address limit) { 119 initialize_misc(); 120 121 if (cb == NULL && begin != NULL) { 122 // allow CodeBlob to be deduced from beginning address 123 cb = CodeCache::find_blob(begin); 124 } 125 assert(cb != NULL, "must be able to deduce nmethod from other arguments"); 126 127 _code = cb; 128 _current = cb->relocation_begin()-1; 129 _end = cb->relocation_end(); 130 _addr = (address) cb->instructions_begin(); 131 132 assert(!has_current(), "just checking"); 133 address code_end = cb->instructions_end(); 134 135 assert(begin == NULL || begin >= cb->instructions_begin(), "in bounds"); 136 // FIX THIS assert(limit == NULL || limit <= code_end, "in bounds"); 137 set_limits(begin, limit); 138} 139 140 141RelocIterator::RelocIterator(CodeSection* cs, address begin, address limit) { 142 initialize_misc(); 143 144 _current = cs->locs_start()-1; 145 _end = cs->locs_end(); 146 _addr = cs->start(); 147 _code = NULL; // Not cb->blob(); 148 149 CodeBuffer* cb = cs->outer(); 150 assert((int)SECT_LIMIT == CodeBuffer::SECT_LIMIT, "my copy must be equal"); 151 for (int n = 0; n < (int)SECT_LIMIT; n++) { 152 _section_start[n] = cb->code_section(n)->start(); 153 } 154 155 assert(!has_current(), "just checking"); 156 157 assert(begin == NULL || begin >= cs->start(), "in bounds"); 158 assert(limit == NULL || limit <= cs->end(), "in bounds"); 159 set_limits(begin, limit); 160} 161 162 163enum { indexCardSize = 128 }; 164struct RelocIndexEntry { 165 jint addr_offset; // offset from header_end of an addr() 166 jint reloc_offset; // offset from header_end of a relocInfo (prefix) 167}; 168 169 170static inline int num_cards(int code_size) { 171 return (code_size-1) / indexCardSize; 172} 173 174 175int RelocIterator::locs_and_index_size(int code_size, int locs_size) { 176 if (!UseRelocIndex) return locs_size; // no index 177 code_size = round_to(code_size, oopSize); 178 locs_size = round_to(locs_size, oopSize); 179 int index_size = num_cards(code_size) * sizeof(RelocIndexEntry); 180 // format of indexed relocs: 181 // relocation_begin: relocInfo ... 182 // index: (addr,reloc#) ... 183 // indexSize :relocation_end 184 return locs_size + index_size + BytesPerInt; 185} 186 187 188void RelocIterator::create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end) { 189 address relocation_begin = (address)dest_begin; 190 address relocation_end = (address)dest_end; 191 int total_size = relocation_end - relocation_begin; 192 int locs_size = dest_count * sizeof(relocInfo); 193 if (!UseRelocIndex) { 194 Copy::fill_to_bytes(relocation_begin + locs_size, total_size-locs_size, 0); 195 return; 196 } 197 int index_size = total_size - locs_size - BytesPerInt; // find out how much space is left 198 int ncards = index_size / sizeof(RelocIndexEntry); 199 assert(total_size == locs_size + index_size + BytesPerInt, "checkin'"); 200 assert(index_size >= 0 && index_size % sizeof(RelocIndexEntry) == 0, "checkin'"); 201 jint* index_size_addr = (jint*)relocation_end - 1; 202 203 assert(sizeof(jint) == BytesPerInt, "change this code"); 204 205 *index_size_addr = index_size; 206 if (index_size != 0) { 207 assert(index_size > 0, "checkin'"); 208 209 RelocIndexEntry* index = (RelocIndexEntry *)(relocation_begin + locs_size); 210 assert(index == (RelocIndexEntry*)index_size_addr - ncards, "checkin'"); 211 212 // walk over the relocations, and fill in index entries as we go 213 RelocIterator iter; 214 const address initial_addr = NULL; 215 relocInfo* const initial_current = dest_begin - 1; // biased by -1 like elsewhere 216 217 iter._code = NULL; 218 iter._addr = initial_addr; 219 iter._limit = (address)(intptr_t)(ncards * indexCardSize); 220 iter._current = initial_current; 221 iter._end = dest_begin + dest_count; 222 223 int i = 0; 224 address next_card_addr = (address)indexCardSize; 225 int addr_offset = 0; 226 int reloc_offset = 0; 227 while (true) { 228 // Checkpoint the iterator before advancing it. 229 addr_offset = iter._addr - initial_addr; 230 reloc_offset = iter._current - initial_current; 231 if (!iter.next()) break; 232 while (iter.addr() >= next_card_addr) { 233 index[i].addr_offset = addr_offset; 234 index[i].reloc_offset = reloc_offset; 235 i++; 236 next_card_addr += indexCardSize; 237 } 238 } 239 while (i < ncards) { 240 index[i].addr_offset = addr_offset; 241 index[i].reloc_offset = reloc_offset; 242 i++; 243 } 244 } 245} 246 247 248void RelocIterator::set_limits(address begin, address limit) { 249 int index_size = 0; 250 if (UseRelocIndex && _code != NULL) { 251 index_size = ((jint*)_end)[-1]; 252 _end = (relocInfo*)( (address)_end - index_size - BytesPerInt ); 253 } 254 255 _limit = limit; 256 257 // the limit affects this next stuff: 258 if (begin != NULL) { 259#ifdef ASSERT 260 // In ASSERT mode we do not actually use the index, but simply 261 // check that its contents would have led us to the right answer. 262 address addrCheck = _addr; 263 relocInfo* infoCheck = _current; 264#endif // ASSERT 265 if (index_size > 0) { 266 // skip ahead 267 RelocIndexEntry* index = (RelocIndexEntry*)_end; 268 RelocIndexEntry* index_limit = (RelocIndexEntry*)((address)index + index_size); 269 assert(_addr == _code->instructions_begin(), "_addr must be unadjusted"); 270 int card = (begin - _addr) / indexCardSize; 271 if (card > 0) { 272 if (index+card-1 < index_limit) index += card-1; 273 else index = index_limit - 1; 274#ifdef ASSERT 275 addrCheck = _addr + index->addr_offset; 276 infoCheck = _current + index->reloc_offset; 277#else 278 // Advance the iterator immediately to the last valid state 279 // for the previous card. Calling "next" will then advance 280 // it to the first item on the required card. 281 _addr += index->addr_offset; 282 _current += index->reloc_offset; 283#endif // ASSERT 284 } 285 } 286 287 relocInfo* backup; 288 address backup_addr; 289 while (true) { 290 backup = _current; 291 backup_addr = _addr; 292#ifdef ASSERT 293 if (backup == infoCheck) { 294 assert(backup_addr == addrCheck, "must match"); addrCheck = NULL; infoCheck = NULL; 295 } else { 296 assert(addrCheck == NULL || backup_addr <= addrCheck, "must not pass addrCheck"); 297 } 298#endif // ASSERT 299 if (!next() || addr() >= begin) break; 300 } 301 assert(addrCheck == NULL || addrCheck == backup_addr, "must have matched addrCheck"); 302 assert(infoCheck == NULL || infoCheck == backup, "must have matched infoCheck"); 303 // At this point, either we are at the first matching record, 304 // or else there is no such record, and !has_current(). 305 // In either case, revert to the immediatly preceding state. 306 _current = backup; 307 _addr = backup_addr; 308 set_has_current(false); 309 } 310} 311 312 313void RelocIterator::set_limit(address limit) { 314 address code_end = (address)code() + code()->size(); 315 assert(limit == NULL || limit <= code_end, "in bounds"); 316 _limit = limit; 317} 318 319 320void PatchingRelocIterator:: prepass() { 321 // turn breakpoints off during patching 322 _init_state = (*this); // save cursor 323 while (next()) { 324 if (type() == relocInfo::breakpoint_type) { 325 breakpoint_reloc()->set_active(false); 326 } 327 } 328 (RelocIterator&)(*this) = _init_state; // reset cursor for client 329} 330 331 332void PatchingRelocIterator:: postpass() { 333 // turn breakpoints back on after patching 334 (RelocIterator&)(*this) = _init_state; // reset cursor again 335 while (next()) { 336 if (type() == relocInfo::breakpoint_type) { 337 breakpoint_Relocation* bpt = breakpoint_reloc(); 338 bpt->set_active(bpt->enabled()); 339 } 340 } 341} 342 343 344// All the strange bit-encodings are in here. 345// The idea is to encode relocation data which are small integers 346// very efficiently (a single extra halfword). Larger chunks of 347// relocation data need a halfword header to hold their size. 348void RelocIterator::advance_over_prefix() { 349 if (_current->is_datalen()) { 350 _data = (short*) _current->data(); 351 _datalen = _current->datalen(); 352 _current += _datalen + 1; // skip the embedded data & header 353 } else { 354 _databuf = _current->immediate(); 355 _data = &_databuf; 356 _datalen = 1; 357 _current++; // skip the header 358 } 359 // The client will see the following relocInfo, whatever that is. 360 // It is the reloc to which the preceding data applies. 361} 362 363 364address RelocIterator::compute_section_start(int n) const { 365// This routine not only computes a section start, but also 366// memoizes it for later. 367#define CACHE ((RelocIterator*)this)->_section_start[n] 368 CodeBlob* cb = code(); 369 guarantee(cb != NULL, "must have a code blob"); 370 if (n == CodeBuffer::SECT_INSTS) 371 return CACHE = cb->instructions_begin(); 372 assert(cb->is_nmethod(), "only nmethods have these sections"); 373 nmethod* nm = (nmethod*) cb; 374 address res = NULL; 375 switch (n) { 376 case CodeBuffer::SECT_STUBS: 377 res = nm->stub_begin(); 378 break; 379 case CodeBuffer::SECT_CONSTS: 380 res = nm->consts_begin(); 381 break; 382 default: 383 ShouldNotReachHere(); 384 } 385 assert(nm->contains(res) || res == nm->instructions_end(), "tame pointer"); 386 CACHE = res; 387 return res; 388#undef CACHE 389} 390 391 392Relocation* RelocIterator::reloc() { 393 // (take the "switch" out-of-line) 394 relocInfo::relocType t = type(); 395 if (false) {} 396 #define EACH_TYPE(name) \ 397 else if (t == relocInfo::name##_type) { \ 398 return name##_reloc(); \ 399 } 400 APPLY_TO_RELOCATIONS(EACH_TYPE); 401 #undef EACH_TYPE 402 assert(t == relocInfo::none, "must be padding"); 403 return new(_rh) Relocation(); 404} 405 406 407//////// Methods for flyweight Relocation types 408 409 410RelocationHolder RelocationHolder::plus(int offset) const { 411 if (offset != 0) { 412 switch (type()) { 413 case relocInfo::none: 414 break; 415 case relocInfo::oop_type: 416 { 417 oop_Relocation* r = (oop_Relocation*)reloc(); 418 return oop_Relocation::spec(r->oop_index(), r->offset() + offset); 419 } 420 default: 421 ShouldNotReachHere(); 422 } 423 } 424 return (*this); 425} 426 427 428void Relocation::guarantee_size() { 429 guarantee(false, "Make _relocbuf bigger!"); 430} 431 432 // some relocations can compute their own values 433address Relocation::value() { 434 ShouldNotReachHere(); 435 return NULL; 436} 437 438 439void Relocation::set_value(address x) { 440 ShouldNotReachHere(); 441} 442 443 444RelocationHolder Relocation::spec_simple(relocInfo::relocType rtype) { 445 if (rtype == relocInfo::none) return RelocationHolder::none; 446 relocInfo ri = relocInfo(rtype, 0); 447 RelocIterator itr; 448 itr.set_current(ri); 449 itr.reloc(); 450 return itr._rh; 451} 452 453 454static inline bool is_index(intptr_t index) { 455 return 0 < index && index < os::vm_page_size(); 456} 457 458 459int32_t Relocation::runtime_address_to_index(address runtime_address) { 460 assert(!is_index((intptr_t)runtime_address), "must not look like an index"); 461 462 if (runtime_address == NULL) return 0; 463 464 StubCodeDesc* p = StubCodeDesc::desc_for(runtime_address); 465 if (p != NULL && p->begin() == runtime_address) { 466 assert(is_index(p->index()), "there must not be too many stubs"); 467 return (int32_t)p->index(); 468 } else { 469 // Known "miscellaneous" non-stub pointers: 470 // os::get_polling_page(), SafepointSynchronize::address_of_state() 471 if (PrintRelocations) { 472 tty->print_cr("random unregistered address in relocInfo: " INTPTR_FORMAT, runtime_address); 473 } 474#ifndef _LP64 475 return (int32_t) (intptr_t)runtime_address; 476#else 477 // didn't fit return non-index 478 return -1; 479#endif /* _LP64 */ 480 } 481} 482 483 484address Relocation::index_to_runtime_address(int32_t index) { 485 if (index == 0) return NULL; 486 487 if (is_index(index)) { 488 StubCodeDesc* p = StubCodeDesc::desc_for_index(index); 489 assert(p != NULL, "there must be a stub for this index"); 490 return p->begin(); 491 } else { 492#ifndef _LP64 493 // this only works on 32bit machines 494 return (address) ((intptr_t) index); 495#else 496 fatal("Relocation::index_to_runtime_address, int32_t not pointer sized"); 497 return NULL; 498#endif /* _LP64 */ 499 } 500} 501 502address Relocation::old_addr_for(address newa, 503 const CodeBuffer* src, CodeBuffer* dest) { 504 int sect = dest->section_index_of(newa); 505 guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); 506 address ostart = src->code_section(sect)->start(); 507 address nstart = dest->code_section(sect)->start(); 508 return ostart + (newa - nstart); 509} 510 511address Relocation::new_addr_for(address olda, 512 const CodeBuffer* src, CodeBuffer* dest) { 513 debug_only(const CodeBuffer* src0 = src); 514 int sect = CodeBuffer::SECT_NONE; 515 // Look for olda in the source buffer, and all previous incarnations 516 // if the source buffer has been expanded. 517 for (; src != NULL; src = src->before_expand()) { 518 sect = src->section_index_of(olda); 519 if (sect != CodeBuffer::SECT_NONE) break; 520 } 521 guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); 522 address ostart = src->code_section(sect)->start(); 523 address nstart = dest->code_section(sect)->start(); 524 return nstart + (olda - ostart); 525} 526 527void Relocation::normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections) { 528 address addr0 = addr; 529 if (addr0 == NULL || dest->allocates2(addr0)) return; 530 CodeBuffer* cb = dest->outer(); 531 addr = new_addr_for(addr0, cb, cb); 532 assert(allow_other_sections || dest->contains2(addr), 533 "addr must be in required section"); 534} 535 536 537void CallRelocation::set_destination(address x) { 538 pd_set_call_destination(x); 539} 540 541void CallRelocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 542 // Usually a self-relative reference to an external routine. 543 // On some platforms, the reference is absolute (not self-relative). 544 // The enhanced use of pd_call_destination sorts this all out. 545 address orig_addr = old_addr_for(addr(), src, dest); 546 address callee = pd_call_destination(orig_addr); 547 // Reassert the callee address, this time in the new copy of the code. 548 pd_set_call_destination(callee); 549} 550 551 552//// pack/unpack methods 553 554void oop_Relocation::pack_data_to(CodeSection* dest) { 555 short* p = (short*) dest->locs_end(); 556 p = pack_2_ints_to(p, _oop_index, _offset); 557 dest->set_locs_end((relocInfo*) p); 558} 559 560 561void oop_Relocation::unpack_data() { 562 unpack_2_ints(_oop_index, _offset); 563} 564 565 566void virtual_call_Relocation::pack_data_to(CodeSection* dest) { 567 short* p = (short*) dest->locs_end(); 568 address point = dest->locs_point(); 569 570 // Try to make a pointer NULL first. 571 if (_oop_limit >= point && 572 _oop_limit <= point + NativeCall::instruction_size) { 573 _oop_limit = NULL; 574 } 575 // If the _oop_limit is NULL, it "defaults" to the end of the call. 576 // See ic_call_Relocation::oop_limit() below. 577 578 normalize_address(_first_oop, dest); 579 normalize_address(_oop_limit, dest); 580 jint x0 = scaled_offset_null_special(_first_oop, point); 581 jint x1 = scaled_offset_null_special(_oop_limit, point); 582 p = pack_2_ints_to(p, x0, x1); 583 dest->set_locs_end((relocInfo*) p); 584} 585 586 587void virtual_call_Relocation::unpack_data() { 588 jint x0, x1; unpack_2_ints(x0, x1); 589 address point = addr(); 590 _first_oop = x0==0? NULL: address_from_scaled_offset(x0, point); 591 _oop_limit = x1==0? NULL: address_from_scaled_offset(x1, point); 592} 593 594 595void static_stub_Relocation::pack_data_to(CodeSection* dest) { 596 short* p = (short*) dest->locs_end(); 597 CodeSection* insts = dest->outer()->insts(); 598 normalize_address(_static_call, insts); 599 p = pack_1_int_to(p, scaled_offset(_static_call, insts->start())); 600 dest->set_locs_end((relocInfo*) p); 601} 602 603void static_stub_Relocation::unpack_data() { 604 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 605 _static_call = address_from_scaled_offset(unpack_1_int(), base); 606} 607 608 609void external_word_Relocation::pack_data_to(CodeSection* dest) { 610 short* p = (short*) dest->locs_end(); 611 int32_t index = runtime_address_to_index(_target); 612#ifndef _LP64 613 p = pack_1_int_to(p, index); 614#else 615 if (is_index(index)) { 616 p = pack_2_ints_to(p, index, 0); 617 } else { 618 jlong t = (jlong) _target; 619 int32_t lo = low(t); 620 int32_t hi = high(t); 621 p = pack_2_ints_to(p, lo, hi); 622 DEBUG_ONLY(jlong t1 = jlong_from(hi, lo)); 623 assert(!is_index(t1) && (address) t1 == _target, "not symmetric"); 624 } 625#endif /* _LP64 */ 626 dest->set_locs_end((relocInfo*) p); 627} 628 629 630void external_word_Relocation::unpack_data() { 631#ifndef _LP64 632 _target = index_to_runtime_address(unpack_1_int()); 633#else 634 int32_t lo, hi; 635 unpack_2_ints(lo, hi); 636 jlong t = jlong_from(hi, lo);; 637 if (is_index(t)) { 638 _target = index_to_runtime_address(t); 639 } else { 640 _target = (address) t; 641 } 642#endif /* _LP64 */ 643} 644 645 646void internal_word_Relocation::pack_data_to(CodeSection* dest) { 647 short* p = (short*) dest->locs_end(); 648 normalize_address(_target, dest, true); 649 650 // Check whether my target address is valid within this section. 651 // If not, strengthen the relocation type to point to another section. 652 int sindex = _section; 653 if (sindex == CodeBuffer::SECT_NONE && _target != NULL 654 && (!dest->allocates(_target) || _target == dest->locs_point())) { 655 sindex = dest->outer()->section_index_of(_target); 656 guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere"); 657 relocInfo* base = dest->locs_end() - 1; 658 assert(base->type() == this->type(), "sanity"); 659 // Change the written type, to be section_word_type instead. 660 base->set_type(relocInfo::section_word_type); 661 } 662 663 // Note: An internal_word relocation cannot refer to its own instruction, 664 // because we reserve "0" to mean that the pointer itself is embedded 665 // in the code stream. We use a section_word relocation for such cases. 666 667 if (sindex == CodeBuffer::SECT_NONE) { 668 assert(type() == relocInfo::internal_word_type, "must be base class"); 669 guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section"); 670 jint x0 = scaled_offset_null_special(_target, dest->locs_point()); 671 assert(!(x0 == 0 && _target != NULL), "correct encoding of null target"); 672 p = pack_1_int_to(p, x0); 673 } else { 674 assert(_target != NULL, "sanity"); 675 CodeSection* sect = dest->outer()->code_section(sindex); 676 guarantee(sect->allocates2(_target), "must be in correct section"); 677 address base = sect->start(); 678 jint offset = scaled_offset(_target, base); 679 assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity"); 680 assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++"); 681 p = pack_1_int_to(p, (offset << section_width) | sindex); 682 } 683 684 dest->set_locs_end((relocInfo*) p); 685} 686 687 688void internal_word_Relocation::unpack_data() { 689 jint x0 = unpack_1_int(); 690 _target = x0==0? NULL: address_from_scaled_offset(x0, addr()); 691 _section = CodeBuffer::SECT_NONE; 692} 693 694 695void section_word_Relocation::unpack_data() { 696 jint x = unpack_1_int(); 697 jint offset = (x >> section_width); 698 int sindex = (x & ((1<<section_width)-1)); 699 address base = binding()->section_start(sindex); 700 701 _section = sindex; 702 _target = address_from_scaled_offset(offset, base); 703} 704 705 706void breakpoint_Relocation::pack_data_to(CodeSection* dest) { 707 short* p = (short*) dest->locs_end(); 708 address point = dest->locs_point(); 709 710 *p++ = _bits; 711 712 assert(_target != NULL, "sanity"); 713 714 if (internal()) normalize_address(_target, dest); 715 716 jint target_bits = 717 (jint)( internal() ? scaled_offset (_target, point) 718 : runtime_address_to_index(_target) ); 719 if (settable()) { 720 // save space for set_target later 721 p = add_jint(p, target_bits); 722 } else { 723 p = add_var_int(p, target_bits); 724 } 725 726 for (int i = 0; i < instrlen(); i++) { 727 // put placeholder words until bytes can be saved 728 p = add_short(p, (short)0x7777); 729 } 730 731 dest->set_locs_end((relocInfo*) p); 732} 733 734 735void breakpoint_Relocation::unpack_data() { 736 _bits = live_bits(); 737 738 int targetlen = datalen() - 1 - instrlen(); 739 jint target_bits = 0; 740 if (targetlen == 0) target_bits = 0; 741 else if (targetlen == 1) target_bits = *(data()+1); 742 else if (targetlen == 2) target_bits = relocInfo::jint_from_data(data()+1); 743 else { ShouldNotReachHere(); } 744 745 _target = internal() ? address_from_scaled_offset(target_bits, addr()) 746 : index_to_runtime_address (target_bits); 747} 748 749 750//// miscellaneous methods 751oop* oop_Relocation::oop_addr() { 752 int n = _oop_index; 753 if (n == 0) { 754 // oop is stored in the code stream 755 return (oop*) pd_address_in_code(); 756 } else { 757 // oop is stored in table at CodeBlob::oops_begin 758 return code()->oop_addr_at(n); 759 } 760} 761 762 763oop oop_Relocation::oop_value() { 764 oop v = *oop_addr(); 765 // clean inline caches store a special pseudo-null 766 if (v == (oop)Universe::non_oop_word()) v = NULL; 767 return v; 768} 769 770 771void oop_Relocation::fix_oop_relocation() { 772 if (!oop_is_immediate()) { 773 // get the oop from the pool, and re-insert it into the instruction: 774 set_value(value()); 775 } 776} 777 778 779RelocIterator virtual_call_Relocation::parse_ic(CodeBlob* &code, address &ic_call, address &first_oop, 780 oop* &oop_addr, bool *is_optimized) { 781 assert(ic_call != NULL, "ic_call address must be set"); 782 assert(ic_call != NULL || first_oop != NULL, "must supply a non-null input"); 783 if (code == NULL) { 784 if (ic_call != NULL) { 785 code = CodeCache::find_blob(ic_call); 786 } else if (first_oop != NULL) { 787 code = CodeCache::find_blob(first_oop); 788 } 789 assert(code != NULL, "address to parse must be in CodeBlob"); 790 } 791 assert(ic_call == NULL || code->contains(ic_call), "must be in CodeBlob"); 792 assert(first_oop == NULL || code->contains(first_oop), "must be in CodeBlob"); 793 794 address oop_limit = NULL; 795 796 if (ic_call != NULL) { 797 // search for the ic_call at the given address 798 RelocIterator iter(code, ic_call, ic_call+1); 799 bool ret = iter.next(); 800 assert(ret == true, "relocInfo must exist at this address"); 801 assert(iter.addr() == ic_call, "must find ic_call"); 802 if (iter.type() == relocInfo::virtual_call_type) { 803 virtual_call_Relocation* r = iter.virtual_call_reloc(); 804 first_oop = r->first_oop(); 805 oop_limit = r->oop_limit(); 806 *is_optimized = false; 807 } else { 808 assert(iter.type() == relocInfo::opt_virtual_call_type, "must be a virtual call"); 809 *is_optimized = true; 810 oop_addr = NULL; 811 first_oop = NULL; 812 return iter; 813 } 814 } 815 816 // search for the first_oop, to get its oop_addr 817 RelocIterator all_oops(code, first_oop); 818 RelocIterator iter = all_oops; 819 iter.set_limit(first_oop+1); 820 bool found_oop = false; 821 while (iter.next()) { 822 if (iter.type() == relocInfo::oop_type) { 823 assert(iter.addr() == first_oop, "must find first_oop"); 824 oop_addr = iter.oop_reloc()->oop_addr(); 825 found_oop = true; 826 break; 827 } 828 } 829 assert(found_oop, "must find first_oop"); 830 831 bool did_reset = false; 832 while (ic_call == NULL) { 833 // search forward for the ic_call matching the given first_oop 834 while (iter.next()) { 835 if (iter.type() == relocInfo::virtual_call_type) { 836 virtual_call_Relocation* r = iter.virtual_call_reloc(); 837 if (r->first_oop() == first_oop) { 838 ic_call = r->addr(); 839 oop_limit = r->oop_limit(); 840 break; 841 } 842 } 843 } 844 guarantee(!did_reset, "cannot find ic_call"); 845 iter = RelocIterator(code); // search the whole CodeBlob 846 did_reset = true; 847 } 848 849 assert(oop_limit != NULL && first_oop != NULL && ic_call != NULL, ""); 850 all_oops.set_limit(oop_limit); 851 return all_oops; 852} 853 854 855address virtual_call_Relocation::first_oop() { 856 assert(_first_oop != NULL && _first_oop < addr(), "must precede ic_call"); 857 return _first_oop; 858} 859 860 861address virtual_call_Relocation::oop_limit() { 862 if (_oop_limit == NULL) 863 return addr() + NativeCall::instruction_size; 864 else 865 return _oop_limit; 866} 867 868 869 870void virtual_call_Relocation::clear_inline_cache() { 871 // No stubs for ICs 872 // Clean IC 873 ResourceMark rm; 874 CompiledIC* icache = CompiledIC_at(this); 875 icache->set_to_clean(); 876} 877 878 879void opt_virtual_call_Relocation::clear_inline_cache() { 880 // No stubs for ICs 881 // Clean IC 882 ResourceMark rm; 883 CompiledIC* icache = CompiledIC_at(this); 884 icache->set_to_clean(); 885} 886 887 888address opt_virtual_call_Relocation::static_stub() { 889 // search for the static stub who points back to this static call 890 address static_call_addr = addr(); 891 RelocIterator iter(code()); 892 while (iter.next()) { 893 if (iter.type() == relocInfo::static_stub_type) { 894 if (iter.static_stub_reloc()->static_call() == static_call_addr) { 895 return iter.addr(); 896 } 897 } 898 } 899 return NULL; 900} 901 902 903void static_call_Relocation::clear_inline_cache() { 904 // Safe call site info 905 CompiledStaticCall* handler = compiledStaticCall_at(this); 906 handler->set_to_clean(); 907} 908 909 910address static_call_Relocation::static_stub() { 911 // search for the static stub who points back to this static call 912 address static_call_addr = addr(); 913 RelocIterator iter(code()); 914 while (iter.next()) { 915 if (iter.type() == relocInfo::static_stub_type) { 916 if (iter.static_stub_reloc()->static_call() == static_call_addr) { 917 return iter.addr(); 918 } 919 } 920 } 921 return NULL; 922} 923 924 925void static_stub_Relocation::clear_inline_cache() { 926 // Call stub is only used when calling the interpreted code. 927 // It does not really need to be cleared, except that we want to clean out the methodoop. 928 CompiledStaticCall::set_stub_to_clean(this); 929} 930 931 932void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 933 address target = _target; 934 if (target == NULL) { 935 // An absolute embedded reference to an external location, 936 // which means there is nothing to fix here. 937 return; 938 } 939 // Probably this reference is absolute, not relative, so the 940 // following is probably a no-op. 941 assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity"); 942 set_value(target); 943} 944 945 946address external_word_Relocation::target() { 947 address target = _target; 948 if (target == NULL) { 949 target = pd_get_address_from_code(); 950 } 951 return target; 952} 953 954 955void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 956 address target = _target; 957 if (target == NULL) { 958 if (addr_in_const()) { 959 target = new_addr_for(*(address*)addr(), src, dest); 960 } else { 961 target = new_addr_for(pd_get_address_from_code(), src, dest); 962 } 963 } 964 set_value(target); 965} 966 967 968address internal_word_Relocation::target() { 969 address target = _target; 970 if (target == NULL) { 971 target = pd_get_address_from_code(); 972 } 973 return target; 974} 975 976 977breakpoint_Relocation::breakpoint_Relocation(int kind, address target, bool internal) { 978 bool active = false; 979 bool enabled = (kind == initialization); 980 bool removable = (kind != safepoint); 981 bool settable = (target == NULL); 982 983 int bits = kind; 984 if (enabled) bits |= enabled_state; 985 if (internal) bits |= internal_attr; 986 if (removable) bits |= removable_attr; 987 if (settable) bits |= settable_attr; 988 989 _bits = bits | high_bit; 990 _target = target; 991 992 assert(this->kind() == kind, "kind encoded"); 993 assert(this->enabled() == enabled, "enabled encoded"); 994 assert(this->active() == active, "active encoded"); 995 assert(this->internal() == internal, "internal encoded"); 996 assert(this->removable() == removable, "removable encoded"); 997 assert(this->settable() == settable, "settable encoded"); 998} 999 1000 1001address breakpoint_Relocation::target() const { 1002 return _target; 1003} 1004 1005 1006void breakpoint_Relocation::set_target(address x) { 1007 assert(settable(), "must be settable"); 1008 jint target_bits = 1009 (jint)(internal() ? scaled_offset (x, addr()) 1010 : runtime_address_to_index(x)); 1011 short* p = &live_bits() + 1; 1012 p = add_jint(p, target_bits); 1013 assert(p == instrs(), "new target must fit"); 1014 _target = x; 1015} 1016 1017 1018void breakpoint_Relocation::set_enabled(bool b) { 1019 if (enabled() == b) return; 1020 1021 if (b) { 1022 set_bits(bits() | enabled_state); 1023 } else { 1024 set_active(false); // remove the actual breakpoint insn, if any 1025 set_bits(bits() & ~enabled_state); 1026 } 1027} 1028 1029 1030void breakpoint_Relocation::set_active(bool b) { 1031 assert(!b || enabled(), "cannot activate a disabled breakpoint"); 1032 1033 if (active() == b) return; 1034 1035 // %%% should probably seize a lock here (might not be the right lock) 1036 //MutexLockerEx ml_patch(Patching_lock, true); 1037 //if (active() == b) return; // recheck state after locking 1038 1039 if (b) { 1040 set_bits(bits() | active_state); 1041 if (instrlen() == 0) 1042 fatal("breakpoints in original code must be undoable"); 1043 pd_swap_in_breakpoint (addr(), instrs(), instrlen()); 1044 } else { 1045 set_bits(bits() & ~active_state); 1046 pd_swap_out_breakpoint(addr(), instrs(), instrlen()); 1047 } 1048} 1049 1050 1051//--------------------------------------------------------------------------------- 1052// Non-product code 1053 1054#ifndef PRODUCT 1055 1056static const char* reloc_type_string(relocInfo::relocType t) { 1057 switch (t) { 1058 #define EACH_CASE(name) \ 1059 case relocInfo::name##_type: \ 1060 return #name; 1061 1062 APPLY_TO_RELOCATIONS(EACH_CASE); 1063 #undef EACH_CASE 1064 1065 case relocInfo::none: 1066 return "none"; 1067 case relocInfo::data_prefix_tag: 1068 return "prefix"; 1069 default: 1070 return "UNKNOWN RELOC TYPE"; 1071 } 1072} 1073 1074 1075void RelocIterator::print_current() { 1076 if (!has_current()) { 1077 tty->print_cr("(no relocs)"); 1078 return; 1079 } 1080 tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT, 1081 _current, type(), reloc_type_string((relocInfo::relocType) type()), _addr); 1082 if (current()->format() != 0) 1083 tty->print(" format=%d", current()->format()); 1084 if (datalen() == 1) { 1085 tty->print(" data=%d", data()[0]); 1086 } else if (datalen() > 0) { 1087 tty->print(" data={"); 1088 for (int i = 0; i < datalen(); i++) { 1089 tty->print("%04x", data()[i] & 0xFFFF); 1090 } 1091 tty->print("}"); 1092 } 1093 tty->print("]"); 1094 switch (type()) { 1095 case relocInfo::oop_type: 1096 { 1097 oop_Relocation* r = oop_reloc(); 1098 oop* oop_addr = NULL; 1099 oop raw_oop = NULL; 1100 oop oop_value = NULL; 1101 if (code() != NULL || r->oop_is_immediate()) { 1102 oop_addr = r->oop_addr(); 1103 raw_oop = *oop_addr; 1104 oop_value = r->oop_value(); 1105 } 1106 tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 1107 oop_addr, (address)raw_oop, r->offset()); 1108 // Do not print the oop by default--we want this routine to 1109 // work even during GC or other inconvenient times. 1110 if (WizardMode && oop_value != NULL) { 1111 tty->print("oop_value=" INTPTR_FORMAT ": ", (address)oop_value); 1112 oop_value->print_value_on(tty); 1113 } 1114 break; 1115 } 1116 case relocInfo::external_word_type: 1117 case relocInfo::internal_word_type: 1118 case relocInfo::section_word_type: 1119 { 1120 DataRelocation* r = (DataRelocation*) reloc(); 1121 tty->print(" | [target=" INTPTR_FORMAT "]", r->value()); //value==target 1122 break; 1123 } 1124 case relocInfo::static_call_type: 1125 case relocInfo::runtime_call_type: 1126 { 1127 CallRelocation* r = (CallRelocation*) reloc(); 1128 tty->print(" | [destination=" INTPTR_FORMAT "]", r->destination()); 1129 break; 1130 } 1131 case relocInfo::virtual_call_type: 1132 { 1133 virtual_call_Relocation* r = (virtual_call_Relocation*) reloc(); 1134 tty->print(" | [destination=" INTPTR_FORMAT " first_oop=" INTPTR_FORMAT " oop_limit=" INTPTR_FORMAT "]", 1135 r->destination(), r->first_oop(), r->oop_limit()); 1136 break; 1137 } 1138 case relocInfo::static_stub_type: 1139 { 1140 static_stub_Relocation* r = (static_stub_Relocation*) reloc(); 1141 tty->print(" | [static_call=" INTPTR_FORMAT "]", r->static_call()); 1142 break; 1143 } 1144 } 1145 tty->cr(); 1146} 1147 1148 1149void RelocIterator::print() { 1150 RelocIterator save_this = (*this); 1151 relocInfo* scan = _current; 1152 if (!has_current()) scan += 1; // nothing to scan here! 1153 1154 bool skip_next = has_current(); 1155 bool got_next; 1156 while (true) { 1157 got_next = (skip_next || next()); 1158 skip_next = false; 1159 1160 tty->print(" @" INTPTR_FORMAT ": ", scan); 1161 relocInfo* newscan = _current+1; 1162 if (!has_current()) newscan -= 1; // nothing to scan here! 1163 while (scan < newscan) { 1164 tty->print("%04x", *(short*)scan & 0xFFFF); 1165 scan++; 1166 } 1167 tty->cr(); 1168 1169 if (!got_next) break; 1170 print_current(); 1171 } 1172 1173 (*this) = save_this; 1174} 1175 1176// For the debugger: 1177extern "C" 1178void print_blob_locs(CodeBlob* cb) { 1179 cb->print(); 1180 RelocIterator iter(cb); 1181 iter.print(); 1182} 1183extern "C" 1184void print_buf_locs(CodeBuffer* cb) { 1185 FlagSetting fs(PrintRelocations, true); 1186 cb->print(); 1187} 1188#endif // !PRODUCT 1189