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