1 /*
   2  * Copyright (c) 2016, 2018, 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 package jdk.tools.jaotc;
  25 
  26 import jdk.tools.jaotc.binformat.BinaryContainer;
  27 import jdk.tools.jaotc.binformat.Symbol;
  28 import jdk.tools.jaotc.StubInformation;
  29 
  30 import jdk.vm.ci.code.site.Call;
  31 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  32 
  33 /**
  34  * Symbol for a regular Java call. This method also creates additional relocations for {@code .plt}
  35  * to {@code .got} and {@code .got} to {@code .plt}.
  36  */
  37 final class JavaCallSiteRelocationSymbol extends CallSiteRelocationSymbol {
  38 
  39     private static final byte[] zeroSlot = new byte[8];
  40     // -1 represents Universe::non_oop_word() value
  41     private static final byte[] minusOneSlot;
  42 
  43     static {
  44         String archStr = System.getProperty("os.arch").toLowerCase();
  45         if (archStr.equals("aarch64")) {
  46             // AArch64 is a special case: it uses 48-bit addresses.
  47             byte[] non_oop_word = {-1, -1, -1, -1, -1, -1, 0, 0};
  48             minusOneSlot = non_oop_word;
  49         } else {
  50             byte[] non_oop_word = {-1, -1, -1, -1, -1, -1, -1, -1};
  51             minusOneSlot = non_oop_word;
  52         }
  53     }
  54 
  55     JavaCallSiteRelocationSymbol(CompiledMethodInfo mi, Call call, CallSiteRelocationInfo callSiteRelocation, BinaryContainer binaryContainer) {
  56         super(createPltEntrySymbol(binaryContainer, mi, call, callSiteRelocation));
  57         StubInformation stub = getStub(mi, call);
  58         addRelocations(mi, stub, binaryContainer, call, callSiteRelocation);
  59     }
  60 
  61     /**
  62      * Returns a unique symbol name with the {@code suffix} appended.
  63      */
  64     private static String relocationSymbolName(String suffix, CompiledMethodInfo mi, Call call, CallSiteRelocationInfo callSiteRelocation) {
  65         return "M" + mi.getCodeId() + "_" + call.pcOffset + "_" + callSiteRelocation.targetSymbol + "_" + suffix;
  66     }
  67 
  68     private static Symbol createPltEntrySymbol(BinaryContainer binaryContainer, CompiledMethodInfo mi, Call call, CallSiteRelocationInfo callSiteRelocation) {
  69         String symbolName = relocationSymbolName("plt.entry", mi, call, callSiteRelocation);
  70         StubInformation stub = getStub(mi, call);
  71         return createCodeContainerSymbol(binaryContainer, symbolName, stub.getOffset());
  72     }
  73 
  74     private static StubInformation getStub(CompiledMethodInfo mi, Call call) {
  75         HotSpotResolvedJavaMethod callTarget = (HotSpotResolvedJavaMethod) call.target;
  76         String callTargetSymbol = JavaMethodInfo.uniqueMethodName(callTarget) + ".at." + call.pcOffset;
  77         return mi.getStubFor(callTargetSymbol);
  78     }
  79 
  80     /**
  81      * Add all the required relocations.
  82      */
  83     private static void addRelocations(CompiledMethodInfo mi, StubInformation stub, BinaryContainer binaryContainer, Call call, CallSiteRelocationInfo callSiteRelocation) {
  84         final boolean isVirtualCall = CallInfo.isVirtualCall(mi, call);
  85 
  86         final int gotStartOffset = binaryContainer.appendExtLinkageGotBytes(zeroSlot, 0, zeroSlot.length);
  87         if (isVirtualCall) {
  88             // Nothing.
  89         } else {
  90             // For c2i stub we need slot with -1 value.
  91             binaryContainer.appendExtLinkageGotBytes(minusOneSlot, 0, minusOneSlot.length);
  92         }
  93 
  94         // Add relocation to GOT cell for call resolution jump.
  95         // This GOT cell will be initialized during JVM startup with address
  96         // of JVM runtime call resolution function.
  97         String gotSymbolName = "got." + getResolveSymbolName(mi, call);
  98         Symbol gotSymbol = binaryContainer.getGotSymbol(gotSymbolName);
  99         addExternalPltToGotRelocation(binaryContainer, gotSymbol, stub.getResolveJumpOffset());
 100 
 101         // Add relocation to resolve call jump instruction address for GOT cell.
 102         // This GOT cell will be initialized with address of resolution jump instruction and
 103         // will be updated with call destination address by JVM runtime call resolution code.
 104         String pltJmpSymbolName = relocationSymbolName("plt.jmp", mi, call, callSiteRelocation);
 105         addCodeContainerRelocation(binaryContainer, pltJmpSymbolName, stub.getResolveJumpStart(), gotStartOffset);
 106 
 107         // Add relocation to GOT cell for dispatch jump.
 108         // The dispatch jump loads destination address from this GOT cell.
 109         String gotEntrySymbolName = relocationSymbolName("got.entry", mi, call, callSiteRelocation);
 110         addExtLinkageGotContainerRelocation(binaryContainer, gotEntrySymbolName, gotStartOffset, stub.getDispatchJumpOffset());
 111 
 112         // Virtual call needs initial -1 value for Klass pointer.
 113         // Non virtual call needs initial 0 value for Method pointer to call c2i adapter.
 114         byte[] slot = isVirtualCall ? minusOneSlot : zeroSlot;
 115         final int gotMetaOffset = binaryContainer.appendExtLinkageGotBytes(slot, 0, slot.length);
 116 
 117         // Add relocation to GOT cell for move instruction (Klass* for virtual, Method* otherwise).
 118         String gotMoveSymbolName = relocationSymbolName("got.move", mi, call, callSiteRelocation);
 119         addExtLinkageGotContainerRelocation(binaryContainer, gotMoveSymbolName, gotMetaOffset, stub.getMovOffset());
 120 
 121         if (isVirtualCall) {
 122             // Nothing.
 123         } else {
 124             // Add relocation to GOT cell for c2i adapter jump.
 125             // The c2i jump instruction loads destination address from this GOT cell.
 126             // This GOT cell is initialized with -1 and will be updated
 127             // by JVM runtime call resolution code.
 128             String gotC2ISymbolName = relocationSymbolName("got.c2i", mi, call, callSiteRelocation);
 129             addExtLinkageGotContainerRelocation(binaryContainer, gotC2ISymbolName, gotStartOffset + 8, stub.getC2IJumpOffset());
 130         }
 131     }
 132 
 133     /**
 134      * Returns the name of the resolve method for this particular call.
 135      */
 136     private static String getResolveSymbolName(CompiledMethodInfo mi, Call call) {
 137         String resolveSymbolName;
 138         String name = call.target.toString();
 139         if (CallInfo.isStaticCall(call)) {
 140             assert mi.hasMark(call, MarkId.INVOKESTATIC);
 141             resolveSymbolName = BinaryContainer.getResolveStaticEntrySymbolName();
 142         } else if (CallInfo.isSpecialCall(call)) {
 143             resolveSymbolName = BinaryContainer.getResolveOptVirtualEntrySymbolName();
 144         } else if (CallInfo.isOptVirtualCall(mi, call)) {
 145             resolveSymbolName = BinaryContainer.getResolveOptVirtualEntrySymbolName();
 146         } else if (CallInfo.isVirtualCall(mi, call)) {
 147             resolveSymbolName = BinaryContainer.getResolveVirtualEntrySymbolName();
 148         } else {
 149             throw new InternalError("Unknown call type in " + mi.asTag() + " @ " + call.pcOffset + " for call" + call.target);
 150         }
 151         return resolveSymbolName;
 152     }
 153 
 154 }