1 /*
2 * Copyright (c) 1997, 2018, 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 "oops/compressedOops.inline.hpp"
32 #include "runtime/flags/flagSetting.hpp"
33 #include "runtime/stubCodeGenerator.hpp"
34 #include "utilities/copy.hpp"
35 #include "oops/oop.inline.hpp"
36
37 const RelocationHolder RelocationHolder::none; // its type is relocInfo::none
38
39
40 // Implementation of relocInfo
41
42 #ifdef ASSERT
relocInfo(relocType t,int off,int f)43 relocInfo::relocInfo(relocType t, int off, int f) {
44 assert(t != data_prefix_tag, "cannot build a prefix this way");
45 assert((t & type_mask) == t, "wrong type");
46 assert((f & format_mask) == f, "wrong format");
47 assert(off >= 0 && off < offset_limit(), "offset out off bounds");
48 assert((off & (offset_unit-1)) == 0, "misaligned offset");
49 (*this) = relocInfo(t, RAW_BITS, off, f);
50 }
51 #endif
52
initialize(CodeSection * dest,Relocation * reloc)53 void relocInfo::initialize(CodeSection* dest, Relocation* reloc) {
54 relocInfo* data = this+1; // here's where the data might go
55 dest->set_locs_end(data); // sync end: the next call may read dest.locs_end
56 reloc->pack_data_to(dest); // maybe write data into locs, advancing locs_end
57 relocInfo* data_limit = dest->locs_end();
58 if (data_limit > data) {
59 relocInfo suffix = (*this);
60 data_limit = this->finish_prefix((short*) data_limit);
61 // Finish up with the suffix. (Hack note: pack_data_to might edit this.)
62 *data_limit = suffix;
63 dest->set_locs_end(data_limit+1);
64 }
65 }
66
finish_prefix(short * prefix_limit)67 relocInfo* relocInfo::finish_prefix(short* prefix_limit) {
68 assert(sizeof(relocInfo) == sizeof(short), "change this code");
69 short* p = (short*)(this+1);
70 assert(prefix_limit >= p, "must be a valid span of data");
71 int plen = prefix_limit - p;
72 if (plen == 0) {
73 debug_only(_value = 0xFFFF);
74 return this; // no data: remove self completely
75 }
76 if (plen == 1 && fits_into_immediate(p[0])) {
77 (*this) = immediate_relocInfo(p[0]); // move data inside self
78 return this+1;
79 }
80 // cannot compact, so just update the count and return the limit pointer
81 (*this) = prefix_relocInfo(plen); // write new datalen
82 assert(data() + datalen() == prefix_limit, "pointers must line up");
83 return (relocInfo*)prefix_limit;
84 }
85
set_type(relocType t)86 void relocInfo::set_type(relocType t) {
87 int old_offset = addr_offset();
88 int old_format = format();
89 (*this) = relocInfo(t, old_offset, old_format);
90 assert(type()==(int)t, "sanity check");
91 assert(addr_offset()==old_offset, "sanity check");
92 assert(format()==old_format, "sanity check");
93 }
94
code_as_nmethod() const95 nmethod* RelocIterator::code_as_nmethod() const {
96 return _code->as_nmethod();
97 }
98
set_format(int f)99 void relocInfo::set_format(int f) {
100 int old_offset = addr_offset();
101 assert((f & format_mask) == f, "wrong format");
102 _value = (_value & ~(format_mask << offset_width)) | (f << offset_width);
103 assert(addr_offset()==old_offset, "sanity check");
104 }
105
106
change_reloc_info_for_address(RelocIterator * itr,address pc,relocType old_type,relocType new_type)107 void relocInfo::change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type) {
108 bool found = false;
109 while (itr->next() && !found) {
110 if (itr->addr() == pc) {
111 assert(itr->type()==old_type, "wrong relocInfo type found");
112 itr->current()->set_type(new_type);
113 found=true;
114 }
115 }
116 assert(found, "no relocInfo found for pc");
117 }
118
119
remove_reloc_info_for_address(RelocIterator * itr,address pc,relocType old_type)120 void relocInfo::remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type) {
121 change_reloc_info_for_address(itr, pc, old_type, none);
122 }
123
124
125 // ----------------------------------------------------------------------------------------------------
126 // Implementation of RelocIterator
127
initialize(CompiledMethod * nm,address begin,address limit)128 void RelocIterator::initialize(CompiledMethod* nm, address begin, address limit) {
129 initialize_misc();
130
131 if (nm == NULL && begin != NULL) {
132 // allow nmethod to be deduced from beginning address
133 CodeBlob* cb = CodeCache::find_blob(begin);
134 nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
135 }
136 guarantee(nm != NULL, "must be able to deduce nmethod from other arguments");
137
138 _code = nm;
139 _current = nm->relocation_begin() - 1;
140 _end = nm->relocation_end();
141 _addr = nm->content_begin();
142
143 // Initialize code sections.
144 _section_start[CodeBuffer::SECT_CONSTS] = nm->consts_begin();
145 _section_start[CodeBuffer::SECT_INSTS ] = nm->insts_begin() ;
146 _section_start[CodeBuffer::SECT_STUBS ] = nm->stub_begin() ;
147
148 _section_end [CodeBuffer::SECT_CONSTS] = nm->consts_end() ;
149 _section_end [CodeBuffer::SECT_INSTS ] = nm->insts_end() ;
150 _section_end [CodeBuffer::SECT_STUBS ] = nm->stub_end() ;
151
152 assert(!has_current(), "just checking");
153 assert(begin == NULL || begin >= nm->code_begin(), "in bounds");
154 assert(limit == NULL || limit <= nm->code_end(), "in bounds");
155 set_limits(begin, limit);
156 }
157
158
RelocIterator(CodeSection * cs,address begin,address limit)159 RelocIterator::RelocIterator(CodeSection* cs, address begin, address limit) {
160 initialize_misc();
161
162 _current = cs->locs_start()-1;
163 _end = cs->locs_end();
164 _addr = cs->start();
165 _code = NULL; // Not cb->blob();
166
167 CodeBuffer* cb = cs->outer();
168 assert((int) SECT_LIMIT == CodeBuffer::SECT_LIMIT, "my copy must be equal");
169 for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) {
170 CodeSection* cs = cb->code_section(n);
171 _section_start[n] = cs->start();
172 _section_end [n] = cs->end();
173 }
174
175 assert(!has_current(), "just checking");
176
177 assert(begin == NULL || begin >= cs->start(), "in bounds");
178 assert(limit == NULL || limit <= cs->end(), "in bounds");
179 set_limits(begin, limit);
180 }
181
182
183 enum { indexCardSize = 128 };
184 struct RelocIndexEntry {
185 jint addr_offset; // offset from header_end of an addr()
186 jint reloc_offset; // offset from header_end of a relocInfo (prefix)
187 };
188
189
addr_in_const() const190 bool RelocIterator::addr_in_const() const {
191 const int n = CodeBuffer::SECT_CONSTS;
192 return section_start(n) <= addr() && addr() < section_end(n);
193 }
194
195
set_limits(address begin,address limit)196 void RelocIterator::set_limits(address begin, address limit) {
197 _limit = limit;
198
199 // the limit affects this next stuff:
200 if (begin != NULL) {
201 relocInfo* backup;
202 address backup_addr;
203 while (true) {
204 backup = _current;
205 backup_addr = _addr;
206 if (!next() || addr() >= begin) break;
207 }
208 // At this point, either we are at the first matching record,
209 // or else there is no such record, and !has_current().
210 // In either case, revert to the immediatly preceding state.
211 _current = backup;
212 _addr = backup_addr;
213 set_has_current(false);
214 }
215 }
216
217
set_limit(address limit)218 void RelocIterator::set_limit(address limit) {
219 address code_end = (address)code() + code()->size();
220 assert(limit == NULL || limit <= code_end, "in bounds");
221 _limit = limit;
222 }
223
224 // All the strange bit-encodings are in here.
225 // The idea is to encode relocation data which are small integers
226 // very efficiently (a single extra halfword). Larger chunks of
227 // relocation data need a halfword header to hold their size.
advance_over_prefix()228 void RelocIterator::advance_over_prefix() {
229 if (_current->is_datalen()) {
230 _data = (short*) _current->data();
231 _datalen = _current->datalen();
232 _current += _datalen + 1; // skip the embedded data & header
233 } else {
234 _databuf = _current->immediate();
235 _data = &_databuf;
236 _datalen = 1;
237 _current++; // skip the header
238 }
239 // The client will see the following relocInfo, whatever that is.
240 // It is the reloc to which the preceding data applies.
241 }
242
243
initialize_misc()244 void RelocIterator::initialize_misc() {
245 set_has_current(false);
246 for (int i = (int) CodeBuffer::SECT_FIRST; i < (int) CodeBuffer::SECT_LIMIT; i++) {
247 _section_start[i] = NULL; // these will be lazily computed, if needed
248 _section_end [i] = NULL;
249 }
250 }
251
252
reloc()253 Relocation* RelocIterator::reloc() {
254 // (take the "switch" out-of-line)
255 relocInfo::relocType t = type();
256 if (false) {}
257 #define EACH_TYPE(name) \
258 else if (t == relocInfo::name##_type) { \
259 return name##_reloc(); \
260 }
261 APPLY_TO_RELOCATIONS(EACH_TYPE);
262 #undef EACH_TYPE
263 assert(t == relocInfo::none, "must be padding");
264 return new(_rh) Relocation();
265 }
266
267
268 //////// Methods for flyweight Relocation types
269
270
plus(int offset) const271 RelocationHolder RelocationHolder::plus(int offset) const {
272 if (offset != 0) {
273 switch (type()) {
274 case relocInfo::none:
275 break;
276 case relocInfo::oop_type:
277 {
278 oop_Relocation* r = (oop_Relocation*)reloc();
279 return oop_Relocation::spec(r->oop_index(), r->offset() + offset);
280 }
281 case relocInfo::metadata_type:
282 {
283 metadata_Relocation* r = (metadata_Relocation*)reloc();
284 return metadata_Relocation::spec(r->metadata_index(), r->offset() + offset);
285 }
286 default:
287 ShouldNotReachHere();
288 }
289 }
290 return (*this);
291 }
292
293
guarantee_size()294 void Relocation::guarantee_size() {
295 guarantee(false, "Make _relocbuf bigger!");
296 }
297
298 // some relocations can compute their own values
value()299 address Relocation::value() {
300 ShouldNotReachHere();
301 return NULL;
302 }
303
304
set_value(address x)305 void Relocation::set_value(address x) {
306 ShouldNotReachHere();
307 }
308
const_set_data_value(address x)309 void Relocation::const_set_data_value(address x) {
310 #ifdef _LP64
311 if (format() == relocInfo::narrow_oop_in_const) {
312 *(narrowOop*)addr() = CompressedOops::encode((oop) x);
313 } else {
314 #endif
315 *(address*)addr() = x;
316 #ifdef _LP64
317 }
318 #endif
319 }
320
const_verify_data_value(address x)321 void Relocation::const_verify_data_value(address x) {
322 #ifdef _LP64
323 if (format() == relocInfo::narrow_oop_in_const) {
324 guarantee(*(narrowOop*)addr() == CompressedOops::encode((oop) x), "must agree");
325 } else {
326 #endif
327 guarantee(*(address*)addr() == x, "must agree");
328 #ifdef _LP64
329 }
330 #endif
331 }
332
333
spec_simple(relocInfo::relocType rtype)334 RelocationHolder Relocation::spec_simple(relocInfo::relocType rtype) {
335 if (rtype == relocInfo::none) return RelocationHolder::none;
336 relocInfo ri = relocInfo(rtype, 0);
337 RelocIterator itr;
338 itr.set_current(ri);
339 itr.reloc();
340 return itr._rh;
341 }
342
old_addr_for(address newa,const CodeBuffer * src,CodeBuffer * dest)343 address Relocation::old_addr_for(address newa,
344 const CodeBuffer* src, CodeBuffer* dest) {
345 int sect = dest->section_index_of(newa);
346 guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address");
347 address ostart = src->code_section(sect)->start();
348 address nstart = dest->code_section(sect)->start();
349 return ostart + (newa - nstart);
350 }
351
new_addr_for(address olda,const CodeBuffer * src,CodeBuffer * dest)352 address Relocation::new_addr_for(address olda,
353 const CodeBuffer* src, CodeBuffer* dest) {
354 debug_only(const CodeBuffer* src0 = src);
355 int sect = CodeBuffer::SECT_NONE;
356 // Look for olda in the source buffer, and all previous incarnations
357 // if the source buffer has been expanded.
358 for (; src != NULL; src = src->before_expand()) {
359 sect = src->section_index_of(olda);
360 if (sect != CodeBuffer::SECT_NONE) break;
361 }
362 guarantee(sect != CodeBuffer::SECT_NONE, "lost track of this address");
363 address ostart = src->code_section(sect)->start();
364 address nstart = dest->code_section(sect)->start();
365 return nstart + (olda - ostart);
366 }
367
normalize_address(address & addr,const CodeSection * dest,bool allow_other_sections)368 void Relocation::normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections) {
369 address addr0 = addr;
370 if (addr0 == NULL || dest->allocates2(addr0)) return;
371 CodeBuffer* cb = dest->outer();
372 addr = new_addr_for(addr0, cb, cb);
373 assert(allow_other_sections || dest->contains2(addr),
374 "addr must be in required section");
375 }
376
377
set_destination(address x)378 void CallRelocation::set_destination(address x) {
379 pd_set_call_destination(x);
380 }
381
fix_relocation_after_move(const CodeBuffer * src,CodeBuffer * dest)382 void CallRelocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
383 // Usually a self-relative reference to an external routine.
384 // On some platforms, the reference is absolute (not self-relative).
385 // The enhanced use of pd_call_destination sorts this all out.
386 address orig_addr = old_addr_for(addr(), src, dest);
387 address callee = pd_call_destination(orig_addr);
388 // Reassert the callee address, this time in the new copy of the code.
389 pd_set_call_destination(callee);
390 }
391
392
393 //// pack/unpack methods
394
pack_data_to(CodeSection * dest)395 void oop_Relocation::pack_data_to(CodeSection* dest) {
396 short* p = (short*) dest->locs_end();
397 p = pack_2_ints_to(p, _oop_index, _offset);
398 dest->set_locs_end((relocInfo*) p);
399 }
400
401
unpack_data()402 void oop_Relocation::unpack_data() {
403 unpack_2_ints(_oop_index, _offset);
404 }
405
pack_data_to(CodeSection * dest)406 void metadata_Relocation::pack_data_to(CodeSection* dest) {
407 short* p = (short*) dest->locs_end();
408 p = pack_2_ints_to(p, _metadata_index, _offset);
409 dest->set_locs_end((relocInfo*) p);
410 }
411
412
unpack_data()413 void metadata_Relocation::unpack_data() {
414 unpack_2_ints(_metadata_index, _offset);
415 }
416
417
pack_data_to(CodeSection * dest)418 void virtual_call_Relocation::pack_data_to(CodeSection* dest) {
419 short* p = (short*) dest->locs_end();
420 address point = dest->locs_point();
421
422 normalize_address(_cached_value, dest);
423 jint x0 = scaled_offset_null_special(_cached_value, point);
424 p = pack_2_ints_to(p, x0, _method_index);
425 dest->set_locs_end((relocInfo*) p);
426 }
427
428
unpack_data()429 void virtual_call_Relocation::unpack_data() {
430 jint x0 = 0;
431 unpack_2_ints(x0, _method_index);
432 address point = addr();
433 _cached_value = x0==0? NULL: address_from_scaled_offset(x0, point);
434 }
435
pack_data_to(CodeSection * dest)436 void runtime_call_w_cp_Relocation::pack_data_to(CodeSection * dest) {
437 short* p = pack_1_int_to((short *)dest->locs_end(), (jint)(_offset >> 2));
438 dest->set_locs_end((relocInfo*) p);
439 }
440
unpack_data()441 void runtime_call_w_cp_Relocation::unpack_data() {
442 _offset = unpack_1_int() << 2;
443 }
444
pack_data_to(CodeSection * dest)445 void static_stub_Relocation::pack_data_to(CodeSection* dest) {
446 short* p = (short*) dest->locs_end();
447 CodeSection* insts = dest->outer()->insts();
448 normalize_address(_static_call, insts);
449 jint is_aot = _is_aot ? 1 : 0;
450 p = pack_2_ints_to(p, scaled_offset(_static_call, insts->start()), is_aot);
451 dest->set_locs_end((relocInfo*) p);
452 }
453
unpack_data()454 void static_stub_Relocation::unpack_data() {
455 address base = binding()->section_start(CodeBuffer::SECT_INSTS);
456 jint offset;
457 jint is_aot;
458 unpack_2_ints(offset, is_aot);
459 _static_call = address_from_scaled_offset(offset, base);
460 _is_aot = (is_aot == 1);
461 }
462
pack_data_to(CodeSection * dest)463 void trampoline_stub_Relocation::pack_data_to(CodeSection* dest ) {
464 short* p = (short*) dest->locs_end();
465 CodeSection* insts = dest->outer()->insts();
466 normalize_address(_owner, insts);
467 p = pack_1_int_to(p, scaled_offset(_owner, insts->start()));
468 dest->set_locs_end((relocInfo*) p);
469 }
470
unpack_data()471 void trampoline_stub_Relocation::unpack_data() {
472 address base = binding()->section_start(CodeBuffer::SECT_INSTS);
473 _owner = address_from_scaled_offset(unpack_1_int(), base);
474 }
475
pack_data_to(CodeSection * dest)476 void external_word_Relocation::pack_data_to(CodeSection* dest) {
477 short* p = (short*) dest->locs_end();
478 #ifndef _LP64
479 p = pack_1_int_to(p, (int32_t) (intptr_t)_target);
480 #else
481 jlong t = (jlong) _target;
482 int32_t lo = low(t);
483 int32_t hi = high(t);
484 p = pack_2_ints_to(p, lo, hi);
485 #endif /* _LP64 */
486 dest->set_locs_end((relocInfo*) p);
487 }
488
489
unpack_data()490 void external_word_Relocation::unpack_data() {
491 #ifndef _LP64
492 _target = (address) (intptr_t)unpack_1_int();
493 #else
494 jint lo, hi;
495 unpack_2_ints(lo, hi);
496 jlong t = jlong_from(hi, lo);;
497 _target = (address) t;
498 #endif /* _LP64 */
499 }
500
501
pack_data_to(CodeSection * dest)502 void internal_word_Relocation::pack_data_to(CodeSection* dest) {
503 short* p = (short*) dest->locs_end();
504 normalize_address(_target, dest, true);
505
506 // Check whether my target address is valid within this section.
507 // If not, strengthen the relocation type to point to another section.
508 int sindex = _section;
509 if (sindex == CodeBuffer::SECT_NONE && _target != NULL
510 && (!dest->allocates(_target) || _target == dest->locs_point())) {
511 sindex = dest->outer()->section_index_of(_target);
512 guarantee(sindex != CodeBuffer::SECT_NONE, "must belong somewhere");
513 relocInfo* base = dest->locs_end() - 1;
514 assert(base->type() == this->type(), "sanity");
515 // Change the written type, to be section_word_type instead.
516 base->set_type(relocInfo::section_word_type);
517 }
518
519 // Note: An internal_word relocation cannot refer to its own instruction,
520 // because we reserve "0" to mean that the pointer itself is embedded
521 // in the code stream. We use a section_word relocation for such cases.
522
523 if (sindex == CodeBuffer::SECT_NONE) {
524 assert(type() == relocInfo::internal_word_type, "must be base class");
525 guarantee(_target == NULL || dest->allocates2(_target), "must be within the given code section");
526 jint x0 = scaled_offset_null_special(_target, dest->locs_point());
527 assert(!(x0 == 0 && _target != NULL), "correct encoding of null target");
528 p = pack_1_int_to(p, x0);
529 } else {
530 assert(_target != NULL, "sanity");
531 CodeSection* sect = dest->outer()->code_section(sindex);
532 guarantee(sect->allocates2(_target), "must be in correct section");
533 address base = sect->start();
534 jint offset = scaled_offset(_target, base);
535 assert((uint)sindex < (uint)CodeBuffer::SECT_LIMIT, "sanity");
536 assert(CodeBuffer::SECT_LIMIT <= (1 << section_width), "section_width++");
537 p = pack_1_int_to(p, (offset << section_width) | sindex);
538 }
539
540 dest->set_locs_end((relocInfo*) p);
541 }
542
543
unpack_data()544 void internal_word_Relocation::unpack_data() {
545 jint x0 = unpack_1_int();
546 _target = x0==0? NULL: address_from_scaled_offset(x0, addr());
547 _section = CodeBuffer::SECT_NONE;
548 }
549
550
unpack_data()551 void section_word_Relocation::unpack_data() {
552 jint x = unpack_1_int();
553 jint offset = (x >> section_width);
554 int sindex = (x & ((1<<section_width)-1));
555 address base = binding()->section_start(sindex);
556
557 _section = sindex;
558 _target = address_from_scaled_offset(offset, base);
559 }
560
561 //// miscellaneous methods
oop_addr()562 oop* oop_Relocation::oop_addr() {
563 int n = _oop_index;
564 if (n == 0) {
565 // oop is stored in the code stream
566 return (oop*) pd_address_in_code();
567 } else {
568 // oop is stored in table at nmethod::oops_begin
569 return code()->oop_addr_at(n);
570 }
571 }
572
573
oop_value()574 oop oop_Relocation::oop_value() {
575 oop v = *oop_addr();
576 // clean inline caches store a special pseudo-null
577 if (v == (oop)Universe::non_oop_word()) v = NULL;
578 return v;
579 }
580
581
fix_oop_relocation()582 void oop_Relocation::fix_oop_relocation() {
583 if (!oop_is_immediate()) {
584 // get the oop from the pool, and re-insert it into the instruction:
585 set_value(value());
586 }
587 }
588
589
verify_oop_relocation()590 void oop_Relocation::verify_oop_relocation() {
591 if (!oop_is_immediate()) {
592 // get the oop from the pool, and re-insert it into the instruction:
593 verify_value(value());
594 }
595 }
596
597 // meta data versions
metadata_addr()598 Metadata** metadata_Relocation::metadata_addr() {
599 int n = _metadata_index;
600 if (n == 0) {
601 // metadata is stored in the code stream
602 return (Metadata**) pd_address_in_code();
603 } else {
604 // metadata is stored in table at nmethod::metadatas_begin
605 return code()->metadata_addr_at(n);
606 }
607 }
608
609
metadata_value()610 Metadata* metadata_Relocation::metadata_value() {
611 Metadata* v = *metadata_addr();
612 // clean inline caches store a special pseudo-null
613 if (v == (Metadata*)Universe::non_oop_word()) v = NULL;
614 return v;
615 }
616
617
fix_metadata_relocation()618 void metadata_Relocation::fix_metadata_relocation() {
619 if (!metadata_is_immediate()) {
620 // get the metadata from the pool, and re-insert it into the instruction:
621 pd_fix_value(value());
622 }
623 }
624
625
verify_metadata_relocation()626 void metadata_Relocation::verify_metadata_relocation() {
627 if (!metadata_is_immediate()) {
628 // get the metadata from the pool, and re-insert it into the instruction:
629 verify_value(value());
630 }
631 }
632
cached_value()633 address virtual_call_Relocation::cached_value() {
634 assert(_cached_value != NULL && _cached_value < addr(), "must precede ic_call");
635 return _cached_value;
636 }
637
method_value()638 Method* virtual_call_Relocation::method_value() {
639 CompiledMethod* cm = code();
640 if (cm == NULL) return (Method*)NULL;
641 Metadata* m = cm->metadata_at(_method_index);
642 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index");
643 assert(m == NULL || m->is_method(), "not a method");
644 return (Method*)m;
645 }
646
clear_inline_cache()647 void virtual_call_Relocation::clear_inline_cache() {
648 // No stubs for ICs
649 // Clean IC
650 ResourceMark rm;
651 CompiledIC* icache = CompiledIC_at(this);
652 icache->set_to_clean();
653 }
654
655
pack_data_to(CodeSection * dest)656 void opt_virtual_call_Relocation::pack_data_to(CodeSection* dest) {
657 short* p = (short*) dest->locs_end();
658 p = pack_1_int_to(p, _method_index);
659 dest->set_locs_end((relocInfo*) p);
660 }
661
unpack_data()662 void opt_virtual_call_Relocation::unpack_data() {
663 _method_index = unpack_1_int();
664 }
665
method_value()666 Method* opt_virtual_call_Relocation::method_value() {
667 CompiledMethod* cm = code();
668 if (cm == NULL) return (Method*)NULL;
669 Metadata* m = cm->metadata_at(_method_index);
670 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index");
671 assert(m == NULL || m->is_method(), "not a method");
672 return (Method*)m;
673 }
674
clear_inline_cache()675 void opt_virtual_call_Relocation::clear_inline_cache() {
676 // No stubs for ICs
677 // Clean IC
678 ResourceMark rm;
679 CompiledIC* icache = CompiledIC_at(this);
680 icache->set_to_clean();
681 }
682
683
static_stub(bool is_aot)684 address opt_virtual_call_Relocation::static_stub(bool is_aot) {
685 // search for the static stub who points back to this static call
686 address static_call_addr = addr();
687 RelocIterator iter(code());
688 while (iter.next()) {
689 if (iter.type() == relocInfo::static_stub_type) {
690 static_stub_Relocation* stub_reloc = iter.static_stub_reloc();
691 if (stub_reloc->static_call() == static_call_addr && stub_reloc->is_aot() == is_aot) {
692 return iter.addr();
693 }
694 }
695 }
696 return NULL;
697 }
698
method_value()699 Method* static_call_Relocation::method_value() {
700 CompiledMethod* cm = code();
701 if (cm == NULL) return (Method*)NULL;
702 Metadata* m = cm->metadata_at(_method_index);
703 assert(m != NULL || _method_index == 0, "should be non-null for non-zero index");
704 assert(m == NULL || m->is_method(), "not a method");
705 return (Method*)m;
706 }
707
pack_data_to(CodeSection * dest)708 void static_call_Relocation::pack_data_to(CodeSection* dest) {
709 short* p = (short*) dest->locs_end();
710 p = pack_1_int_to(p, _method_index);
711 dest->set_locs_end((relocInfo*) p);
712 }
713
unpack_data()714 void static_call_Relocation::unpack_data() {
715 _method_index = unpack_1_int();
716 }
717
clear_inline_cache()718 void static_call_Relocation::clear_inline_cache() {
719 // Safe call site info
720 CompiledStaticCall* handler = this->code()->compiledStaticCall_at(this);
721 handler->set_to_clean();
722 }
723
724
static_stub(bool is_aot)725 address static_call_Relocation::static_stub(bool is_aot) {
726 // search for the static stub who points back to this static call
727 address static_call_addr = addr();
728 RelocIterator iter(code());
729 while (iter.next()) {
730 if (iter.type() == relocInfo::static_stub_type) {
731 static_stub_Relocation* stub_reloc = iter.static_stub_reloc();
732 if (stub_reloc->static_call() == static_call_addr && stub_reloc->is_aot() == is_aot) {
733 return iter.addr();
734 }
735 }
736 }
737 return NULL;
738 }
739
740 // Finds the trampoline address for a call. If no trampoline stub is
741 // found NULL is returned which can be handled by the caller.
get_trampoline_for(address call,nmethod * code)742 address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) {
743 // There are no relocations available when the code gets relocated
744 // because of CodeBuffer expansion.
745 if (code->relocation_size() == 0)
746 return NULL;
747
748 RelocIterator iter(code, call);
749 while (iter.next()) {
750 if (iter.type() == relocInfo::trampoline_stub_type) {
751 if (iter.trampoline_stub_reloc()->owner() == call) {
752 return iter.addr();
753 }
754 }
755 }
756
757 return NULL;
758 }
759
clear_inline_cache()760 void static_stub_Relocation::clear_inline_cache() {
761 // Call stub is only used when calling the interpreted code.
762 // It does not really need to be cleared, except that we want to clean out the methodoop.
763 CompiledDirectStaticCall::set_stub_to_clean(this);
764 }
765
766
fix_relocation_after_move(const CodeBuffer * src,CodeBuffer * dest)767 void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
768 if (_target != NULL) {
769 // Probably this reference is absolute, not relative, so the following is
770 // probably a no-op.
771 set_value(_target);
772 }
773 // If target is NULL, this is an absolute embedded reference to an external
774 // location, which means there is nothing to fix here. In either case, the
775 // resulting target should be an "external" address.
776 postcond(src->section_index_of(target()) == CodeBuffer::SECT_NONE);
777 postcond(dest->section_index_of(target()) == CodeBuffer::SECT_NONE);
778 }
779
780
target()781 address external_word_Relocation::target() {
782 address target = _target;
783 if (target == NULL) {
784 target = pd_get_address_from_code();
785 }
786 return target;
787 }
788
789
fix_relocation_after_move(const CodeBuffer * src,CodeBuffer * dest)790 void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
791 address target = _target;
792 if (target == NULL) {
793 target = new_addr_for(this->target(), src, dest);
794 }
795 set_value(target);
796 }
797
798
target()799 address internal_word_Relocation::target() {
800 address target = _target;
801 if (target == NULL) {
802 if (addr_in_const()) {
803 target = *(address*)addr();
804 } else {
805 target = pd_get_address_from_code();
806 }
807 }
808 return target;
809 }
810
811 //---------------------------------------------------------------------------------
812 // Non-product code
813
814 #ifndef PRODUCT
815
reloc_type_string(relocInfo::relocType t)816 static const char* reloc_type_string(relocInfo::relocType t) {
817 switch (t) {
818 #define EACH_CASE(name) \
819 case relocInfo::name##_type: \
820 return #name;
821
822 APPLY_TO_RELOCATIONS(EACH_CASE);
823 #undef EACH_CASE
824
825 case relocInfo::none:
826 return "none";
827 case relocInfo::data_prefix_tag:
828 return "prefix";
829 default:
830 return "UNKNOWN RELOC TYPE";
831 }
832 }
833
834
print_current()835 void RelocIterator::print_current() {
836 if (!has_current()) {
837 tty->print_cr("(no relocs)");
838 return;
839 }
840 tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d",
841 p2i(_current), type(), reloc_type_string((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset());
842 if (current()->format() != 0)
843 tty->print(" format=%d", current()->format());
844 if (datalen() == 1) {
845 tty->print(" data=%d", data()[0]);
846 } else if (datalen() > 0) {
847 tty->print(" data={");
848 for (int i = 0; i < datalen(); i++) {
849 tty->print("%04x", data()[i] & 0xFFFF);
850 }
851 tty->print("}");
852 }
853 tty->print("]");
854 switch (type()) {
855 case relocInfo::oop_type:
856 {
857 oop_Relocation* r = oop_reloc();
858 oop* oop_addr = NULL;
859 oop raw_oop = NULL;
860 oop oop_value = NULL;
861 if (code() != NULL || r->oop_is_immediate()) {
862 oop_addr = r->oop_addr();
863 raw_oop = *oop_addr;
864 oop_value = r->oop_value();
865 }
866 tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]",
867 p2i(oop_addr), p2i(raw_oop), r->offset());
868 // Do not print the oop by default--we want this routine to
869 // work even during GC or other inconvenient times.
870 if (WizardMode && oop_value != NULL) {
871 tty->print("oop_value=" INTPTR_FORMAT ": ", p2i(oop_value));
872 if (oopDesc::is_oop(oop_value)) {
873 oop_value->print_value_on(tty);
874 }
875 }
876 break;
877 }
878 case relocInfo::metadata_type:
879 {
880 metadata_Relocation* r = metadata_reloc();
881 Metadata** metadata_addr = NULL;
882 Metadata* raw_metadata = NULL;
883 Metadata* metadata_value = NULL;
884 if (code() != NULL || r->metadata_is_immediate()) {
885 metadata_addr = r->metadata_addr();
886 raw_metadata = *metadata_addr;
887 metadata_value = r->metadata_value();
888 }
889 tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]",
890 p2i(metadata_addr), p2i(raw_metadata), r->offset());
891 if (metadata_value != NULL) {
892 tty->print("metadata_value=" INTPTR_FORMAT ": ", p2i(metadata_value));
893 metadata_value->print_value_on(tty);
894 }
895 break;
896 }
897 case relocInfo::external_word_type:
898 case relocInfo::internal_word_type:
899 case relocInfo::section_word_type:
900 {
901 DataRelocation* r = (DataRelocation*) reloc();
902 tty->print(" | [target=" INTPTR_FORMAT "]", p2i(r->value())); //value==target
903 break;
904 }
905 case relocInfo::static_call_type:
906 {
907 static_call_Relocation* r = (static_call_Relocation*) reloc();
908 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
909 p2i(r->destination()), p2i(r->method_value()));
910 break;
911 }
912 case relocInfo::runtime_call_type:
913 case relocInfo::runtime_call_w_cp_type:
914 {
915 CallRelocation* r = (CallRelocation*) reloc();
916 tty->print(" | [destination=" INTPTR_FORMAT "]", p2i(r->destination()));
917 break;
918 }
919 case relocInfo::virtual_call_type:
920 {
921 virtual_call_Relocation* r = (virtual_call_Relocation*) reloc();
922 tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
923 p2i(r->destination()), p2i(r->cached_value()), p2i(r->method_value()));
924 break;
925 }
926 case relocInfo::static_stub_type:
927 {
928 static_stub_Relocation* r = (static_stub_Relocation*) reloc();
929 tty->print(" | [static_call=" INTPTR_FORMAT "]", p2i(r->static_call()));
930 break;
931 }
932 case relocInfo::trampoline_stub_type:
933 {
934 trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc();
935 tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", p2i(r->owner()));
936 break;
937 }
938 case relocInfo::opt_virtual_call_type:
939 {
940 opt_virtual_call_Relocation* r = (opt_virtual_call_Relocation*) reloc();
941 tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
942 p2i(r->destination()), p2i(r->method_value()));
943 break;
944 }
945 default:
946 break;
947 }
948 tty->cr();
949 }
950
951
print()952 void RelocIterator::print() {
953 RelocIterator save_this = (*this);
954 relocInfo* scan = _current;
955 if (!has_current()) scan += 1; // nothing to scan here!
956
957 bool skip_next = has_current();
958 bool got_next;
959 while (true) {
960 got_next = (skip_next || next());
961 skip_next = false;
962
963 tty->print(" @" INTPTR_FORMAT ": ", p2i(scan));
964 relocInfo* newscan = _current+1;
965 if (!has_current()) newscan -= 1; // nothing to scan here!
966 while (scan < newscan) {
967 tty->print("%04x", *(short*)scan & 0xFFFF);
968 scan++;
969 }
970 tty->cr();
971
972 if (!got_next) break;
973 print_current();
974 }
975
976 (*this) = save_this;
977 }
978
979 // For the debugger:
980 extern "C"
print_blob_locs(nmethod * nm)981 void print_blob_locs(nmethod* nm) {
982 nm->print();
983 RelocIterator iter(nm);
984 iter.print();
985 }
986 extern "C"
print_buf_locs(CodeBuffer * cb)987 void print_buf_locs(CodeBuffer* cb) {
988 FlagSetting fs(PrintRelocations, true);
989 cb->print();
990 }
991 #endif // !PRODUCT
992