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