26package org.jau.sys.elf;
28import static org.jau.sys.elf.IOUtils.readBytes;
29import static org.jau.sys.elf.IOUtils.seek;
30import static org.jau.sys.elf.IOUtils.shortToInt;
31import static org.jau.sys.elf.IOUtils.toHexString;
33import java.io.IOException;
34import java.io.RandomAccessFile;
35import java.nio.ByteBuffer;
37import org.jau.sys.PlatformTypes.ABIType;
38import org.jau.sys.PlatformTypes.CPUFamily;
39import org.jau.sys.PlatformTypes.CPUType;
157 readBytes (in, buf, 0, buf.length);
158 final ByteBuffer eh2Bytes = ByteBuffer.wrap(buf, 0, buf.length);
161 sht = readSectionHeaderTable(in);
165 String armCpuName =
null;
166 String armCpuRawName =
null;
167 boolean abiVFPArgsAcceptsVFPVariant =
false;
168 final SectionHeader sh =
getSectionHeader(SectionHeader.SHT_ARM_ATTRIBUTES);
170 System.err.println(
"ELF-2: Got ARM Attribs Section Header: "+sh);
173 final SectionArmAttributes sArmAttrs = (SectionArmAttributes) sh.readSection(in);
174 if(ElfHeaderPart1.DEBUG) {
175 System.err.println(
"ELF-2: Got ARM Attribs Section Block : "+sArmAttrs);
177 final SectionArmAttributes.Attribute cpuNameArgsAttr = sArmAttrs.get(SectionArmAttributes.Tag.CPU_name);
178 if(
null != cpuNameArgsAttr && cpuNameArgsAttr.isNTBS() ) {
179 armCpuName = cpuNameArgsAttr.getNTBS();
181 final SectionArmAttributes.Attribute cpuRawNameArgsAttr = sArmAttrs.get(SectionArmAttributes.Tag.CPU_raw_name);
182 if(
null != cpuRawNameArgsAttr && cpuRawNameArgsAttr.isNTBS() ) {
183 armCpuRawName = cpuRawNameArgsAttr.getNTBS();
185 final SectionArmAttributes.Attribute abiVFPArgsAttr = sArmAttrs.get(SectionArmAttributes.Tag.ABI_VFP_args);
186 if(
null != abiVFPArgsAttr ) {
187 abiVFPArgsAcceptsVFPVariant = SectionArmAttributes.abiVFPArgsAcceptsVFPVariant(abiVFPArgsAttr.getULEB128());
193 if(
null != armCpuName && armCpuName.length() > 0 ) {
194 _cpuName = armCpuName.toLowerCase().replace(
' ',
'-');
195 _cpuType = queryCPUTypeSafe(_cpuName,
true );
196 }
else if(
null != armCpuRawName && armCpuRawName.length() > 0 ) {
197 _cpuName = armCpuRawName.toLowerCase().replace(
' ',
'-');
198 _cpuType = queryCPUTypeSafe(_cpuName,
true );
204 if(
null == _cpuType ) {
206 _cpuName =
"arm-"+_cpuName;
207 _cpuType = queryCPUTypeSafe(_cpuName,
true );
208 if(
null == _cpuType ) {
211 _cpuType = queryCPUTypeSafe(_cpuName,
true );
212 if(
null == _cpuType ) {
213 throw new InternalError(
"XXX: "+_cpuName+
", "+
eh1);
219 if(ElfHeaderPart1.DEBUG) {
220 System.err.println(
"ELF-2: abiARM cpuName "+_cpuName+
"[armCpuName "+armCpuName+
", armCpuRawName "+armCpuRawName+
"] -> "+
cpuName+
" -> "+
cpuType+
", abiVFPArgsAcceptsVFPVariant "+abiVFPArgsAcceptsVFPVariant);
230 if(ElfHeaderPart1.DEBUG) {
234 private static CPUType queryCPUTypeSafe(
final String
cpuName,
final boolean isARM) {
237 if( isARM &&
null != res ) {
238 if( res.family != CPUFamily.ARM32 && res.family != CPUFamily.ARM64 ) {
239 if(ElfHeaderPart1.DEBUG) {
240 System.err.println(
"ELF-2: queryCPUTypeSafe("+
cpuName+
", isARM="+isARM+
") -> "+res+
" ( "+res.family+
" ) rejected");
246 }
catch (
final Throwable t) {
247 if(ElfHeaderPart1.DEBUG) {
248 System.err.println(
"ELF-2: queryCPUTypeSafe("+
cpuName+
", isARM="+isARM+
"): "+t.getMessage());
296 for(
int i=0; i<
sht.length; i++) {
307 for(
int i=0; i<
sht.length; i++) {
309 if( sh.
getName().equals(name) ) {
319 final String armFlagsS;
328 final SectionHeader[] readSectionHeaderTable(
final RandomAccessFile in)
throws IOException, IllegalArgumentException {
337 final SectionHeader[]
sht;
344 final byte[] buf0 =
new byte[size];
345 readBytes(in, buf0, 0, size);
346 final SectionHeader sh0 =
new SectionHeader(
this, buf0, 0, size, 0);
347 num = (int) sh0.raw.getSh_size();
349 throw new IllegalArgumentException(
"EHdr sh_num == 0 and 1st SHdr size == 0");
351 sht =
new SectionHeader[num];
356 sht =
new SectionHeader[num];
360 final byte[] buf =
new byte[size];
361 readBytes(in, buf, 0, size);
362 sht[i] =
new SectionHeader(
this, buf, 0, size, i);
364 if( SectionHeader.SHN_UNDEF != strndx ) {
366 if( shortToInt(SectionHeader.SHN_LORESERVE) <= strndx ) {
367 throw new InternalError(
"TODO strndx: "+SectionHeader.SHN_LORESERVE+
" < "+strndx);
369 final SectionHeader strShdr =
sht[strndx];
370 if( SectionHeader.SHT_STRTAB != strShdr.getType() ) {
371 throw new IllegalArgumentException(
"Ref. string Shdr["+strndx+
"] is of type "+strShdr.raw.getSh_type());
373 final Section strS = strShdr.readSection(in);
374 for(i=0; i<num; i++) {
375 sht[i].initName(strS,
sht[i].
raw.getSh_name());
static Ehdr_p2 create(final int mdIdx)
short getE_shnum()
Getter for native field: CType['uint16_t', size [fixed true, lnx64 2], [int]].
int getE_flags()
Getter for native field: CType['uint32_t', size [fixed true, lnx64 4], [int]].
short getE_shstrndx()
Getter for native field: CType['uint16_t', size [fixed true, lnx64 2], [int]].
static int size(final int mdIdx)
short getE_shentsize()
Getter for native field: CType['uint16_t', size [fixed true, lnx64 2], [int]].
short getE_ehsize()
Getter for native field: CType['uint16_t', size [fixed true, lnx64 2], [int]].
long getE_shoff()
Getter for native field: CType['ElfN_Off' (typedef), size [fixed false, lnx64 8], [int]].