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