1 /*
   2  * Copyright (c) 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 package jdk.tools.jaotc;
  25 
  26 import java.util.ArrayList;
  27 
  28 import jdk.tools.jaotc.binformat.BinaryContainer;
  29 import jdk.tools.jaotc.binformat.CodeContainer;
  30 import jdk.tools.jaotc.binformat.Symbol;
  31 import jdk.tools.jaotc.CompiledMethodInfo.StubInformation;
  32 import org.graalvm.compiler.code.CompilationResult;
  33 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
  34 
  35 import jdk.vm.ci.code.TargetDescription;
  36 import jdk.vm.ci.code.site.Call;
  37 import jdk.vm.ci.code.site.Infopoint;
  38 import jdk.vm.ci.code.site.InfopointReason;
  39 import jdk.vm.ci.meta.ResolvedJavaMethod;
  40 
  41 class CodeSectionProcessor {
  42 
  43     private final TargetDescription target;
  44 
  45     private final BinaryContainer binaryContainer;
  46 
  47     CodeSectionProcessor(DataBuilder dataBuilder) {
  48         this.target = dataBuilder.getBackend().getTarget();
  49         this.binaryContainer = dataBuilder.getBinaryContainer();
  50     }
  51 
  52     /**
  53      * Method that looks at code section of a compiled result {@code compClass} and records function
  54      * entry point symbols along with the text section contents. Note that the text section contents
  55      * are not yet ready to be written in the form of a binary text section since the contents may
  56      * need to be patched with references to other sections.
  57      *
  58      * @param compClass Graal compilation result.
  59      */
  60     void process(AOTCompiledClass compClass) {
  61         ArrayList<CompiledMethodInfo> compiledMethods = compClass.getCompiledMethods();
  62 
  63         for (CompiledMethodInfo methodInfo : compiledMethods) {
  64             CompilationResult compResult = methodInfo.getCompilationResult();
  65 
  66             byte[] targetCode = compResult.getTargetCode();
  67             int targetCodeSize = compResult.getTargetCodeSize();
  68             JavaMethodInfo compMethod = methodInfo.getMethodInfo();
  69 
  70             // Step through all foreign calls, for every call, clear destination.
  71             for (Infopoint infopoint : compResult.getInfopoints()) {
  72                 if (infopoint.reason == InfopointReason.CALL) {
  73                     final Call callInfopoint = (Call) infopoint;
  74                     if (callInfopoint.target instanceof HotSpotForeignCallLinkage) {
  75                         // TODO 4 is x86 size of relative displacement.
  76                         // For SPARC need something different.
  77                         int destOffset = infopoint.pcOffset + callInfopoint.size - 4;
  78                         targetCode[destOffset + 0] = 0;
  79                         targetCode[destOffset + 1] = 0;
  80                         targetCode[destOffset + 2] = 0;
  81                         targetCode[destOffset + 3] = 0;
  82                     }
  83                 }
  84             }
  85 
  86             String entry = compMethod.getSymbolName();
  87             assert entry != null : "missing name for compiled method";
  88 
  89             // Align and pad method entry
  90             CodeContainer codeSection = binaryContainer.getCodeContainer();
  91             int codeIdOffset = binaryContainer.alignUp(codeSection, binaryContainer.getCodeSegmentSize());
  92             // Store CodeId into code. It will be use by find_aot() using code.segments
  93             methodInfo.setCodeId();
  94             binaryContainer.appendIntToCode(methodInfo.getCodeId());
  95             int textBaseOffset = binaryContainer.alignUp(codeSection, binaryContainer.getCodeEntryAlignment());
  96 
  97             codeSection.createSymbol(textBaseOffset, Symbol.Kind.JAVA_FUNCTION, Symbol.Binding.LOCAL, targetCodeSize, entry);
  98 
  99             // Set the offset at which the text section of this method would be layed out
 100             methodInfo.setTextSectionOffset(textBaseOffset);
 101 
 102             // Write code bytes of the current method into byte stream
 103             binaryContainer.appendCodeBytes(targetCode, 0, targetCodeSize);
 104             int currentStubOffset = binaryContainer.alignUp(codeSection, 8);
 105             // Set the offset at which stubs of this method would be laid out
 106             methodInfo.setStubsOffset(currentStubOffset - textBaseOffset);
 107             // step through all calls, for every call, add a stub
 108             for (Infopoint infopoint : compResult.getInfopoints()) {
 109                 if (infopoint.reason == InfopointReason.CALL) {
 110                     final Call callInfopoint = (Call) infopoint;
 111                     if (callInfopoint.target instanceof ResolvedJavaMethod) {
 112                         ResolvedJavaMethod call = (ResolvedJavaMethod) callInfopoint.target;
 113                         StubInformation stub = addCallStub(MiscUtils.isVirtualCall(methodInfo, callInfopoint));
 114                         // Get the targetSymbol. A symbol for this will be created later during plt
 115                         // creation
 116                         String targetSymbol = MiscUtils.uniqueMethodName(call) + ".at." + infopoint.pcOffset;
 117                         methodInfo.addStubCode(targetSymbol, stub);
 118                         currentStubOffset += stub.getSize();
 119                     }
 120                 }
 121             }
 122             assert currentStubOffset == codeSection.getByteStreamSize() : "wrong offset";
 123             binaryContainer.addCodeSegments(codeIdOffset, currentStubOffset);
 124         }
 125     }
 126 
 127     private StubInformation addCallStub(boolean isVirtualCall) {
 128         final int startOffset = binaryContainer.getCodeContainer().getByteStreamSize();
 129         StubInformation stub = new StubInformation(startOffset, isVirtualCall);
 130         ELFMacroAssembler masm = ELFMacroAssembler.getELFMacroAssembler(target);
 131         byte[] code;
 132         if (isVirtualCall) {
 133             code = masm.getPLTVirtualEntryCode(stub);
 134         } else {
 135             code = masm.getPLTStaticEntryCode(stub);
 136         }
 137         binaryContainer.appendCodeBytes(code, 0, code.length);
 138         return stub;
 139     }
 140 
 141 }