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