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