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