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(CompiledMethod* 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_compiled_method_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 RelocationHolder
 286 
 287 RelocationHolder RelocationHolder::plus(int offset) const {
 288   if (offset != 0) {
 289     switch (type()) {
 290     case relocInfo::none:
 291       break;
 292     case relocInfo::oop_type:
 293       {
 294         oop_Relocation* r = (oop_Relocation*)reloc();
 295         return oop_Relocation::spec(r->oop_index(), r->offset() + offset);
 296       }
 297     case relocInfo::metadata_type:
 298       {
 299         metadata_Relocation* r = (metadata_Relocation*)reloc();
 300         return metadata_Relocation::spec(r->metadata_index(), r->offset() + offset);
 301       }
 302     default:
 303       ShouldNotReachHere();
 304     }
 305   }
 306   return (*this);
 307 }
 308 
 309 //////// Methods for flyweight Relocation types
 310 
 311 // some relocations can compute their own values
 312 address Relocation::value() {
 313   ShouldNotReachHere();
 314   return nullptr;
 315 }
 316 
 317 
 318 void Relocation::set_value(address x) {
 319   ShouldNotReachHere();
 320 }
 321 
 322 void Relocation::const_set_data_value(address x) {
 323 #ifdef _LP64
 324   if (format() == relocInfo::narrow_oop_in_const) {
 325     *(narrowOop*)addr() = CompressedOops::encode(cast_to_oop(x));
 326   } else {
 327 #endif
 328     *(address*)addr() = x;
 329 #ifdef _LP64
 330   }
 331 #endif
 332 }
 333 
 334 void Relocation::const_verify_data_value(address x) {
 335 #ifdef _LP64
 336   if (format() == relocInfo::narrow_oop_in_const) {
 337     guarantee(*(narrowOop*)addr() == CompressedOops::encode(cast_to_oop(x)), "must agree");
 338   } else {
 339 #endif
 340     guarantee(*(address*)addr() == x, "must agree");
 341 #ifdef _LP64
 342   }
 343 #endif
 344 }
 345 
 346 
 347 RelocationHolder Relocation::spec_simple(relocInfo::relocType rtype) {
 348   if (rtype == relocInfo::none)  return RelocationHolder::none;
 349   relocInfo ri = relocInfo(rtype, 0);
 350   RelocIterator itr;
 351   itr.set_current(ri);
 352   itr.reloc();
 353   return itr._rh;
 354 }
 355 
 356 address Relocation::old_addr_for(address newa,
 357                                  const CodeBuffer* src, CodeBuffer* dest) {
 358   int sect = dest->section_index_of(newa);
 359   guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address");
 360   address ostart = src->code_section(sect)->start();
 361   address nstart = dest->code_section(sect)->start();
 362   return ostart + (newa - nstart);
 363 }
 364 
 365 address Relocation::new_addr_for(address olda,
 366                                  const CodeBuffer* src, CodeBuffer* dest) {
 367   debug_only(const CodeBuffer* src0 = src);
 368   int sect = CodeBuffer::SECT_NONE;
 369   // Look for olda in the source buffer, and all previous incarnations
 370   // if the source buffer has been expanded.
 371   for (; src != nullptr; src = src->before_expand()) {
 372     sect = src->section_index_of(olda);
 373     if (sect != CodeBuffer::SECT_NONE)  break;
 374   }
 375   guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address");
 376   address ostart = src->code_section(sect)->start();
 377   address nstart = dest->code_section(sect)->start();
 378   return nstart + (olda - ostart);
 379 }
 380 
 381 void Relocation::normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections) {
 382   address addr0 = addr;
 383   if (addr0 == nullptr || dest->allocates2(addr0))  return;
 384   CodeBuffer* cb = dest->outer();
 385   addr = new_addr_for(addr0, cb, cb);
 386   assert(allow_other_sections || dest->contains2(addr),
 387          "addr must be in required section");
 388 }
 389 
 390 
 391 void CallRelocation::set_destination(address x) {
 392   pd_set_call_destination(x);
 393 }
 394 
 395 void CallRelocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 396   // Usually a self-relative reference to an external routine.
 397   // On some platforms, the reference is absolute (not self-relative).
 398   // The enhanced use of pd_call_destination sorts this all out.
 399   address orig_addr = old_addr_for(addr(), src, dest);
 400   address callee    = pd_call_destination(orig_addr);
 401   // Reassert the callee address, this time in the new copy of the code.
 402   pd_set_call_destination(callee);
 403 }
 404 
 405 
 406 //// pack/unpack methods
 407 
 408 void oop_Relocation::pack_data_to(CodeSection* dest) {
 409   short* p = (short*) dest->locs_end();
 410   p = pack_2_ints_to(p, _oop_index, _offset);
 411   dest->set_locs_end((relocInfo*) p);
 412 }
 413 
 414 
 415 void oop_Relocation::unpack_data() {
 416   unpack_2_ints(_oop_index, _offset);
 417 }
 418 
 419 void metadata_Relocation::pack_data_to(CodeSection* dest) {
 420   short* p = (short*) dest->locs_end();
 421   p = pack_2_ints_to(p, _metadata_index, _offset);
 422   dest->set_locs_end((relocInfo*) p);
 423 }
 424 
 425 
 426 void metadata_Relocation::unpack_data() {
 427   unpack_2_ints(_metadata_index, _offset);
 428 }
 429 
 430 
 431 void virtual_call_Relocation::pack_data_to(CodeSection* dest) {
 432   short*  p     = (short*) dest->locs_end();
 433   address point =          dest->locs_point();
 434 
 435   normalize_address(_cached_value, dest);
 436   jint x0 = scaled_offset_null_special(_cached_value, point);
 437   p = pack_2_ints_to(p, x0, _method_index);
 438   dest->set_locs_end((relocInfo*) p);
 439 }
 440 
 441 
 442 void virtual_call_Relocation::unpack_data() {
 443   jint x0 = 0;
 444   unpack_2_ints(x0, _method_index);
 445   address point = addr();
 446   _cached_value = x0==0? nullptr: address_from_scaled_offset(x0, point);
 447 }
 448 
 449 void runtime_call_w_cp_Relocation::pack_data_to(CodeSection * dest) {
 450   short* p = pack_1_int_to((short *)dest->locs_end(), (jint)(_offset >> 2));
 451   dest->set_locs_end((relocInfo*) p);
 452 }
 453 
 454 void runtime_call_w_cp_Relocation::unpack_data() {
 455   _offset = unpack_1_int() << 2;
 456 }
 457 
 458 void static_stub_Relocation::pack_data_to(CodeSection* dest) {
 459   short* p = (short*) dest->locs_end();
 460   CodeSection* insts = dest->outer()->insts();
 461   normalize_address(_static_call, insts);
 462   p = pack_1_int_to(p, scaled_offset(_static_call, insts->start()));
 463   dest->set_locs_end((relocInfo*) p);
 464 }
 465 
 466 void static_stub_Relocation::unpack_data() {
 467   address base = binding()->section_start(CodeBuffer::SECT_INSTS);
 468   jint offset = unpack_1_int();
 469   _static_call = address_from_scaled_offset(offset, base);
 470 }
 471 
 472 void trampoline_stub_Relocation::pack_data_to(CodeSection* dest ) {
 473   short* p = (short*) dest->locs_end();
 474   CodeSection* insts = dest->outer()->insts();
 475   normalize_address(_owner, insts);
 476   p = pack_1_int_to(p, scaled_offset(_owner, insts->start()));
 477   dest->set_locs_end((relocInfo*) p);
 478 }
 479 
 480 void trampoline_stub_Relocation::unpack_data() {
 481   address base = binding()->section_start(CodeBuffer::SECT_INSTS);
 482   _owner = address_from_scaled_offset(unpack_1_int(), base);
 483 }
 484 
 485 short* external_word_Relocation::pack_data_to(short* p) {
 486 #ifndef _LP64
 487   return pack_1_int_to(p, (int32_t) (intptr_t)_target);
 488 #else
 489   jlong t = (jlong) _target;
 490   int32_t lo = low(t);
 491   int32_t hi = high(t);
 492   return pack_2_ints_to(p, lo, hi);
 493 #endif /* _LP64 */
 494 }
 495 
 496 void external_word_Relocation::pack_data_to(CodeSection* dest) {
 497   short* p = (short*) dest->locs_end();
 498   dest->set_locs_end((relocInfo*)pack_data_to(p));
 499 }
 500 
 501 void external_word_Relocation::unpack_data() {
 502 #ifndef _LP64
 503   _target = (address) (intptr_t)unpack_1_int();
 504 #else
 505   jint lo, hi;
 506   unpack_2_ints(lo, hi);
 507   jlong t = jlong_from(hi, lo);;
 508   _target = (address) t;
 509 #endif /* _LP64 */
 510 }
 511 
 512 
 513 void internal_word_Relocation::pack_data_to(CodeSection* dest) {
 514   short* p = (short*) dest->locs_end();
 515   normalize_address(_target, dest, true);
 516 
 517   // Check whether my target address is valid within this section.
 518   // If not, strengthen the relocation type to point to another section.
 519   int sindex = _section;
 520   if (sindex == CodeBuffer::SECT_NONE && _target != nullptr
 521       && (!dest->allocates(_target) || _target == dest->locs_point())) {
 522     sindex = dest->outer()->section_index_of(_target);
 523     guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere");
 524     relocInfo* base = dest->locs_end() - 1;
 525     assert(base->type() == this->type(), "sanity");
 526     // Change the written type, to be section_word_type instead.
 527     base->set_type(relocInfo::section_word_type);
 528   }
 529 
 530   // Note: An internal_word relocation cannot refer to its own instruction,
 531   // because we reserve "0" to mean that the pointer itself is embedded
 532   // in the code stream.  We use a section_word relocation for such cases.
 533 
 534   if (sindex == CodeBuffer::SECT_NONE) {
 535     assert(type() == relocInfo::internal_word_type, "must be base class");
 536     guarantee(_target == nullptr || dest->allocates2(_target), "must be within the given code section");
 537     jint x0 = scaled_offset_null_special(_target, dest->locs_point());
 538     assert(!(x0 == 0 && _target != nullptr), "correct encoding of null target");
 539     p = pack_1_int_to(p, x0);
 540   } else {
 541     assert(_target != nullptr, "sanity");
 542     CodeSection* sect = dest->outer()->code_section(sindex);
 543     guarantee(sect->allocates2(_target), "must be in correct section");
 544     address base = sect->start();
 545     jint offset = scaled_offset(_target, base);
 546     assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity");
 547     assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++");
 548     p = pack_1_int_to(p, (offset << section_width) | sindex);
 549   }
 550 
 551   dest->set_locs_end((relocInfo*) p);
 552 }
 553 
 554 
 555 void internal_word_Relocation::unpack_data() {
 556   jint x0 = unpack_1_int();
 557   _target = x0==0? nullptr: address_from_scaled_offset(x0, addr());
 558   _section = CodeBuffer::SECT_NONE;
 559 }
 560 
 561 
 562 void section_word_Relocation::unpack_data() {
 563   jint    x      = unpack_1_int();
 564   jint    offset = (x >> section_width);
 565   int     sindex = (x & ((1<<section_width)-1));
 566   address base   = binding()->section_start(sindex);
 567 
 568   _section = sindex;
 569   _target  = address_from_scaled_offset(offset, base);
 570 }
 571 
 572 //// miscellaneous methods
 573 oop* oop_Relocation::oop_addr() {
 574   int n = _oop_index;
 575   if (n == 0) {
 576     // oop is stored in the code stream
 577     return (oop*) pd_address_in_code();
 578   } else {
 579     // oop is stored in table at nmethod::oops_begin
 580     return code()->oop_addr_at(n);
 581   }
 582 }
 583 
 584 
 585 oop oop_Relocation::oop_value() {
 586   // clean inline caches store a special pseudo-null
 587   if (Universe::contains_non_oop_word(oop_addr())) {
 588     return nullptr;
 589   }
 590   return *oop_addr();
 591 }
 592 
 593 
 594 void oop_Relocation::fix_oop_relocation() {
 595   if (!oop_is_immediate()) {
 596     // get the oop from the pool, and re-insert it into the instruction:
 597     set_value(value());
 598   }
 599 }
 600 
 601 
 602 void oop_Relocation::verify_oop_relocation() {
 603   if (!oop_is_immediate()) {
 604     // get the oop from the pool, and re-insert it into the instruction:
 605     verify_value(value());
 606   }
 607 }
 608 
 609 // meta data versions
 610 Metadata** metadata_Relocation::metadata_addr() {
 611   int n = _metadata_index;
 612   if (n == 0) {
 613     // metadata is stored in the code stream
 614     return (Metadata**) pd_address_in_code();
 615     } else {
 616     // metadata is stored in table at nmethod::metadatas_begin
 617     return code()->metadata_addr_at(n);
 618     }
 619   }
 620 
 621 
 622 Metadata* metadata_Relocation::metadata_value() {
 623   Metadata* v = *metadata_addr();
 624   // clean inline caches store a special pseudo-null
 625   if (v == (Metadata*)Universe::non_oop_word())  v = nullptr;
 626   return v;
 627   }
 628 
 629 
 630 void metadata_Relocation::fix_metadata_relocation() {
 631   if (!metadata_is_immediate()) {
 632     // get the metadata from the pool, and re-insert it into the instruction:
 633     pd_fix_value(value());
 634   }
 635 }
 636 
 637 address virtual_call_Relocation::cached_value() {
 638   assert(_cached_value != nullptr && _cached_value < addr(), "must precede ic_call");
 639   return _cached_value;
 640 }
 641 
 642 Method* virtual_call_Relocation::method_value() {
 643   CompiledMethod* cm = code();
 644   if (cm == nullptr) return (Method*)nullptr;
 645   Metadata* m = cm->metadata_at(_method_index);
 646   assert(m != nullptr || _method_index == 0, "should be non-null for non-zero index");
 647   assert(m == nullptr || m->is_method(), "not a method");
 648   return (Method*)m;
 649 }
 650 
 651 bool virtual_call_Relocation::clear_inline_cache() {
 652   // No stubs for ICs
 653   // Clean IC
 654   ResourceMark rm;
 655   CompiledIC* icache = CompiledIC_at(this);
 656   return 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   CompiledMethod* cm = code();
 672   if (cm == nullptr) return (Method*)nullptr;
 673   Metadata* m = cm->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 template<typename CompiledICorStaticCall>
 680 static bool set_to_clean_no_ic_refill(CompiledICorStaticCall* ic) {
 681   guarantee(ic->set_to_clean(), "Should not need transition stubs");
 682   return true;
 683 }
 684 
 685 bool opt_virtual_call_Relocation::clear_inline_cache() {
 686   // No stubs for ICs
 687   // Clean IC
 688   ResourceMark rm;
 689   CompiledIC* icache = CompiledIC_at(this);
 690   return set_to_clean_no_ic_refill(icache);
 691 }
 692 
 693 address opt_virtual_call_Relocation::static_stub() {
 694   // search for the static stub who points back to this static call
 695   address static_call_addr = addr();
 696   RelocIterator iter(code());
 697   while (iter.next()) {
 698     if (iter.type() == relocInfo::static_stub_type) {
 699       static_stub_Relocation* stub_reloc = iter.static_stub_reloc();
 700       if (stub_reloc->static_call() == static_call_addr) {
 701         return iter.addr();
 702       }
 703     }
 704   }
 705   return nullptr;
 706 }
 707 
 708 Method* static_call_Relocation::method_value() {
 709   CompiledMethod* cm = code();
 710   if (cm == nullptr) return (Method*)nullptr;
 711   Metadata* m = cm->metadata_at(_method_index);
 712   assert(m != nullptr || _method_index == 0, "should be non-null for non-zero index");
 713   assert(m == nullptr || m->is_method(), "not a method");
 714   return (Method*)m;
 715 }
 716 
 717 void static_call_Relocation::pack_data_to(CodeSection* dest) {
 718   short* p = (short*) dest->locs_end();
 719   p = pack_1_int_to(p, _method_index);
 720   dest->set_locs_end((relocInfo*) p);
 721 }
 722 
 723 void static_call_Relocation::unpack_data() {
 724   _method_index = unpack_1_int();
 725 }
 726 
 727 bool static_call_Relocation::clear_inline_cache() {
 728   // Safe call site info
 729   CompiledStaticCall* handler = this->code()->compiledStaticCall_at(this);
 730   return set_to_clean_no_ic_refill(handler);
 731 }
 732 
 733 
 734 address static_call_Relocation::static_stub() {
 735   // search for the static stub who points back to this static call
 736   address static_call_addr = addr();
 737   RelocIterator iter(code());
 738   while (iter.next()) {
 739     if (iter.type() == relocInfo::static_stub_type) {
 740       static_stub_Relocation* stub_reloc = iter.static_stub_reloc();
 741       if (stub_reloc->static_call() == static_call_addr) {
 742         return iter.addr();
 743       }
 744     }
 745   }
 746   return nullptr;
 747 }
 748 
 749 // Finds the trampoline address for a call. If no trampoline stub is
 750 // found nullptr is returned which can be handled by the caller.
 751 address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) {
 752   // There are no relocations available when the code gets relocated
 753   // because of CodeBuffer expansion.
 754   if (code->relocation_size() == 0)
 755     return nullptr;
 756 
 757   RelocIterator iter(code, call);
 758   while (iter.next()) {
 759     if (iter.type() == relocInfo::trampoline_stub_type) {
 760       if (iter.trampoline_stub_reloc()->owner() == call) {
 761         return iter.addr();
 762       }
 763     }
 764   }
 765 
 766   return nullptr;
 767 }
 768 
 769 bool static_stub_Relocation::clear_inline_cache() {
 770   // Call stub is only used when calling the interpreted code.
 771   // It does not really need to be cleared, except that we want to clean out the methodoop.
 772   CompiledDirectStaticCall::set_stub_to_clean(this);
 773   return true;
 774 }
 775 
 776 
 777 void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 778   if (_target != nullptr) {
 779     // Probably this reference is absolute,  not relative, so the following is
 780     // probably a no-op.
 781     set_value(_target);
 782   }
 783   // If target is nullptr, this is  an absolute embedded reference to an external
 784   // location, which means  there is nothing to fix here.  In either case, the
 785   // resulting target should be an "external" address.
 786 #ifdef ASSERT
 787   if (SCCache::is_on()) {
 788     // SCA needs relocation info for card table base which may point to CodeCache
 789     if (is_card_table_address(target())) {
 790       return;
 791     }
 792   }
 793 #endif
 794   postcond(src->section_index_of(target()) == CodeBuffer::SECT_NONE);
 795   postcond(dest->section_index_of(target()) == CodeBuffer::SECT_NONE);
 796 }
 797 
 798 
 799 address external_word_Relocation::target() {
 800   address target = _target;
 801   if (target == nullptr) {
 802     target = pd_get_address_from_code();
 803   }
 804   return target;
 805 }
 806 
 807 
 808 void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 809   address target = _target;
 810   if (target == nullptr) {
 811     target = new_addr_for(this->target(), src, dest);
 812   }
 813   set_value(target);
 814 }
 815 
 816 
 817 address internal_word_Relocation::target() {
 818   address target = _target;
 819   if (target == nullptr) {
 820     if (addr_in_const()) {
 821       target = *(address*)addr();
 822     } else {
 823       target = pd_get_address_from_code();
 824     }
 825   }
 826   return target;
 827 }
 828 
 829 //---------------------------------------------------------------------------------
 830 // Non-product code
 831 
 832 #ifndef PRODUCT
 833 
 834 static const char* reloc_type_string(relocInfo::relocType t) {
 835   switch (t) {
 836   #define EACH_CASE(name) \
 837   case relocInfo::name##_type: \
 838     return #name;
 839 
 840   APPLY_TO_RELOCATIONS(EACH_CASE);
 841   #undef EACH_CASE
 842 
 843   case relocInfo::none:
 844     return "none";
 845   case relocInfo::data_prefix_tag:
 846     return "prefix";
 847   default:
 848     return "UNKNOWN RELOC TYPE";
 849   }
 850 }
 851 
 852 
 853 void RelocIterator::print_current() {
 854   if (!has_current()) {
 855     tty->print_cr("(no relocs)");
 856     return;
 857   }
 858   tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d",
 859              p2i(_current), type(), reloc_type_string((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset());
 860   if (current()->format() != 0)
 861     tty->print(" format=%d", current()->format());
 862   if (datalen() == 1) {
 863     tty->print(" data=%d", data()[0]);
 864   } else if (datalen() > 0) {
 865     tty->print(" data={");
 866     for (int i = 0; i < datalen(); i++) {
 867       tty->print("%04x", data()[i] & 0xFFFF);
 868     }
 869     tty->print("}");
 870   }
 871   tty->print("]");
 872   switch (type()) {
 873   case relocInfo::oop_type:
 874     {
 875       oop_Relocation* r = oop_reloc();
 876       oop* oop_addr  = nullptr;
 877       oop  raw_oop   = nullptr;
 878       oop  oop_value = nullptr;
 879       if (code() != nullptr || r->oop_is_immediate()) {
 880         oop_addr  = r->oop_addr();
 881         raw_oop   = *oop_addr;
 882         oop_value = r->oop_value();
 883       }
 884       tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " index=%d offset=%d]",
 885                  p2i(oop_addr), p2i(raw_oop), r->oop_index(), r->offset());
 886       // Do not print the oop by default--we want this routine to
 887       // work even during GC or other inconvenient times.
 888       if (WizardMode && oop_value != nullptr) {
 889         tty->print("oop_value=" INTPTR_FORMAT ": ", p2i(oop_value));
 890         if (oopDesc::is_oop(oop_value)) {
 891           oop_value->print_value_on(tty);
 892         }
 893       }
 894       break;
 895     }
 896   case relocInfo::metadata_type:
 897     {
 898       metadata_Relocation* r = metadata_reloc();
 899       Metadata** metadata_addr  = nullptr;
 900       Metadata*    raw_metadata   = nullptr;
 901       Metadata*    metadata_value = nullptr;
 902       if (code() != nullptr || r->metadata_is_immediate()) {
 903         metadata_addr  = r->metadata_addr();
 904         raw_metadata   = *metadata_addr;
 905         metadata_value = r->metadata_value();
 906       }
 907       tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " index=%d offset=%d]",
 908                  p2i(metadata_addr), p2i(raw_metadata), r->metadata_index(), r->offset());
 909       if (metadata_value != nullptr) {
 910         tty->print("metadata_value=" INTPTR_FORMAT ": ", p2i(metadata_value));
 911         metadata_value->print_value_on(tty);
 912       }
 913       break;
 914     }
 915   case relocInfo::external_word_type:
 916   case relocInfo::internal_word_type:
 917   case relocInfo::section_word_type:
 918     {
 919       DataRelocation* r = (DataRelocation*) reloc();
 920       tty->print(" | [target=" INTPTR_FORMAT "]", p2i(r->value())); //value==target
 921       break;
 922     }
 923   case relocInfo::static_call_type:
 924     {
 925       static_call_Relocation* r = (static_call_Relocation*) reloc();
 926       tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
 927                  p2i(r->destination()), p2i(r->method_value()));
 928       CodeBlob* cb = CodeCache::find_blob(r->destination());
 929       if (cb != nullptr) {
 930         tty->print(" Blob::%s", cb->name());
 931       }
 932       break;
 933     }
 934   case relocInfo::runtime_call_type:
 935   case relocInfo::runtime_call_w_cp_type:
 936     {
 937       CallRelocation* r = (CallRelocation*) reloc();
 938       address dest = r->destination();
 939       tty->print(" | [destination=" INTPTR_FORMAT "]", p2i(dest));
 940       if (StubRoutines::contains(dest)) {
 941         StubCodeDesc* desc = StubCodeDesc::desc_for(dest);
 942         if (desc == nullptr) {
 943           desc = StubCodeDesc::desc_for(dest + frame::pc_return_offset);
 944         }
 945         if (desc != nullptr) {
 946           tty->print(" Stub::%s", desc->name());
 947         }
 948       } else {
 949         CodeBlob* cb = CodeCache::find_blob(dest);
 950         if (cb != nullptr) {
 951           tty->print(" Blob::%s", cb->name());
 952         } else {
 953           ResourceMark rm;
 954           const int buflen = 1024;
 955           char* buf = NEW_RESOURCE_ARRAY(char, buflen);
 956           int offset;
 957           if (os::dll_address_to_function_name(dest, buf, buflen, &offset)) {
 958             tty->print(" %s", buf);
 959             if (offset != 0) {
 960               tty->print("+%d", offset);
 961             }
 962           }
 963         }
 964       }
 965       break;
 966     }
 967   case relocInfo::virtual_call_type:
 968     {
 969       virtual_call_Relocation* r = (virtual_call_Relocation*) reloc();
 970       tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
 971                  p2i(r->destination()), p2i(r->cached_value()), p2i(r->method_value()));
 972       CodeBlob* cb = CodeCache::find_blob(r->destination());
 973       if (cb != nullptr) {
 974         tty->print(" Blob::%s", cb->name());
 975       }
 976       break;
 977     }
 978   case relocInfo::static_stub_type:
 979     {
 980       static_stub_Relocation* r = (static_stub_Relocation*) reloc();
 981       tty->print(" | [static_call=" INTPTR_FORMAT "]", p2i(r->static_call()));
 982       break;
 983     }
 984   case relocInfo::trampoline_stub_type:
 985     {
 986       trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc();
 987       tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", p2i(r->owner()));
 988       break;
 989     }
 990   case relocInfo::opt_virtual_call_type:
 991     {
 992       opt_virtual_call_Relocation* r = (opt_virtual_call_Relocation*) reloc();
 993       tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
 994                  p2i(r->destination()), p2i(r->method_value()));
 995       CodeBlob* cb = CodeCache::find_blob(r->destination());
 996       if (cb != nullptr) {
 997         tty->print(" Blob::%s", cb->name());
 998       }
 999       break;
