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