1package com.jogamp.common.os;
3import java.io.BufferedOutputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStream;
8import java.io.RandomAccessFile;
11import jogamp.common.os.PlatformPropsImpl;
12import jogamp.common.os.elf.ElfHeaderPart1;
13import jogamp.common.os.elf.ElfHeaderPart2;
14import jogamp.common.os.elf.Section;
15import jogamp.common.os.elf.SectionArmAttributes;
16import jogamp.common.os.elf.SectionHeader;
20import com.jogamp.common.os.Platform.OSType;
21import com.jogamp.junit.util.SingletonJunitCase;
23import org.junit.FixMethodOrder;
24import org.junit.runners.MethodSorters;
26@FixMethodOrder(MethodSorters.NAME_ASCENDING)
28 public static String GNU_LINUX_SELF_EXE =
"/proc/self/exe";
29 public static String ARM_HF_EXE =
"tst-exe-armhf";
30 public static String ARM_SF_EXE =
"tst-exe-arm";
31 static File userFile =
null;
33 private static boolean checkFileReadAccess(
final File file) {
35 return file.isFile() && file.canRead();
36 }
catch (
final Throwable t) { }
39 static File findJVMLib(
final String libName) {
42 for(
int i=0; i<possibleLibPaths.size(); i++) {
43 final NativeLibrary.LibPath libPath = possibleLibPaths.get(i);
44 final File lib =
new File(libPath.path);
45 System.err.println(
"XXX2 #"+i+
": test "+lib);
46 if( checkFileReadAccess(lib) ) {
49 System.err.println(
"XXX2 #"+i+
": "+lib+
" not readable");
56 if(
null == userFile ) {
58 final File f =
new File(GNU_LINUX_SELF_EXE);
59 if( checkFileReadAccess(f) ) {
60 testElfHeaderImpl(f,
false);
68 if(
null == userFile ) {
69 File jvmLib = findJVMLib(
"java");
70 if(
null == jvmLib ) {
71 jvmLib = findJVMLib(
"jvm");
73 if(
null != jvmLib ) {
74 testElfHeaderImpl(jvmLib,
false);
81 if(
null != userFile ) {
82 testElfHeaderImpl(userFile,
false);
86 void testElfHeaderImpl(
final File file,
final boolean fileOutSections)
throws IOException {
88 System.err.println(
"Test file "+file.getAbsolutePath());
89 final RandomAccessFile in =
new RandomAccessFile(file,
"r");
91 final ElfHeaderPart1 eh1;
92 final ElfHeaderPart2 eh2;
94 eh1 = ElfHeaderPart1.read(PlatformPropsImpl.OS_TYPE, in);
95 eh2 = ElfHeaderPart2.read(eh1, in);
96 }
catch (
final Exception e) {
97 System.err.println(
"Probably not an ELF file - or not in current format: (caught) "+e.getMessage());
102 System.err.println(eh1);
103 System.err.println(eh2);
104 System.err.println(
"SH entsz "+eh2.raw.getE_shentsize());
105 System.err.println(
"SH off "+toHexString(eh2.raw.getE_shoff()));
106 System.err.println(
"SH strndx "+eh2.raw.getE_shstrndx());
107 System.err.println(
"SH num "+eh2.sht.length);
108 if( 0 < eh2.sht.length ) {
109 System.err.println(
"SH size "+eh2.sht[0].raw.getBuffer().limit());
112 final SectionHeader sh = eh2.getSectionHeader(SectionHeader.SHT_ARM_ATTRIBUTES);
113 boolean abiVFPArgsAcceptsVFPVariant =
false;
115 final SectionArmAttributes sArmAttrs = (SectionArmAttributes) sh.readSection(in);
116 final SectionArmAttributes.Attribute abiVFPArgsAttr = sArmAttrs.get(SectionArmAttributes.Tag.ABI_VFP_args);
117 if(
null != abiVFPArgsAttr ) {
118 abiVFPArgsAcceptsVFPVariant = SectionArmAttributes.abiVFPArgsAcceptsVFPVariant(abiVFPArgsAttr.getULEB128());
121 System.err.println(
"abiVFPArgsAcceptsVFPVariant "+abiVFPArgsAcceptsVFPVariant);
123 for(i=0; i<eh2.sht.length; i++) {
124 final SectionHeader sh = eh2.sht[i];
125 System.err.println(sh);
126 final int type = sh.getType();
127 if( SectionHeader.SHT_STRTAB == type ) {
128 dumpSection(in, sh,
"SHT_STRTAB", fileOutSections);
129 }
else if( SectionHeader.SHT_ARM_ATTRIBUTES == type ) {
130 dumpSection(in, sh,
"SHT_ARM_ATTRIBUTES", fileOutSections);
138 static void dumpSection(
final RandomAccessFile in,
final SectionHeader sh,
final String name,
final boolean fileOut)
throws IllegalArgumentException, IOException {
139 final Section s = sh.readSection(in);
141 final File outFile =
new File(
"ElfSection-"+sh.getIndex()+
"-"+name);
142 final OutputStream out =
new BufferedOutputStream(
new FileOutputStream(outFile));
144 out.write(s.data, s.offset, s.length);
149 System.err.println(name+
": read "+s.length+
", "+s);
152 public static void main(
final String args[])
throws IOException {
153 for(
int i=0; i<args.length; i++) {
154 if(args[i].equals(
"-file")) {
156 userFile =
new File(args[i]);
160 org.junit.runner.JUnitCore.
main(tstname);
163 static String toHexString(
final int i) {
return "0x"+Integer.toHexString(i); }
164 static String toHexString(
final long i) {
return "0x"+Long.toHexString(i); }
Native Library Path Specification.
Provides low-level, relatively platform-independent access to shared ("native") libraries.
static final List< LibPath > enumerateLibraryPaths(final String windowsLibName, final String unixLibName, final String macOSXLibName, final ClassLoader loader)
Given the base library names (no prefixes/suffixes) for the various platforms, enumerate the possible...
static void main(final String args[])
void test01GNULinuxSelfExe()