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