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