1 /*
   2  * Copyright (c) 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.pecoff;
  25 
  26 /**
  27  *
  28  * Support for the creation of Coff files.
  29  * Current support is limited to 64 bit x86_64.
  30  *
  31  */
  32 
  33 public class PECoff {
  34 
  35     /**
  36      * IMAGE_FILE_HEADER structure defines
  37      */
  38     public enum IMAGE_FILE_HEADER {
  39                      Machine( 0, 2),
  40             NumberOfSections( 2, 2),
  41                TimeDateStamp( 4, 4),
  42         PointerToSymbolTable( 8, 4),
  43              NumberOfSymbols(12, 4),
  44         SizeOfOptionalHeader(16, 2),
  45              Characteristics(18, 2);
  46 
  47         public final int off;
  48         public final int sz;
  49 
  50         IMAGE_FILE_HEADER(int offset, int size) {
  51             this.off = offset;
  52             this.sz = size;
  53         }
  54 
  55         public static int totalsize = 20;
  56 
  57         /**
  58          * IMAGE_FILE_HEADER defines
  59          */
  60 
  61         /**
  62          * Machine
  63          */
  64         public static final char IMAGE_FILE_MACHINE_UNKNOWN = 0x0;
  65         public static final char IMAGE_FILE_MACHINE_AMD64   = 0x8664;
  66 
  67     }
  68 
  69     /**
  70      * IMAGE_SECTION_HEADER structure defines
  71      */
  72     public enum IMAGE_SECTION_HEADER {
  73                         Name( 0, 8),
  74              PhysicalAddress( 8, 4),
  75                  VirtualSize( 8, 4),
  76               VirtualAddress(12, 4),
  77                SizeOfRawData(16, 4),
  78             PointerToRawData(20, 4),
  79         PointerToRelocations(24, 4),
  80         PointerToLinenumbers(28, 4),
  81          NumberOfRelocations(32, 2),
  82          NumberOfLinenumbers(34, 2),
  83              Characteristics(36, 4);
  84 
  85         public final int off;
  86         public final int sz;
  87 
  88         IMAGE_SECTION_HEADER(int offset, int size) {
  89             this.off = offset;
  90             this.sz = size;
  91         }
  92 
  93         public static int totalsize = 40;
  94 
  95         /**
  96          * IMAGE_SECTION_HEADER defines
  97          */
  98 
  99         /**
 100          * Characteristics
 101          */
 102         public static final int IMAGE_SCN_CNT_CODE               = 0x20;
 103         public static final int IMAGE_SCN_CNT_INITIALIZED_DATA   = 0x40;
 104         public static final int IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x80;
 105         public static final int IMAGE_SCN_LNK_COMDAT             = 0x1000;
 106         public static final int IMAGE_SCN_LNK_INFO               = 0x200;
 107         public static final int IMAGE_SCN_LNK_REMOVE             = 0x800;
 108 
 109         public static final int IMAGE_SCN_ALIGN_1BYTES           = 0x100000;
 110         public static final int IMAGE_SCN_ALIGN_2BYTES           = 0x200000;
 111         public static final int IMAGE_SCN_ALIGN_4BYTES           = 0x300000;
 112         public static final int IMAGE_SCN_ALIGN_8BYTES           = 0x400000;
 113         public static final int IMAGE_SCN_ALIGN_16BYTES          = 0x500000;
 114         public static final int IMAGE_SCN_ALIGN_MASK             = 0xf00000;
 115         public static final int IMAGE_SCN_ALIGN_SHIFT            = 20;
 116 
 117         public static final int IMAGE_SCN_LNK_NRELOC_OVFL        = 0x01000000;
 118 
 119         public static final int IMAGE_SCN_MEM_SHARED             = 0x10000000;
 120         public static final int IMAGE_SCN_MEM_EXECUTE            = 0x20000000;
 121         public static final int IMAGE_SCN_MEM_READ               = 0x40000000;
 122         public static final int IMAGE_SCN_MEM_WRITE              = 0x80000000;
 123 
 124     }
 125 
 126     /**
 127      * Symbol table entry definitions
 128      *
 129      * IMAGE_SYMBOL structure defines
 130      */
 131     public enum IMAGE_SYMBOL {
 132                    ShortName( 0, 8),
 133                        Short( 0, 4),
 134                         Long( 4, 4),
 135                        Value( 8, 4),
 136                SectionNumber(12, 2),
 137                         Type(14, 2),
 138                 StorageClass(16, 1),
 139           NumberOfAuxSymbols(17, 1);
 140 
 141         public final int off;
 142         public final int sz;
 143 
 144         IMAGE_SYMBOL(int offset, int size) {
 145             this.off = offset;
 146             this.sz = size;
 147         }
 148 
 149         public static int totalsize = 18;
 150 
 151         /**
 152          * Type
 153          */
 154         public static final int IMAGE_SYM_DTYPE_NONE     = 0x0;
 155         public static final int IMAGE_SYM_DTYPE_FUNCTION = 0x20;
 156 
 157         /**
 158          * StorageClass
 159          */
 160         public static final int IMAGE_SYM_CLASS_NULL     = 0x0;
 161         public static final int IMAGE_SYM_CLASS_EXTERNAL = 0x2;
 162         public static final int IMAGE_SYM_CLASS_STATIC   = 0x3;
 163         public static final int IMAGE_SYM_CLASS_LABEL    = 0x6;
 164 
 165     }
 166 
 167     /**
 168      * IMAGE_RELOCATION structure defines
 169      */
 170     public enum IMAGE_RELOCATION {
 171               VirtualAddress( 0, 4),
 172             SymbolTableIndex( 4, 4),
 173                         Type( 8, 2);
 174 
 175         public final int off;
 176         public final int sz;
 177 
 178         IMAGE_RELOCATION(int offset, int size) {
 179             this.off = offset;
 180             this.sz = size;
 181         }
 182 
 183         public static int totalsize = 10;
 184 
 185         /**
 186          * Relocation types
 187          */
 188         public static final int IMAGE_REL_AMD64_ABSOLUTE = 0x0;
 189         public static final int IMAGE_REL_AMD64_ADDR32   = 0x2;
 190         public static final int IMAGE_REL_AMD64_ADDR64   = 0x1;
 191         public static final int IMAGE_REL_AMD64_REL32    = 0x4;
 192         public static final int IMAGE_REL_AMD64_REL32_1  = 0x5;
 193         public static final int IMAGE_REL_AMD64_REL32_2  = 0x6;
 194         public static final int IMAGE_REL_AMD64_REL32_3  = 0x7;
 195         public static final int IMAGE_REL_AMD64_REL32_4  = 0x8;
 196         public static final int IMAGE_REL_AMD64_REL32_5  = 0x9;
 197 
 198     }
 199 
 200 }