/** * 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 *
* References: *
null
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 > {@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 ≤ than this section size
* @throws IOException if read error occurs
* @throws IllegalArgumentException if section offset or size mismatch including size > {@link Integer#MAX_VALUE}
* @throws IllegalArgumentException if requested read length is > 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);
}
}
}