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