aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/jogamp/common/os/elf/SectionHeader.java
blob: 18a3293ec83f7e6912213735304fc97557b88164 (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
/**
 * 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}
     */
    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 raw elf section header */
    public final Shdr d;
    
    private int idx;
    private String name;
  
    SectionHeader(byte[] buf, int offset, int length, int sectionIdx) {
        this( ByteBuffer.wrap(buf, 0, buf.length), sectionIdx );
    }
    SectionHeader(java.nio.ByteBuffer buf, int idx) {
        d = Shdr.create(buf);
        this.idx = idx;
        name = null;
    }
    
    public String toString() {
        return "SectionHeader[idx "+idx+", name "+name+", type "+toHexString(getType())+", link "+d.getSh_link()+", info "+toHexString(d.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 d.getSh_type();
    }

    /** Returns the flags of this section. */
    public long getFlags() {
        return d.getSh_flags();
    }
    
    /** Returns the size of this section. */
    public long getSize() {
        return d.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(RandomAccessFile in) throws IOException, IllegalArgumentException {
        final int s_size = long2Int(d.getSh_size());
        if( 0 == s_size || 0 > s_size ) {
            throw new IllegalArgumentException("Shdr["+idx+"] has invalid int size: "+d.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(RandomAccessFile in, byte[] b, int b_off, int r_len) throws IOException, IllegalArgumentException {
        final int s_size = long2Int(d.getSh_size());
        if( 0 == s_size || 0 > s_size ) {
            throw new IllegalArgumentException("Shdr["+idx+"] has invalid int size: "+d.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(RandomAccessFile in, byte[] b, int b_off, int r_len) throws IOException, IllegalArgumentException {
        final long s_off = d.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);
        }
    }    
}