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(); 72 for (Infopoint infopoint : compResult.getInfopoints()) { 73 if (infopoint.reason == InfopointReason.CALL) { 74 final Call callInfopoint = (Call) infopoint; 75 if (callInfopoint.target instanceof HotSpotForeignCallLinkage) { 76 // TODO 4 is x86 size of relative displacement. 77 // For SPARC need something different. 78 int destOffset = infopoint.pcOffset + callInfopoint.size - 4; 79 targetCode[destOffset + 0] = 0; 80 targetCode[destOffset + 1] = 0; 81 targetCode[destOffset + 2] = 0; 82 targetCode[destOffset + 3] = 0; 83 } 84 } 85 } 86 87 String entry = compMethod.getSymbolName(); 88 assert entry != null : "missing name for compiled method"; 89 90 // Align and pad method entry 91 CodeContainer codeSection = binaryContainer.getCodeContainer(); 92 int codeIdOffset = binaryContainer.alignUp(codeSection, binaryContainer.getCodeSegmentSize()); 93 // Store CodeId into code. It will be use by find_aot() using code.segments 94 methodInfo.setCodeId(); 95 binaryContainer.appendIntToCode(methodInfo.getCodeId()); 96 int textBaseOffset = binaryContainer.alignUp(codeSection, binaryContainer.getCodeEntryAlignment()); 97 98 codeSection.createSymbol(textBaseOffset, Symbol.Kind.JAVA_FUNCTION, Symbol.Binding.LOCAL, targetCodeSize, entry); 99 100 // Set the offset at which the text section of this method would be layed out 101 methodInfo.setTextSectionOffset(textBaseOffset); 102 103 // Write code bytes of the current method into byte stream 104 binaryContainer.appendCodeBytes(targetCode, 0, targetCodeSize); 105 int currentStubOffset = binaryContainer.alignUp(codeSection, 8); 106 // Set the offset at which stubs of this method would be laid out 107 methodInfo.setStubsOffset(currentStubOffset - textBaseOffset); 108 // step through all calls, for every call, add a stub 109 for (Infopoint infopoint : compResult.getInfopoints()) { 110 if (infopoint.reason == InfopointReason.CALL) { 111 final Call callInfopoint = (Call) infopoint; 112 if (callInfopoint.target instanceof ResolvedJavaMethod) { 113 ResolvedJavaMethod call = (ResolvedJavaMethod) callInfopoint.target; 114 StubInformation stub = addCallStub(MiscUtils.isVirtualCall(methodInfo, callInfopoint)); 115 // Get the targetSymbol. A symbol for this will be created later during plt 116 // creation 117 String targetSymbol = MiscUtils.uniqueMethodName(call) + ".at." + infopoint.pcOffset; 118 methodInfo.addStubCode(targetSymbol, stub); 119 currentStubOffset += stub.getSize(); 120 } 121 } 122 } 123 assert currentStubOffset == codeSection.getByteStreamSize() : "wrong offset"; 124 binaryContainer.addCodeSegments(codeIdOffset, currentStubOffset); 125 } 126 } 127 128 private StubInformation addCallStub(boolean isVirtualCall) { 129 final int startOffset = binaryContainer.getCodeContainer().getByteStreamSize(); 130 StubInformation stub = new StubInformation(startOffset, isVirtualCall); 131 ELFMacroAssembler masm = ELFMacroAssembler.getELFMacroAssembler(target); 132 byte[] code; 133 if (isVirtualCall) { 134 code = masm.getPLTVirtualEntryCode(stub); 135 } else { 136 code = masm.getPLTStaticEntryCode(stub); 137 } | 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 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.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 final 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(); 72 for (Infopoint infopoint : compResult.getInfopoints()) { 73 if (infopoint.reason == InfopointReason.CALL) { 74 final Call callInfopoint = (Call) infopoint; 75 if (callInfopoint.target instanceof HotSpotForeignCallLinkage) { 76 // TODO 4 is x86 size of relative displacement. 77 // For SPARC need something different. 78 int destOffset = infopoint.pcOffset + callInfopoint.size - 4; 79 targetCode[destOffset + 0] = 0; 80 targetCode[destOffset + 1] = 0; 81 targetCode[destOffset + 2] = 0; 82 targetCode[destOffset + 3] = 0; 83 } 84 } 85 } 86 87 String entry = compMethod.getSymbolName(); 88 assert entry != null : "missing name for compiled method"; 89 90 // Align and pad method entry 91 CodeContainer codeSection = binaryContainer.getCodeContainer(); 92 int codeIdOffset = BinaryContainer.alignUp(codeSection, binaryContainer.getCodeSegmentSize()); 93 // Store CodeId into code. It will be use by find_aot() using code.segments 94 methodInfo.setCodeId(); 95 binaryContainer.appendIntToCode(methodInfo.getCodeId()); 96 int textBaseOffset = BinaryContainer.alignUp(codeSection, binaryContainer.getCodeEntryAlignment()); 97 98 codeSection.createSymbol(textBaseOffset, Symbol.Kind.JAVA_FUNCTION, Symbol.Binding.LOCAL, targetCodeSize, entry); 99 100 // Set the offset at which the text section of this method would be layed out 101 methodInfo.setTextSectionOffset(textBaseOffset); 102 103 // Write code bytes of the current method into byte stream 104 binaryContainer.appendCodeBytes(targetCode, 0, targetCodeSize); 105 int currentStubOffset = BinaryContainer.alignUp(codeSection, 8); 106 // Set the offset at which stubs of this method would be laid out 107 methodInfo.setStubsOffset(currentStubOffset - textBaseOffset); 108 // step through all calls, for every call, add a stub 109 for (Infopoint infopoint : compResult.getInfopoints()) { 110 if (infopoint.reason == InfopointReason.CALL) { 111 final Call callInfopoint = (Call) infopoint; 112 if (callInfopoint.target instanceof ResolvedJavaMethod) { 113 ResolvedJavaMethod call = (ResolvedJavaMethod) callInfopoint.target; 114 StubInformation stub = addCallStub(CallInfo.isVirtualCall(methodInfo, callInfopoint)); 115 // Get the targetSymbol. A symbol for this will be created later during plt 116 // creation 117 String targetSymbol = JavaMethodInfo.uniqueMethodName(call) + ".at." + infopoint.pcOffset; 118 methodInfo.addStubCode(targetSymbol, stub); 119 currentStubOffset += stub.getSize(); 120 } 121 } 122 } 123 assert currentStubOffset == codeSection.getByteStreamSize() : "wrong offset"; 124 binaryContainer.addCodeSegments(codeIdOffset, currentStubOffset); 125 } 126 } 127 128 private StubInformation addCallStub(boolean isVirtualCall) { 129 final int startOffset = binaryContainer.getCodeContainer().getByteStreamSize(); 130 StubInformation stub = new StubInformation(startOffset, isVirtualCall); 131 ELFMacroAssembler masm = ELFMacroAssembler.getELFMacroAssembler(target); 132 byte[] code; 133 if (isVirtualCall) { 134 code = masm.getPLTVirtualEntryCode(stub); 135 } else { 136 code = masm.getPLTStaticEntryCode(stub); 137 } |