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_2_ints_to(p, x0, _method_index); 585 dest->set_locs_end((relocInfo*) p); 586 } 587 588 589 void virtual_call_Relocation::unpack_data() { 590 jint x0 = 0; 591 unpack_2_ints(x0, _method_index); 592 address point = addr(); 593 _cached_value = x0==0? NULL: address_from_scaled_offset(x0, point); 594 } 595 596 597 void static_stub_Relocation::pack_data_to(CodeSection* dest) { 598 short* p = (short*) dest->locs_end(); 599 CodeSection* insts = dest->outer()->insts(); 600 normalize_address(_static_call, insts); 601 p = pack_1_int_to(p, scaled_offset(_static_call, insts->start())); 602 dest->set_locs_end((relocInfo*) p); 603 } 604 605 void static_stub_Relocation::unpack_data() { 606 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 607 jint offset = unpack_1_int(); 608 _static_call = address_from_scaled_offset(offset, base); 609 } 610 611 void trampoline_stub_Relocation::pack_data_to(CodeSection* dest ) { 612 short* p = (short*) dest->locs_end(); 613 CodeSection* insts = dest->outer()->insts(); 614 normalize_address(_owner, insts); 615 p = pack_1_int_to(p, scaled_offset(_owner, insts->start())); 616 dest->set_locs_end((relocInfo*) p); 617 } 618 619 void trampoline_stub_Relocation::unpack_data() { 620 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 621 _owner = address_from_scaled_offset(unpack_1_int(), base); 622 } 623 624 void external_word_Relocation::pack_data_to(CodeSection* dest) { 625 short* p = (short*) dest->locs_end(); 626 int32_t index = runtime_address_to_index(_target); 627 #ifndef _LP64 628 p = pack_1_int_to(p, index); 629 #else 630 if (is_reloc_index(index)) { 631 p = pack_2_ints_to(p, index, 0); 632 } else { 633 jlong t = (jlong) _target; 634 int32_t lo = low(t); 635 int32_t hi = high(t); 636 p = pack_2_ints_to(p, lo, hi); 637 DEBUG_ONLY(jlong t1 = jlong_from(hi, lo)); 638 assert(!is_reloc_index(t1) && (address) t1 == _target, "not symmetric"); 639 } 640 #endif /* _LP64 */ 641 dest->set_locs_end((relocInfo*) p); 642 } 643 644 645 void external_word_Relocation::unpack_data() { 646 #ifndef _LP64 647 _target = index_to_runtime_address(unpack_1_int()); 648 #else 649 int32_t lo, hi; 650 unpack_2_ints(lo, hi); 651 jlong t = jlong_from(hi, lo);; 652 if (is_reloc_index(t)) { 653 _target = index_to_runtime_address(t); 654 } else { 655 _target = (address) t; 656 } 657 #endif /* _LP64 */ 658 } 659 660 661 void internal_word_Relocation::pack_data_to(CodeSection* dest) { 662 short* p = (short*) dest->locs_end(); 663 normalize_address(_target, dest, true); 664 665 // Check whether my target address is valid within this section. 666 // If not, strengthen the relocation type to point to another section. 667 int sindex = _section; 668 if (sindex == CodeBuffer::SECT_NONE && _target != NULL 669 && (!dest->allocates(_target) || _target == dest->locs_point())) { 670 sindex = dest->outer()->section_index_of(_target); 671 guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere"); 672 relocInfo* base = dest->locs_end() - 1; 673 assert(base->type() == this->type(), "sanity"); 674 // Change the written type, to be section_word_type instead. 675 base->set_type(relocInfo::section_word_type); 676 } 677 678 // Note: An internal_word relocation cannot refer to its own instruction, 679 // because we reserve "0" to mean that the pointer itself is embedded 680 // in the code stream. We use a section_word relocation for such cases. 681 682 if (sindex == CodeBuffer::SECT_NONE) { 683 assert(type() == relocInfo::internal_word_type, "must be base class"); 684 guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section"); 685 jint x0 = scaled_offset_null_special(_target, dest->locs_point()); 686 assert(!(x0 == 0 && _target != NULL), "correct encoding of null target"); 687 p = pack_1_int_to(p, x0); 688 } else { 689 assert(_target != NULL, "sanity"); 690 CodeSection* sect = dest->outer()->code_section(sindex); 691 guarantee(sect->allocates2(_target), "must be in correct section"); 692 address base = sect->start(); 693 jint offset = scaled_offset(_target, base); 694 assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity"); 695 assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++"); 696 p = pack_1_int_to(p, (offset << section_width) | sindex); 697 } 698 699 dest->set_locs_end((relocInfo*) p); 700 } 701 702 703 void internal_word_Relocation::unpack_data() { 704 jint x0 = unpack_1_int(); 705 _target = x0==0? NULL: address_from_scaled_offset(x0, addr()); 706 _section = CodeBuffer::SECT_NONE; 707 } 708 709 710 void section_word_Relocation::unpack_data() { 711 jint x = unpack_1_int(); 712 jint offset = (x >> section_width); 713 int sindex = (x & ((1<<section_width)-1)); 714 address base = binding()->section_start(sindex); 715 716 _section = sindex; 717 _target = address_from_scaled_offset(offset, base); 718 } 719 720 //// miscellaneous methods 721 oop* oop_Relocation::oop_addr() { 722 int n = _oop_index; 723 if (n == 0) { 724 // oop is stored in the code stream 725 return (oop*) pd_address_in_code(); 726 } else { 727 // oop is stored in table at nmethod::oops_begin 728 return code()->oop_addr_at(n); 729 } 730 } 731 732 733 oop oop_Relocation::oop_value() { 734 oop v = *oop_addr(); 735 // clean inline caches store a special pseudo-null 736 if (v == (oop)Universe::non_oop_word()) v = NULL; 737 return v; 738 } 739 740 741 void oop_Relocation::fix_oop_relocation() { 742 if (!oop_is_immediate()) { 743 // get the oop from the pool, and re-insert it into the instruction: 744 set_value(value()); 745 } 746 } 747 748 749 void oop_Relocation::verify_oop_relocation() { 750 if (!oop_is_immediate()) { 751 // get the oop from the pool, and re-insert it into the instruction: 752 verify_value(value()); 753 } 754 } 755 756 // meta data versions 757 Metadata** metadata_Relocation::metadata_addr() { 758 int n = _metadata_index; 759 if (n == 0) { 760 // metadata is stored in the code stream 761 return (Metadata**) pd_address_in_code(); 762 } else { 763 // metadata is stored in table at nmethod::metadatas_begin 764 return code()->metadata_addr_at(n); 765 } 766 } 767 768 769 Metadata* metadata_Relocation::metadata_value() { 770 Metadata* v = *metadata_addr(); 771 // clean inline caches store a special pseudo-null 772 if (v == (Metadata*)Universe::non_oop_word()) v = NULL; 773 return v; 774 } 775 776 777 void metadata_Relocation::fix_metadata_relocation() { 778 if (!metadata_is_immediate()) { 779 // get the metadata from the pool, and re-insert it into the instruction: 780 pd_fix_value(value()); 781 } 782 } 783 784 785 void metadata_Relocation::verify_metadata_relocation() { 786 if (!metadata_is_immediate()) { 787 // get the metadata from the pool, and re-insert it into the instruction: 788 verify_value(value()); 789 } 790 } 791 792 address virtual_call_Relocation::cached_value() { 793 assert(_cached_value != NULL && _cached_value < addr(), "must precede ic_call"); 794 return _cached_value; 795 } 796 797 Method* virtual_call_Relocation::method_value() { 798 Metadata* m = code()->metadata_at(_method_index); 799 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 800 assert(m == NULL || m->is_method(), "not a method"); 801 return (Method*)m; 802 } 803 804 void virtual_call_Relocation::clear_inline_cache() { 805 // No stubs for ICs 806 // Clean IC 807 ResourceMark rm; 808 CompiledIC* icache = CompiledIC_at(this); 809 icache->set_to_clean(); 810 } 811 812 813 void opt_virtual_call_Relocation::pack_data_to(CodeSection* dest) { 814 short* p = (short*) dest->locs_end(); 815 p = pack_1_int_to(p, _method_index); 816 dest->set_locs_end((relocInfo*) p); 817 } 818 819 void opt_virtual_call_Relocation::unpack_data() { 820 _method_index = unpack_1_int(); 821 } 822 823 Method* opt_virtual_call_Relocation::method_value() { 824 Metadata* m = code()->metadata_at(_method_index); 825 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 826 assert(m == NULL || m->is_method(), "not a method"); 827 return (Method*)m; 828 } 829 830 void opt_virtual_call_Relocation::clear_inline_cache() { 831 // No stubs for ICs 832 // Clean IC 833 ResourceMark rm; 834 CompiledIC* icache = CompiledIC_at(this); 835 icache->set_to_clean(); 836 } 837 838 839 address opt_virtual_call_Relocation::static_stub() { 840 // search for the static stub who points back to this static call 841 address static_call_addr = addr(); 842 RelocIterator iter(code()); 843 while (iter.next()) { 844 if (iter.type() == relocInfo::static_stub_type) { 845 static_stub_Relocation* stub_reloc = iter.static_stub_reloc(); 846 if (stub_reloc->static_call() == static_call_addr) { 847 return iter.addr(); 848 } 849 } 850 } 851 return NULL; 852 } 853 854 Method* static_call_Relocation::method_value() { 855 Metadata* m = code()->metadata_at(_method_index); 856 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 857 assert(m == NULL || m->is_method(), "not a method"); 858 return (Method*)m; 859 } 860 861 void static_call_Relocation::pack_data_to(CodeSection* dest) { 862 short* p = (short*) dest->locs_end(); 863 p = pack_1_int_to(p, _method_index); 864 dest->set_locs_end((relocInfo*) p); 865 } 866 867 void static_call_Relocation::unpack_data() { 868 _method_index = unpack_1_int(); 869 } 870 871 void static_call_Relocation::clear_inline_cache() { 872 // Safe call site info 873 CompiledStaticCall* handler = compiledStaticCall_at(this); 874 handler->set_to_clean(); 875 } 876 877 878 address static_call_Relocation::static_stub() { 879 // search for the static stub who points back to this static call 880 address static_call_addr = addr(); 881 RelocIterator iter(code()); 882 while (iter.next()) { 883 if (iter.type() == relocInfo::static_stub_type) { 884 static_stub_Relocation* stub_reloc = iter.static_stub_reloc(); 885 if (stub_reloc->static_call() == static_call_addr) { 886 return iter.addr(); 887 } 888 } 889 } 890 return NULL; 891 } 892 893 // Finds the trampoline address for a call. If no trampoline stub is 894 // found NULL is returned which can be handled by the caller. 895 address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) { 896 // There are no relocations available when the code gets relocated 897 // because of CodeBuffer expansion. 898 if (code->relocation_size() == 0) 899 return NULL; 900 901 RelocIterator iter(code, call); 902 while (iter.next()) { 903 if (iter.type() == relocInfo::trampoline_stub_type) { 904 if (iter.trampoline_stub_reloc()->owner() == call) { 905 return iter.addr(); 906 } 907 } 908 } 909 910 return NULL; 911 } 912 913 void static_stub_Relocation::clear_inline_cache() { 914 // Call stub is only used when calling the interpreted code. 915 // It does not really need to be cleared, except that we want to clean out the methodoop. 916 CompiledStaticCall::set_stub_to_clean(this); 917 } 918 919 920 void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 921 address target = _target; 922 if (target == NULL) { 923 // An absolute embedded reference to an external location, 924 // which means there is nothing to fix here. 925 return; 926 } 927 // Probably this reference is absolute, not relative, so the 928 // following is probably a no-op. 929 assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity"); 930 set_value(target); 931 } 932 933 934 address external_word_Relocation::target() { 935 address target = _target; 936 if (target == NULL) { 937 target = pd_get_address_from_code(); 938 } 939 return target; 940 } 941 942 943 void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 944 address target = _target; 945 if (target == NULL) { 946 target = new_addr_for(this->target(), src, dest); 947 } 948 set_value(target); 949 } 950 951 952 address internal_word_Relocation::target() { 953 address target = _target; 954 if (target == NULL) { 955 if (addr_in_const()) { 956 target = *(address*)addr(); 957 } else { 958 target = pd_get_address_from_code(); 959 } 960 } 961 return target; 962 } 963 964 //--------------------------------------------------------------------------------- 965 // Non-product code 966 967 #ifndef PRODUCT 968 969 static const char* reloc_type_string(relocInfo::relocType t) { 970 switch (t) { 971 #define EACH_CASE(name) \ 972 case relocInfo::name##_type: \ 973 return #name; 974 975 APPLY_TO_RELOCATIONS(EACH_CASE); 976 #undef EACH_CASE 977 978 case relocInfo::none: 979 return "none"; 980 case relocInfo::data_prefix_tag: 981 return "prefix"; 982 default: 983 return "UNKNOWN RELOC TYPE"; 984 } 985 } 986 987 988 void RelocIterator::print_current() { 989 if (!has_current()) { 990 tty->print_cr("(no relocs)"); 991 return; 992 } 993 tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d", 994 p2i(_current), type(), reloc_type_string((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset()); 995 if (current()->format() != 0) 996 tty->print(" format=%d", current()->format()); 997 if (datalen() == 1) { 998 tty->print(" data=%d", data()[0]); 999 } else if (datalen() > 0) { 1000 tty->print(" data={"); 1001 for (int i = 0; i < datalen(); i++) { 1002 tty->print("%04x", data()[i] & 0xFFFF); 1003 } 1004 tty->print("}"); 1005 } 1006 tty->print("]"); 1007 switch (type()) { 1008 case relocInfo::oop_type: 1009 { 1010 oop_Relocation* r = oop_reloc(); 1011 oop* oop_addr = NULL; 1012 oop raw_oop = NULL; 1013 oop oop_value = NULL; 1014 if (code() != NULL || r->oop_is_immediate()) { 1015 oop_addr = r->oop_addr(); 1016 raw_oop = *oop_addr; 1017 oop_value = r->oop_value(); 1018 } 1019 tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 1020 p2i(oop_addr), p2i(raw_oop), r->offset()); 1021 // Do not print the oop by default--we want this routine to 1022 // work even during GC or other inconvenient times. 1023 if (WizardMode && oop_value != NULL) { 1024 tty->print("oop_value=" INTPTR_FORMAT ": ", p2i(oop_value)); 1025 oop_value->print_value_on(tty); 1026 } 1027 break; 1028 } 1029 case relocInfo::metadata_type: 1030 { 1031 metadata_Relocation* r = metadata_reloc(); 1032 Metadata** metadata_addr = NULL; 1033 Metadata* raw_metadata = NULL; 1034 Metadata* metadata_value = NULL; 1035 if (code() != NULL || r->metadata_is_immediate()) { 1036 metadata_addr = r->metadata_addr(); 1037 raw_metadata = *metadata_addr; 1038 metadata_value = r->metadata_value(); 1039 } 1040 tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 1041 p2i(metadata_addr), p2i(raw_metadata), r->offset()); 1042 if (metadata_value != NULL) { 1043 tty->print("metadata_value=" INTPTR_FORMAT ": ", p2i(metadata_value)); 1044 metadata_value->print_value_on(tty); 1045 } 1046 break; 1047 } 1048 case relocInfo::external_word_type: 1049 case relocInfo::internal_word_type: 1050 case relocInfo::section_word_type: 1051 { 1052 DataRelocation* r = (DataRelocation*) reloc(); 1053 tty->print(" | [target=" INTPTR_FORMAT "]", p2i(r->value())); //value==target 1054 break; 1055 } 1056 case relocInfo::static_call_type: 1057 { 1058 static_call_Relocation* r = (static_call_Relocation*) reloc(); 1059 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1060 p2i(r->destination()), p2i(r->method_value())); 1061 break; 1062 } 1063 case relocInfo::runtime_call_type: 1064 { 1065 CallRelocation* r = (CallRelocation*) reloc(); 1066 tty->print(" | [destination=" INTPTR_FORMAT "]", p2i(r->destination())); 1067 break; 1068 } 1069 case relocInfo::virtual_call_type: 1070 { 1071 virtual_call_Relocation* r = (virtual_call_Relocation*) reloc(); 1072 tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1073 p2i(r->destination()), p2i(r->cached_value()), p2i(r->method_value())); 1074 break; 1075 } 1076 case relocInfo::static_stub_type: 1077 { 1078 static_stub_Relocation* r = (static_stub_Relocation*) reloc(); 1079 tty->print(" | [static_call=" INTPTR_FORMAT "]", p2i(r->static_call())); 1080 break; 1081 } 1082 case relocInfo::trampoline_stub_type: 1083 { 1084 trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc(); 1085 tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", p2i(r->owner())); 1086 break; 1087 } 1088 case relocInfo::opt_virtual_call_type: 1089 { 1090 opt_virtual_call_Relocation* r = (opt_virtual_call_Relocation*) reloc(); 1091 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1092 p2i(r->destination()), p2i(r->method_value())); 1093 break; 1094 } 1095 } 1096 tty->cr(); 1097 } 1098 1099 1100 void RelocIterator::print() { 1101 RelocIterator save_this = (*this); 1102 relocInfo* scan = _current; 1103 if (!has_current()) scan += 1; // nothing to scan here! 1104 1105 bool skip_next = has_current(); 1106 bool got_next; 1107 while (true) { 1108 got_next = (skip_next || next()); 1109 skip_next = false; 1110 1111 tty->print(" @" INTPTR_FORMAT ": ", p2i(scan)); 1112 relocInfo* newscan = _current+1; 1113 if (!has_current()) newscan -= 1; // nothing to scan here! 1114 while (scan < newscan) { 1115 tty->print("%04x", *(short*)scan & 0xFFFF); 1116 scan++; 1117 } 1118 tty->cr(); 1119 1120 if (!got_next) break; 1121 print_current(); 1122 } 1123 1124 (*this) = save_this; 1125 } 1126 1127 // For the debugger: 1128 extern "C" 1129 void print_blob_locs(nmethod* nm) { 1130 nm->print(); 1131 RelocIterator iter(nm); 1132 iter.print(); 1133 } 1134 extern "C" 1135 void print_buf_locs(CodeBuffer* cb) { 1136 FlagSetting fs(PrintRelocations, true); 1137 cb->print(); 1138 } 1139 #endif // !PRODUCT