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.nio.ByteBuffer;
  27 import java.nio.ByteOrder;
  28 
  29 import jdk.tools.jaotc.binformat.BinaryContainer;
  30 import jdk.tools.jaotc.binformat.Relocation;
  31 import jdk.tools.jaotc.binformat.Relocation.RelocType;
  32 import jdk.tools.jaotc.binformat.Symbol;
  33 import jdk.tools.jaotc.binformat.Symbol.Binding;
  34 import jdk.tools.jaotc.binformat.Symbol.Kind;
  35 import jdk.tools.jaotc.AOTCompiledClass;
  36 import org.graalvm.compiler.code.DataSection;
  37 import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction;
  38 
  39 import jdk.vm.ci.code.TargetDescription;
  40 import jdk.vm.ci.code.site.ConstantReference;
  41 import jdk.vm.ci.code.site.DataPatch;
  42 import jdk.vm.ci.code.site.DataSectionReference;
  43 import jdk.vm.ci.code.site.Reference;
  44 import jdk.vm.ci.hotspot.HotSpotConstantPoolObject;
  45 import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
  46 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  47 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  48 import jdk.vm.ci.hotspot.HotSpotSentinelConstant;
  49 import jdk.vm.ci.meta.VMConstant;
  50 
  51 final class DataPatchProcessor {
  52 
  53     private final TargetDescription target;
  54 
  55     private final BinaryContainer binaryContainer;
  56 
  57     DataPatchProcessor(DataBuilder dataBuilder) {
  58         this.target = dataBuilder.getBackend().getTarget();
  59         this.binaryContainer = dataBuilder.getBinaryContainer();
  60     }
  61 
  62     /**
  63      * Process a {@link DataPatch} generated by the compiler and create all needed binary section
  64      * constructs.
  65      */
  66     void process(CompiledMethodInfo methodInfo, DataPatch dataPatch) {
  67         Reference reference = dataPatch.reference;
  68         if (reference instanceof ConstantReference) {
  69             processConstantReference(dataPatch, methodInfo);
  70         } else if (reference instanceof DataSectionReference) {
  71             processDataSectionReference(dataPatch, methodInfo);
  72         } else {
  73             throw new InternalError("Unknown data patch reference: " + reference);
  74         }
  75     }
  76 
  77     private void processConstantReference(DataPatch dataPatch, CompiledMethodInfo methodInfo) {
  78         HotSpotConstantLoadAction action = (HotSpotConstantLoadAction) dataPatch.note;
  79         ConstantReference constantReference = (ConstantReference) dataPatch.reference;
  80         assert action != null : "action should be set";
  81 
  82         VMConstant constant = constantReference.getConstant();
  83         String targetSymbol = null;
  84         String gotName = null;
  85         if (constant instanceof HotSpotMetaspaceConstant) {
  86             HotSpotMetaspaceConstant metaspaceConstant = (HotSpotMetaspaceConstant) constant;
  87             if (metaspaceConstant.asResolvedJavaType() != null) {
  88                 HotSpotResolvedObjectType type = metaspaceConstant.asResolvedJavaType();
  89                 methodInfo.addDependentKlassData(binaryContainer, type);
  90                 targetSymbol = AOTCompiledClass.metadataName(type);
  91                 gotName = ((action == HotSpotConstantLoadAction.INITIALIZE) ? "got.init." : "got.") + targetSymbol;
  92             } else if (metaspaceConstant.asResolvedJavaMethod() != null && action == HotSpotConstantLoadAction.LOAD_COUNTERS) {
  93                 targetSymbol = "counters." + JavaMethodInfo.uniqueMethodName(metaspaceConstant.asResolvedJavaMethod());
  94                 gotName = "got." + targetSymbol;
  95                 binaryContainer.addCountersSymbol(targetSymbol);
  96             }
  97         } else if (constant instanceof HotSpotObjectConstant) {
  98             HotSpotObjectConstant oopConstant = (HotSpotObjectConstant) constant;
  99             if (oopConstant instanceof HotSpotConstantPoolObject) {
 100                 HotSpotConstantPoolObject cpo = (HotSpotConstantPoolObject)oopConstant;
 101                 // Even if two locations use the same object, resolve separately
 102                 targetSymbol = "ldc." + cpo.getCpType().getName() + cpo.getCpi();
 103             } else {
 104                 // String constant.
 105                 targetSymbol = "ldc." + oopConstant.toValueString();
 106             }
 107             Integer offset = binaryContainer.addOopSymbol(targetSymbol);
 108             gotName = "got.ldc." + offset;
 109         } else if (constant instanceof HotSpotSentinelConstant) {
 110             targetSymbol = "state.M" + methodInfo.getCodeId();
 111             gotName = "got." + targetSymbol;
 112         }
 113 
 114         assert gotName != null : "Unknown constant type: " + constant;
 115 
 116         InstructionDecoder decoder = InstructionDecoder.getInstructionDecoder(target);
 117         decoder.decodePosition(methodInfo.getCompilationResult().getTargetCode(), dataPatch.pcOffset);
 118         int instructionEndOffset = decoder.currentEndOfInstruction();
 119 
 120         int textBaseOffset = methodInfo.getTextSectionOffset();
 121         int relocOffset = textBaseOffset + instructionEndOffset;
 122 
 123         Symbol relocationSymbol = binaryContainer.getSymbol(gotName);
 124         assert relocationSymbol != null : "symbol for " + gotName + " missing";
 125         Relocation reloc = new Relocation(relocOffset, RelocType.METASPACE_GOT_REFERENCE, 0, binaryContainer.getCodeContainer(), relocationSymbol);
 126         binaryContainer.addRelocation(reloc);
 127     }
 128 
 129     private void processDataSectionReference(DataPatch dataPatch, CompiledMethodInfo methodInfo) {
 130         DataSectionReference dataReference = (DataSectionReference) dataPatch.reference;
 131 
 132         InstructionDecoder decoder = InstructionDecoder.getInstructionDecoder(target);
 133         decoder.decodePosition(methodInfo.getCompilationResult().getTargetCode(), dataPatch.pcOffset);
 134         int instructionEndOffset = decoder.currentEndOfInstruction();
 135 
 136         int textBaseOffset = methodInfo.getTextSectionOffset();
 137         int relocOffset = textBaseOffset + instructionEndOffset;
 138         int dataOffset = dataReference.getOffset();
 139 
 140         DataSection dataSection = methodInfo.getCompilationResult().getDataSection();
 141         DataSection.Data data = dataSection.findData(dataReference);
 142         int size = data.getSize();
 143         int alignment = data.getAlignment();
 144         byte[] value = new byte[size];
 145         ByteBuffer buffer = ByteBuffer.wrap(value).order(ByteOrder.nativeOrder());
 146         DataSection.emit(buffer, data, p -> {
 147         });
 148         String targetSymbol = "data.M" + methodInfo.getCodeId() + "." + dataOffset;
 149         Symbol relocationSymbol = binaryContainer.getSymbol(targetSymbol);
 150         if (relocationSymbol == null) {
 151             int symSize = Math.max(8, size);
 152             int symAlig = Math.max(8, alignment);
 153             int offsetInConstantDataSection = binaryContainer.addConstantData(value, symAlig);
 154             relocationSymbol = binaryContainer.getConstantDataContainer().createSymbol(offsetInConstantDataSection, Kind.OBJECT, Binding.LOCAL, symSize, targetSymbol);
 155         }
 156         Relocation reloc = new Relocation(relocOffset, RelocType.METASPACE_GOT_REFERENCE, 0, binaryContainer.getCodeContainer(), relocationSymbol);
 157         binaryContainer.addRelocation(reloc);
 158     }
 159 
 160 }