aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/jogamp/common/os/elf/SectionHeader.java
blob: 533c04bfb7aed91482c4bf3aa7f5bcbf5cceb3c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
 * Copyright 2013 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package jogamp.common.os.elf;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

import static jogamp.common.os.elf.IOUtils.long2Int;
import static jogamp.common.os.elf.IOUtils.readBytes;
import static jogamp.common.os.elf.IOUtils.seek;
import static jogamp.common.os.elf.IOUtils.getString;
import static jogamp.common.os.elf.IOUtils.toHexString;

/**
 * ELF ABI Section Header
 * <p>
 * References:
 * <ul>
 *   <li>http://linux.die.net/man/5/elf</li>
 *   <li>http://www.sco.com/developers/gabi/latest/contents.html</li>
 *   <li>http://infocenter.arm.com/
 *   <ul>
 *      <li>ARM IHI 0044E, current through ABI release 2.09</li>
 *   </ul></li>
 * </ul>
 * </p>
 */
public class SectionHeader {
    /**
     * {@value}
     */
    public static final int SHT_NULL        = 0;
    /**
     * {@value}
     */
    public static final int SHT_PROGBITS    = 1;
    /**
     * {@value}
     */
    public static final int SHT_SYMTAB      = 2;
    /**
     * {@value}
     */
    public static final int SHT_STRTAB      = 3;
    /**
     * {@value}
     */
    public static final int SHT_RELA        = 4;
    /**
     * {@value}
     */
    public static final int SHT_HASH        = 5;
    /**
     * {@value}
     */
    public static final int SHT_DYNAMIC     = 6;
    /**
     * {@value}
     */
    public static final int SHT_NOTE        = 7;
    /**
     * {@value}
     */
    public static final int SHT_NOBITS      = 8;
    /**
     * {@value}
     */
    public static final int SHT_REL         = 9;
    /**
     * {@value}
     */
    public static final int SHT_SHLIB       = 10;
    /**
     * {@value}
     */
    public static final int SHT_DYNSYM      = 11;
    /**
     * {@value}
     */
    public static final int SHT_NUM         = 12;
    /**
     * {@value}
     */
    public static final int SHT_LOPROC      = 0x70000000;
    /**
     * {@value}
     */
    public static final int SHT_HIPROC      = 0x7fffffff;
    /**
     * {@value}
     */
    public static final int SHT_LOUSER      = 0x80000000;
    /**
     * {@value}
     */
    public static final int SHT_HIUSER      = 0xffffffff;

    /**
     * {@value}
     */
    public static final int SHT_ARM_EXIDX          = 0x70000001;
    /**
     * {@value}
     */
    public static final int SHT_ARM_PREEMPTMAP     = 0x70000002;
    /**
     * {@value}
     */
    public static final int SHT_ARM_ATTRIBUTES     = 0x70000003;

    /**
     * {@value}. FIXME: Same as {@link #SHT_ARM_ATTRIBUTES}, ok?
     */
    public static final int SHT_AARCH64_ATTRIBUTES = 0x70000003;

    /**
     * {@value}
     */
    public static final int SHT_ARM_DEBUGOVERLAY   = 0x70000004;
    /**
     * {@value}
     */
    public static final int SHT_ARM_OVERLAYSECTION = 0x70000005;

    /**
     * {@value}
     */
    public static final short SHN_UNDEF     = (short)0;
    /**
     * {@value}
     */
    public static final short SHN_LORESERVE = (short)0xff00;
    /**
     * {@value}
     */
    public static final short SHN_LOPROC    = (short)0xff00;
    /**
     * {@value}
     */
    public static final short SHN_HIPROC    = (short)0xff1f;
    /**
     * {@value}
     */
    public static final short SHN_ABS       = (short)0xfff1;
    /**
     * {@value}
     */
    public static final short SHN_COMMON    = (short)0xfff2;
    /**
     * {@value}
     */
    public static final short SHN_HIRESERVE = (short)0xffff;

    /** Public access to the elf header */
    public final ElfHeaderPart2 eh2;

