1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 package jdk.tools.jaotc.binformat.elf;
  26 
  27 /**
  28  *
  29  * Support for the creation of Elf Object files. Current support is limited to 64 bit x86_64.
  30  *
  31  */
  32 
  33 final class Elf {
  34     //@formatter:off
  35     /**
  36      * Elf64_Ehdr structure defines
  37      */
  38     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         final int off;
  55         final int sz;
  56 
  57         Elf64_Ehdr(int offset, int size) {
  58             this.off = offset;
  59             this.sz = size;
  60         }
  61 
  62         static int totalsize = 64;
  63 
  64         /**
  65          * Elf64_Ehdr defines
  66          */
  67 
  68         /**
  69          * e_ident
  70          */
  71         static final int  EI_MAG0        = 0;
  72         static final byte ELFMAG0        = 0x7f;
  73         static final int  EI_MAG1        = 1;
  74         static final byte ELFMAG1        = 0x45;
  75         static final int  EI_MAG2        = 2;
  76         static final byte ELFMAG2        = 0x4c;
  77         static final int  EI_MAG3        = 3;
  78         static final byte ELFMAG3        = 0x46;
  79         static final int  EI_CLASS       = 4;
  80         static final byte ELFCLASS64     = 0x2;
  81 
  82         static final int  EI_DATA        = 5;
  83         static final byte ELFDATA2LSB    = 0x1;
  84 
  85         static final int  EI_VERSION     = 6;
  86         static final byte EV_CURRENT     = 0x1;
  87 
  88         static final int  EI_OSABI       = 7;
  89         static final byte ELFOSABI_NONE  = 0x0;
  90 
  91         /**
  92          * e_type
  93          */
  94         static final char ET_REL         = 0x1;
  95 
  96         /**
  97          * e_machine
  98          */
  99         static final char EM_NONE        = 0;
 100         static final char EM_X86_64      = 62;
 101         static final char EM_AARCH64     = 183;
 102 
 103     }
 104 
 105     /**
 106      * Elf64_Shdr structure defines
 107      */
 108     enum Elf64_Shdr {
 109                sh_name( 0, 4),
 110                sh_type( 4, 4),
 111               sh_flags( 8, 8),
 112                sh_addr(16, 8),
 113              sh_offset(24, 8),
 114                sh_size(32, 8),
 115                sh_link(40, 4),
 116                sh_info(44, 4),
 117           sh_addralign(48, 8),
 118             sh_entsize(56, 8);
 119 
 120         final int off;
 121         final int sz;
 122 
 123         Elf64_Shdr(int offset, int size) {
 124             this.off = offset;
 125             this.sz = size;
 126         }
 127 
 128         static int totalsize = 64;
 129 
 130         /**
 131          * Elf64_Shdr defines
 132          */
 133 
 134         /**
 135          * sh_type
 136          */
 137         static final int SHT_PROGBITS   = 0x1;
 138         static final int SHT_SYMTAB     = 0x2;
 139         static final int SHT_STRTAB     = 0x3;
 140         static final int SHT_RELA       = 0x4;
 141         static final int SHT_NOBITS     = 0x8;
 142         static final int SHT_REL        = 0x9;
 143 
 144         static final byte SHN_UNDEF     = 0x0;
 145 
 146         /**
 147          * sh_flag
 148          */
 149         static final int SHF_WRITE      = 0x1;
 150         static final int SHF_ALLOC      = 0x2;
 151         static final int SHF_EXECINSTR  = 0x4;
 152 
 153     }
 154 
 155     /**
 156      * Symbol table entry definitions
 157      *
 158      * Elf64_Sym structure defines
 159      */
 160     enum Elf64_Sym {
 161                st_name( 0, 4),
 162                st_info( 4, 1),
 163               st_other( 5, 1),
 164               st_shndx( 6, 2),
 165               st_value( 8, 8),
 166                st_size(16, 8);
 167 
 168         final int off;
 169         final int sz;
 170 
 171         Elf64_Sym(int offset, int size) {
 172             this.off = offset;
 173             this.sz = size;
 174         }
 175 
 176         static int totalsize = 24;
 177 
 178         /* ST_BIND is in bits 4-7 of st_info.  ST_TYPE is in low 4 bits */
 179         static final byte STB_LOCAL   = 0x0;
 180         static final byte STB_GLOBAL  = 0x1;
 181 
 182         static final byte STT_NOTYPE  = 0x0;
 183         static final byte STT_OBJECT  = 0x1;
 184         static final byte STT_FUNC    = 0x2;
 185 
 186         static byte ELF64_ST_INFO(byte bind, byte type) {
 187             return (byte)(((bind) << 4) + ((type) & 0xf));
 188         }
 189 
 190     }
 191 
 192     /**
 193      * Elf64_Rel structure defines
 194      */
 195     enum Elf64_Rel {
 196               r_offset( 0, 8),
 197                 r_info( 8, 8);
 198 
 199         final int off;
 200         final int sz;
 201 
 202         Elf64_Rel(int offset, int size) {
 203             this.off = offset;
 204             this.sz = size;
 205         }
 206 
 207         static int totalsize = 16;
 208 
 209         /**
 210          * Relocation types
 211          */
 212 
 213         static final int R_X86_64_NONE     = 0x0;
 214         static final int R_X86_64_64       = 0x1;
 215         static final int R_X86_64_PC32     = 0x2;
 216         static final int R_X86_64_PLT32    = 0x4;
 217         static final int R_X86_64_GOTPCREL = 0x9;
 218 
 219         static final int R_AARCH64_NONE     = 256;
 220         static final int R_AARCH64_ABS64    = 257;
 221         static final int R_AARCH64_CALL26   = 283;
 222         static final int R_AARCH64_ADR_GOT_PAGE = 311;
 223         static final int R_AARCH64_LD64_GOT_LO12_NC = 312;
 224 
 225         static final int R_AARCH64_MOVW_UABS_G0_NC = 264;
 226         static final int R_AARCH64_MOVW_UABS_G1_NC = 266;
 227         static final int R_AARCH64_MOVW_UABS_G2_NC = 268;
 228 
 229         static final int R_AARCH64_ADR_PREL_PG_HI21 = 275;
 230         static final int R_AARCH64_ADD_ABS_LO12_NC = 277;
 231         static final int R_AARCH64_LDST64_ABS_LO12_NC = 286;
 232     }
 233 
 234     /**
 235      * Elf64_Rela structure defines
 236      */
 237     enum Elf64_Rela {
 238               r_offset( 0, 8),
 239                 r_info( 8, 8),
 240               r_addend(16, 8);
 241 
 242         final int off;
 243         final int sz;
 244 
 245         Elf64_Rela(int offset, int size) {
 246             this.off = offset;
 247             this.sz = size;
 248         }
 249 
 250         static int totalsize = 24;
 251 
 252         static final int R_X86_64_NONE     = 0x0;
 253         static final int R_X86_64_64       = 0x1;
 254         static final int R_X86_64_PC32     = 0x2;
 255         static final int R_X86_64_PLT32    = 0x4;
 256         static final int R_X86_64_GOTPCREL = 0x9;
 257 
 258         static final int R_AARCH64_NONE     = 256;
 259         static final int R_AARCH64_ABS64    = 257;
 260         static final int R_AARCH64_CALL26   = 283;
 261         static final int R_AARCH64_ADR_GOT_PAGE = 311;
 262         static final int R_AARCH64_LD64_GOT_LO12_NC = 312;
 263 
 264         static final int R_AARCH64_MOVW_UABS_G0_NC = 264;
 265         static final int R_AARCH64_MOVW_UABS_G1_NC = 266;
 266         static final int R_AARCH64_MOVW_UABS_G2_NC = 268;
 267 
 268         static final int R_AARCH64_ADR_PREL_PG_HI21 = 275;
 269         static final int R_AARCH64_ADD_ABS_LO12_NC = 277;
 270         static final int R_AARCH64_LDST64_ABS_LO12_NC = 286;
 271 
 272         static long ELF64_R_INFO(int symidx, int type) {
 273             return (((long)symidx << 32) + type);
 274         }
 275 
 276     }
 277     //@formatter:on
 278 }