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 
  25 
  26 package jdk.tools.jaotc.binformat.pecoff;
  27 
  28 import java.nio.ByteBuffer;
  29 
  30 import jdk.tools.jaotc.binformat.pecoff.PECoff.IMAGE_SECTION_HEADER;
  31 
  32 final class PECoffSection {
  33     private final ByteBuffer section;
  34     private final byte[] data;
  35     private final boolean hasrelocations;
  36     private final int sectionIndex;
  37     private final int align;
  38 
  39     PECoffSection(String sectName, byte[] sectData0, int sectFlags0, int sectAlign, boolean hasRelocations, int sectIndex) {
  40 
  41         section = PECoffByteBuffer.allocate(IMAGE_SECTION_HEADER.totalsize);
  42 
  43         // If .oop.got section is empty, VM exits since .oop.got
  44         // symbol ends up as external forwarded reference.
  45         byte[] sectData = sectData0;
  46         if (sectData0.length == 0) {
  47             sectData = new byte[8];
  48         }
  49 
  50         // Copy only Max allowed bytes to Section Entry
  51         byte[] name = sectName.getBytes();
  52         int max = name.length <= IMAGE_SECTION_HEADER.Name.sz ? name.length : IMAGE_SECTION_HEADER.Name.sz;
  53 
  54         assert !(sectAlign < 1 || sectAlign > 1024 || (sectAlign & (sectAlign - 1)) != 0) : "section alignment is not valid: " + sectAlign;
  55         align = sectAlign;
  56 
  57         // Using 32 because IMAGE_SCN_ALIGN_*BYTES is value + 1
  58         int sectAlignBits = (32 - Integer.numberOfLeadingZeros(align)) << IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_SHIFT;
  59         // Clear and set alignment bits
  60         int sectFlags = (sectFlags0 & ~IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_MASK) | (sectAlignBits & IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_MASK);
  61 
  62         section.put(name, IMAGE_SECTION_HEADER.Name.off, max);
  63 
  64         section.putInt(IMAGE_SECTION_HEADER.VirtualSize.off, 0);
  65         section.putInt(IMAGE_SECTION_HEADER.VirtualAddress.off, 0);
  66         section.putInt(IMAGE_SECTION_HEADER.SizeOfRawData.off, sectData.length);
  67         section.putInt(IMAGE_SECTION_HEADER.PointerToLinenumbers.off, 0);
  68         section.putChar(IMAGE_SECTION_HEADER.NumberOfLinenumbers.off, (char) 0);
  69 
  70         section.putInt(IMAGE_SECTION_HEADER.Characteristics.off, sectFlags);
  71 
  72         data = sectData;
  73         hasrelocations = hasRelocations;
  74         sectionIndex = sectIndex;
  75     }
  76 
  77     long getSize() {
  78         return section.getInt(IMAGE_SECTION_HEADER.SizeOfRawData.off);
  79     }
  80 
  81     int getDataAlign() {
  82         return (align);
  83     }
  84 
  85     // Alignment requirements for the IMAGE_SECTION_HEADER structures
  86     static int getShdrAlign() {
  87         return (4);
  88     }
  89 
  90     byte[] getArray() {
  91         return section.array();
  92     }
  93 
  94     byte[] getDataArray() {
  95         return data;
  96     }
  97 
  98     void setOffset(long offset) {
  99         section.putInt(IMAGE_SECTION_HEADER.PointerToRawData.off, (int) offset);
 100     }
 101 
 102     long getOffset() {
 103         return (section.getInt(IMAGE_SECTION_HEADER.PointerToRawData.off));
 104     }
 105 
 106     void setReloff(int offset) {
 107         section.putInt(IMAGE_SECTION_HEADER.PointerToRelocations.off, offset);
 108     }
 109 
 110     void setRelcount(int count) {
 111         // If the number of relocs is larger than 65K, then set
 112         // the overflow bit. The real count will be written to
 113         // the first reloc entry for this section.
 114         if (count > 0xFFFF) {
 115             int flags;
 116             section.putChar(IMAGE_SECTION_HEADER.NumberOfRelocations.off, (char) 0xFFFF);
 117             flags = section.getInt(IMAGE_SECTION_HEADER.Characteristics.off);
 118             flags |= IMAGE_SECTION_HEADER.IMAGE_SCN_LNK_NRELOC_OVFL;
 119             section.putInt(IMAGE_SECTION_HEADER.Characteristics.off, flags);
 120         } else {
 121             section.putChar(IMAGE_SECTION_HEADER.NumberOfRelocations.off, (char) count);
 122         }
 123     }
 124 
 125     boolean hasRelocations() {
 126         return hasrelocations;
 127     }
 128 
 129     int getSectionId() {
 130         return sectionIndex;
 131     }
 132 
 133 }