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