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.jnilibelf; 25 26 import java.nio.ByteOrder; 27 28 import jdk.tools.jaotc.jnilibelf.JNILibELFAPI.ELF; 29 30 /** 31 * Class that abstracts ELF target details. 32 * 33 */ 34 public class JNIELFTargetInfo { 35 /** 36 * ELF Class of the target. 37 */ 38 private static final int elfClass; 39 /** 40 * Target architecture. 41 */ 42 private static final int arch; 43 /** 44 * Architecture endian-ness. 45 */ 46 private static final int endian; 47 48 /** 49 * Target OS string. 50 */ 51 private static final String osName; 52 53 static { 54 // Find the target arch details 55 String archStr = System.getProperty("os.arch").toLowerCase(); 56 String datamodelStr = System.getProperty("sun.arch.data.model"); 57 58 if (datamodelStr.equals("32")) { 59 elfClass = ELF.ELFCLASS32; 60 } else if (datamodelStr.equals("64")) { 61 elfClass = ELF.ELFCLASS64; 62 } else { 63 System.out.println("Failed to discover ELF class!"); 64 elfClass = ELF.ELFCLASSNONE; 65 } 66 67 ByteOrder bo = ByteOrder.nativeOrder(); 68 if (bo == ByteOrder.LITTLE_ENDIAN) { 69 endian = ELF.ELFDATA2LSB; 70 } else if (bo == ByteOrder.BIG_ENDIAN) { 71 endian = ELF.ELFDATA2MSB; 72 } else { 73 System.out.println("Failed to discover endian-ness!"); 74 endian = ELF.ELFDATANONE; 75 } 76 77 if (archStr.equals("x86")) { 78 arch = ELF.EM_386; 79 } else if (archStr.equals("amd64") || archStr.equals("x86_64")) { 80 arch = ELF.EM_X64_64; 81 } else if (archStr.equals("sparcv9")) { 82 arch = ELF.EM_SPARCV9; 83 } else { 84 System.out.println("Unsupported architecture " + archStr); 85 arch = ELF.EM_NONE; 86 } 87 88 osName = System.getProperty("os.name").toLowerCase(); 89 } 90 91 public static int getELFArch() { 92 return arch; 93 } 94 95 public static int getELFClass() { 96 return elfClass; 97 } 98 99 public static int getELFEndian() { 100 return endian; 101 } 102 103 public static String getOsName() { 104 return osName; 105 } 106 107 public static int createReloca() { 108 switch (arch) { 109 case ELF.EM_X64_64: 110 return 1; 111 default: 112 return 0; 113 } 114 } 115 116 public static int sizeOfSymtabEntry() { 117 return JNILibELFAPI.size_of_Sym(elfClass); 118 } 119 120 public static int sizeOfRelocEntry() { 121 if (createReloca() == 1) { 122 return JNILibELFAPI.size_of_Rela(elfClass); 123 } else { 124 return JNILibELFAPI.size_of_Rel(elfClass); 125 } 126 } 127 }