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