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