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