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
 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 
 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 
 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 
 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 
 95 nmethod* RelocIterator::code_as_nmethod() const {
 96   return _code->as_nmethod();
 97 }
 98 
 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 
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 
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 
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 
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 
190 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 
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 
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.
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 
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 
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 
271 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 
294 void Relocation::guarantee_size() {
295   guarantee(false, "Make _relocbuf bigger!");
296 }
297 
298     // some relocations can compute their own values
299 address Relocation::value() {
300   ShouldNotReachHere();
301   return NULL;
302 }
303 
304 
305 void Relocation::set_value(address x) {
306   ShouldNotReachHere();
307 }
308 
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 
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 
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 
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 
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 
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 
378 void CallRelocation::set_destination(address x) {
379   pd_set_call_destination(x);
380 }
381 
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 
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 
402 void oop_Relocation::unpack_data() {
403   unpack_2_ints(_oop_index, _offset);
404 }
405 
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 
413 void metadata_Relocation::unpack_data() {
414   unpack_2_ints(_metadata_index, _offset);
415 }
416 
417 
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 
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 
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 
441 void runtime_call_w_cp_Relocation::unpack_data() {
442   _offset = unpack_1_int() << 2;
443 }
444 
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 
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 
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 
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 
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 
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 
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 
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 
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
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 
574 oop oop_Relocation::oop_value() {
575   oop v = *oop_addr();
576   // clean inline caches store a special pseudo-null
577   if (v == Universe::non_oop_word())  v = NULL;
578   return v;
579 }
580 
581 
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 
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
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 
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 
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 
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 
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 
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 
647 bool virtual_call_Relocation::clear_inline_cache() {
648   // No stubs for ICs
649   // Clean IC
650   ResourceMark rm;
651   CompiledIC* icache = CompiledIC_at(this);
652   return icache->set_to_clean();
653 }
654 
655 
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 
662 void opt_virtual_call_Relocation::unpack_data() {
663   _method_index = unpack_1_int();
664 }
665 
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 
675 bool 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   guarantee(icache->set_to_clean(), "opt_virtual_call cleaning should never fail");
681   return true;
682 }
683 
684 
685 address opt_virtual_call_Relocation::static_stub(bool is_aot) {
686   // search for the static stub who points back to this static call
687   address static_call_addr = addr();
688   RelocIterator iter(code());
689   while (iter.next()) {
690     if (iter.type() == relocInfo::static_stub_type) {
691       static_stub_Relocation* stub_reloc = iter.static_stub_reloc();
692       if (stub_reloc->static_call() == static_call_addr && stub_reloc->is_aot() == is_aot) {
693         return iter.addr();
694       }
695     }
696   }
697   return NULL;
698 }
699 
700 Method* static_call_Relocation::method_value() {
701   CompiledMethod* cm = code();
702   if (cm == NULL) return (Method*)NULL;
703   Metadata* m = cm->metadata_at(_method_index);
704   assert(m != NULL || _method_index == 0, "should be non-null for non-zero index");
705   assert(m == NULL || m->is_method(), "not a method");
706   return (Method*)m;
707 }
708 
709 void static_call_Relocation::pack_data_to(CodeSection* dest) {
710   short* p = (short*) dest->locs_end();
711   p = pack_1_int_to(p, _method_index);
712   dest->set_locs_end((relocInfo*) p);
713 }
714 
715 void static_call_Relocation::unpack_data() {
716   _method_index = unpack_1_int();
717 }
718 
719 bool static_call_Relocation::clear_inline_cache() {
720   // Safe call site info
721   CompiledStaticCall* handler = this->code()->compiledStaticCall_at(this);
722   guarantee(handler->set_to_clean(), "CompiledStaticCall cleaning should not fail");
723   return true;
724 }
725 
726 
727 address static_call_Relocation::static_stub(bool is_aot) {
728   // search for the static stub who points back to this static call
729   address static_call_addr = addr();
730   RelocIterator iter(code());
731   while (iter.next()) {
732     if (iter.type() == relocInfo::static_stub_type) {
733       static_stub_Relocation* stub_reloc = iter.static_stub_reloc();
734       if (stub_reloc->static_call() == static_call_addr && stub_reloc->is_aot() == is_aot) {
735         return iter.addr();
736       }
737     }
738   }
739   return NULL;
740 }
741 
742 // Finds the trampoline address for a call. If no trampoline stub is
743 // found NULL is returned which can be handled by the caller.
744 address trampoline_stub_Relocation::get_trampoline_for(address call, nmethod* code) {
745   // There are no relocations available when the code gets relocated
746   // because of CodeBuffer expansion.
747   if (code->relocation_size() == 0)
748     return NULL;
749 
750   RelocIterator iter(code, call);
751   while (iter.next()) {
752     if (iter.type() == relocInfo::trampoline_stub_type) {
753       if (iter.trampoline_stub_reloc()->owner() == call) {
754         return iter.addr();
755       }
756     }
757   }
758 
759   return NULL;
760 }
761 
762 bool static_stub_Relocation::clear_inline_cache() {
763   // Call stub is only used when calling the interpreted code.
764   // It does not really need to be cleared, except that we want to clean out the methodoop.
765   CompiledDirectStaticCall::set_stub_to_clean(this);
766   return true;
767 }
768 
769 
770 void external_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
771   address target = _target;
772   if (target == NULL) {
773     // An absolute embedded reference to an external location,
774     // which means there is nothing to fix here.
775     return;
776   }
777   // Probably this reference is absolute, not relative, so the
778   // following is probably a no-op.
779   assert(src->section_index_of(target) == CodeBuffer::SECT_NONE, "sanity");
780   set_value(target);
781 }
782 
783 
784 address external_word_Relocation::target() {
785   address target = _target;
786   if (target == NULL) {
787     target = pd_get_address_from_code();
788   }
789   return target;
790 }
791 
792 
793 void internal_word_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
794   address target = _target;
795   if (target == NULL) {
796     target = new_addr_for(this->target(), src, dest);
797   }
798   set_value(target);
799 }
800 
801 
802 address internal_word_Relocation::target() {
803   address target = _target;
804   if (target == NULL) {
805     if (addr_in_const()) {
806       target = *(address*)addr();
807     } else {
808       target = pd_get_address_from_code();
809     }
810   }
811   return target;
812 }
813 
814 //---------------------------------------------------------------------------------
815 // Non-product code
816 
817 #ifndef PRODUCT
818 
819 static const char* reloc_type_string(relocInfo::relocType t) {
820   switch (t) {
821   #define EACH_CASE(name) \
822   case relocInfo::name##_type: \
823     return #name;
824 
825   APPLY_TO_RELOCATIONS(EACH_CASE);
826   #undef EACH_CASE
827 
828   case relocInfo::none:
829     return "none";
830   case relocInfo::data_prefix_tag:
831     return "prefix";
832   default:
833     return "UNKNOWN RELOC TYPE";
834   }
835 }
836 
837 
838 void RelocIterator::print_current() {
839   if (!has_current()) {
840     tty->print_cr("(no relocs)");
841     return;
842   }
843   tty->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d",
844              p2i(_current), type(), reloc_type_string((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset());
845   if (current()->format() != 0)
846     tty->print(" format=%d", current()->format());
847   if (datalen() == 1) {
848     tty->print(" data=%d", data()[0]);
849   } else if (datalen() > 0) {
850     tty->print(" data={");
851     for (int i = 0; i < datalen(); i++) {
852       tty->print("%04x", data()[i] & 0xFFFF);
853     }
854     tty->print("}");
855   }
856   tty->print("]");
857   switch (type()) {
858   case relocInfo::oop_type:
859     {
860       oop_Relocation* r = oop_reloc();
861       oop* oop_addr  = NULL;
862       oop  raw_oop   = NULL;
863       oop  oop_value = NULL;
864       if (code() != NULL || r->oop_is_immediate()) {
865         oop_addr  = r->oop_addr();
866         raw_oop   = *oop_addr;
867         oop_value = r->oop_value();
868       }
869       tty->print(" | [oop_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]",
870                  p2i(oop_addr), p2i(raw_oop), r->offset());
871       // Do not print the oop by default--we want this routine to
872       // work even during GC or other inconvenient times.
873       if (WizardMode && oop_value != NULL) {
874         tty->print("oop_value=" INTPTR_FORMAT ": ", p2i(oop_value));
875         if (oopDesc::is_oop(oop_value)) {
876           oop_value->print_value_on(tty);
877         }
878       }
879       break;
880     }
881   case relocInfo::metadata_type:
882     {
883       metadata_Relocation* r = metadata_reloc();
884       Metadata** metadata_addr  = NULL;
885       Metadata*    raw_metadata   = NULL;
886       Metadata*    metadata_value = NULL;
887       if (code() != NULL || r->metadata_is_immediate()) {
888         metadata_addr  = r->metadata_addr();
889         raw_metadata   = *metadata_addr;
890         metadata_value = r->metadata_value();
891       }
892       tty->print(" | [metadata_addr=" INTPTR_FORMAT " *=" INTPTR_FORMAT " offset=%d]",
893                  p2i(metadata_addr), p2i(raw_metadata), r->offset());
894       if (metadata_value != NULL) {
895         tty->print("metadata_value=" INTPTR_FORMAT ": ", p2i(metadata_value));
896         metadata_value->print_value_on(tty);
897       }
898       break;
899     }
900   case relocInfo::external_word_type:
901   case relocInfo::internal_word_type:
902   case relocInfo::section_word_type:
903     {
904       DataRelocation* r = (DataRelocation*) reloc();
905       tty->print(" | [target=" INTPTR_FORMAT "]", p2i(r->value())); //value==target
906       break;
907     }
908   case relocInfo::static_call_type:
909     {
910       static_call_Relocation* r = (static_call_Relocation*) reloc();
911       tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
912                  p2i(r->destination()), p2i(r->method_value()));
913       break;
914     }
915   case relocInfo::runtime_call_type:
916   case relocInfo::runtime_call_w_cp_type:
917     {
918       CallRelocation* r = (CallRelocation*) reloc();
919       tty->print(" | [destination=" INTPTR_FORMAT "]", p2i(r->destination()));
920       break;
921     }
922   case relocInfo::virtual_call_type:
923     {
924       virtual_call_Relocation* r = (virtual_call_Relocation*) reloc();
925       tty->print(" | [destination=" INTPTR_FORMAT " cached_value=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
926                  p2i(r->destination()), p2i(r->cached_value()), p2i(r->method_value()));
927       break;
928     }
929   case relocInfo::static_stub_type:
930     {
931       static_stub_Relocation* r = (static_stub_Relocation*) reloc();
932       tty->print(" | [static_call=" INTPTR_FORMAT "]", p2i(r->static_call()));
933       break;
934     }
935   case relocInfo::trampoline_stub_type:
936     {
937       trampoline_stub_Relocation* r = (trampoline_stub_Relocation*) reloc();
938       tty->print(" | [trampoline owner=" INTPTR_FORMAT "]", p2i(r->owner()));
939       break;
940     }
941   case relocInfo::opt_virtual_call_type:
942     {
943       opt_virtual_call_Relocation* r = (opt_virtual_call_Relocation*) reloc();
944       tty->print(" | [destination=" INTPTR_FORMAT " metadata=" INTPTR_FORMAT "]",
945                  p2i(r->destination()), p2i(r->method_value()));
946       break;
947     }
948   default:
949     break;
950   }
951   tty->cr();
952 }
953 
954 
955 void RelocIterator::print() {
956   RelocIterator save_this = (*this);
957   relocInfo* scan = _current;
958   if (!has_current())  scan += 1;  // nothing to scan here!
959 
960   bool skip_next = has_current();
961   bool got_next;
962   while (true) {
963     got_next = (skip_next || next());
964     skip_next = false;
965 
966     tty->print("         @" INTPTR_FORMAT ": ", p2i(scan));
967     relocInfo* newscan = _current+1;
968     if (!has_current())  newscan -= 1;  // nothing to scan here!
969     while (scan < newscan) {
970       tty->print("%04x", *(short*)scan & 0xFFFF);
971       scan++;
972     }
973     tty->cr();
974 
975     if (!got_next)  break;
976     print_current();
977   }
978 
979   (*this) = save_this;
980 }
981 
982 // For the debugger:
983 extern "C"
984 void print_blob_locs(nmethod* nm) {
985   nm->print();
986   RelocIterator iter(nm);
987   iter.print();
988 }
989 extern "C"
990 void print_buf_locs(CodeBuffer* cb) {
991   FlagSetting fs(PrintRelocations, true);
992   cb->print();
993 }
994 #endif // !PRODUCT