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