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