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