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