60public class ElfHeaderPart2 {
141 public static ElfHeaderPart2
read(
final ElfHeaderPart1 eh1,
final RandomAccessFile in)
throws IOException, IllegalArgumentException {
142 return new ElfHeaderPart2(
eh1, in);
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 );
200 _cpuName =
eh1.cpuName;
201 _cpuType =
eh1.cpuType;
204 if(
null == _cpuType ) {
206 _cpuName =
"arm-"+_cpuName;
207 _cpuType = queryCPUTypeSafe(_cpuName,
true );
208 if(
null == _cpuType ) {
210 _cpuName =
eh1.cpuName;
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);
223 abiType = abiVFPArgsAcceptsVFPVariant ? ABIType.EABI_GNU_ARMHF : ABIType.EABI_GNU_ARMEL;
230 if(ElfHeaderPart1.DEBUG) {
234 private static CPUType queryCPUTypeSafe(
final String
cpuName,
final boolean isARM) {
236 final CPUType res = CPUType.query(
cpuName);
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());
259 return raw.getE_flags();
269 final int f =
raw.getE_flags();
282 final int f =
raw.getE_flags();
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());