jaulib v1.3.0
Jau Support Library (C++, Java, ..)
SectionHeader.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2013 Gothel Software e.K.
5 * Copyright (c) 2013 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.sys.elf;
27
28import static org.jau.sys.elf.IOUtils.getString;
29import static org.jau.sys.elf.IOUtils.long2Int;
30import static org.jau.sys.elf.IOUtils.readBytes;
31import static org.jau.sys.elf.IOUtils.seek;
32import static org.jau.sys.elf.IOUtils.toHexString;
33
34import java.io.IOException;
35import java.io.RandomAccessFile;
36import java.nio.ByteBuffer;
37
38/**
39 * ELF ABI Section Header
40 * <p>
41 * References:
42 * <ul>
43 * <li>http://linux.die.net/man/5/elf</li>
44 * <li>http://www.sco.com/developers/gabi/latest/contents.html</li>
45 * <li>http://infocenter.arm.com/
46 * <ul>
47 * <li>ARM IHI 0044E, current through ABI release 2.09</li>
48 * </ul></li>
49 * </ul>
50 * </p>
51 */
52public class SectionHeader {
53 /**
54 * {@value}
55 */
56 public static final int SHT_NULL = 0;
57 /**
58 * {@value}
59 */
60 public static final int SHT_PROGBITS = 1;
61 /**
62 * {@value}
63 */
64 public static final int SHT_SYMTAB = 2;
65 /**
66 * {@value}
67 */
68 public static final int SHT_STRTAB = 3;
69 /**
70 * {@value}
71 */
72 public static final int SHT_RELA = 4;
73 /**
74 * {@value}
75 */
76 public static final int SHT_HASH = 5;
77 /**
78 * {@value}
79 */
80 public static final int SHT_DYNAMIC = 6;
81 /**
82 * {@value}
83 */
84 public static final int SHT_NOTE = 7;
85 /**
86 * {@value}
87 */
88 public static final int SHT_NOBITS = 8;
89 /**
90 * {@value}
91 */
92 public static final int SHT_REL = 9;
93 /**
94 * {@value}
95 */
96 public static final int SHT_SHLIB = 10;
97 /**
98 * {@value}
99 */
100 public static final int SHT_DYNSYM = 11;
101 /**
102 * {@value}
103 */
104 public static final int SHT_NUM = 12;
105 /**
106 * {@value}
107 */
108 public static final int SHT_LOPROC = 0x70000000;
109 /**
110 * {@value}
111 */
112 public static final int SHT_HIPROC = 0x7fffffff;
113 /**
114 * {@value}
115 */
116 public static final int SHT_LOUSER = 0x80000000;
117 /**
118 * {@value}
119 */
120 public static final int SHT_HIUSER = 0xffffffff;
121
122 /**
123 * {@value}
124 */
125 public static final int SHT_ARM_EXIDX = 0x70000001;
126 /**
127 * {@value}
128 */
129 public static final int SHT_ARM_PREEMPTMAP = 0x70000002;
130 /**
131 * {@value}
132 */
133 public static final int SHT_ARM_ATTRIBUTES = 0x70000003;
134
135 /**
136 * {@value}. FIXME: Same as {@link #SHT_ARM_ATTRIBUTES}, ok?
137 */
138 public static final int SHT_AARCH64_ATTRIBUTES = 0x70000003;
139
140 /**
141 * {@value}
142 */
143 public static final int SHT_ARM_DEBUGOVERLAY = 0x70000004;
144 /**
145 * {@value}
146 */
147 public static final int SHT_ARM_OVERLAYSECTION = 0x70000005;
148
149 /**
150 * {@value}
151 */
152 public static final short SHN_UNDEF = (short)0;
153 /**
154 * {@value}
155 */
156 public static final short SHN_LORESERVE = (short)0xff00;
157 /**
158 * {@value}
159 */
160 public static final short SHN_LOPROC = (short)0xff00;
161 /**
162 * {@value}
163 */
164 public static final short SHN_HIPROC = (short)0xff1f;
165 /**
166 * {@value}
167 */
168 public static final short SHN_ABS = (short)0xfff1;
169 /**
170 * {@value}
171 */
172 public static final short SHN_COMMON = (short)0xfff2;
173 /**
174 * {@value}
175 */
176 public static final short SHN_HIRESERVE = (short)0xffff;
177
178 /** Public access to the elf header */
179 public final ElfHeaderPart2 eh2;
180
181 /** Public access to the raw elf section header */
182 public final Shdr raw;
183
184 private final int idx;
185 private String name;
186
187 SectionHeader(final ElfHeaderPart2 eh, final byte[] buf, final int offset, final int length, final int sectionIdx) {
188 this( eh, ByteBuffer.wrap(buf, 0, buf.length), sectionIdx );
189 }
190 SectionHeader(final ElfHeaderPart2 eh, final java.nio.ByteBuffer buf, final int idx) {
191 this.eh2 = eh;
192 this.raw = Shdr.create(eh.eh1.machDesc.ordinal(), buf);
193 this.idx = idx;
194 this.name = null;
195 }
196
197 @Override
198 public String toString() {
199 return "SectionHeader[idx "+idx+", name "+name+", type "+toHexString(getType())+", link "+raw.getSh_link()+", info "+toHexString(raw.getSh_info())+", flags "+toHexString(getFlags())+"]";
200 }
201
202 /**
203 * @param strS the {@link SectionHeader#SHT_STRTAB} section containing all strings
204 * @param nameOffset name offset within strS
205 */
206 void initName(final Section strS, final int nameOffset) throws IndexOutOfBoundsException {
207 name = getString(strS.data, strS.offset + nameOffset, strS.length - nameOffset, null);
208 }
209
210 /** Returns the index of this section within the Elf section header table. */
211 public int getIndex() {
212 return idx;
213 }
214
215 /** Returns the type of this section. */
216 public int getType() {
217 return raw.getSh_type();
218 }
219
220 /** Returns the flags of this section. */
221 public long getFlags() {
222 return raw.getSh_flags();
223 }
224
225 /** Returns the size of this section. */
226 public long getSize() {
227 return raw.getSh_size();
228 }
229
230 /** Returns this section name, maybe <code>null</code> if not read. */
231 public String getName() {
232 return name;
233 }
234
235 /**
236 * Returns the Section referenced w/ this section header
237 *
238 * @param in file owning the section
239 * @throws IOException if read error occurs
240 * @throws IllegalArgumentException if section offset or size mismatch including size &gt; {@link Integer#MAX_VALUE}
241 */
242 public Section readSection(final RandomAccessFile in) throws IOException, IllegalArgumentException {
243 final int s_size = long2Int(raw.getSh_size());
244 if( 0 == s_size || 0 > s_size ) {
245 throw new IllegalArgumentException("Shdr["+idx+"] has invalid int size: "+raw.getSh_size()+" -> "+s_size);
246 }
247 final byte[] s_buf = new byte[s_size];
248 return readSectionImpl(in, s_buf, 0, s_size);
249 }
250
251 /**
252 * Returns the Section referenced w/ this section header using given byte array.
253 *
254 * @param in file owning the section
255 * @param b destination buffer
256 * @param b_off offset in destination buffer
257 * @param r_len requested read length in bytes, which shall be &le; than this section size
258 * @throws IOException if read error occurs
259 * @throws IllegalArgumentException if section offset or size mismatch including size &gt; {@link Integer#MAX_VALUE}
260 * @throws IllegalArgumentException if requested read length is &gt; section size
261 */
262 public Section readSection(final RandomAccessFile in, final byte[] b, final int b_off, final int r_len) throws IOException, IllegalArgumentException {
263 final int s_size = long2Int(raw.getSh_size());
264 if( 0 == s_size || 0 > s_size ) {
265 throw new IllegalArgumentException("Shdr["+idx+"] has invalid int size: "+raw.getSh_size()+" -> "+s_size);
266 }
267 if( r_len > s_size ) {
268 throw new IllegalArgumentException("Shdr["+idx+"] has only "+s_size+" bytes, while read request is of "+r_len+" bytes");
269 }
270 return readSectionImpl(in, b, b_off, r_len);
271 }
272
273 Section readSectionImpl(final RandomAccessFile in, final byte[] b, final int b_off, final int r_len) throws IOException, IllegalArgumentException {
274 final long s_off = raw.getSh_offset();
275 seek(in, s_off);
276 readBytes(in, b, b_off, r_len);
278 return new SectionArmAttributes(this, b, b_off, r_len);
279 } else {
280 return new Section(this, b, b_off, r_len);
281 }
282 }
283}
final MachineDataInfo.StaticConfig machDesc
final ElfHeaderPart1 eh1
Public access to the elf header part-1 (CPU/ABI independent read)
ARM EABI attributes within section header SectionHeader#SHT_ARM_ATTRIBUTES.
ELF ABI Section Header.
static final int SHT_DYNAMIC
{@value}
static final int SHT_DYNSYM
{@value}
int getType()
Returns the type of this section.
static final short SHN_HIPROC
{@value}
Section readSection(final RandomAccessFile in, final byte[] b, final int b_off, final int r_len)
Returns the Section referenced w/ this section header using given byte array.
static final int SHT_SHLIB
{@value}
int getIndex()
Returns the index of this section within the Elf section header table.
static final int SHT_RELA
{@value}
static final short SHN_ABS
{@value}
static final int SHT_HIUSER
{@value}
static final int SHT_ARM_PREEMPTMAP
{@value}
static final int SHT_REL
{@value}
static final int SHT_STRTAB
{@value}
static final int SHT_LOUSER
{@value}
static final int SHT_HASH
{@value}
static final int SHT_ARM_DEBUGOVERLAY
{@value}
static final int SHT_NOBITS
{@value}
static final int SHT_ARM_ATTRIBUTES
{@value}
String getName()
Returns this section name, maybe null if not read.
static final short SHN_LOPROC
{@value}
static final int SHT_NUM
{@value}
long getFlags()
Returns the flags of this section.
static final short SHN_LORESERVE
{@value}
static final int SHT_ARM_EXIDX
{@value}
final ElfHeaderPart2 eh2
Public access to the elf header.
static final int SHT_PROGBITS
{@value}
static final int SHT_SYMTAB
{@value}
long getSize()
Returns the size of this section.
static final int SHT_NOTE
{@value}
static final int SHT_LOPROC
{@value}
static final short SHN_COMMON
{@value}
static final short SHN_UNDEF
{@value}
static final int SHT_AARCH64_ATTRIBUTES
{@value}.
static final int SHT_HIPROC
{@value}
static final int SHT_ARM_OVERLAYSECTION
{@value}
static final int SHT_NULL
{@value}
final Shdr raw
Public access to the raw elf section header.
static final short SHN_HIRESERVE
{@value}
Section readSection(final RandomAccessFile in)
Returns the Section referenced w/ this section header.
long getSh_flags()
Getter for native field: CType['ElfN_size' (typedef), size [fixed false, lnx64 8],...
Definition: Shdr.java:94
int getSh_info()
Getter for native field: CType['uint32_t', size [fixed true, lnx64 4], [int]].
Definition: Shdr.java:149
int getSh_link()
Getter for native field: CType['uint32_t', size [fixed true, lnx64 4], [int]].
Definition: Shdr.java:138
static Shdr create(final int mdIdx)
Definition: Shdr.java:180
long getSh_size()
Getter for native field: CType['ElfN_size' (typedef), size [fixed false, lnx64 8],...
Definition: Shdr.java:127
int getSh_type()
Getter for native field: CType['uint32_t', size [fixed true, lnx64 4], [int]].
Definition: Shdr.java:83
long getSh_offset()
Getter for native field: CType['ElfN_Off' (typedef), size [fixed false, lnx64 8], [int]].
Definition: Shdr.java:116