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