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