1 /*
  2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "asm/macroAssembler.hpp"
 28 #include "code/nmethod.hpp"
 29 #include "code/relocInfo.hpp"
 30 #include "nativeInst_aarch64.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "runtime/safepoint.hpp"
 33 
 34 
 35 void Relocation::pd_set_data_value(address x, bool verify_only) {
 36   if (verify_only)
 37     return;
 38 
 39   int bytes;
 40 
 41   switch(type()) {
 42   case relocInfo::oop_type:
 43     {
 44       oop_Relocation *reloc = (oop_Relocation *)this;
 45       if (NativeInstruction::is_ldr_literal_at(addr())) {
 46         address constptr = (address)code()->oop_addr_at(reloc->oop_index());
 47         bytes = MacroAssembler::pd_patch_instruction_size(addr(), constptr);
 48         assert(*(address*)constptr == x, "error in oop relocation");
 49       } else{
 50         bytes = MacroAssembler::patch_oop(addr(), x);
 51       }
 52     }
 53     break;
 54   default:
 55     bytes = MacroAssembler::pd_patch_instruction_size(addr(), x);
 56     break;
 57   }
 58   ICache::invalidate_range(addr(), bytes);
 59 }
 60 
 61 address Relocation::pd_call_destination(address orig_addr) {
 62   assert(is_call(), "should be a call here");
 63   if (orig_addr == nullptr) {
 64     if (NativeCall::is_call_at(addr())) {
 65       NativeCall* call = nativeCall_at(addr());
 66       return call->destination();
 67     }
 68   } else {
 69     address new_addr = MacroAssembler::pd_call_destination(orig_addr);
 70     // If call is branch to self, don't try to relocate it, just leave it
 71     // as branch to self. This happens during code generation if the code
 72     // buffer expands. It will be relocated to the trampoline above once
 73     // code generation is complete.
 74     new_addr = (new_addr == orig_addr) ? addr() : new_addr;
 75     return new_addr;
 76   }
 77   return MacroAssembler::pd_call_destination(addr());
 78 }
 79 
 80 
 81 void Relocation::pd_set_call_destination(address x) {
 82   assert(is_call(), "should be a call here");
 83   if (NativeCall::is_call_at(addr())) {
 84     NativeCall* call = nativeCall_at(addr());
 85     call->set_destination(x);
 86   } else {
 87     MacroAssembler::pd_patch_instruction(addr(), x);
 88   }
 89   assert(pd_call_destination(addr()) == x, "fail in reloc");
 90 }
 91 
 92 void trampoline_stub_Relocation::pd_fix_owner_after_move() {
 93   NativeCall* call = nativeCall_at(owner());
 94   assert(call->raw_destination() == owner(), "destination should be empty");
 95   address trampoline = addr();
 96   address dest = nativeCallTrampolineStub_at(trampoline)->destination();
 97   if (!Assembler::reachable_from_branch_at(owner(), dest)) {
 98     dest = trampoline;
 99   }
100   call->set_destination(dest);
101 }
102 
103 
104 address* Relocation::pd_address_in_code() {
105   return (address*)(addr() + 8);
106 }
107 
108 
109 address Relocation::pd_get_address_from_code() {
110   return MacroAssembler::pd_call_destination(addr());
111 }
112 
113 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
114   if (NativeInstruction::maybe_cpool_ref(addr())) {
115     address old_addr = old_addr_for(addr(), src, dest);
116     MacroAssembler::pd_patch_instruction(addr(), MacroAssembler::target_addr_for_insn(old_addr));
117   }
118 }
119 
120 void metadata_Relocation::pd_fix_value(address x) {
121 }