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