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