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