1 /* 2 * Copyright (c) 1997, 2016, 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 const RelocationHolder RelocationHolder::none; // its type is relocInfo::none 36 37 38 // Implementation of relocInfo 39 40 #ifdef ASSERT 41 relocInfo::relocInfo(relocType t, int off, int f) { 42 assert(t != data_prefix_tag, "cannot build a prefix this way"); 43 assert((t & type_mask) == t, "wrong type"); 44 assert((f & format_mask) == f, "wrong format"); 45 assert(off >= 0 && off < offset_limit(), "offset out off bounds"); 46 assert((off & (offset_unit-1)) == 0, "misaligned offset"); 47 (*this) = relocInfo(t, RAW_BITS, off, f); 48 } 49 #endif 50 51 void relocInfo::initialize(CodeSection* dest, Relocation* reloc) { 52 relocInfo* data = this+1; // here's where the data might go 53 dest->set_locs_end(data); // sync end: the next call may read dest.locs_end 54 reloc->pack_data_to(dest); // maybe write data into locs, advancing locs_end 55 relocInfo* data_limit = dest->locs_end(); 56 if (data_limit > data) { 57 relocInfo suffix = (*this); 58 data_limit = this->finish_prefix((short*) data_limit); 59 // Finish up with the suffix. (Hack note: pack_data_to might edit this.) 60 *data_limit = suffix; 61 dest->set_locs_end(data_limit+1); 62 } 63 } 64 65 relocInfo* relocInfo::finish_prefix(short* prefix_limit) { 66 assert(sizeof(relocInfo) == sizeof(short), "change this code"); 67 short* p = (short*)(this+1); 68 assert(prefix_limit >= p, "must be a valid span of data"); 69 int plen = prefix_limit - p; 70 if (plen == 0) { 71 debug_only(_value = 0xFFFF); 72 return this; // no data: remove self completely 73 } 74 if (plen == 1 && fits_into_immediate(p[0])) { 75 (*this) = immediate_relocInfo(p[0]); // move data inside self 76 return this+1; 77 } 78 // cannot compact, so just update the count and return the limit pointer 79 (*this) = prefix_relocInfo(plen); // write new datalen 80 assert(data() + datalen() == prefix_limit, "pointers must line up"); 81 return (relocInfo*)prefix_limit; 82 } 83 84 void relocInfo::set_type(relocType t) { 85 int old_offset = addr_offset(); 86 int old_format = format(); 87 (*this) = relocInfo(t, old_offset, old_format); 88 assert(type()==(int)t, "sanity check"); 89 assert(addr_offset()==old_offset, "sanity check"); 90 assert(format()==old_format, "sanity check"); 91 } 92 93 nmethod* RelocIterator::code_as_nmethod() const { 94 return _code->as_nmethod(); 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(CompiledMethod* 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_compiled_method_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 guarantee(*(narrowOop*)addr() == oopDesc::encode_heap_oop((oop) x), "must agree"); 444 } else { 445 #endif 446 guarantee(*(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 address Relocation::old_addr_for(address newa, 463 const CodeBuffer* src, CodeBuffer* dest) { 464 int sect = dest->section_index_of(newa); 465 guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); 466 address ostart = src->code_section(sect)->start(); 467 address nstart = dest->code_section(sect)->start(); 468 return ostart + (newa - nstart); 469 } 470 471 address Relocation::new_addr_for(address olda, 472 const CodeBuffer* src, CodeBuffer* dest) { 473 debug_only(const CodeBuffer* src0 = src); 474 int sect = CodeBuffer::SECT_NONE; 475 // Look for olda in the source buffer, and all previous incarnations 476 // if the source buffer has been expanded. 477 for (; src != NULL; src = src->before_expand()) { 478 sect = src->section_index_of(olda); 479 if (sect != CodeBuffer::SECT_NONE) break; 480 } 481 guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address"); 482 address ostart = src->code_section(sect)->start(); 483 address nstart = dest->code_section(sect)->start(); 484 return nstart + (olda - ostart); 485 } 486 487 void Relocation::normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections) { 488 address addr0 = addr; 489 if (addr0 == NULL || dest->allocates2(addr0)) return; 490 CodeBuffer* cb = dest->outer(); 491 addr = new_addr_for(addr0, cb, cb); 492 assert(allow_other_sections || dest->contains2(addr), 493 "addr must be in required section"); 494 } 495 496 497 void CallRelocation::set_destination(address x) { 498 pd_set_call_destination(x); 499 } 500 501 void CallRelocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 502 // Usually a self-relative reference to an external routine. 503 // On some platforms, the reference is absolute (not self-relative). 504 // The enhanced use of pd_call_destination sorts this all out. 505 address orig_addr = old_addr_for(addr(), src, dest); 506 address callee = pd_call_destination(orig_addr); 507 // Reassert the callee address, this time in the new copy of the code. 508 pd_set_call_destination(callee); 509 } 510 511 512 //// pack/unpack methods 513 514 void oop_Relocation::pack_data_to(CodeSection* dest) { 515 short* p = (short*) dest->locs_end(); 516 p = pack_2_ints_to(p, _oop_index, _offset); 517 dest->set_locs_end((relocInfo*) p); 518 } 519 520 521 void oop_Relocation::unpack_data() { 522 unpack_2_ints(_oop_index, _offset); 523 } 524 525 void metadata_Relocation::pack_data_to(CodeSection* dest) { 526 short* p = (short*) dest->locs_end(); 527 p = pack_2_ints_to(p, _metadata_index, _offset); 528 dest->set_locs_end((relocInfo*) p); 529 } 530 531 532 void metadata_Relocation::unpack_data() { 533 unpack_2_ints(_metadata_index, _offset); 534 } 535 536 537 void virtual_call_Relocation::pack_data_to(CodeSection* dest) { 538 short* p = (short*) dest->locs_end(); 539 address point = dest->locs_point(); 540 541 normalize_address(_cached_value, dest); 542 jint x0 = scaled_offset_null_special(_cached_value, point); 543 p = pack_2_ints_to(p, x0, _method_index); 544 dest->set_locs_end((relocInfo*) p); 545 } 546 547 548 void virtual_call_Relocation::unpack_data() { 549 jint x0 = 0; 550 unpack_2_ints(x0, _method_index); 551 address point = addr(); 552 _cached_value = x0==0? NULL: address_from_scaled_offset(x0, point); 553 } 554 555 void runtime_call_w_cp_Relocation::pack_data_to(CodeSection * dest) { 556 short* p = pack_1_int_to((short *)dest->locs_end(), (jint)(_offset >> 2)); 557 dest->set_locs_end((relocInfo*) p); 558 } 559 560 void runtime_call_w_cp_Relocation::unpack_data() { 561 _offset = unpack_1_int() << 2; 562 } 563 564 void static_stub_Relocation::pack_data_to(CodeSection* dest) { 565 short* p = (short*) dest->locs_end(); 566 CodeSection* insts = dest->outer()->insts(); 567 normalize_address(_static_call, insts); 568 p = pack_1_int_to(p, scaled_offset(_static_call, insts->start())); 569 dest->set_locs_end((relocInfo*) p); 570 } 571 572 void static_stub_Relocation::unpack_data() { 573 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 574 jint offset = unpack_1_int(); 575 _static_call = address_from_scaled_offset(offset, base); 576 } 577 578 void trampoline_stub_Relocation::pack_data_to(CodeSection* dest ) { 579 short* p = (short*) dest->locs_end(); 580 CodeSection* insts = dest->outer()->insts(); 581 normalize_address(_owner, insts); 582 p = pack_1_int_to(p, scaled_offset(_owner, insts->start())); 583 dest->set_locs_end((relocInfo*) p); 584 } 585 586 void trampoline_stub_Relocation::unpack_data() { 587 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 588 _owner = address_from_scaled_offset(unpack_1_int(), base); 589 } 590 591 void external_word_Relocation::pack_data_to(CodeSection* dest) { 592 short* p = (short*) dest->locs_end(); 593 #ifndef _LP64 594 p = pack_1_int_to(p, (int32_t) (intptr_t)_target); 595 #else 596 jlong t = (jlong) _target; 597 int32_t lo = low(t); 598 int32_t hi = high(t); 599 p = pack_2_ints_to(p, lo, hi); 600 #endif /* _LP64 */ 601 dest->set_locs_end((relocInfo*) p); 602 } 603 604 605 void external_word_Relocation::unpack_data() { 606 #ifndef _LP64 607 _target = (address) (intptr_t)unpack_1_int(); 608 #else 609 int32_t lo, hi; 610 unpack_2_ints(lo, hi); 611 jlong t = jlong_from(hi, lo);; 612 _target = (address) t; 613 #endif /* _LP64 */ 614 } 615 616 617 void internal_word_Relocation::pack_data_to(CodeSection* dest) { 618 short* p = (short*) dest->locs_end(); 619 normalize_address(_target, dest, true); 620 621 // Check whether my target address is valid within this section. 622 // If not, strengthen the relocation type to point to another section. 623 int sindex = _section; 624 if (sindex == CodeBuffer::SECT_NONE && _target != NULL 625 && (!dest->allocates(_target) || _target == dest->locs_point())) { 626 sindex = dest->outer()->section_index_of(_target); 627 guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere"); 628 relocInfo* base = dest->locs_end() - 1; 629 assert(base->type() == this->type(), "sanity"); 630 // Change the written type, to be section_word_type instead. 631 base->set_type(relocInfo::section_word_type); 632 } 633 634 // Note: An internal_word relocation cannot refer to its own instruction, 635 // because we reserve "0" to mean that the pointer itself is embedded 636 // in the code stream. We use a section_word relocation for such cases. 637 638 if (sindex == CodeBuffer::SECT_NONE) { 639 assert(type() == relocInfo::internal_word_type, "must be base class"); 640 guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section"); 641 jint x0 = scaled_offset_null_special(_target, dest->locs_point()); 642 assert(!(x0 == 0 && _target != NULL), "correct encoding of null target"); 643 p = pack_1_int_to(p, x0); 644 } else { 645 assert(_target != NULL, "sanity"); 646 CodeSection* sect = dest->outer()->code_section(sindex); 647 guarantee(sect->allocates2(_target), "must be in correct section"); 648 address base = sect->start(); 649 jint offset = scaled_offset(_target, base); 650 assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity"); 651 assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++"); 652 p = pack_1_int_to(p, (offset << section_width) | sindex); 653 } 654 655 dest->set_locs_end((relocInfo*) p); 656 } 657 658 659 void internal_word_Relocation::unpack_data() { 660 jint x0 = unpack_1_int(); 661 _target = x0==0? NULL: address_from_scaled_offset(x0, addr()); 662 _section = CodeBuffer::SECT_NONE; 663 } 664 665 666 void section_word_Relocation::unpack_data() { 667 jint x = unpack_1_int(); 668 jint offset = (x >> section_width); 669 int sindex = (x & ((1<<section_width)-1)); 670 address base = binding()->section_start(sindex); 671 672 _section = sindex; 673 _target = address_from_scaled_offset(offset, base); 674 } 675 676 //// miscellaneous methods 677 oop* oop_Relocation::oop_addr() { 678 int n = _oop_index; 679 if (n == 0) { 680 // oop is stored in the code stream 681 return (oop*) pd_address_in_code(); 682 } else { 683 // oop is stored in table at nmethod::oops_begin 684 return code()->oop_addr_at(n); 685 } 686 } 687 688 689 oop oop_Relocation::oop_value() { 690 oop v = *oop_addr(); 691 // clean inline caches store a special pseudo-null 692 if (v == (oop)Universe::non_oop_word()) v = NULL; 693 return v; 694 } 695 696 697 void oop_Relocation::fix_oop_relocation() { 698 if (!oop_is_immediate()) { 699 // get the oop from the pool, and re-insert it into the instruction: 700 set_value(value()); 701 } 702 } 703 704 705 void oop_Relocation::verify_oop_relocation() { 706 if (!oop_is_immediate()) { 707 // get the oop from the pool, and re-insert it into the instruction: 708 verify_value(value()); 709 } 710 } 711 712 // meta data versions 713 Metadata** metadata_Relocation::metadata_addr() { 714 int n = _metadata_index; 715 if (n == 0) { 716 // metadata is stored in the code stream 717 return (Metadata**) pd_address_in_code(); 718 } else { 719 // metadata is stored in table at nmethod::metadatas_begin 720 return code()->metadata_addr_at(n); 721 } 722 } 723 724 725 Metadata* metadata_Relocation::metadata_value() { 726 Metadata* v = *metadata_addr(); 727 // clean inline caches store a special pseudo-null 728 if (v == (Metadata*)Universe::non_oop_word()) v = NULL; 729 return v; 730 } 731 732 733 void metadata_Relocation::fix_metadata_relocation() { 734 if (!metadata_is_immediate()) { 735 // get the metadata from the pool, and re-insert it into the instruction: 736 pd_fix_value(value()); 737 } 738 } 739 740 741 void metadata_Relocation::verify_metadata_relocation() { 742 if (!metadata_is_immediate()) { 743 // get the metadata from the pool, and re-insert it into the instruction: 744 verify_value(value()); 745 } 746 } 747 748 address virtual_call_Relocation::cached_value() { 749 assert(_cached_value != NULL && _cached_value < addr(), "must precede ic_call"); 750 return _cached_value; 751 } 752 753 Method* virtual_call_Relocation::method_value() { 754 Metadata* m = code()->metadata_at(_method_index); 755 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 756 assert(m == NULL || m->is_method(), "not a method"); 757 return (Method*)m; 758 } 759 760 void virtual_call_Relocation::clear_inline_cache() { 761 // No stubs for ICs 762 // Clean IC 763 ResourceMark rm; 764 CompiledIC* icache = CompiledIC_at(this); 765 icache->set_to_clean(); 766 } 767 768 769 void opt_virtual_call_Relocation::pack_data_to(CodeSection* dest) { 770 short* p = (short*) dest->locs_end(); 771 p = pack_1_int_to(p, _method_index); 772 dest->set_locs_end((relocInfo*) p); 773 } 774 775 void opt_virtual_call_Relocation::unpack_data() { 776 _method_index = unpack_1_int(); 777 } 778 779 Method* opt_virtual_call_Relocation::method_value() { 780 Metadata* m = code()->metadata_at(_method_index); 781 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 782 assert(m == NULL || m->is_method(), "not a method"); 783 return (Method*)m; 784 } 785 786 void opt_virtual_call_Relocation::clear_inline_cache() { 787 // No stubs for ICs 788 // Clean IC 789 ResourceMark rm; 790 CompiledIC* icache = CompiledIC_at(this); 791 icache->set_to_clean(); 792 } 793 794 795 address opt_virtual_call_Relocation::static_stub() { 796 // search for the static stub who points back to this static call 797 address static_call_addr = addr(); 798 RelocIterator iter(code()); 799 while (iter.next()) { 800 if (iter.type() == relocInfo::static_stub_type) { 801 static_stub_Relocation* stub_reloc = iter.static_stub_reloc(); 802 if (stub_reloc->static_call() == static_call_addr) { 803 return iter.addr(); 804 } 805 } 806 } 807 return NULL; 808 } 809 810 Method* static_call_Relocation::method_value() { 811 Metadata* m = code()->metadata_at(_method_index); 812 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 813 assert(m == NULL || m->is_method(), "not a method"); 814 return (Method*)m; 815 } 816 817 void static_call_Relocation::pack_data_to(CodeSection* dest) { 818 short* p = (short*) dest->locs_end(); 819 p = pack_1_int_to(p, _method_index); 820 dest->set_locs_end((relocInfo*) p); 821 } 822 823 void static_call_Relocation::unpack_data() { 824 _method_index = unpack_1_int(); 825 } 826 827 void static_call_Relocation::clear_inline_cache() { 828 // Safe call site info 829 CompiledStaticCall* handler = compiledStaticCall_at(this); 830 handler->set_to_clean(); 831 } 832 833 834 address static_call_Relocation::static_stub() { 835 // search for the static stub who points back to this static call 836 address static_call_addr = addr(); 837 RelocIterator iter(code()); 838 while (iter.next()) { 839 if (iter.type() == relocInfo::static_stub_type) { 840 static_stub_Relocation* stub_reloc = iter.static_stub_reloc(); 841 if (stub_reloc->static_call() == static_call_addr) { 842 return iter.addr(); 843 } 844 } 845 } 846 return NULL; 847 } 848 849 // Finds the trampoline address for a call. If no trampoline stub is 850 // found NULL is returned which can be handled by the caller. 851 address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) { 852 // There are no relocations available when the code gets relocated 853 // because of CodeBuffer expansion. 854 if (code->relocation_size() == 0) 855 return NULL; 856 857 RelocIterator iter(code, call); 858 while (iter.next()) { 859 if (iter.type() == relocInfo::trampoline_stub_type) { 860 if (iter.trampoline_stub_reloc()->owner() == call) { 861 return iter.addr(); 862 } 863 } 864 } 865 866 return NULL; 867 } 868 869 void static_stub_Relocation::clear_inline_cache() { 870 // Call stub is only used when calling the interpreted code. 871 // It does not really need to be cleared, except that we want to clean out the methodoop. 872 CompiledStaticCall::set_stub_to_clean(this); 873 } 874 875 876 void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 877 address target = _target; 878 if (target == NULL) { 879 // An absolute embedded reference to an external location, 880 // which means there is nothing to fix here. 881 return; 882 } 883 // Probably this reference is absolute, not relative, so the 884 // following is probably a no-op. 885 assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity"); 886 set_value(target); 887 } 888 889 890 address external_word_Relocation::target() { 891 address target = _target; 892 if (target == NULL) { 893 target = pd_get_address_from_code(); 894 } 895 return target; 896 } 897 898 899 void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 900 address target = _target; 901 if (target == NULL) { 902 target = new_addr_for(this->target(), src, dest); 903 } 904 set_value(target); 905 } 906 907 908 address internal_word_Relocation::target() { 909 address target = _target; 910 if (target == NULL) { 911 if (addr_in_const()) { 912 target = *(address*)addr(); 913 } else { 914 target = pd_get_address_from_code(); 915 } 916 } 917 return target; 918 } 919 920 //--------------------------------------------------------------------------------- 921 // Non-product code 922 923 #ifndef PRODUCT 924 925 static const char* reloc_type_string(relocInfo::relocType t) { 926 switch (t) { 927 #define EACH_CASE(name) \ 928 case relocInfo::name##_type: \ 929 return #name; 930 931 APPLY_TO_RELOCATIONS(EACH_CASE); 932 #undef EACH_CASE 933 934 case relocInfo::none: 935 return "none"; 936 case relocInfo::data_prefix_tag: 937 return "prefix"; 938 default: 939 return "UNKNOWN RELOC TYPE"; 940 } 941 } 942 943 944 void RelocIterator::print_current() { 945 if (!has_current()) { 946 tty->print_cr("(no relocs)"); 947 return; 948 } 949 tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d", 950 p2i(_current), type(), reloc_type_string((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset()); 951 if (current()->format() != 0) 952 tty->print(" format=%d", current()->format()); 953 if (datalen() == 1) { 954 tty->print(" data=%d", data()[0]); 955 } else if (datalen() > 0) { 956 tty->print(" data={"); 957 for (int i = 0; i < datalen(); i++) { 958 tty->print("%04x", data()[i] & 0xFFFF); 959 } 960 tty->print("}"); 961 } 962 tty->print("]"); 963 switch (type()) { 964 case relocInfo::oop_type: 965 { 966 oop_Relocation* r = oop_reloc(); 967 oop* oop_addr = NULL; 968 oop raw_oop = NULL; 969 oop oop_value = NULL; 970 if (code() != NULL || r->oop_is_immediate()) { 971 oop_addr = r->oop_addr(); 972 raw_oop = *oop_addr; 973 oop_value = r->oop_value(); 974 } 975 tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 976 p2i(oop_addr), p2i(raw_oop), r->offset()); 977 // Do not print the oop by default--we want this routine to 978 // work even during GC or other inconvenient times. 979 if (WizardMode && oop_value != NULL) { 980 tty->print("oop_value=" INTPTR_FORMAT ": ", p2i(oop_value)); 981 oop_value->print_value_on(tty); 982 } 983 break; 984 } 985 case relocInfo::metadata_type: 986 { 987 metadata_Relocation* r = metadata_reloc(); 988 Metadata** metadata_addr = NULL; 989 Metadata* raw_metadata = NULL; 990 Metadata* metadata_value = NULL; 991 if (code() != NULL || r->metadata_is_immediate()) { 992 metadata_addr = r->metadata_addr(); 993 raw_metadata = *metadata_addr; 994 metadata_value = r->metadata_value(); 995 } 996 tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 997 p2i(metadata_addr), p2i(raw_metadata), r->offset()); 998 if (metadata_value != NULL) { 999 tty->print("metadata_value=" INTPTR_FORMAT ": ", p2i(metadata_value)); 1000 metadata_value->print_value_on(tty); 1001 } 1002 break; 1003 } 1004 case relocInfo::external_word_type: 1005 case relocInfo::internal_word_type: 1006 case relocInfo::section_word_type: 1007 { 1008 DataRelocation* r = (DataRelocation*) reloc(); 1009 tty->print(" | [target=" INTPTR_FORMAT "]", p2i(r->value())); //value==target 1010 break; 1011 } 1012 case relocInfo::static_call_type: 1013 { 1014 static_call_Relocation* r = (static_call_Relocation*) reloc(); 1015 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1016 p2i(r->destination()), p2i(r->method_value())); 1017 break; 1018 } 1019 case relocInfo::runtime_call_type: 1020 case relocInfo::runtime_call_w_cp_type: 1021 { 1022 CallRelocation* r = (CallRelocation*) reloc(); 1023 tty->print(" | [destination=" INTPTR_FORMAT "]", p2i(r->destination())); 1024 break; 1025 } 1026 case relocInfo::virtual_call_type: 1027 { 1028 virtual_call_Relocation* r = (virtual_call_Relocation*) reloc(); 1029 tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1030 p2i(r->destination()), p2i(r->cached_value()), p2i(r->method_value())); 1031 break; 1032 } 1033 case relocInfo::static_stub_type: 1034 { 1035 static_stub_Relocation* r = (static_stub_Relocation*) reloc(); 1036 tty->print(" | [static_call=" INTPTR_FORMAT "]", p2i(r->static_call())); 1037 break; 1038 } 1039 case relocInfo::trampoline_stub_type: 1040 { 1041 trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc(); 1042 tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", p2i(r->owner())); 1043 break; 1044 } 1045 case relocInfo::opt_virtual_call_type: 1046 { 1047 opt_virtual_call_Relocation* r = (opt_virtual_call_Relocation*) reloc(); 1048 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1049 p2i(r->destination()), p2i(r->method_value())); 1050 break; 1051 } 1052 } 1053 tty->cr(); 1054 } 1055 1056 1057 void RelocIterator::print() { 1058 RelocIterator save_this = (*this); 1059 relocInfo* scan = _current; 1060 if (!has_current()) scan += 1; // nothing to scan here! 1061 1062 bool skip_next = has_current(); 1063 bool got_next; 1064 while (true) { 1065 got_next = (skip_next || next()); 1066 skip_next = false; 1067 1068 tty->print(" @" INTPTR_FORMAT ": ", p2i(scan)); 1069 relocInfo* newscan = _current+1; 1070 if (!has_current()) newscan -= 1; // nothing to scan here! 1071 while (scan < newscan) { 1072 tty->print("%04x", *(short*)scan & 0xFFFF); 1073 scan++; 1074 } 1075 tty->cr(); 1076 1077 if (!got_next) break; 1078 print_current(); 1079 } 1080 1081 (*this) = save_this; 1082 } 1083 1084 // For the debugger: 1085 extern "C" 1086 void print_blob_locs(nmethod* nm) { 1087 nm->print(); 1088 RelocIterator iter(nm); 1089 iter.print(); 1090 } 1091 extern "C" 1092 void print_buf_locs(CodeBuffer* cb) { 1093 FlagSetting fs(PrintRelocations, true); 1094 cb->print(); 1095 } 1096 #endif // !PRODUCT