1 /*
2 * Copyright (c) 1997, 2026, 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 #ifndef SHARE_CODE_RELOCINFO_HPP
26 #define SHARE_CODE_RELOCINFO_HPP
27
28 #include "cppstdlib/new.hpp"
29 #include "memory/allocation.hpp"
30 #include "oops/oopsHierarchy.hpp"
31 #include "runtime/osInfo.hpp"
32 #include "utilities/checkedCast.hpp"
33 #include "utilities/globalDefinitions.hpp"
34 #include "utilities/macros.hpp"
35
36 class CodeBlob;
37 class Metadata;
38 class NativeMovConstReg;
39 class nmethod;
40
41 // Types in this file:
42 // relocInfo
43 // One element of an array of halfwords encoding compressed relocations.
44 // Also, the source of relocation types (relocInfo::oop_type, ...).
45 // Relocation
46 // A flyweight object representing a single relocation.
47 // It is fully unpacked from the compressed relocation array.
48 // metadata_Relocation, ... (subclasses of Relocation)
49 // The location of some type-specific operations (metadata_addr, ...).
50 // Also, the source of relocation specs (metadata_Relocation::spec, ...).
51 // oop_Relocation, ... (subclasses of Relocation)
52 // oops in the code stream (strings, class loaders)
53 // Also, the source of relocation specs (oop_Relocation::spec, ...).
54 // RelocationHolder
55 // A value type which acts as a union holding a Relocation object.
56 // Represents a relocation spec passed into a CodeBuffer during assembly.
57 // RelocIterator
58 // A StackObj which iterates over the relocations associated with
59 // a range of code addresses. Can be used to operate a copy of code.
60
61
62 // Notes on relocType:
63 //
64 // These hold enough information to read or write a value embedded in
65 // the instructions of an CodeBlob. They're used to update:
66 //
67 // 1) embedded oops (isOop() == true)
68 // 2) inline caches (isIC() == true)
69 // 3) runtime calls (isRuntimeCall() == true)
70 // 4) internal word ref (isInternalWord() == true)
71 // 5) external word ref (isExternalWord() == true)
72 //
73 // when objects move (GC) or if code moves (compacting the code heap).
74 // They are also used to patch the code (if a call site must change)
75 //
76 // A relocInfo is represented in 16 bits:
77 // 4 bits indicating the relocation type
78 // 12 bits indicating the offset from the previous relocInfo address
79 //
80 // The offsets accumulate along the relocInfo stream to encode the
81 // address within the CodeBlob, which is named RelocIterator::addr().
82 // The address of a particular relocInfo always points to the first
83 // byte of the relevant instruction (and not to any of its subfields
84 // or embedded immediate constants).
85 //
86 // The offset value is scaled appropriately for the target machine.
87 // (See relocInfo_<arch>.hpp for the offset scaling.)
88 //
89 // On some machines, there may also be a "format" field which may provide
90 // additional information about the format of the instruction stream
91 // at the corresponding code address. The format value is usually zero.
92 // Any machine (such as Intel) whose instructions can sometimes contain
93 // more than one relocatable constant needs format codes to distinguish
94 // which operand goes with a given relocation.
95 //
96 // If the target machine needs N format bits, the offset has 12-N bits,
97 // the format is encoded between the offset and the type, and the
98 // relocInfo_<arch>.hpp file has manifest constants for the format codes.
99 //
100 // If the type is "data_prefix_tag" then the offset bits are further encoded,
101 // and in fact represent not a code-stream offset but some inline data.
102 // The data takes the form of a counted sequence of halfwords, which
103 // precedes the actual relocation record. (Clients never see it directly.)
104 // The interpretation of this extra data depends on the relocation type.
105 //
106 // On machines that have 32-bit immediate fields, there is usually
107 // little need for relocation "prefix" data, because the instruction stream
108 // is a perfectly reasonable place to store the value. On machines in
109 // which 32-bit values must be "split" across instructions, the relocation
110 // data is the "true" specification of the value, which is then applied
111 // to some field of the instruction (22 or 13 bits, on SPARC).
112 //
113 // Whenever the location of the CodeBlob changes, any PC-relative
114 // relocations, and any internal_word_type relocations, must be reapplied.
115 // After the GC runs, oop_type relocations must be reapplied.
116 //
117 //
118 // Here are meanings of the types:
119 //
120 // relocInfo::none -- a filler record
121 // Value: none
122 // Instruction: The corresponding code address is ignored
123 // Data: Any data prefix and format code are ignored
124 // (This means that any relocInfo can be disabled by setting
125 // its type to none. See relocInfo::remove.)
126 //
127 // relocInfo::oop_type, relocInfo::metadata_type -- a reference to an oop or meta data
128 // Value: an oop, or else the address (handle) of an oop
129 // Instruction types: memory (load), set (load address)
130 // Data: [] an oop stored in 4 bytes of instruction
131 // [[N]n] the index of an oop in the CodeBlob's oop pool
132 //
133 // relocInfo::internal_word_type -- an address within the same CodeBlob
134 // relocInfo::section_word_type -- same, but can refer to another section
135 // Value: an address in the CodeBlob's code or constants section
136 // Instruction types: memory (load), set (load address)
137 // Data: [] stored in 4 bytes of instruction
138 // [[L]l] a relative offset (see [About Offsets] below)
139 // In the case of section_word_type, the offset is relative to a section
140 // base address, and the section number (e.g., SECT_INSTS) is encoded
141 // into the low two bits of the offset L.
142 //
143 // relocInfo::external_word_type -- a fixed address in the runtime system
144 // Value: an address
145 // Instruction types: memory (load), set (load address)
146 // Data: [] stored in 4 bytes of instruction
147 // [n] the index of a "well-known" stub (usual case on RISC)
148 // [Ll] a 32-bit address
149 //
150 // relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
151 // Value: an address
152 // Instruction types: PC-relative call (or a PC-relative branch)
153 // Data: [] stored in 4 bytes of instruction
154 //
155 // relocInfo::static_call_type -- a static call
156 // Value: an CodeBlob, a stub, or a fixup routine
157 // Instruction types: a call
158 // Data: []
159 // The identity of the callee is extracted from debugging information.
160 // //%note reloc_3
161 //
162 // relocInfo::virtual_call_type -- a virtual call site (which includes an inline
163 // cache)
164 // Value: an CodeBlob, a stub, the interpreter, or a fixup routine
165 // Instruction types: a call, plus some associated set-oop instructions
166 // Data: [] the associated set-oops are adjacent to the call
167 // [n] n is a relative offset to the first set-oop
168 // [[N]n l] and l is a limit within which the set-oops occur
169 // [Nn Ll] both n and l may be 32 bits if necessary
170 // The identity of the callee is extracted from debugging information.
171 //
172 // relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
173 //
174 // Same info as a static_call_type. We use a special type, so the handling of
175 // virtuals and statics are separated.
176 //
177 //
178 // The offset n points to the first set-oop. (See [About Offsets] below.)
179 // In turn, the set-oop instruction specifies or contains an oop cell devoted
180 // exclusively to the IC call, which can be patched along with the call.
181 //
182 // The locations of any other set-oops are found by searching the relocation
183 // information starting at the first set-oop, and continuing until all
184 // relocations up through l have been inspected. The value l is another
185 // relative offset. (Both n and l are relative to the call's first byte.)
186 //
187 // The limit l of the search is exclusive. However, if it points within
188 // the call (e.g., offset zero), it is adjusted to point after the call.
189 //
190 // Since the offsets could be as wide as 32-bits, these conventions
191 // put no restrictions whatever upon code reorganization.
192 //
193 // The compiler is responsible for ensuring that transition from a clean
194 // state to a monomorphic compiled state is MP-safe. This implies that
195 // the system must respond well to intermediate states where a random
196 // subset of the set-oops has been correctly from the clean state
197 // upon entry to the VEP of the compiled method. In the case of a
198 // machine (Intel) with a single set-oop instruction, the 32-bit
199 // immediate field must not straddle a unit of memory coherence.
200 // //%note reloc_3
201 //
202 // relocInfo::static_stub_type -- an extra stub for each static_call_type
203 // Value: none
204 // Instruction types: a virtual call: { set_oop; jump; }
205 // Data: [[N]n] the offset of the associated static_call reloc
206 // This stub becomes the target of a static call which must be upgraded
207 // to a virtual call (because the callee is interpreted).
208 // See [About Offsets] below.
209 // //%note reloc_2
210 //
211 // relocInfo::poll_[return_]type -- a safepoint poll
212 // Value: none
213 // Instruction types: memory load or test
214 // Data: none
215 //
216 // For example:
217 //
218 // INSTRUCTIONS RELOC: TYPE PREFIX DATA
219 // ------------ ---- -----------
220 // sethi %hi(myObject), R oop_type [n(myObject)]
221 // ld [R+%lo(myObject)+fldOffset], R2 oop_type [n(myObject) fldOffset]
222 // add R2, 1, R2
223 // st R2, [R+%lo(myObject)+fldOffset] oop_type [n(myObject) fldOffset]
224 //%note reloc_1
225 //
226 // This uses 4 instruction words, 8 relocation halfwords,
227 // and an entry (which is shareable) in the CodeBlob's oop pool,
228 // for a total of 36 bytes.
229 //
230 // Note that the compiler is responsible for ensuring the "fldOffset" when
231 // added to "%lo(myObject)" does not overflow the immediate fields of the
232 // memory instructions.
233 //
234 //
235 // [About Offsets] Relative offsets are supplied to this module as
236 // positive byte offsets, but they may be internally stored scaled
237 // and/or negated, depending on what is most compact for the target
238 // system. Since the object pointed to by the offset typically
239 // precedes the relocation address, it is profitable to store
240 // these negative offsets as positive numbers, but this decision
241 // is internal to the relocation information abstractions.
242 //
243
244 class Relocation;
245 class CodeBuffer;
246 class CodeSection;
247 class RelocIterator;
248
249 class relocInfo {
250 friend class RelocIterator;
251 public:
252 enum relocType {
253 none = 0, // Used when no relocation should be generated
254 oop_type = 1, // embedded oop
255 virtual_call_type = 2, // a standard inline cache call for a virtual send
256 opt_virtual_call_type = 3, // a virtual call that has been statically bound (i.e., no IC cache)
257 static_call_type = 4, // a static send
258 static_stub_type = 5, // stub-entry for static send (takes care of interpreter case)
259 runtime_call_type = 6, // call to fixed external routine
260 external_word_type = 7, // reference to fixed external address
261 internal_word_type = 8, // reference within the current code blob
262 section_word_type = 9, // internal, but a cross-section reference
263 poll_type = 10, // polling instruction for safepoints
264 poll_return_type = 11, // polling instruction for safepoints at return
265 metadata_type = 12, // metadata that used to be oops
266 trampoline_stub_type = 13, // stub-entry for trampoline
267 runtime_call_w_cp_type = 14, // Runtime call which may load its target from the constant pool
268 data_prefix_tag = 15, // tag for a prefix (carries data arguments)
269 post_call_nop_type = 16, // A tag for post call nop relocations
270 barrier_type = 17, // GC barrier data
271 type_mask = 31 // A mask which selects only the above values
272 };
273
274 private:
275 unsigned short _value;
276
277 static const enum class RawBitsToken {} RAW_BITS{};
278
279 relocInfo(relocType type, RawBitsToken, int bits)
280 : _value(checked_cast<unsigned short>((type << nontype_width) + bits)) { }
281
282 static relocType check_relocType(relocType type) NOT_DEBUG({ return type; });
283
284 static void check_offset_and_format(int offset, int format) NOT_DEBUG_RETURN;
285
286 static int compute_bits(int offset, int format) {
287 check_offset_and_format(offset, format);
288 return (offset / offset_unit) + (format << offset_width);
289 }
290
291 public:
292 relocInfo(relocType type, int offset, int format = 0)
293 : relocInfo(check_relocType(type), RAW_BITS, compute_bits(offset, format)) {}
294
295 #define APPLY_TO_RELOCATIONS(visitor) \
296 visitor(oop) \
297 visitor(metadata) \
298 visitor(virtual_call) \
299 visitor(opt_virtual_call) \
300 visitor(static_call) \
301 visitor(static_stub) \
302 visitor(runtime_call) \
303 visitor(runtime_call_w_cp) \
304 visitor(external_word) \
305 visitor(internal_word) \
306 visitor(poll) \
307 visitor(poll_return) \
308 visitor(section_word) \
309 visitor(trampoline_stub) \
310 visitor(post_call_nop) \
311 visitor(barrier) \
312
313
314 public:
315 enum : unsigned short{
316 value_width = sizeof(unsigned short) * BitsPerByte,
317 type_width = 5, // == log2(type_mask+1)
318 nontype_width = value_width - type_width,
319 datalen_width = nontype_width-1,
320 datalen_tag = 1 << datalen_width, // or-ed into _value
321 datalen_limit = 1 << datalen_width,
322 datalen_mask = (1 << datalen_width)-1
323 };
324
325 // accessors
326 public:
327 relocType type() const { return (relocType)((unsigned)_value >> nontype_width); }
328 int format() const { return format_mask==0? 0: format_mask &
329 ((unsigned)_value >> offset_width); }
330 int addr_offset() const { assert(!is_prefix(), "must have offset");
331 return (_value & offset_mask)*offset_unit; }
332
333 protected:
334 const short* data() const { assert(is_datalen(), "must have data");
335 return (const short*)(this + 1); }
336 unsigned short datalen() const { assert(is_datalen(), "must have data");
337 return (_value & datalen_mask); }
338 unsigned short immediate() const { assert(is_immediate(), "must have immed");
339 return (_value & datalen_mask); }
340 public:
341 static int addr_unit() { return offset_unit; }
342 static int offset_limit() { return (1 << offset_width) * offset_unit; }
343
344 void set_type(relocType type);
345
346 void remove() { set_type(none); }
347
348 protected:
349 bool is_none() const { return type() == none; }
350 bool is_prefix() const { return type() == data_prefix_tag; }
351 bool is_datalen() const { assert(is_prefix(), "must be prefix");
352 return (_value & datalen_tag) != 0; }
353 bool is_immediate() const { assert(is_prefix(), "must be prefix");
354 return (_value & datalen_tag) == 0; }
355
356 public:
357 // Occasionally records of type relocInfo::none will appear in the stream.
358 // We do not bother to filter these out, but clients should ignore them.
359 // These records serve as "filler" in three ways:
360 // - to skip large spans of unrelocated code (this is rare)
361 // - to pad out the relocInfo array to the required oop alignment
362 // - to disable old relocation information which is no longer applicable
363
364 static relocInfo filler_info() {
365 return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
366 }
367
368 // Every non-prefix relocation may be preceded by at most one prefix,
369 // which supplies 1 or more halfwords of associated data. Conventionally,
370 // an int is represented by 0, 1, or 2 halfwords, depending on how
371 // many bits are required to represent the value. (In addition,
372 // if the sole halfword is a 10-bit unsigned number, it is made
373 // "immediate" in the prefix header word itself. This optimization
374 // is invisible outside this module.)
375
376 static relocInfo prefix_info(int datalen = 0) {
377 assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
378 return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
379 }
380
381 private:
382 // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
383 static relocInfo immediate_relocInfo(int data0) {
384 assert(fits_into_immediate(data0), "data0 in limits");
385 return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
386 }
387 static bool fits_into_immediate(int data0) {
388 return (data0 >= 0 && data0 < datalen_limit);
389 }
390
391 public:
392 // Support routines for compilers.
393
394 // This routine takes an infant relocInfo (unprefixed) and
395 // edits in its prefix, if any. It also updates dest.locs_end.
396 void initialize(CodeSection* dest, Relocation* reloc);
397
398 // This routine updates a prefix and returns the limit pointer.
399 // It tries to compress the prefix from 32 to 16 bits, and if
400 // successful returns a reduced "prefix_limit" pointer.
401 relocInfo* finish_prefix(short* prefix_limit);
402
403 // bit-packers for the data array:
404
405 // As it happens, the bytes within the shorts are ordered natively,
406 // but the shorts within the word are ordered big-endian.
407 // This is an arbitrary choice, made this way mainly to ease debugging.
408 static short data0_from_int(jint x) { return (short)(x >> value_width); }
409 static short data1_from_int(jint x) { return (short)x; }
410 static jint jint_from_data(short* data) {
411 return (data[0] << value_width) + (unsigned short)data[1];
412 }
413
414 static jint short_data_at(int n, short* data, int datalen) {
415 return datalen > n ? data[n] : 0;
416 }
417
418 static jint jint_data_at(int n, short* data, int datalen) {
419 return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
420 }
421
422 // Update methods for relocation information
423 // (since code is dynamically patched, we also need to dynamically update the relocation info)
424 // Both methods takes old_type, so it is able to perform sanity checks on the information removed.
425 static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
426
427 // Machine dependent stuff
428 #include CPU_HEADER(relocInfo)
429
430 protected:
431 // Derived constant, based on format_width which is PD:
432 enum {
433 offset_width = nontype_width - format_width,
434 offset_mask = (1<<offset_width) - 1,
435 format_mask = (1<<format_width) - 1
436 };
437 public:
438 enum {
439 #ifdef _LP64
440 // for use in format
441 // format_width must be at least 1 on _LP64
442 narrow_oop_in_const = 1,
443 #endif
444 // Conservatively large estimate of maximum length (in shorts)
445 // of any relocation record.
446 // Extended format is length prefix, data words, and tag/offset suffix.
447 length_limit = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
448 have_format = format_width > 0
449 };
450
451 static const char* type_name(relocInfo::relocType t);
452 };
453
454 #define FORWARD_DECLARE_EACH_CLASS(name) \
455 class name##_Relocation;
456 APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
457 #undef FORWARD_DECLARE_EACH_CLASS
458
459 // Holder for flyweight relocation objects.
460 // Although the flyweight subclasses are of varying sizes,
461 // the holder is "one size fits all".
462 class RelocationHolder {
463 friend class Relocation;
464
465 private:
466 // A Relocation is "held" by placement constructing a Relocation into
467 // _relocbuf. Hence, _relocbuf must accomodate all subclasses of
468 // Relocation. We also need the Relocation base class to be at the same
469 // address as the start of the object, e.g. at the address of _relocbuf.
470 // Both of these requirements are checked (see emplace_relocation).
471 // The placement of the base class subobject isn't guaranteed by C++, since
472 // these aren't standard layout classes, but all supported implementations
473 // provide that behavior. If that changes, we can instead add a Relocation*
474 // _reloc member to capture the result of the placement new, and use that to
475 // access the base subobject.
476 static const size_t _relocbuf_size = 5 * sizeof(void*);
477 alignas(void*) char _relocbuf[_relocbuf_size];
478
479 template<typename Reloc, typename... Args>
480 void emplace_relocation(const Args&... args) {
481 static_assert(std::is_base_of<Relocation, Reloc>::value, "not Relocation");
482 static_assert(sizeof(Reloc) <= sizeof(_relocbuf), "_relocbuf too small");
483 Relocation* reloc = ::new (_relocbuf) Reloc(args...);
484 // Verify the base class subobject of the object constructed into
485 // _relocbuf is at the same address as the derived object.
486 assert(static_cast<const void*>(reloc) == _relocbuf, "invariant");
487 }
488
489 // Support for Relocation::copy_into.
490 // reloc should be a most derived object.
491 template<typename Reloc>
492 void copy_into_impl(const Reloc& reloc) {
493 emplace_relocation<Reloc>(reloc);
494 }
495
496 // Tag for selecting the constructor below and carrying the type of the
497 // relocation object the new holder will (initially) contain.
498 template<typename Reloc> struct Construct {};
499
500 // Constructor used by construct(). Constructs a new holder containing a
501 // relocation of type Reloc that is constructed using the provided args.
502 template<typename Reloc, typename... Args>
503 RelocationHolder(Construct<Reloc>, const Args&... args) {
504 emplace_relocation<Reloc>(args...);
505 }
506
507 public:
508 Relocation* reloc() const { return (Relocation*)_relocbuf; }
509 inline relocInfo::relocType type() const;
510
511 // Return a holder containing a relocation of type Reloc, constructed using args.
512 template<typename Reloc, typename... Args>
513 static RelocationHolder construct(const Args&... args) {
514 return RelocationHolder(Construct<Reloc>(), args...);
515 }
516
517 RelocationHolder(); // Initializes type to none.
518
519 // Depends on the destructor for all relocation types being trivial
520 // (verified in .cpp file).
521 ~RelocationHolder() = default;
522
523 RelocationHolder(const RelocationHolder& from);
524 RelocationHolder& operator=(const RelocationHolder& from);
525
526 static const RelocationHolder none;
527 };
528
529 // A RelocIterator iterates through the relocation information of a CodeBlob.
530 // It provides access to successive relocations as it is advanced through a
531 // code stream.
532 // Usage:
533 // RelocIterator iter(nm);
534 // while (iter.next()) {
535 // iter.reloc()->some_operation();
536 // }
537 // or:
538 // RelocIterator iter(nm);
539 // while (iter.next()) {
540 // switch (iter.type()) {
541 // case relocInfo::oop_type :
542 // case relocInfo::ic_type :
543 // case relocInfo::prim_type :
544 // case relocInfo::uncommon_type :
545 // case relocInfo::runtime_call_type :
546 // case relocInfo::internal_word_type:
547 // case relocInfo::external_word_type:
548 // ...
549 // }
550 // }
551
552 class RelocIterator : public StackObj {
553 friend class section_word_Relocation; // for section verification
554 enum { SECT_LIMIT = 3 }; // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor
555 friend class Relocation;
556 friend class relocInfo; // for change_reloc_info_for_address only
557 typedef relocInfo::relocType relocType;
558
559 private:
560 address _limit; // stop producing relocations after this _addr
561 relocInfo* _current; // the current relocation information
562 relocInfo* _end; // end marker; we're done iterating when _current == _end
563 nmethod* _code; // compiled method containing _addr
564 address _addr; // instruction to which the relocation applies
565 short _databuf; // spare buffer for compressed data
566 short* _data; // pointer to the relocation's data
567 short _datalen; // number of halfwords in _data
568
569 // Base addresses needed to compute targets of section_word_type relocs.
570 address _section_start[SECT_LIMIT];
571 address _section_end [SECT_LIMIT];
572
573 void set_has_current(bool b) {
574 _datalen = !b ? -1 : 0;
575 DEBUG_ONLY(_data = nullptr);
576 }
577 void set_current(relocInfo& ri) {
578 _current = &ri;
579 set_has_current(true);
580 }
581
582 RelocationHolder _rh; // where the current relocation is allocated
583
584 relocInfo* current() const { assert(has_current(), "must have current");
585 return _current; }
586
587 void set_limits(address begin, address limit);
588
589 void advance_over_prefix(); // helper method
590
591 void initialize_misc();
592
593 void initialize(nmethod* nm, address begin, address limit);
594
595 RelocIterator() { initialize_misc(); }
596
597 public:
598 // constructor
599 RelocIterator(nmethod* nm, address begin = nullptr, address limit = nullptr);
600 RelocIterator(CodeSection* cb, address begin = nullptr, address limit = nullptr);
601 RelocIterator(CodeBlob* cb);
602
603 // get next reloc info, return !eos
604 bool next() {
605 _current++;
606 assert(_current <= _end, "must not overrun relocInfo");
607 if (_current == _end) {
608 set_has_current(false);
609 return false;
610 }
611 set_has_current(true);
612
613 if (_current->is_prefix()) {
614 advance_over_prefix();
615 assert(!current()->is_prefix(), "only one prefix at a time");
616 }
617
618 _addr += _current->addr_offset();
619
620 if (_limit != nullptr && _addr >= _limit) {
621 set_has_current(false);
622 return false;
623 }
624
625 return true;
626 }
627
628 // accessors
629 address limit() const { return _limit; }
630 relocType type() const { return current()->type(); }
631 int format() const { return (relocInfo::have_format) ? current()->format() : 0; }
632 address addr() const { return _addr; }
633 nmethod* code() const { return _code; }
634 short* data() const { return _data; }
635 int datalen() const { return _datalen; }
636 bool has_current() const { return _datalen >= 0; }
637 bool addr_in_const() const;
638
639 address section_start(int n) const {
640 assert(_section_start[n], "section %d must be initialized", n);
641 return _section_start[n];
642 }
643 address section_end(int n) const {
644 assert(_section_end[n], "section %d must be initialized", n);
645 return _section_end[n];
646 }
647
648 // The address points to the affected displacement part of the instruction.
649 // For RISC, this is just the whole instruction.
650 // For Intel, this is an unaligned 32-bit word.
651
652 // type-specific relocation accessors: oop_Relocation* oop_reloc(), etc.
653 #define EACH_TYPE(name) \
654 inline name##_Relocation* name##_reloc();
655 APPLY_TO_RELOCATIONS(EACH_TYPE)
656 #undef EACH_TYPE
657 // generic relocation accessor; switches on type to call the above
658 Relocation* reloc();
659
660 public:
661 void print_on(outputStream* st);
662 void print_current_on(outputStream* st);
663 };
664
665
666 // A Relocation is a flyweight object allocated within a RelocationHolder.
667 // It represents the relocation data of relocation record.
668 // So, the RelocIterator unpacks relocInfos into Relocations.
669
670 class Relocation {
671 friend class RelocIterator;
672 friend class AOTCodeReader;
673
674 private:
675 // When a relocation has been created by a RelocIterator,
676 // this field is non-null. It allows the relocation to know
677 // its context, such as the address to which it applies.
678 RelocIterator* _binding;
679
680 relocInfo::relocType _rtype;
681
682 protected:
683 RelocIterator* binding() const {
684 assert(_binding != nullptr, "must be bound");
685 return _binding;
686 }
687 void set_binding(RelocIterator* b) {
688 assert(_binding == nullptr, "must be unbound");
689 _binding = b;
690 assert(_binding != nullptr, "must now be bound");
691 }
692
693 explicit Relocation(relocInfo::relocType rtype) : _binding(nullptr), _rtype(rtype) { }
694
695 // Helper for copy_into functions for derived classes.
696 // Forwards operation to RelocationHolder::copy_into_impl so that
697 // RelocationHolder only needs to befriend this class, rather than all
698 // derived classes that implement copy_into.
699 template<typename Reloc>
700 static void copy_into_helper(const Reloc& reloc, RelocationHolder& holder) {
701 holder.copy_into_impl(reloc);
702 }
703
704 public:
705 // make a generic relocation for a given type (if possible)
706 static RelocationHolder spec_simple(relocInfo::relocType rtype);
707
708 // here is the type-specific hook which writes relocation data:
709 virtual void pack_data_to(CodeSection* dest) { }
710
711 // here is the type-specific hook which reads (unpacks) relocation data:
712 virtual void unpack_data() {
713 assert(datalen()==0 || type()==relocInfo::none, "no data here");
714 }
715
716 protected:
717 // Helper functions for pack_data_to() and unpack_data().
718
719 // Most of the compression logic is confined here.
720 // (The "immediate data" mechanism of relocInfo works independently
721 // of this stuff, and acts to further compress most 1-word data prefixes.)
722
723 // A variable-width int is encoded as a short if it will fit in 16 bits.
724 // The decoder looks at datalen to decide whether to unpack short or jint.
725 // Most relocation records are quite simple, containing at most two ints.
726
727 static bool is_short(jint x) { return x == (short)x; }
728 static short* add_short(short* p, short x) { *p++ = x; return p; }
729 static short* add_jint (short* p, jint x) {
730 *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
731 return p;
732 }
733 static short* add_var_int(short* p, jint x) { // add a variable-width int
734 if (is_short(x)) p = add_short(p, (short)x);
735 else p = add_jint (p, x);
736 return p;
737 }
738
739 static short* pack_1_int_to(short* p, jint x0) {
740 // Format is one of: [] [x] [Xx]
741 if (x0 != 0) p = add_var_int(p, x0);
742 return p;
743 }
744 int unpack_1_int() {
745 assert(datalen() <= 2, "too much data");
746 return relocInfo::jint_data_at(0, data(), datalen());
747 }
748
749 // With two ints, the short form is used only if both ints are short.
750 short* pack_2_ints_to(short* p, jint x0, jint x1) {
751 // Format is one of: [] [x y?] [Xx Y?y]
752 if (x0 == 0 && x1 == 0) {
753 // no halfwords needed to store zeroes
754 } else if (is_short(x0) && is_short(x1)) {
755 // 1-2 halfwords needed to store shorts
756 p = add_short(p, (short)x0); if (x1!=0) p = add_short(p, (short)x1);
757 } else {
758 // 3-4 halfwords needed to store jints
759 p = add_jint(p, x0); p = add_var_int(p, x1);
760 }
761 return p;
762 }
763 void unpack_2_ints(jint& x0, jint& x1) {
764 int dlen = datalen();
765 short* dp = data();
766 if (dlen <= 2) {
767 x0 = relocInfo::short_data_at(0, dp, dlen);
768 x1 = relocInfo::short_data_at(1, dp, dlen);
769 } else {
770 assert(dlen <= 4, "too much data");
771 x0 = relocInfo::jint_data_at(0, dp, dlen);
772 x1 = relocInfo::jint_data_at(2, dp, dlen);
773 }
774 }
775
776 protected:
777 // platform-independent utility for patching constant section
778 void const_set_data_value (address x);
779 void const_verify_data_value (address x);
780 // platform-dependent utilities for decoding and patching instructions
781 void pd_set_data_value (address x, bool verify_only = false); // a set or mem-ref
782 void pd_verify_data_value (address x) { pd_set_data_value(x, true); }
783 address pd_call_destination (address orig_addr = nullptr);
784 void pd_set_call_destination (address x);
785
786 // this extracts the address of an address in the code stream instead of the reloc data
787 address* pd_address_in_code ();
788
789 // this extracts an address from the code stream instead of the reloc data
790 address pd_get_address_from_code ();
791
792 // these convert from byte offsets, to scaled offsets, to addresses
793 static jint scaled_offset(address x, address base) {
794 int byte_offset = checked_cast<int>(x - base);
795 int offset = -byte_offset / relocInfo::addr_unit();
796 assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
797 return offset;
798 }
799 static jint scaled_offset_null_special(address x, address base) {
800 // Some relocations treat offset=0 as meaning nullptr.
801 // Handle this extra convention carefully.
802 if (x == nullptr) return 0;
803 assert(x != base, "offset must not be zero");
804 return scaled_offset(x, base);
805 }
806 static address address_from_scaled_offset(jint offset, address base) {
807 int byte_offset = -( offset * relocInfo::addr_unit() );
808 return base + byte_offset;
809 }
810
811 // helpers for mapping between old and new addresses after a move or resize
812 address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
813 address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
814 void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);
815
816 public:
817 // accessors which only make sense for a bound Relocation
818 address addr() const { return binding()->addr(); }
819 nmethod* code() const { return binding()->code(); }
820 bool addr_in_const() const { return binding()->addr_in_const(); }
821 protected:
822 short* data() const { return binding()->data(); }
823 int datalen() const { return binding()->datalen(); }
824
825 public:
826 // Make a filler relocation.
827 Relocation() : Relocation(relocInfo::none) {}
828
829 // Intentionally public non-virtual destructor, even though polymorphic. We
830 // never heap allocate a Relocation, so never delete through a base pointer.
831 // RelocationHolder depends on the destructor for all relocation types being
832 // trivial, so this must not be virtual (and hence non-trivial).
833 ~Relocation() = default;
834
835 int format() const { return binding()->format(); }
836
837 relocInfo::relocType type() const { return _rtype; }
838
839 // Copy this relocation into holder.
840 virtual void copy_into(RelocationHolder& holder) const;
841
842 // is it a call instruction?
843 virtual bool is_call() { return false; }
844
845 // is it a data movement instruction?
846 virtual bool is_data() { return false; }
847
848 // some relocations can compute their own values
849 virtual address value();
850
851 // all relocations are able to reassert their values
852 virtual void set_value(address x);
853
854 virtual void clear_inline_cache() {}
855
856 // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
857 // ic_call_type is not always position dependent (depending on the state of the cache)). However, this is
858 // probably a reasonable assumption, since empty caches simplifies code reloacation.
859 virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }
860 };
861
862
863 // certain inlines must be deferred until class Relocation is defined:
864
865 inline RelocationHolder::RelocationHolder() :
866 RelocationHolder(Construct<Relocation>())
867 {}
868
869 inline RelocationHolder::RelocationHolder(const RelocationHolder& from) {
870 from.reloc()->copy_into(*this);
871 }
872
873 inline RelocationHolder& RelocationHolder::operator=(const RelocationHolder& from) {
874 // All Relocation types are trivially destructible (verified in .cpp file),
875 // so we don't need to destruct our old value before copying over it.
876 // If not for that we would need to decide what to do about self-assignment.
877 from.reloc()->copy_into(*this);
878 return *this;
879 }
880
881 relocInfo::relocType RelocationHolder::type() const {
882 return reloc()->type();
883 }
884
885 // A DataRelocation always points at a memory or load-constant instruction..
886 // It is absolute on most machines, and the constant is split on RISCs.
887 // The specific subtypes are oop, external_word, and internal_word.
888 class DataRelocation : public Relocation {
889 public:
890 DataRelocation(relocInfo::relocType type) : Relocation(type) {}
891
892 bool is_data() override { return true; }
893
894 // target must be computed somehow from relocation data
895 address value() override = 0;
896 void set_value(address x) override {
897 if (addr_in_const()) {
898 const_set_data_value(x);
899 } else {
900 pd_set_data_value(x);
901 }
902 }
903 void verify_value(address x) {
904 if (addr_in_const()) {
905 const_verify_data_value(x);
906 } else {
907 pd_verify_data_value(x);
908 }
909 }
910 };
911
912 class post_call_nop_Relocation : public Relocation {
913 friend class RelocationHolder;
914
915 public:
916 post_call_nop_Relocation() : Relocation(relocInfo::post_call_nop_type) { }
917
918 static RelocationHolder spec() {
919 return RelocationHolder::construct<post_call_nop_Relocation>();
920 }
921
922 void copy_into(RelocationHolder& holder) const override;
923 };
924
925 // A CallRelocation always points at a call instruction.
926 // It is PC-relative on most machines.
927 class CallRelocation : public Relocation {
928 public:
929 CallRelocation(relocInfo::relocType type) : Relocation(type) { }
930
931 bool is_call() override { return true; }
932
933 address destination() { return pd_call_destination(); }
934 void set_destination(address x); // pd_set_call_destination
935
936 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) override;
937 address value() override { return destination(); }
938 void set_value(address x) override { set_destination(x); }
939 };
940
941 class oop_Relocation : public DataRelocation {
942 public:
943 // an oop in the CodeBlob's oop pool; encoded as [n] or [Nn]
944 static RelocationHolder spec(int oop_index) {
945 assert(oop_index > 0, "must be a pool-resident oop");
946 return RelocationHolder::construct<oop_Relocation>(oop_index);
947 }
948 // an oop in the instruction stream; encoded as []
949 static RelocationHolder spec_for_immediate() {
950 // If no immediate oops are generated, we can skip some walks over nmethods.
951 // Assert that they don't get generated accidentally!
952 assert(relocInfo::mustIterateImmediateOopsInCode(),
953 "Must return true so we will search for oops as roots etc. in the code.");
954 const int oop_index = 0;
955 return RelocationHolder::construct<oop_Relocation>(oop_index);
956 }
957
958 void copy_into(RelocationHolder& holder) const override;
959
960 private:
961 jint _oop_index; // if > 0, index into CodeBlob::oop_at
962
963 oop_Relocation(int oop_index)
964 : DataRelocation(relocInfo::oop_type), _oop_index(oop_index) { }
965
966 friend class RelocationHolder;
967 oop_Relocation() : DataRelocation(relocInfo::oop_type) {}
968
969 public:
970 int oop_index() { return _oop_index; }
971
972 // oop_index is packed in "1_int" format: [n] or [Nn]
973 void pack_data_to(CodeSection* dest) override;
974 void unpack_data() override;
975
976 void fix_oop_relocation(); // reasserts oop value
977
978 void verify_oop_relocation();
979
980 address value() override { return *reinterpret_cast<address*>(oop_addr()); }
981
982 bool oop_is_immediate() { return oop_index() == 0; }
983
984 oop* oop_addr(); // addr or &pool[jint_data]
985 oop oop_value(); // *oop_addr
986 // Note: oop_value transparently converts Universe::non_oop_word to nullptr.
987 };
988
989
990 // copy of oop_Relocation for now but may delete stuff in both/either
991 class metadata_Relocation : public DataRelocation {
992
993 public:
994 // an metadata in the CodeBlob's metadata pool; encoded as [n] or [Nn]
995 static RelocationHolder spec(int metadata_index) {
996 assert(metadata_index > 0, "must be a pool-resident metadata");
997 return RelocationHolder::construct<metadata_Relocation>(metadata_index);
998 }
999 // an metadata in the instruction stream; encoded as []
1000 static RelocationHolder spec_for_immediate() {
1001 const int metadata_index = 0;
1002 return RelocationHolder::construct<metadata_Relocation>(metadata_index);
1003 }
1004
1005 void copy_into(RelocationHolder& holder) const override;
1006
1007 private:
1008 jint _metadata_index; // if > 0, index into nmethod::metadata_at
1009
1010 metadata_Relocation(int metadata_index)
1011 : DataRelocation(relocInfo::metadata_type), _metadata_index(metadata_index) { }
1012
1013 friend class RelocationHolder;
1014 metadata_Relocation() : DataRelocation(relocInfo::metadata_type) { }
1015
1016 // Fixes a Metadata pointer in the code. Most platforms embeds the
1017 // Metadata pointer in the code at compile time so this is empty
1018 // for them.
1019 void pd_fix_value(address x);
1020
1021 public:
1022 int metadata_index() { return _metadata_index; }
1023
1024 // metadata_index is packed in "1_int" format: [n] or [Nn]
1025 void pack_data_to(CodeSection* dest) override;
1026 void unpack_data() override;
1027
1028 void fix_metadata_relocation(); // reasserts metadata value
1029
1030 address value() override { return (address) *metadata_addr(); }
1031
1032 bool metadata_is_immediate() { return metadata_index() == 0; }
1033
1034 Metadata** metadata_addr(); // addr or &pool[jint_data]
1035 Metadata* metadata_value(); // *metadata_addr
1036 // Note: metadata_value transparently converts Universe::non_metadata_word to nullptr.
1037 };
1038
1039
1040 class barrier_Relocation : public Relocation {
1041
1042 public:
1043 // The uninitialized value used before the relocation has been patched.
1044 // Code assumes that the unpatched value is zero.
1045 static const int16_t unpatched = 0;
1046
1047 static RelocationHolder spec() {
1048 return RelocationHolder::construct<barrier_Relocation>();
1049 }
1050
1051 void copy_into(RelocationHolder& holder) const override;
1052
1053 private:
1054 friend class RelocIterator;
1055 friend class RelocationHolder;
1056 barrier_Relocation() : Relocation(relocInfo::barrier_type) { }
1057 };
1058
1059
1060 class virtual_call_Relocation : public CallRelocation {
1061
1062 public:
1063 // "cached_value" points to the first associated set-oop.
1064 // The oop_limit helps find the last associated set-oop.
1065 // (See comments at the top of this file.)
1066 static RelocationHolder spec(address cached_value, jint method_index = 0) {
1067 return RelocationHolder::construct<virtual_call_Relocation>(cached_value, method_index);
1068 }
1069
1070 void copy_into(RelocationHolder& holder) const override;
1071
1072 private:
1073 address _cached_value; // location of set-value instruction
1074 jint _method_index; // resolved method for a Java call
1075
1076 virtual_call_Relocation(address cached_value, int method_index)
1077 : CallRelocation(relocInfo::virtual_call_type),
1078 _cached_value(cached_value),
1079 _method_index(method_index) {
1080 assert(cached_value != nullptr, "first oop address must be specified");
1081 }
1082
1083 friend class RelocationHolder;
1084 virtual_call_Relocation() : CallRelocation(relocInfo::virtual_call_type) { }
1085
1086 public:
1087 address cached_value();
1088
1089 int method_index() { return _method_index; }
1090 Method* method_value();
1091
1092 // data is packed as scaled offsets in "2_ints" format: [f l] or [Ff Ll]
1093 // oop_limit is set to 0 if the limit falls somewhere within the call.
1094 // When unpacking, a zero oop_limit is taken to refer to the end of the call.
1095 void pack_data_to(CodeSection* dest) override;
1096 void unpack_data() override;
1097
1098 void clear_inline_cache() override;
1099 };
1100
1101
1102 class opt_virtual_call_Relocation : public CallRelocation {
1103 public:
1104 static RelocationHolder spec(int method_index = 0) {
1105 return RelocationHolder::construct<opt_virtual_call_Relocation>(method_index);
1106 }
1107
1108 void copy_into(RelocationHolder& holder) const override;
1109
1110 private:
1111 jint _method_index; // resolved method for a Java call
1112
1113 opt_virtual_call_Relocation(int method_index)
1114 : CallRelocation(relocInfo::opt_virtual_call_type),
1115 _method_index(method_index) { }
1116
1117 friend class RelocationHolder;
1118 opt_virtual_call_Relocation() : CallRelocation(relocInfo::opt_virtual_call_type) {}
1119
1120 public:
1121 int method_index() { return _method_index; }
1122 Method* method_value();
1123
1124 void pack_data_to(CodeSection* dest) override;
1125 void unpack_data() override;
1126
1127 void clear_inline_cache() override;
1128
1129 // find the matching static_stub
1130 address static_stub();
1131 };
1132
1133
1134 class static_call_Relocation : public CallRelocation {
1135 public:
1136 static RelocationHolder spec(int method_index = 0) {
1137 return RelocationHolder::construct<static_call_Relocation>(method_index);
1138 }
1139
1140 void copy_into(RelocationHolder& holder) const override;
1141
1142 private:
1143 jint _method_index; // resolved method for a Java call
1144
1145 static_call_Relocation(int method_index)
1146 : CallRelocation(relocInfo::static_call_type),
1147 _method_index(method_index) { }
1148
1149 friend class RelocationHolder;
1150 static_call_Relocation() : CallRelocation(relocInfo::static_call_type) {}
1151
1152 public:
1153 int method_index() { return _method_index; }
1154 Method* method_value();
1155
1156 void pack_data_to(CodeSection* dest) override;
1157 void unpack_data() override;
1158
1159 void clear_inline_cache() override;
1160
1161 // find the matching static_stub
1162 address static_stub();
1163 };
1164
1165 class static_stub_Relocation : public Relocation {
1166 public:
1167 static RelocationHolder spec(address static_call) {
1168 return RelocationHolder::construct<static_stub_Relocation>(static_call);
1169 }
1170
1171 void copy_into(RelocationHolder& holder) const override;
1172
1173 private:
1174 address _static_call; // location of corresponding static_call
1175
1176 static_stub_Relocation(address static_call)
1177 : Relocation(relocInfo::static_stub_type),
1178 _static_call(static_call) { }
1179
1180 friend class RelocationHolder;
1181 static_stub_Relocation() : Relocation(relocInfo::static_stub_type) { }
1182
1183 public:
1184 void clear_inline_cache() override;
1185
1186 address static_call() { return _static_call; }
1187
1188 // data is packed as a scaled offset in "1_int" format: [c] or [Cc]
1189 void pack_data_to(CodeSection* dest) override;
1190 void unpack_data() override;
1191 };
1192
1193 class runtime_call_Relocation : public CallRelocation {
1194
1195 public:
1196 static RelocationHolder spec() {
1197 return RelocationHolder::construct<runtime_call_Relocation>();
1198 }
1199
1200 void copy_into(RelocationHolder& holder) const override;
1201
1202 private:
1203 friend class RelocationHolder;
1204 runtime_call_Relocation() : CallRelocation(relocInfo::runtime_call_type) { }
1205 };
1206
1207
1208 class runtime_call_w_cp_Relocation : public CallRelocation {
1209 public:
1210 static RelocationHolder spec() {
1211 return RelocationHolder::construct<runtime_call_w_cp_Relocation>();
1212 }
1213
1214 void copy_into(RelocationHolder& holder) const override;
1215
1216 private:
1217 friend class RelocationHolder;
1218 runtime_call_w_cp_Relocation()
1219 : CallRelocation(relocInfo::runtime_call_w_cp_type),
1220 _offset(-4) /* <0 = invalid */ { }
1221
1222 // On z/Architecture, runtime calls are either a sequence
1223 // of two instructions (load destination of call from constant pool + do call)
1224 // or a pc-relative call. The pc-relative call is faster, but it can only
1225 // be used if the destination of the call is not too far away.
1226 // In order to be able to patch a pc-relative call back into one using
1227 // the constant pool, we have to remember the location of the call's destination
1228 // in the constant pool.
1229 int _offset;
1230
1231 public:
1232 void set_constant_pool_offset(int offset) { _offset = offset; }
1233 int get_constant_pool_offset() { return _offset; }
1234 void pack_data_to(CodeSection * dest) override;
1235 void unpack_data() override;
1236 };
1237
1238 // Trampoline Relocations.
1239 // A trampoline allows to encode a small branch in the code, even if there
1240 // is the chance that this branch can not reach all possible code locations.
1241 // If the relocation finds that a branch is too far for the instruction
1242 // in the code, it can patch it to jump to the trampoline where is
1243 // sufficient space for a far branch. Needed on PPC.
1244 class trampoline_stub_Relocation : public Relocation {
1245 #ifdef USE_TRAMPOLINE_STUB_FIX_OWNER
1246 void pd_fix_owner_after_move();
1247 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) override;
1248 #endif
1249
1250 public:
1251 static RelocationHolder spec(address static_call) {
1252 return RelocationHolder::construct<trampoline_stub_Relocation>(static_call);
1253 }
1254
1255 void copy_into(RelocationHolder& holder) const override;
1256
1257 private:
1258 address _owner; // Address of the NativeCall that owns the trampoline.
1259
1260 trampoline_stub_Relocation(address owner)
1261 : Relocation(relocInfo::trampoline_stub_type),
1262 _owner(owner) { }
1263
1264 friend class RelocationHolder;
1265 trampoline_stub_Relocation() : Relocation(relocInfo::trampoline_stub_type) { }
1266
1267 public:
1268
1269 // Return the address of the NativeCall that owns the trampoline.
1270 address owner() { return _owner; }
1271
1272 void pack_data_to(CodeSection * dest) override;
1273 void unpack_data() override;
1274
1275 // Find the trampoline stub for a call.
1276 static address get_trampoline_for(address call, nmethod* code);
1277 };
1278
1279 class external_word_Relocation : public DataRelocation {
1280 public:
1281 static RelocationHolder spec(address target) {
1282 assert(target != nullptr, "must not be null");
1283 return RelocationHolder::construct<external_word_Relocation>(target);
1284 }
1285
1286 // Use this one where all 32/64 bits of the target live in the code stream.
1287 // The target must be an intptr_t, and must be absolute (not relative).
1288 static RelocationHolder spec_for_immediate() {
1289 return RelocationHolder::construct<external_word_Relocation>(nullptr);
1290 }
1291
1292 void copy_into(RelocationHolder& holder) const override;
1293
1294 // Some address looking values aren't safe to treat as relocations
1295 // and should just be treated as constants.
1296 static bool can_be_relocated(address target) {
1297 assert(target == nullptr || (uintptr_t)target >= (uintptr_t)OSInfo::vm_page_size(), INTPTR_FORMAT, (intptr_t)target);
1298 return target != nullptr;
1299 }
1300
1301 private:
1302 address _target; // address in runtime
1303
1304 external_word_Relocation(address target)
1305 : DataRelocation(relocInfo::external_word_type), _target(target) { }
1306
1307 friend class RelocationHolder;
1308 external_word_Relocation() : DataRelocation(relocInfo::external_word_type) { }
1309
1310 public:
1311 // data is packed as a well-known address in "1_int" format: [a] or [Aa]
1312 // The function runtime_address_to_index is used to turn full addresses
1313 // to short indexes, if they are pre-registered by the stub mechanism.
1314 // If the "a" value is 0 (i.e., _target is nullptr), the address is stored
1315 // in the code stream. See external_word_Relocation::target().
1316 void pack_data_to(CodeSection* dest) override;
1317 void unpack_data() override;
1318
1319 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) override;
1320 address target(); // if _target==nullptr, fetch addr from code stream
1321 address value() override { return target(); }
1322 };
1323
1324 class internal_word_Relocation : public DataRelocation {
1325
1326 public:
1327 static RelocationHolder spec(address target) {
1328 assert(target != nullptr, "must not be null");
1329 return RelocationHolder::construct<internal_word_Relocation>(target);
1330 }
1331
1332 // use this one where all the bits of the target can fit in the code stream:
1333 static RelocationHolder spec_for_immediate() {
1334 return RelocationHolder::construct<internal_word_Relocation>(nullptr);
1335 }
1336
1337 void copy_into(RelocationHolder& holder) const override;
1338
1339 // default section -1 means self-relative
1340 internal_word_Relocation(address target, int section = -1,
1341 relocInfo::relocType type = relocInfo::internal_word_type)
1342 : DataRelocation(type), _target(target), _section(section) { }
1343
1344 protected:
1345 address _target; // address in CodeBlob
1346 int _section; // section providing base address, if any
1347
1348 friend class RelocationHolder;
1349 internal_word_Relocation(relocInfo::relocType type = relocInfo::internal_word_type)
1350 : DataRelocation(type) { }
1351
1352 // bit-width of LSB field in packed offset, if section >= 0
1353 enum { section_width = 2 }; // must equal CodeBuffer::sect_bits
1354
1355 public:
1356 // data is packed as a scaled offset in "1_int" format: [o] or [Oo]
1357 // If the "o" value is 0 (i.e., _target is nullptr), the offset is stored
1358 // in the code stream. See internal_word_Relocation::target().
1359 // If _section is not -1, it is appended to the low bits of the offset.
1360 void pack_data_to(CodeSection* dest) override;
1361 void unpack_data() override;
1362
1363 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) override;
1364 void fix_relocation_after_aot_load(address orig_base_addr, address current_base_addr);
1365
1366 address target(); // if _target==nullptr, fetch addr from code stream
1367 int section() { return _section; }
1368 address value() override { return target(); }
1369 };
1370
1371 class section_word_Relocation : public internal_word_Relocation {
1372 public:
1373 static RelocationHolder spec(address target, int section) {
1374 return RelocationHolder::construct<section_word_Relocation>(target, section);
1375 }
1376
1377 void copy_into(RelocationHolder& holder) const override;
1378
1379 section_word_Relocation(address target, int section)
1380 : internal_word_Relocation(target, section, relocInfo::section_word_type) {
1381 assert(target != nullptr, "must not be null");
1382 assert(section >= 0 && section < RelocIterator::SECT_LIMIT, "must be a valid section");
1383 }
1384
1385 //void pack_data_to -- inherited
1386 void unpack_data() override;
1387
1388 private:
1389 friend class RelocationHolder;
1390 section_word_Relocation() : internal_word_Relocation(relocInfo::section_word_type) { }
1391 };
1392
1393
1394 class poll_Relocation : public Relocation {
1395 bool is_data() override { return true; }
1396 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) override;
1397 public:
1398 poll_Relocation(relocInfo::relocType type = relocInfo::poll_type) : Relocation(type) { }
1399
1400 void copy_into(RelocationHolder& holder) const override;
1401 };
1402
1403 class poll_return_Relocation : public poll_Relocation {
1404 public:
1405 poll_return_Relocation() : poll_Relocation(relocInfo::relocInfo::poll_return_type) { }
1406
1407 void copy_into(RelocationHolder& holder) const override;
1408 };
1409
1410 // We know all the xxx_Relocation classes, so now we can define these:
1411 #define EACH_CASE_AUX(Accessor, Reloc) \
1412 inline Reloc* RelocIterator::Accessor() { \
1413 static const RelocationHolder proto = RelocationHolder::construct<Reloc>(); \
1414 assert(type() == proto.type(), "type must agree"); \
1415 _rh = proto; \
1416 Reloc* r = static_cast<Reloc*>(_rh.reloc()); \
1417 r->set_binding(this); \
1418 r->Reloc::unpack_data(); \
1419 return r; \
1420 }
1421 #define EACH_CASE(name) \
1422 EACH_CASE_AUX(PASTE_TOKENS(name, _reloc), PASTE_TOKENS(name, _Relocation))
1423 APPLY_TO_RELOCATIONS(EACH_CASE);
1424 #undef EACH_CASE_AUX
1425 #undef EACH_CASE
1426
1427 inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {
1428 initialize(nm, begin, limit);
1429 }
1430
1431 #endif // SHARE_CODE_RELOCINFO_HPP