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