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 jint is_aot = _is_aot ? 1 : 0; 569 p = pack_2_ints_to(p, scaled_offset(_static_call, insts->start()), is_aot); 570 dest->set_locs_end((relocInfo*) p); 571 } 572 573 void static_stub_Relocation::unpack_data() { 574 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 575 jint offset; 576 jint is_aot; 577 unpack_2_ints(offset, is_aot); 578 _static_call = address_from_scaled_offset(offset, base); 579 _is_aot = (is_aot == 1); 580 } 581 582 void trampoline_stub_Relocation::pack_data_to(CodeSection* dest ) { 583 short* p = (short*) dest->locs_end(); 584 CodeSection* insts = dest->outer()->insts(); 585 normalize_address(_owner, insts); 586 p = pack_1_int_to(p, scaled_offset(_owner, insts->start())); 587 dest->set_locs_end((relocInfo*) p); 588 } 589 590 void trampoline_stub_Relocation::unpack_data() { 591 address base = binding()->section_start(CodeBuffer::SECT_INSTS); 592 _owner = address_from_scaled_offset(unpack_1_int(), base); 593 } 594 595 void external_word_Relocation::pack_data_to(CodeSection* dest) { 596 short* p = (short*) dest->locs_end(); 597 #ifndef _LP64 598 p = pack_1_int_to(p, (int32_t) (intptr_t)_target); 599 #else 600 jlong t = (jlong) _target; 601 int32_t lo = low(t); 602 int32_t hi = high(t); 603 p = pack_2_ints_to(p, lo, hi); 604 #endif /* _LP64 */ 605 dest->set_locs_end((relocInfo*) p); 606 } 607 608 609 void external_word_Relocation::unpack_data() { 610 #ifndef _LP64 611 _target = (address) (intptr_t)unpack_1_int(); 612 #else 613 int32_t lo, hi; 614 unpack_2_ints(lo, hi); 615 jlong t = jlong_from(hi, lo);; 616 _target = (address) t; 617 #endif /* _LP64 */ 618 } 619 620 621 void internal_word_Relocation::pack_data_to(CodeSection* dest) { 622 short* p = (short*) dest->locs_end(); 623 normalize_address(_target, dest, true); 624 625 // Check whether my target address is valid within this section. 626 // If not, strengthen the relocation type to point to another section. 627 int sindex = _section; 628 if (sindex == CodeBuffer::SECT_NONE && _target != NULL 629 && (!dest->allocates(_target) || _target == dest->locs_point())) { 630 sindex = dest->outer()->section_index_of(_target); 631 guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere"); 632 relocInfo* base = dest->locs_end() - 1; 633 assert(base->type() == this->type(), "sanity"); 634 // Change the written type, to be section_word_type instead. 635 base->set_type(relocInfo::section_word_type); 636 } 637 638 // Note: An internal_word relocation cannot refer to its own instruction, 639 // because we reserve "0" to mean that the pointer itself is embedded 640 // in the code stream. We use a section_word relocation for such cases. 641 642 if (sindex == CodeBuffer::SECT_NONE) { 643 assert(type() == relocInfo::internal_word_type, "must be base class"); 644 guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section"); 645 jint x0 = scaled_offset_null_special(_target, dest->locs_point()); 646 assert(!(x0 == 0 && _target != NULL), "correct encoding of null target"); 647 p = pack_1_int_to(p, x0); 648 } else { 649 assert(_target != NULL, "sanity"); 650 CodeSection* sect = dest->outer()->code_section(sindex); 651 guarantee(sect->allocates2(_target), "must be in correct section"); 652 address base = sect->start(); 653 jint offset = scaled_offset(_target, base); 654 assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity"); 655 assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++"); 656 p = pack_1_int_to(p, (offset << section_width) | sindex); 657 } 658 659 dest->set_locs_end((relocInfo*) p); 660 } 661 662 663 void internal_word_Relocation::unpack_data() { 664 jint x0 = unpack_1_int(); 665 _target = x0==0? NULL: address_from_scaled_offset(x0, addr()); 666 _section = CodeBuffer::SECT_NONE; 667 } 668 669 670 void section_word_Relocation::unpack_data() { 671 jint x = unpack_1_int(); 672 jint offset = (x >> section_width); 673 int sindex = (x & ((1<<section_width)-1)); 674 address base = binding()->section_start(sindex); 675 676 _section = sindex; 677 _target = address_from_scaled_offset(offset, base); 678 } 679 680 //// miscellaneous methods 681 oop* oop_Relocation::oop_addr() { 682 int n = _oop_index; 683 if (n == 0) { 684 // oop is stored in the code stream 685 return (oop*) pd_address_in_code(); 686 } else { 687 // oop is stored in table at nmethod::oops_begin 688 return code()->oop_addr_at(n); 689 } 690 } 691 692 693 oop oop_Relocation::oop_value() { 694 oop v = *oop_addr(); 695 // clean inline caches store a special pseudo-null 696 if (v == (oop)Universe::non_oop_word()) v = NULL; 697 return v; 698 } 699 700 701 void oop_Relocation::fix_oop_relocation() { 702 if (!oop_is_immediate()) { 703 // get the oop from the pool, and re-insert it into the instruction: 704 set_value(value()); 705 } 706 } 707 708 709 void oop_Relocation::verify_oop_relocation() { 710 if (!oop_is_immediate()) { 711 // get the oop from the pool, and re-insert it into the instruction: 712 verify_value(value()); 713 } 714 } 715 716 // meta data versions 717 Metadata** metadata_Relocation::metadata_addr() { 718 int n = _metadata_index; 719 if (n == 0) { 720 // metadata is stored in the code stream 721 return (Metadata**) pd_address_in_code(); 722 } else { 723 // metadata is stored in table at nmethod::metadatas_begin 724 return code()->metadata_addr_at(n); 725 } 726 } 727 728 729 Metadata* metadata_Relocation::metadata_value() { 730 Metadata* v = *metadata_addr(); 731 // clean inline caches store a special pseudo-null 732 if (v == (Metadata*)Universe::non_oop_word()) v = NULL; 733 return v; 734 } 735 736 737 void metadata_Relocation::fix_metadata_relocation() { 738 if (!metadata_is_immediate()) { 739 // get the metadata from the pool, and re-insert it into the instruction: 740 pd_fix_value(value()); 741 } 742 } 743 744 745 void metadata_Relocation::verify_metadata_relocation() { 746 if (!metadata_is_immediate()) { 747 // get the metadata from the pool, and re-insert it into the instruction: 748 verify_value(value()); 749 } 750 } 751 752 address virtual_call_Relocation::cached_value() { 753 assert(_cached_value != NULL && _cached_value < addr(), "must precede ic_call"); 754 return _cached_value; 755 } 756 757 Method* virtual_call_Relocation::method_value() { 758 CompiledMethod* cm = code(); 759 if (cm == NULL) return (Method*)NULL; 760 Metadata* m = cm->metadata_at(_method_index); 761 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 762 assert(m == NULL || m->is_method(), "not a method"); 763 return (Method*)m; 764 } 765 766 void virtual_call_Relocation::clear_inline_cache() { 767 // No stubs for ICs 768 // Clean IC 769 ResourceMark rm; 770 CompiledIC* icache = CompiledIC_at(this); 771 icache->set_to_clean(); 772 } 773 774 775 void opt_virtual_call_Relocation::pack_data_to(CodeSection* dest) { 776 short* p = (short*) dest->locs_end(); 777 p = pack_1_int_to(p, _method_index); 778 dest->set_locs_end((relocInfo*) p); 779 } 780 781 void opt_virtual_call_Relocation::unpack_data() { 782 _method_index = unpack_1_int(); 783 } 784 785 Method* opt_virtual_call_Relocation::method_value() { 786 CompiledMethod* cm = code(); 787 if (cm == NULL) return (Method*)NULL; 788 Metadata* m = cm->metadata_at(_method_index); 789 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 790 assert(m == NULL || m->is_method(), "not a method"); 791 return (Method*)m; 792 } 793 794 void opt_virtual_call_Relocation::clear_inline_cache() { 795 // No stubs for ICs 796 // Clean IC 797 ResourceMark rm; 798 CompiledIC* icache = CompiledIC_at(this); 799 icache->set_to_clean(); 800 } 801 802 803 address opt_virtual_call_Relocation::static_stub(bool is_aot) { 804 // search for the static stub who points back to this static call 805 address static_call_addr = addr(); 806 RelocIterator iter(code()); 807 while (iter.next()) { 808 if (iter.type() == relocInfo::static_stub_type) { 809 static_stub_Relocation* stub_reloc = iter.static_stub_reloc(); 810 if (stub_reloc->static_call() == static_call_addr && stub_reloc->is_aot() == is_aot) { 811 return iter.addr(); 812 } 813 } 814 } 815 return NULL; 816 } 817 818 Method* static_call_Relocation::method_value() { 819 CompiledMethod* cm = code(); 820 if (cm == NULL) return (Method*)NULL; 821 Metadata* m = cm->metadata_at(_method_index); 822 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index"); 823 assert(m == NULL || m->is_method(), "not a method"); 824 return (Method*)m; 825 } 826 827 void static_call_Relocation::pack_data_to(CodeSection* dest) { 828 short* p = (short*) dest->locs_end(); 829 p = pack_1_int_to(p, _method_index); 830 dest->set_locs_end((relocInfo*) p); 831 } 832 833 void static_call_Relocation::unpack_data() { 834 _method_index = unpack_1_int(); 835 } 836 837 void static_call_Relocation::clear_inline_cache() { 838 // Safe call site info 839 CompiledStaticCall* handler = this->code()->compiledStaticCall_at(this); 840 handler->set_to_clean(); 841 } 842 843 844 address static_call_Relocation::static_stub(bool is_aot) { 845 // search for the static stub who points back to this static call 846 address static_call_addr = addr(); 847 RelocIterator iter(code()); 848 while (iter.next()) { 849 if (iter.type() == relocInfo::static_stub_type) { 850 static_stub_Relocation* stub_reloc = iter.static_stub_reloc(); 851 if (stub_reloc->static_call() == static_call_addr && stub_reloc->is_aot() == is_aot) { 852 return iter.addr(); 853 } 854 } 855 } 856 return NULL; 857 } 858 859 // Finds the trampoline address for a call. If no trampoline stub is 860 // found NULL is returned which can be handled by the caller. 861 address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) { 862 // There are no relocations available when the code gets relocated 863 // because of CodeBuffer expansion. 864 if (code->relocation_size() == 0) 865 return NULL; 866 867 RelocIterator iter(code, call); 868 while (iter.next()) { 869 if (iter.type() == relocInfo::trampoline_stub_type) { 870 if (iter.trampoline_stub_reloc()->owner() == call) { 871 return iter.addr(); 872 } 873 } 874 } 875 876 return NULL; 877 } 878 879 void static_stub_Relocation::clear_inline_cache() { 880 // Call stub is only used when calling the interpreted code. 881 // It does not really need to be cleared, except that we want to clean out the methodoop. 882 CompiledDirectStaticCall::set_stub_to_clean(this); 883 } 884 885 886 void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 887 address target = _target; 888 if (target == NULL) { 889 // An absolute embedded reference to an external location, 890 // which means there is nothing to fix here. 891 return; 892 } 893 // Probably this reference is absolute, not relative, so the 894 // following is probably a no-op. 895 assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity"); 896 set_value(target); 897 } 898 899 900 address external_word_Relocation::target() { 901 address target = _target; 902 if (target == NULL) { 903 target = pd_get_address_from_code(); 904 } 905 return target; 906 } 907 908 909 void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { 910 address target = _target; 911 if (target == NULL) { 912 target = new_addr_for(this->target(), src, dest); 913 } 914 set_value(target); 915 } 916 917 918 address internal_word_Relocation::target() { 919 address target = _target; 920 if (target == NULL) { 921 if (addr_in_const()) { 922 target = *(address*)addr(); 923 } else { 924 target = pd_get_address_from_code(); 925 } 926 } 927 return target; 928 } 929 930 //--------------------------------------------------------------------------------- 931 // Non-product code 932 933 #ifndef PRODUCT 934 935 static const char* reloc_type_string(relocInfo::relocType t) { 936 switch (t) { 937 #define EACH_CASE(name) \ 938 case relocInfo::name##_type: \ 939 return #name; 940 941 APPLY_TO_RELOCATIONS(EACH_CASE); 942 #undef EACH_CASE 943 944 case relocInfo::none: 945 return "none"; 946 case relocInfo::data_prefix_tag: 947 return "prefix"; 948 default: 949 return "UNKNOWN RELOC TYPE"; 950 } 951 } 952 953 954 void RelocIterator::print_current() { 955 if (!has_current()) { 956 tty->print_cr("(no relocs)"); 957 return; 958 } 959 tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d", 960 p2i(_current), type(), reloc_type_string((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset()); 961 if (current()->format() != 0) 962 tty->print(" format=%d", current()->format()); 963 if (datalen() == 1) { 964 tty->print(" data=%d", data()[0]); 965 } else if (datalen() > 0) { 966 tty->print(" data={"); 967 for (int i = 0; i < datalen(); i++) { 968 tty->print("%04x", data()[i] & 0xFFFF); 969 } 970 tty->print("}"); 971 } 972 tty->print("]"); 973 switch (type()) { 974 case relocInfo::oop_type: 975 { 976 oop_Relocation* r = oop_reloc(); 977 oop* oop_addr = NULL; 978 oop raw_oop = NULL; 979 oop oop_value = NULL; 980 if (code() != NULL || r->oop_is_immediate()) { 981 oop_addr = r->oop_addr(); 982 raw_oop = *oop_addr; 983 oop_value = r->oop_value(); 984 } 985 tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 986 p2i(oop_addr), p2i(raw_oop), r->offset()); 987 // Do not print the oop by default--we want this routine to 988 // work even during GC or other inconvenient times. 989 if (WizardMode && oop_value != NULL) { 990 tty->print("oop_value=" INTPTR_FORMAT ": ", p2i(oop_value)); 991 if (oop_value->is_oop()) { 992 oop_value->print_value_on(tty); 993 } 994 } 995 break; 996 } 997 case relocInfo::metadata_type: 998 { 999 metadata_Relocation* r = metadata_reloc(); 1000 Metadata** metadata_addr = NULL; 1001 Metadata* raw_metadata = NULL; 1002 Metadata* metadata_value = NULL; 1003 if (code() != NULL || r->metadata_is_immediate()) { 1004 metadata_addr = r->metadata_addr(); 1005 raw_metadata = *metadata_addr; 1006 metadata_value = r->metadata_value(); 1007 } 1008 tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]", 1009 p2i(metadata_addr), p2i(raw_metadata), r->offset()); 1010 if (metadata_value != NULL) { 1011 tty->print("metadata_value=" INTPTR_FORMAT ": ", p2i(metadata_value)); 1012 metadata_value->print_value_on(tty); 1013 } 1014 break; 1015 } 1016 case relocInfo::external_word_type: 1017 case relocInfo::internal_word_type: 1018 case relocInfo::section_word_type: 1019 { 1020 DataRelocation* r = (DataRelocation*) reloc(); 1021 tty->print(" | [target=" INTPTR_FORMAT "]", p2i(r->value())); //value==target 1022 break; 1023 } 1024 case relocInfo::static_call_type: 1025 { 1026 static_call_Relocation* r = (static_call_Relocation*) reloc(); 1027 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1028 p2i(r->destination()), p2i(r->method_value())); 1029 break; 1030 } 1031 case relocInfo::runtime_call_type: 1032 case relocInfo::runtime_call_w_cp_type: 1033 { 1034 CallRelocation* r = (CallRelocation*) reloc(); 1035 tty->print(" | [destination=" INTPTR_FORMAT "]", p2i(r->destination())); 1036 break; 1037 } 1038 case relocInfo::virtual_call_type: 1039 { 1040 virtual_call_Relocation* r = (virtual_call_Relocation*) reloc(); 1041 tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1042 p2i(r->destination()), p2i(r->cached_value()), p2i(r->method_value())); 1043 break; 1044 } 1045 case relocInfo::static_stub_type: 1046 { 1047 static_stub_Relocation* r = (static_stub_Relocation*) reloc(); 1048 tty->print(" | [static_call=" INTPTR_FORMAT "]", p2i(r->static_call())); 1049 break; 1050 } 1051 case relocInfo::trampoline_stub_type: 1052 { 1053 trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc(); 1054 tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", p2i(r->owner())); 1055 break; 1056 } 1057 case relocInfo::opt_virtual_call_type: 1058 { 1059 opt_virtual_call_Relocation* r = (opt_virtual_call_Relocation*) reloc(); 1060 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]", 1061 p2i(r->destination()), p2i(r->method_value())); 1062 break; 1063 } 1064 } 1065 tty->cr(); 1066 } 1067 1068 1069 void RelocIterator::print() { 1070 RelocIterator save_this = (*this); 1071 relocInfo* scan = _current; 1072 if (!has_current()) scan += 1; // nothing to scan here! 1073 1074 bool skip_next = has_current(); 1075 bool got_next; 1076 while (true) { 1077 got_next = (skip_next || next()); 1078 skip_next = false; 1079 1080 tty->print(" @" INTPTR_FORMAT ": ", p2i(scan)); 1081 relocInfo* newscan = _current+1; 1082 if (!has_current()) newscan -= 1; // nothing to scan here! 1083 while (scan < newscan) { 1084 tty->print("%04x", *(short*)scan & 0xFFFF); 1085 scan++; 1086 } 1087 tty->cr(); 1088 1089 if (!got_next) break; 1090 print_current(); 1091 } 1092 1093 (*this) = save_this; 1094 } 1095 1096 // For the debugger: 1097 extern "C" 1098 void print_blob_locs(nmethod* nm) { 1099 nm->print(); 1100 RelocIterator iter(nm); 1101 iter.print(); 1102 } 1103 extern "C" 1104 void print_buf_locs(CodeBuffer* cb) { 1105 FlagSetting fs(PrintRelocations, true); 1106 cb->print(); 1107 } 1108 #endif // !PRODUCT