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