    /** Public access to the raw elf section header */
    public final Shdr raw;

    private final int idx;
    private String name;

    SectionHeader(final ElfHeaderPart2 eh, final byte[] buf, final int offset, final int length, final int sectionIdx) {
        this( eh, ByteBuffer.wrap(buf, 0, buf.length), sectionIdx );
    }
    SectionHeader(final ElfHeaderPart2 eh, final java.nio.ByteBuffer buf, final int idx) {
        this.eh2 = eh;
        this.raw = Shdr.create(eh.eh1.machDesc.ordinal(), buf);
        this.idx = idx;
        this.name = null;
    }

    @Override
    public String toString() {
        return "SectionHeader[idx "+idx+", name "+name+", type "+toHexString(getType())+", link "+raw.getSh_link()+", info "+toHexString(raw.getSh_info())+", flags "+toHexString(getFlags())+"]";
    }

    /**
     * @param strS the {@link SectionHeader#SHT_STRTAB} section containing all strings
     * @param nameOffset name offset within strS
     */
    void initName(final Section strS, final int nameOffset) throws IndexOutOfBoundsException {
        name = getString(strS.data, strS.offset + nameOffset, strS.length - nameOffset, null);
    }

    /** Returns the index of this section within the Elf section header table. */
    public int getIndex() {
        return idx;
    }

    /** Returns the type of this section. */
    public int getType() {
        return raw.getSh_type();
    }

    /** Returns the flags of this section. */
    public long getFlags() {
        return raw.getSh_flags();
    }

    /** Returns the size of this section. */
    public long getSize() {
        return raw.getSh_size();
    }

    /** Returns this section name, maybe <code>null</code> if not read. */
    public String getName() {
        return name;
    }

    /**
     * Returns the Section referenced w/ this section header
     *
     * @param in file owning the section
     * @throws IOException if read error occurs
     * @throws IllegalArgumentException if section offset or size mismatch including size &gt; {@link Integer#MAX_VALUE}
     */
    public Section readSection(final RandomAccessFile in) throws IOException, IllegalArgumentException {
        final int s_size = long2Int(raw.getSh_size());
        if( 0 == s_size || 0 > s_size ) {
            throw new IllegalArgumentException("Shdr["+idx+"] has invalid int size: "+raw.getSh_size()+" -> "+s_size);
        }
        final byte[] s_buf = new byte[s_size];
        return readSectionImpl(in, s_buf, 0, s_size);
    }

    /**
     * Returns the Section referenced w/ this section header using given byte array.
     *
     * @param in file owning the section
     * @param b destination buffer
     * @param b_off offset in destination buffer
     * @param r_len requested read length in bytes, which shall be &le; than this section size
     * @throws IOException if read error occurs
     * @throws IllegalArgumentException if section offset or size mismatch including size &gt; {@link Integer#MAX_VALUE}
     * @throws IllegalArgumentException if requested read length is &gt; section size
     */
    public Section readSection(final RandomAccessFile in, final byte[] b, final int b_off, final int r_len) throws IOException, IllegalArgumentException {
        final int s_size = long2Int(raw.getSh_size());
        if( 0 == s_size || 0 > s_size ) {
            throw new IllegalArgumentException("Shdr["+idx+"] has invalid int size: "+raw.getSh_size()+" -> "+s_size);
        }
        if( r_len > s_size ) {
            throw new IllegalArgumentException("Shdr["+idx+"] has only "+s_size+" bytes, while read request is of "+r_len+" bytes");
        }
        return readSectionImpl(in, b, b_off, r_len);
    }

    Section readSectionImpl(final RandomAccessFile in, final byte[] b, final int b_off, final int r_len) throws IOException, IllegalArgumentException {
        final long s_off = raw.getSh_offset();
        seek(in, s_off);
        readBytes(in, b, b_off, r_len);
        if( SectionHeader.SHT_ARM_ATTRIBUTES == getType() ) {
            return new SectionArmAttributes(this, b, b_off, r_len);
        } else {
            return new Section(this, b, b_off, r_len);
        }
    }
}