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