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