1000     }
1001   default:
1002     break;
1003   }
1004   tty->cr();
1005 }
1006 
1007 
1008 void RelocIterator::print() {
1009   RelocIterator save_this = (*this);
1010   relocInfo* scan = _current;
1011   if (!has_current())  scan += 1;  // nothing to scan here!
1012 
1013   bool skip_next = has_current();
1014   bool got_next;
1015   while (true) {
1016     got_next = (skip_next || next());
1017     skip_next = false;
1018 
1019     tty->print("         @" INTPTR_FORMAT ": ", p2i(scan));
1020     relocInfo* newscan = _current+1;
1021     if (!has_current())  newscan -= 1;  // nothing to scan here!
1022     while (scan < newscan) {
1023       tty->print("%04x", *(short*)scan & 0xFFFF);
1024       scan++;
1025     }
1026     tty->cr();
1027 
1028     if (!got_next)  break;
1029     print_current();
1030   }
1031 
1032   (*this) = save_this;
1033 }
1034 
1035 // For the debugger:
1036 extern "C"
1037 void print_blob_locs(nmethod* nm) {
1038   nm->print();
1039   RelocIterator iter(nm);
1040   iter.print();
1041 }
1042 extern "C"
1043 void print_buf_locs(CodeBuffer* cb) {
1044   FlagSetting fs(PrintRelocations, true);
1045   cb->print();
1046 }
1047 #endif // !PRODUCT