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