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.macho;
  27 
  28 import java.nio.ByteBuffer;
  29 
  30 import jdk.tools.jaotc.binformat.macho.MachO.section_64;
  31 
  32 final class MachOSection {
  33     private final ByteBuffer section;
  34     private final byte[] data;
  35     private final boolean hasrelocations;
  36 
  37     MachOSection(String sectName, String segName, byte[] sectData, int sectFlags, boolean hasRelocations, int align) {
  38         section = MachOByteBuffer.allocate(section_64.totalsize);
  39 
  40         // TODO: Hotspot uses long section names.
  41         // They are getting truncated.
  42         // Is this a problem??
  43         byte[] sectNameBytes = sectName.getBytes();
  44         int sectNameMax = section_64.sectname.sz < sectNameBytes.length ? section_64.sectname.sz : sectNameBytes.length;
  45 
  46         for (int i = 0; i < sectNameMax; i++) {
  47             section.put(section_64.sectname.off + i, sectNameBytes[i]);
  48         }
  49         byte[] segNameBytes = segName.getBytes();
  50         int segNameMax = section_64.segname.sz < segNameBytes.length ? section_64.segname.sz : segNameBytes.length;
  51 
  52         for (int i = 0; i < segNameMax; i++) {
  53             section.put(section_64.segname.off + i, segNameBytes[i]);
  54         }
  55         section.putLong(section_64.size.off, sectData.length);
  56 
  57         section.putInt(section_64.align.off, 31 - Integer.numberOfLeadingZeros(align));
  58 
  59         section.putInt(section_64.flags.off, sectFlags);
  60 
  61         data = sectData;
  62 
  63         hasrelocations = hasRelocations;
  64     }
  65 
  66     long getSize() {
  67         return section.getLong(section_64.size.off);
  68     }
  69 
  70     int getAlign() {
  71         return (1 << section.getInt(section_64.align.off));
  72     }
  73 
  74     byte[] getArray() {
  75         return section.array();
  76     }
  77 
  78     byte[] getDataArray() {
  79         return data;
  80     }
  81 
  82     void setAddr(long addr) {
  83         section.putLong(section_64.addr.off, addr);
  84     }
  85 
  86     long getAddr() {
  87         return (section.getLong(section_64.addr.off));
  88     }
  89 
  90     void setOffset(int offset) {
  91         section.putInt(section_64.offset.off, offset);
  92     }
  93 
  94     int getOffset() {
  95         return (section.getInt(section_64.offset.off));
  96     }
  97 
  98     void setReloff(int offset) {
  99         section.putInt(section_64.reloff.off, offset);
 100     }
 101 
 102     void setRelcount(int count) {
 103         section.putInt(section_64.nreloc.off, count);
 104     }
 105 
 106     boolean hasRelocations() {
 107         return hasrelocations;
 108     }
 109 }