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
24package jdk.tools.jaotc;
25
26import jdk.tools.jaotc.binformat.BinaryContainer;
27import jdk.tools.jaotc.binformat.Symbol;
28
29import jdk.vm.ci.code.site.Call;
30
31final class ForeignGotCallSiteRelocationSymbol extends CallSiteRelocationSymbol {
32
33    public ForeignGotCallSiteRelocationSymbol(CompiledMethodInfo mi, Call call, CallSiteRelocationInfo callSiteRelocation, DataBuilder dataBuilder) {
34        super(createPltSymbol(dataBuilder, mi, call, callSiteRelocation));
35    }
36
37    private static Symbol createPltSymbol(DataBuilder dataBuilder, CompiledMethodInfo mi, Call call, CallSiteRelocationInfo callSiteRelocation) {
38        BinaryContainer binaryContainer = dataBuilder.getBinaryContainer();
39        String vmSymbolName = callSiteRelocation.targetSymbol;
40
41        // Add relocation to GOT cell for call resolution jump.
42        String pltSymbolName = "plt." + vmSymbolName;
43        Symbol pltSymbol = binaryContainer.getSymbol(pltSymbolName);
44
45        if (pltSymbol == null) {
46            String gotSymbolName = "got." + vmSymbolName;
47            Symbol gotSymbol = binaryContainer.getGotSymbol(gotSymbolName);
48            assert gotSymbol != null : "undefined VM got symbol '" + gotSymbolName + "' for call at " + call.pcOffset + " in " + mi.getMethodInfo().getSymbolName();
49
50            // Generate PLT jump (do it only once).
51            final int pltStartOffset = binaryContainer.getCodeContainer().getByteStreamSize();
52            final int pltEndOffset = pltStartOffset + addPltJump(dataBuilder);
53
54            // Link GOT cell to PLT jump.
55            pltSymbol = createCodeContainerSymbol(binaryContainer, pltSymbolName, pltStartOffset);
56            addExternalPltToGotRelocation(binaryContainer, gotSymbol, pltEndOffset);
57        }
58
59        return pltSymbol;
60    }
61
62    private static int addPltJump(DataBuilder dataBuilder) {
63        ELFMacroAssembler masm = ELFMacroAssembler.getELFMacroAssembler(dataBuilder.getBackend().getTarget());
64        byte[] code = masm.getPLTJumpCode(); // It includes alignment nops.
65        int size = masm.currentEndOfInstruction();
66        dataBuilder.getBinaryContainer().appendCodeBytes(code, 0, code.length);
67        return size;
68    }
69
70}
71