1 /*
   2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.inline.hpp"
  27 #include "assembler_arm.inline.hpp"
  28 #include "code/relocInfo.hpp"
  29 #include "nativeInst_arm.hpp"
  30 #include "compressedOops.inline.hpp"
  31 #include "oops/oop.hpp"
  32 #include "runtime/safepoint.hpp"
  33 
  34 void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) {
  35 
  36   NativeMovConstReg* ni = nativeMovConstReg_at(addr());
  37 #if defined(AARCH64) && defined(COMPILER2)
  38   if (ni->is_movz()) {
  39     assert(type() == relocInfo::oop_type, "!");
  40     if (verify_only) {
  41       uintptr_t d = ni->data();
  42       guarantee((d >> 32) == 0, "not narrow oop");
  43       narrowOop no = d;
  44       oop o = CompressedOops::decode(no);
  45       guarantee(cast_from_oop<intptr_t>(o) == (intptr_t)x, "instructions must match");
  46     } else {
  47       ni->set_data((intptr_t)x);
  48     }
  49     return;
  50   }
  51 #endif
  52   if (verify_only) {
  53     guarantee(ni->data() == (intptr_t)(x + o), "instructions must match");
  54   } else {
  55     ni->set_data((intptr_t)(x + o));
  56   }
  57 }
  58 
  59 address Relocation::pd_call_destination(address orig_addr) {
  60   address pc = addr();
  61 
  62   int adj = 0;
  63   if (orig_addr != NULL) {
  64     // We just moved this call instruction from orig_addr to addr().
  65     // This means that, when relative, its target will appear to have grown by addr() - orig_addr.
  66     adj = orig_addr - pc;
  67   }
  68 
  69   RawNativeInstruction* ni = rawNativeInstruction_at(pc);
  70 
  71 #if (!defined(AARCH64))
  72   if (NOT_AARCH64(ni->is_add_lr()) AARCH64_ONLY(ni->is_adr_aligned_lr())) {
  73     // On arm32, skip the optional 'add LR, PC, #offset'
  74     // (Allowing the jump support code to handle fat_call)
  75     pc = ni->next_raw_instruction_address();
  76     ni = nativeInstruction_at(pc);
  77   }
  78 #endif
  79 
  80   if (AARCH64_ONLY(ni->is_call()) NOT_AARCH64(ni->is_bl())) {
  81     // For arm32, fat_call are handled by is_jump for the new 'ni',
  82     // requiring only to support is_bl.
  83     //
  84     // For AARCH64, skipping a leading adr is not sufficient
  85     // to reduce calls to a simple bl.
  86     return rawNativeCall_at(pc)->destination(adj);
  87   }
  88 
  89   if (ni->is_jump()) {
  90     return rawNativeJump_at(pc)->jump_destination(adj);
  91   }
  92   ShouldNotReachHere();
  93   return NULL;
  94 }
  95 
  96 void Relocation::pd_set_call_destination(address x) {
  97   address pc = addr();
  98   NativeInstruction* ni = nativeInstruction_at(pc);
  99 
 100 #if (!defined(AARCH64))
 101   if (NOT_AARCH64(ni->is_add_lr()) AARCH64_ONLY(ni->is_adr_aligned_lr())) {
 102     // On arm32, skip the optional 'add LR, PC, #offset'
 103     // (Allowing the jump support code to handle fat_call)
 104     pc = ni->next_raw_instruction_address();
 105     ni = nativeInstruction_at(pc);
 106   }
 107 #endif
 108 
 109   if (AARCH64_ONLY(ni->is_call()) NOT_AARCH64(ni->is_bl())) {
 110     // For arm32, fat_call are handled by is_jump for the new 'ni',
 111     // requiring only to support is_bl.
 112     //
 113     // For AARCH64, skipping a leading adr is not sufficient
 114     // to reduce calls to a simple bl.
 115     rawNativeCall_at(pc)->set_destination(x);
 116     return;
 117   }
 118 
 119   if (ni->is_jump()) { // raw jump
 120     rawNativeJump_at(pc)->set_jump_destination(x);
 121     return;
 122   }
 123   ShouldNotReachHere();
 124 }
 125 
 126 
 127 address* Relocation::pd_address_in_code() {
 128   return (address*)addr();
 129 }
 130 
 131 address Relocation::pd_get_address_from_code() {
 132   return *pd_address_in_code();
 133 }
 134 
 135 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 136 }
 137 
 138 void metadata_Relocation::pd_fix_value(address x) {
 139   assert(! addr_in_const(), "Do not use");
 140 #ifdef AARCH64
 141 #ifdef COMPILER2
 142   NativeMovConstReg* ni = nativeMovConstReg_at(addr());
 143   if (ni->is_mov_slow()) {
 144     return;
 145   }
 146 #endif
 147   set_value(x);
 148 #else
 149   if (!VM_Version::supports_movw()) {
 150     set_value(x);
 151 #ifdef ASSERT
 152   } else {
 153     // the movw/movt data should be correct
 154     NativeMovConstReg* ni = nativeMovConstReg_at(addr());
 155     assert(ni->is_movw(), "not a movw");
 156     // The following assert should be correct but the shared code
 157     // currently 'fixes' the metadata instructions before the
 158     // metadata_table is copied in the new method (see
 159     // JDK-8042845). This means that 'x' (which comes from the table)
 160     // does not match the value inlined in the code (which is
 161     // correct). Failure can be temporarily ignored since the code is
 162     // correct and the table is copied shortly afterward.
 163     //
 164     // assert(ni->data() == (int)x, "metadata relocation mismatch");
 165 #endif
 166   }
 167 #endif // !AARCH64
 168 }