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