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