1 /*
2  * Copyright (c) 2016, 2018, 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.binformat.elf;
27 
28 import jdk.tools.jaotc.binformat.BinaryContainer;
29 import jdk.tools.jaotc.binformat.Relocation;
30 import jdk.tools.jaotc.binformat.Relocation.RelocType;
31 import jdk.tools.jaotc.binformat.Symbol;
32 import jdk.tools.jaotc.binformat.elf.Elf.Elf64_Ehdr;
33 import jdk.tools.jaotc.binformat.elf.Elf.Elf64_Rela;
34 
35 public class AMD64JELFRelocObject extends JELFRelocObject {
36 
AMD64JELFRelocObject(BinaryContainer binContainer, String outputFileName)37     AMD64JELFRelocObject(BinaryContainer binContainer, String outputFileName) {
38         super(binContainer, outputFileName);
39     }
40 
41     @Override
createRelocation(Symbol symbol, Relocation reloc, ElfRelocTable elfRelocTable)42     protected void createRelocation(Symbol symbol, Relocation reloc, ElfRelocTable elfRelocTable) {
43         RelocType relocType = reloc.getType();
44 
45         int elfRelocType = getELFRelocationType(relocType);
46         ElfSymbol sym = (ElfSymbol) symbol.getNativeSymbol();
47         int symno = sym.getIndex();
48         int sectindex = reloc.getSection().getSectionId();
49         int offset = reloc.getOffset();
50         int addend = 0;
51 
52         switch (relocType) {
53             case JAVA_CALL_DIRECT:
54             case STUB_CALL_DIRECT:
55             case FOREIGN_CALL_INDIRECT_GOT: {
56                 // Create relocation entry
57                 addend = -4; // Size in bytes of the patch location
58                 // Relocation should be applied at the location after call operand
59                 offset = offset + reloc.getSize() + addend;
60                 break;
61             }
62             case JAVA_CALL_INDIRECT:
63             case METASPACE_GOT_REFERENCE:
64             case EXTERNAL_PLT_TO_GOT: {
65                 addend = -4; // Size of 32-bit address of the GOT
66                 /*
67                  * Relocation should be applied before the test instruction to the move instruction.
68                  * reloc.getOffset() points to the test instruction after the instruction that loads
69                  * the address of polling page. So set the offset appropriately.
70                  */
71                 offset = offset + addend;
72                 break;
73             }
74             case EXTERNAL_GOT_TO_PLT: {
75                 // this is load time relocations
76                 break;
77             }
78             default:
79                 throw new InternalError("Unhandled relocation type: " + relocType);
80         }
81         elfRelocTable.createRelocationEntry(sectindex, offset, symno, elfRelocType, addend);
82     }
83 
getELFRelocationType(RelocType relocType)84     private static int getELFRelocationType(RelocType relocType) {
85         int elfRelocType = 0; // R_<ARCH>_NONE if #define'd to 0 for all values of ARCH
86         switch (ElfTargetInfo.getElfArch()) {
87             case Elf64_Ehdr.EM_X86_64:
88                 // Return R_X86_64_* entries based on relocType
89                 if (relocType == RelocType.JAVA_CALL_DIRECT ||
90                                 relocType == RelocType.FOREIGN_CALL_INDIRECT_GOT) {
91                     elfRelocType = Elf64_Rela.R_X86_64_PLT32;
92                 } else if (relocType == RelocType.STUB_CALL_DIRECT) {
93                     elfRelocType = Elf64_Rela.R_X86_64_PC32;
94                 } else if (relocType == RelocType.JAVA_CALL_INDIRECT) {
95                     elfRelocType = Elf64_Rela.R_X86_64_NONE;
96                 } else if (relocType == RelocType.METASPACE_GOT_REFERENCE ||
97                                 relocType == RelocType.EXTERNAL_PLT_TO_GOT) {
98                     elfRelocType = Elf64_Rela.R_X86_64_PC32;
99                 } else if (relocType == RelocType.EXTERNAL_GOT_TO_PLT) {
100                     elfRelocType = Elf64_Rela.R_X86_64_64;
101                 } else {
102                     assert false : "Unhandled relocation type: " + relocType;
103                 }
104                 break;
105 
106             default:
107                 System.out.println("Relocation Type mapping: Unhandled architecture: " + ElfTargetInfo.getElfArch());
108         }
109         return elfRelocType;
110     }
111 }
112