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 public class JNILibELFAPI {
  27 
  28     static {
  29         System.loadLibrary("jelfshim");
  30     }
  31 
  32     /**
  33      * Definitions for file open.
  34      */
  35     public static enum OpenFlags {
  36         O_RDONLY(0x0),
  37         O_WRONLY(0x1),
  38         O_RDWR(0x2),
  39         O_CREAT(0x40);
  40 
  41         private final int intVal;
  42 
  43         private OpenFlags(int v) {
  44             intVal = v;
  45         }
  46 
  47         public int intValue() {
  48             return intVal;
  49         }
  50     }
  51 
  52     /**
  53      * Definitions reflecting those in elf.h.
  54      *
  55      */
  56     public interface ELF {
  57         int EI_NIDENT = 16;
  58 
  59         int EI_CLASS = 4; /* File class byte index */
  60         int ELFCLASSNONE = 0; /* Invalid class */
  61         int ELFCLASS32 = 1; /* 32-bit objects */
  62         int ELFCLASS64 = 2; /* 64-bit objects */
  63         int ELFCLASSNUM = 3;
  64 
  65         int EI_DATA = 5; /* Data encoding byte index */
  66         int ELFDATANONE = 0; /* Invalid data encoding */
  67         int ELFDATA2LSB = 1; /* 2's complement, little endian */
  68         int ELFDATA2MSB = 2; /* 2's complement, big endian */
  69         int ELFDATANUM = 3;
  70 
  71         // Legal architecture values for e_machine (add others as needed)
  72         int EM_NONE = 0; /* No machine */
  73         int EM_SPARC = 2; /* SUN SPARC */
  74         int EM_386 = 3; /* Intel 80386 */
  75         int EM_SPARCV9 = 43; /* SPARC v9 64-bit */
  76         int EM_X64_64 = 62; /* AMD x86-64 architecture */
  77 
  78         /* Legal values for e_type (object file type). */
  79 
  80         int ET_NONE = 0; /* No file type */
  81         int ET_REL = 1; /* Relocatable file */
  82         int ET_EXEC = 2; /* Executable file */
  83         int ET_DYN = 3; /* Shared object file */
  84         int ET_CORE = 4; /* Core file */
  85         int ET_NUM = 5; /* Number of defined types */
  86         int ET_LOOS = 0xfe00; /* OS-specific range start */
  87         int ET_HIOS = 0xfeff; /* OS-specific range end */
  88         int ET_LOPROC = 0xff00; /* Processor-specific range start */
  89         int ET_HIPROC = 0xffff; /* Processor-specific range end */
  90 
  91         /* Legal values for e_version (version). */
  92 
  93         int EV_NONE = 0; /* Invalid ELF version */
  94         int EV_CURRENT = 1; /* Current version */
  95         int EV_NUM = 2;
  96 
  97         /* Legal values for p_type (segment type). */
  98 
  99         int PT_NULL = 0; /* Program header table entry unused */
 100         int PT_LOAD = 1; /* Loadable program segment */
 101         int PT_DYNAMIC = 2; /* Dynamic linking information */
 102         int PT_INTERP = 3; /* Program interpreter */
 103         int PT_NOTE = 4; /* Auxiliary information */
 104         int PT_SHLIB = 5; /* Reserved */
 105         int PT_PHDR = 6; /* Entry for header table itself */
 106         int PT_TLS = 7; /* Thread-local storage segment */
 107         int PT_NUM = 8; /* Number of defined types */
 108         int PT_LOOS = 0x60000000; /* Start of OS-specific */
 109         int PT_GNU_EH_FRAME = 0x6474e550; /* GCC .eh_frame_hdr segment */
 110         int PT_GNU_STACK = 0x6474e551; /* Indicates stack executability */
 111         int PT_GNU_RELRO = 0x6474e552; /* Read-only after relocation */
 112         int PT_LOSUNW = 0x6ffffffa;
 113         int PT_SUNWBSS = 0x6ffffffa; /* Sun Specific segment */
 114         int PT_SUNWSTACK = 0x6ffffffb; /* Stack segment */
 115         int PT_HISUNW = 0x6fffffff;
 116         int PT_HIOS = 0x6fffffff; /* End of OS-specific */
 117         int PT_LOPROC = 0x70000000; /* Start of processor-specific */
 118         int PT_HIPROC = 0x7fffffff; /* End of processor-specific */
 119 
 120         /* Special section indices. */
 121 
 122         int SHN_UNDEF = 0; /* Undefined section */
 123         int SHN_LORESERVE = 0xff00; /* Start of reserved indices */
 124         int SHN_LOPROC = 0xff00; /* Start of processor-specific */
 125         int SHN_BEFORE = 0xff00; /* Order section before all others (Solaris). */
 126         int SHN_AFTER = 0xff01; /* Order section after all others (Solaris). */
 127         int SHN_HIPROC = 0xff1f; /* End of processor-specific */
 128         int SHN_LOOS = 0xff20; /* Start of OS-specific */
 129         int SHN_HIOS = 0xff3f; /* End of OS-specific */
 130         int SHN_ABS = 0xfff1; /* Associated symbol is absolute */
 131         int SHN_COMMON = 0xfff2; /* Associated symbol is common */
 132         int SHN_XINDEX = 0xffff; /* Index is in extra table. */
 133         int SHN_HIRESERVE = 0xffff; /* End of reserved indices */
 134 
 135         /* Legal values for sh_type (section type). */
 136 
 137         int SHT_NULL = 0; /* Section header table entry unused */
 138         int SHT_PROGBITS = 1; /* Program data */
 139         int SHT_SYMTAB = 2; /* Symbol table */
 140         int SHT_STRTAB = 3; /* String table */
 141         int SHT_RELA = 4; /* Relocation entries with addends */
 142         int SHT_HASH = 5; /* Symbol hash table */
 143         int SHT_DYNAMIC = 6; /* Dynamic linking information */
 144         int SHT_NOTE = 7; /* Notes */
 145         int SHT_NOBITS = 8; /* Program space with no data (bss) */
 146         int SHT_REL = 9; /* Relocation entries, no addends */
 147         int SHT_SHLIB = 10; /* Reserved */
 148         int SHT_DYNSYM = 11; /* Dynamic linker symbol table */
 149         int SHT_INIT_ARRAY = 14; /* Array of constructors */
 150         int SHT_FINI_ARRAY = 15; /* Array of destructors */
 151         int SHT_PREINIT_ARRAY = 16; /* Array of pre-constructors */
 152         int SHT_GROUP = 17; /* Section group */
 153         int SHT_SYMTAB_SHNDX = 18; /* Extended section indeces */
 154         int SHT_NUM = 19; /* Number of defined types. */
 155         int SHT_LOOS = 0x60000000; /* Start OS-specific. */
 156         int SHT_GNU_ATTRIBUTES = 0x6ffffff5; /* Object attributes. */
 157         int SHT_GNU_HASH = 0x6ffffff6; /* GNU-style hash table. */
 158         int SHT_GNU_LIBLIST = 0x6ffffff7; /* Prelink library list */
 159         int SHT_CHECKSUM = 0x6ffffff8; /* Checksum for DSO content. */
 160         int SHT_LOSUNW = 0x6ffffffa; /* Sun-specific low bound. */
 161         int SHT_SUNW_move = 0x6ffffffa;
 162         int SHT_SUNW_COMDAT = 0x6ffffffb;
 163         int SHT_SUNW_syminfo = 0x6ffffffc;
 164         int SHT_GNU_verdef = 0x6ffffffd; /* Version definition section. */
 165         int SHT_GNU_verneed = 0x6ffffffe; /* Version needs section. */
 166         int SHT_GNU_versym = 0x6fffffff; /* Version symbol table. */
 167         int SHT_HISUNW = 0x6fffffff; /* Sun-specific high bound. */
 168         int SHT_HIOS = 0x6fffffff; /* End OS-specific type */
 169         int SHT_LOPROC = 0x70000000; /* Start of processor-specific */
 170         int SHT_HIPROC = 0x7fffffff; /* End of processor-specific */
 171         int SHT_LOUSER = 0x80000000; /* Start of application-specific */
 172         int SHT_HIUSER = 0x8fffffff; /* End of application-specific */
 173 
 174         /* Legal values for sh_flags (section flags). */
 175 
 176         int SHF_WRITE = (1 << 0); /* Writable */
 177         int SHF_ALLOC = (1 << 1); /* Occupies memory during execution */
 178         int SHF_EXECINSTR = (1 << 2); /* Executable */
 179         int SHF_MERGE = (1 << 4); /* Might be merged */
 180         int SHF_STRINGS = (1 << 5); /* Contains nul-terminated strings */
 181         int SHF_INFO_LINK = (1 << 6); /* `sh_info' contains SHT index */
 182         int SHF_LINK_ORDER = (1 << 7); /* Preserve order after combining */
 183         int SHF_OS_NONCONFORMING = (1 << 8); /* Non-standard OS specific handling required */
 184         int SHF_GROUP = (1 << 9); /* Section is member of a group. */
 185         int SHF_TLS = (1 << 10); /* Section hold thread-local data. */
 186         int SHF_MASKOS = 0x0ff00000; /* OS-specific. */
 187         int SHF_MASKPROC = 0xf0000000; /* Processor-specific */
 188         int SHF_ORDERED = (1 << 30); /* Special ordering requirement (Solaris). */
 189         int SHF_EXCLUDE = (1 << 31); /*
 190                                       * Section is excluded unless referenced or allocated
 191                                       * (Solaris).
 192                                       */
 193 
 194         /* Legal values for ST_BIND subfield of st_info (symbol binding). */
 195 
 196         int STB_LOCAL = 0; /* Local symbol */
 197         int STB_GLOBAL = 1; /* Global symbol */
 198         int STB_WEAK = 2; /* Weak symbol */
 199         int STB_NUM = 3; /* Number of defined types. */
 200         int STB_LOOS = 10; /* Start of OS-specific */
 201         int STB_GNU_UNIQUE = 10; /* Unique symbol. */
 202         int STB_HIOS = 12; /* End of OS-specific */
 203         int STB_LOPROC = 13; /* Start of processor-specific */
 204         int STB_HIPROC = 15; /* End of processor-specific */
 205 
 206         /* Legal values for ST_TYPE subfield of st_info (symbol type). */
 207 
 208         int STT_NOTYPE = 0; /* Symbol type is unspecified */
 209         int STT_OBJECT = 1; /* Symbol is a data object */
 210         int STT_FUNC = 2; /* Symbol is a code object */
 211         int STT_SECTION = 3; /* Symbol associated with a section */
 212         int STT_FILE = 4; /* Symbol's name is file name */
 213         int STT_COMMON = 5; /* Symbol is a common data object */
 214         int STT_TLS = 6; /* Symbol is thread-local data object */
 215         int STT_NUM = 7; /* Number of defined types. */
 216         int STT_LOOS = 10; /* Start of OS-specific */
 217         int STT_GNU_IFUNC = 10; /* Symbol is indirect code object */
 218         int STT_HIOS = 12; /* End of OS-specific */
 219         int STT_LOPROC = 13; /* Start of processor-specific */
 220         int STT_HIPROC = 15; /* End of processor-specific */
 221     }
 222 
 223     /**
 224      * Definitions reflecting those in libelf.h.
 225      *
 226      */
 227     public interface LibELF {
 228 
 229         public static enum Elf_Cmd {
 230             ELF_C_NULL("NULL"), /* Nothing, terminate, or compute only. */
 231             ELF_C_READ("READ"), /* Read .. */
 232             ELF_C_RDWR("RDWR"), /* Read and write .. */
 233             ELF_C_WRITE("WRITE"), /* Write .. */
 234             ELF_C_CLR("CLR"), /* Clear flag. */
 235             ELF_C_SET("SET"), /* Set flag. */
 236             ELF_C_FDDONE("FDDONE"), /*
 237                                      * Signal that file descriptor will not be used anymore.
 238                                      */
 239             ELF_C_FDREAD("FDREAD"), /*
 240                                      * Read rest of data so that file descriptor is not used
 241                                      * anymore.
 242                                      */
 243             /* The following are Linux-only extensions. */
 244             ELF_C_READ_MMAP("READ_MMAP"), /* Read, but mmap the file if possible. */
 245             ELF_C_RDWR_MMAP("RDWR_MMAP"), /* Read and write, with mmap. */
 246             ELF_C_WRITE_MMAP("WRITE_MMAP"), /* Write, with mmap. */
 247             ELF_C_READ_MMAP_PRIVATE("READ_MMAP_PRIVATE"), /*
 248                                                            * Read, but memory is writable, results
 249                                                            * are not written to the file.
 250                                                            */
 251             ELF_C_EMPTY("EMPTY"), /* Copy basic file data but not the content. */
 252             /* The following are SunOS-only enums */
 253             ELF_C_WRIMAGE("WRIMAGE"),
 254             ELF_C_IMAGE("IMAGE"),
 255             /* Common last entry. */
 256             ELF_C_NUM("NUM");
 257             private final int intVal;
 258             private final String name;
 259 
 260             private Elf_Cmd(String cmd) {
 261                 name = "ELF_C_" + cmd;
 262                 switch (cmd) {
 263                     case "NULL":
 264                         // ELF_C_NULL has the same enum ordinal on both Linux and SunOS
 265                         intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_NULL.ordinal();
 266                         break;
 267 
 268                     case "READ":
 269                         // ELF_C_READ has the same enum ordinal on both Linux and SunOS
 270                         intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_READ.ordinal();
 271                         break;
 272 
 273                     // Enums defined in libelf.h of both Linux and SunOS
 274                     // but with different ordinals
 275                     case "RDWR":
 276                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 277                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_RDWR.ordinal();
 278                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 279                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_RDWR.ordinal();
 280                         } else {
 281                             // Unsupported platform
 282                             intVal = -1;
 283                         }
 284                         break;
 285 
 286                     case "WRITE":
 287                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 288                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_WRITE.ordinal();
 289                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 290                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_WRITE.ordinal();
 291                         } else {
 292                             // Unsupported platform
 293                             intVal = -1;
 294                         }
 295                         break;
 296 
 297                     case "CLR":
 298                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 299                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_CLR.ordinal();
 300                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 301                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_CLR.ordinal();
 302                         } else {
 303                             // Unsupported platform
 304                             intVal = -1;
 305                         }
 306                         break;
 307 
 308                     case "SET":
 309                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 310                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_SET.ordinal();
 311                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 312                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_SET.ordinal();
 313                         } else {
 314                             // Unsupported platform
 315                             intVal = -1;
 316                         }
 317                         break;
 318 
 319                     case "FDDONE":
 320                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 321                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_FDDONE.ordinal();
 322                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 323                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_FDDONE.ordinal();
 324                         } else {
 325                             // Unsupported platform
 326                             intVal = -1;
 327                         }
 328                         break;
 329 
 330                     case "FDREAD":
 331                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 332                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_FDREAD.ordinal();
 333                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 334                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_FDREAD.ordinal();
 335                         } else {
 336                             // Unsupported platform
 337                             intVal = -1;
 338                         }
 339                         break;
 340 
 341                     case "NUM":
 342                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 343                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_NUM.ordinal();
 344                         } else if (JNIELFTargetInfo.getOsName().equals("sunos")) {
 345                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_NUM.ordinal();
 346                         } else {
 347                             // Unsupported platform
 348                             intVal = -1;
 349                         }
 350                         break;
 351 
 352                     // Linux-only Elf_Cmd enums
 353                     case "READ_MMAP":
 354                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 355                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_READ_MMAP.ordinal();
 356                         } else {
 357                             // Unsupported platform
 358                             intVal = -1;
 359                         }
 360                         break;
 361 
 362                     case "RDWR_MMAP":
 363                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 364                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_RDWR_MMAP.ordinal();
 365                         } else {
 366                             // Unsupported platform
 367                             intVal = -1;
 368                         }
 369                         break;
 370 
 371                     case "WRITE_MMAP":
 372                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 373                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_WRITE_MMAP.ordinal();
 374                         } else {
 375                             // Unsupported platform
 376                             intVal = -1;
 377                         }
 378                         break;
 379 
 380                     case "READ_MMAP_PRIVATE":
 381                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 382                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_READ_MMAP_PRIVATE.ordinal();
 383                         } else {
 384                             // Unsupported platform
 385                             intVal = -1;
 386                         }
 387                         break;
 388 
 389                     case "EMPTY":
 390                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 391                             intVal = jdk.tools.jaotc.jnilibelf.linux.Elf_Cmd.ELF_C_EMPTY.ordinal();
 392                         } else {
 393                             // Unsupported platform
 394                             intVal = -1;
 395                         }
 396                         break;
 397                     // SunOS-only Elf_Cmd enums
 398                     case "WRIMAGE":
 399                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 400                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_WRIMAGE.ordinal();
 401                         } else {
 402                             // Unsupported platform
 403                             intVal = -1;
 404                         }
 405                         break;
 406                     case "IMAGE":
 407                         if (JNIELFTargetInfo.getOsName().equals("linux")) {
 408                             intVal = jdk.tools.jaotc.jnilibelf.sunos.Elf_Cmd.ELF_C_IMAGE.ordinal();
 409                         } else {
 410                             // Unsupported platform
 411                             intVal = -1;
 412                         }
 413                         break;
 414                     default:
 415                         intVal = -1;
 416                 }
 417             }
 418 
 419             public int intValue() {
 420                 assert intVal != -1 : "enum " + name + "not supported on " + JNIELFTargetInfo.getOsName();
 421                 return intVal;
 422             }
 423 
 424             public String getName() {
 425                 return name;
 426             }
 427         }
 428 
 429         public static enum Elf_Type {
 430             ELF_T_BYTE(0), /* unsigned char */
 431             ELF_T_ADDR(1), /* Elf32_Addr, Elf64_Addr, ... */
 432             ELF_T_DYN(2), /* Dynamic section record. */
 433             ELF_T_EHDR(3), /* ELF header. */
 434             ELF_T_HALF(4), /* Elf32_Half, Elf64_Half, ... */
 435             ELF_T_OFF(5), /* Elf32_Off, Elf64_Off, ... */
 436             ELF_T_PHDR(6), /* Program header. */
 437             ELF_T_RELA(7), /* Relocation entry with addend. */
 438             ELF_T_REL(8), /* Relocation entry. */
 439             ELF_T_SHDR(9), /* Section header. */
 440             ELF_T_SWORD(10), /* Elf32_Sword, Elf64_Sword, ... */
 441             ELF_T_SYM(11), /* Symbol record. */
 442             ELF_T_WORD(12), /* Elf32_Word, Elf64_Word, ... */
 443             ELF_T_XWORD(13), /* Elf32_Xword, Elf64_Xword, ... */
 444             ELF_T_SXWORD(14), /* Elf32_Sxword, Elf64_Sxword, ... */
 445             ELF_T_VDEF(15), /* Elf32_Verdef, Elf64_Verdef, ... */
 446             ELF_T_VDAUX(16), /* Elf32_Verdaux, Elf64_Verdaux, ... */
 447             ELF_T_VNEED(17), /* Elf32_Verneed, Elf64_Verneed, ... */
 448             ELF_T_VNAUX(18), /* Elf32_Vernaux, Elf64_Vernaux, ... */
 449             ELF_T_NHDR(19), /* Elf32_Nhdr, Elf64_Nhdr, ... */
 450             ELF_T_SYMINFO(20), /* Elf32_Syminfo, Elf64_Syminfo, ... */
 451             ELF_T_MOVE(21), /* Elf32_Move, Elf64_Move, ... */
 452             ELF_T_LIB(22), /* Elf32_Lib, Elf64_Lib, ... */
 453             ELF_T_GNUHASH(23), /* GNU-style hash section. */
 454             ELF_T_AUXV(24), /* Elf32_auxv_t, Elf64_auxv_t, ... */
 455             /* Keep this the last entry. */
 456             ELF_T_NUM(25);
 457 
 458             private final int intVal;
 459 
 460             private Elf_Type(int v) {
 461                 intVal = v;
 462             }
 463 
 464             public int intValue() {
 465                 return intVal;
 466             }
 467         }
 468 
 469         /* Flags for the ELF structures. */
 470         int ELF_F_DIRTY = 0x1;
 471         int ELF_F_LAYOUT = 0x4;
 472         int ELF_F_PERMISSIVE = 0x8;
 473 
 474         public static enum Elf_Kind {
 475             ELF_K_NONE(0), /* Unknown. */
 476             ELF_K_AR(1), /* Archive. */
 477             ELF_K_COFF(2), /* Stupid old COFF. */
 478             ELF_K_ELF(3), /* ELF file. */
 479             /* Keep this the last entry. */
 480             ELF_K_NUM(4);
 481             private final int intVal;
 482 
 483             private Elf_Kind(int v) {
 484                 intVal = v;
 485             }
 486 
 487             public int intValue() {
 488                 return intVal;
 489             }
 490         }
 491     }
 492 
 493     /**
 494      * Invoke native libelf function unsigned int elf_version (unsigned int v).
 495      *
 496      * @param v version
 497      * @return return value of native call
 498      */
 499     // Checkstyle: stop method name check
 500     static native int elf_version(int v);
 501 
 502     /**
 503      * Return version recorded in libelfshim.
 504      *
 505      * @return return version string
 506      */
 507     // Checkstyle: stop method name check
 508     static native String elfshim_version();
 509 
 510     /**
 511      * Invoke native libelf function Elf *elf_begin (int fildes, Elf_Cmd cmd, Elf *elfPtr).
 512      *
 513      * @param fildes open file descriptor
 514      * @param elfCRead command
 515      * @param elfHdrPtr pointer to ELF header
 516      * @return return value of native call
 517      */
 518     static native Pointer elf_begin(int fildes, int elfCRead, Pointer elfHdrPtr);
 519 
 520     /**
 521      * Invoke native libelf function elf_end (Elf *elfPtr).
 522      *
 523      * @param elfPtr pointer to ELF header
 524      * @return return value of native call
 525      */
 526     static native int elf_end(Pointer elfPtr);
 527 
 528     /**
 529      * Invoke native libelf function elf_end (Elf *elfPtr).
 530      *
 531      * @param elfPtr pointer to ELF header
 532      * @return return value of native call
 533      */
 534     static native int elf_kind(Pointer elfPtr);
 535 
 536     /**
 537      * Invoke native libelf function unsigned int elf_flagphdr (Elf *elf, Elf_Cmd cmd, unsigned int
 538      * flags).
 539      *
 540      * @param elfPtr Pointer to ELF descriptor
 541      * @param cmd command
 542      * @param flags flags
 543      * @return return value of native call
 544      */
 545     static native int elf_flagphdr(Pointer elfPtr, int cmd, int flags);
 546 
 547     /**
 548      * Invoke native libelf function Elf_Scn *elf_newscn (Elf *elfPtr).
 549      *
 550      * @param elfPtr Elf header pointer
 551      * @return return value of native call
 552      */
 553     static native Pointer elf_newscn(Pointer elfPtr);
 554 
 555     /**
 556      * Invoke native libelf function Elf_Data *elf_newdata (Elf_Scn *scn).
 557      *
 558      * @param scnPtr pointer to section for which the new data descriptor is to be created
 559      * @return return value of native call
 560      */
 561     static native Pointer elf_newdata(Pointer scnPtr);
 562 
 563     /**
 564      * Invoke native libelf function Elf64_Shdr *elf64_getshdr (Elf_Scn *scnPtr).
 565      *
 566      * @param scnPtr pointer to section whose header information is to be retrieved
 567      * @return return value of native call
 568      */
 569     static native Pointer elf64_getshdr(Pointer scnPtr);
 570 
 571     /**
 572      * Invoke native libelf function loff_t elf_update (Elf *elfPtr, Elf_Cmd cmd).
 573      *
 574      * @param elfPtr Pointer to ELF descriptor
 575      * @param cmd command
 576      * @return return value of native call
 577      */
 578     static native long elf_update(Pointer elfPtr, int cmd);
 579 
 580     /**
 581      * Invoke native libelf function char *elf_errmsg (int error).
 582      *
 583      * @param error error
 584      * @return return value of native call
 585      */
 586     static native String elf_errmsg(int error);
 587 
 588     /**
 589      * Invoke native libelf function size_t elf_ndxscn (Elf_Scn *scn).
 590      *
 591      * @param scn section pointer
 592      * @return return value of native call
 593      */
 594     static native int elf_ndxscn(Pointer scn);
 595 
 596     /**
 597      * GELF interfaces
 598      */
 599     /**
 600      * Invoke native libelf function unsigned long int gelf_newehdr (Elf *elf, int elfClass).
 601      *
 602      * @param elf ELF Header pointer
 603      * @param elfclass ELF class
 604      * @return return value of native call boxed as a pointer
 605      */
 606     static native Pointer gelf_newehdr(Pointer elf, int elfclass);
 607 
 608     /**
 609      * Invoke native libelf function unsigned long int gelf_newphdr (Elf *elf, size_t phnum).
 610      *
 611      * @param elf ELF header pointer
 612      * @param phnum number of program headers
 613      * @return return value of native call boxed as a pointer
 614      */
 615     static native Pointer gelf_newphdr(Pointer elf, int phnum);
 616 
 617     /**
 618      * Miscellaneous convenience native methods that help peek and poke ELF data structures.
 619      */
 620     static native int size_of_Sym(int elfClass);
 621 
 622     static native int size_of_Rela(int elfClass);
 623 
 624     static native int size_of_Rel(int elfClass);
 625 
 626     static native void ehdr_set_data_encoding(Pointer ehdr, int val);
 627 
 628     static native void set_Ehdr_e_machine(int elfclass, Pointer structPtr, int val);
 629 
 630     static native void set_Ehdr_e_type(int elfclass, Pointer structPtr, int val);
 631 
 632     static native void set_Ehdr_e_version(int elfclass, Pointer structPtr, int val);
 633 
 634     static native void set_Ehdr_e_shstrndx(int elfclass, Pointer structPtr, int val);
 635 
 636     static native void phdr_set_type_self(int elfclass, Pointer ehdr, Pointer phdr);
 637 
 638     static native void set_Shdr_sh_name(int elfclass, Pointer structPtr, int val);
 639 
 640     static native void set_Shdr_sh_type(int elfclass, Pointer structPtr, int val);
 641 
 642     static native void set_Shdr_sh_flags(int elfclass, Pointer structPtr, int val);
 643 
 644     static native void set_Shdr_sh_entsize(int elfclass, Pointer structPtr, int val);
 645 
 646     static native void set_Shdr_sh_link(int elfclass, Pointer structPtr, int val);
 647 
 648     static native void set_Shdr_sh_info(int elfclass, Pointer structPtr, int val);
 649 
 650     static native void set_Data_d_align(Pointer structPtr, int val);
 651 
 652     static native void set_Data_d_off(Pointer structPtr, int val);
 653 
 654     static native void set_Data_d_buf(Pointer structPtr, Pointer val);
 655 
 656     static native void set_Data_d_type(Pointer structPtr, int val);
 657 
 658     static native void set_Data_d_size(Pointer structPtr, int val);
 659 
 660     static native void set_Data_d_version(Pointer structPtr, int val);
 661 
 662     static native long create_sym_entry(int elfclass, int index, int type, int bind, int shndx, int size, int value);
 663 
 664     static native long create_reloc_entry(int elfclass, int roffset, int symtabIdx, int relocType, int raddend, int reloca);
 665 
 666     /**
 667      * File Operations.
 668      */
 669     static native int open_rw(String fileName);
 670 
 671     static native int open(String fileName, int flags);
 672 
 673     static native int open(String fileName, int flags, int mode);
 674 
 675     static native int close(int fd);
 676     // Checkstyle: resume method name check
 677 }