1 /* 2 * Copyright (c) 2016, 2017, 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 /** 27 * 28 * Support for the creation of Elf Object files. 29 * Current support is limited to 64 bit x86_64. 30 * 31 */ 32 33 public class Elf { 34 35 /** 36 * Elf64_Ehdr structure defines 37 */ 38 public enum Elf64_Ehdr { 39 e_ident( 0,16), 40 e_type(16, 2), 41 e_machine(18, 2), 42 e_version(20, 4), 43 e_entry(24, 8), 44 e_phoff(32, 8), 45 e_shoff(40, 8), 46 e_flags(48, 4), 47 e_ehsize(52, 2), 48 e_phentsize(54, 2), 49 e_phnum(56, 2), 50 e_shentsize(58, 2), 51 e_shnum(60, 2), 52 e_shstrndx(62, 2); 53 54 public final int off; 55 public final int sz; 56 57 Elf64_Ehdr(int offset, int size) { 58 this.off = offset; 59 this.sz = size; 60 } 61 62 public static int totalsize = 64; 63 64 /** 65 * Elf64_Ehdr defines 66 */ 67 68 /** 69 * e_ident 70 */ 71 public static final int EI_MAG0 = 0; 72 public static final byte ELFMAG0 = 0x7f; 73 public static final int EI_MAG1 = 1; 74 public static final byte ELFMAG1 = 0x45; 75 public static final int EI_MAG2 = 2; 76 public static final byte ELFMAG2 = 0x4c; 77 public static final int EI_MAG3 = 3; 78 public static final byte ELFMAG3 = 0x46; 79 80 public static final int EI_CLASS = 4; 81 public static final byte ELFCLASS64 = 0x2; 82 83 public static final int EI_DATA = 5; 84 public static final byte ELFDATA2LSB = 0x1; 85 86 public static final int EI_VERSION = 6; 87 public static final byte EV_CURRENT = 0x1; 88 89 public static final int EI_OSABI = 7; 90 public static final byte ELFOSABI_NONE = 0x0; 91 92 /** 93 * e_type 94 */ 95 public static final char ET_REL = 0x1; 96 97 /** 98 * e_machine 99 */ 100 public static final char EM_NONE = 0; 101 public static final char EM_X86_64 = 62; 102 public static final char EM_AARCH64 = 183; 103 104 /** 105 * e_version 106 */ 107 // public static final int EV_CURRENT = 1; 108 109 } 110 111 /** 112 * Elf64_Shdr structure defines 113 */ 114 public enum Elf64_Shdr { 115 sh_name( 0, 4), 116 sh_type( 4, 4), 117 sh_flags( 8, 8), 118 sh_addr(16, 8), 119 sh_offset(24, 8), 120 sh_size(32, 8), 121 sh_link(40, 4), 122 sh_info(44, 4), 123 sh_addralign(48, 8), 124 sh_entsize(56, 8); 125 126 public final int off; 127 public final int sz; 128 129 Elf64_Shdr(int offset, int size) { 130 this.off = offset; 131 this.sz = size; 132 } 133 134 public static int totalsize = 64; 135 136 /** 137 * Elf64_Shdr defines 138 */ 139 140 /** 141 * sh_type 142 */ 143 public static final int SHT_PROGBITS = 0x1; 144 public static final int SHT_SYMTAB = 0x2; 145 public static final int SHT_STRTAB = 0x3; 146 public static final int SHT_RELA = 0x4; 147 public static final int SHT_NOBITS = 0x8; 148 public static final int SHT_REL = 0x9; 149 150 public static final byte SHN_UNDEF = 0x0; 151 152 /** 153 * sh_flag 154 */ 155 public static final int SHF_WRITE = 0x1; 156 public static final int SHF_ALLOC = 0x2; 157 public static final int SHF_EXECINSTR = 0x4; 158 159 } 160 161 /** 162 * Symbol table entry definitions 163 * 164 * Elf64_Sym structure defines 165 */ 166 public enum Elf64_Sym { 167 st_name( 0, 4), 168 st_info( 4, 1), 169 st_other( 5, 1), 170 st_shndx( 6, 2), 171 st_value( 8, 8), 172 st_size(16, 8); 173 174 public final int off; 175 public final int sz; 176 177 Elf64_Sym(int offset, int size) { 178 this.off = offset; 179 this.sz = size; 180 } 181 182 public static int totalsize = 24; 183 184 /* ST_BIND is in bits 4-7 of st_info. ST_TYPE is in low 4 bits */ 185 public static final byte STB_LOCAL = 0x0; 186 public static final byte STB_GLOBAL = 0x1; 187 188 public static final byte STT_NOTYPE = 0x0; 189 public static final byte STT_OBJECT = 0x1; 190 public static final byte STT_FUNC = 0x2; 191 192 public static byte ELF64_ST_INFO(byte bind, byte type) { 193 return (byte)(((bind) << 4) + ((type) & 0xf)); 194 } 195 196 } 197 198 /** 199 * Elf64_Rel structure defines 200 */ 201 public enum Elf64_Rel { 202 r_offset( 0, 8), 203 r_info( 8, 8); 204 205 public final int off; 206 public final int sz; 207 208 Elf64_Rel(int offset, int size) { 209 this.off = offset; 210 this.sz = size; 211 } 212 213 public static int totalsize = 16; 214 215 /** 216 * Relocation types 217 */ 218 public static final int R_X86_64_NONE = 0x0; 219 public static final int R_X86_64_64 = 0x1; 220 public static final int R_X86_64_PC32 = 0x2; 221 public static final int R_X86_64_PLT32 = 0x4; 222 public static final int R_X86_64_GOTPCREL = 0x9; 223 224 } 225 226 /** 227 * Elf64_Rela structure defines 228 */ 229 public enum Elf64_Rela { 230 r_offset( 0, 8), 231 r_info( 8, 8), 232 r_addend(16, 8); 233 234 public final int off; 235 public final int sz; 236 237 Elf64_Rela(int offset, int size) { 238 this.off = offset; 239 this.sz = size; 240 } 241 242 public static int totalsize = 24; 243 244 public static final int R_X86_64_NONE = 0x0; 245 public static final int R_X86_64_64 = 0x1; 246 public static final int R_X86_64_PC32 = 0x2; 247 public static final int R_X86_64_PLT32 = 0x4; 248 public static final int R_X86_64_GOTPCREL = 0x9; 249 250 public static long ELF64_R_INFO(int symidx, int type) { 251 return (((long)symidx << 32) + ((long)type)); 252 } 253 254 } 255 256 }