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