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