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