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