GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestElfReader01.java
Go to the documentation of this file.
1package com.jogamp.common.os;
2
3import java.io.BufferedOutputStream;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStream;
8import java.io.RandomAccessFile;
9import java.util.List;
10
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;
17
18import org.junit.Test;
19
20import com.jogamp.common.os.Platform.OSType;
21import com.jogamp.junit.util.SingletonJunitCase;
22
23import org.junit.FixMethodOrder;
24import org.junit.runners.MethodSorters;
25
26@FixMethodOrder(MethodSorters.NAME_ASCENDING)
27public class TestElfReader01 extends SingletonJunitCase {
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;
32
33 private static boolean checkFileReadAccess(final File file) {
34 try {
35 return file.isFile() && file.canRead();
36 } catch (final Throwable t) { }
37 return false;
38 }
39 static File findJVMLib(final String libName) {
40 final ClassLoader cl = TestElfReader01.class.getClassLoader();
41 final List<NativeLibrary.LibPath> possibleLibPaths = NativeLibrary.enumerateLibraryPaths(libName, libName, libName, true, cl);
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) ) {
47 return lib;
48 }
49 System.err.println("XXX2 #"+i+": "+lib+" not readable");
50 }
51 return null;
52 }
53
54 @Test
55 public void test01GNULinuxSelfExe () throws IOException {
56 if( null == userFile ) {
57 if( OSType.LINUX == Platform.getOSType() ) {
58 final File f = new File(GNU_LINUX_SELF_EXE);
59 if( checkFileReadAccess(f) ) {
60 testElfHeaderImpl(f, false);
61 }
62 }
63 }
64 }
65
66 @Test
67 public void test02JavaLib () throws IOException {
68 if( null == userFile ) {
69 File jvmLib = findJVMLib("java");
70 if( null == jvmLib ) {
71 jvmLib = findJVMLib("jvm");
72 }
73 if( null != jvmLib ) {
74 testElfHeaderImpl(jvmLib, false);
75 }
76 }
77 }
78
79 @Test
80 public void test99UserFile() throws IOException {
81 if( null != userFile ) {
82 testElfHeaderImpl(userFile, false);
83 }
84 }
85
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");
90 try {
91 final ElfHeaderPart1 eh1;
92 final ElfHeaderPart2 eh2;
93 try {
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());
98 e.printStackTrace();
99 return;
100 }
101 int i=0;
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());
110 }
111 {
112 final SectionHeader sh = eh2.getSectionHeader(SectionHeader.SHT_ARM_ATTRIBUTES);
113 boolean abiVFPArgsAcceptsVFPVariant = false;
114 if( null != sh ) {
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());
119 }
120 }
121 System.err.println("abiVFPArgsAcceptsVFPVariant "+abiVFPArgsAcceptsVFPVariant);
122 }
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);
131 }
132 }
133 } finally {
134 in.close();
135 }
136 }
137
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);
140 if(fileOut) {
141 final File outFile = new File("ElfSection-"+sh.getIndex()+"-"+name);
142 final OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
143 try {
144 out.write(s.data, s.offset, s.length);
145 } finally {
146 out.close();
147 }
148 }
149 System.err.println(name+": read "+s.length+", "+s);
150 }
151
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")) {
155 i++;
156 userFile = new File(args[i]);
157 }
158 }
159 final String tstname = TestElfReader01.class.getName();
160 org.junit.runner.JUnitCore.main(tstname);
161 }
162
163 static String toHexString(final int i) { return "0x"+Integer.toHexString(i); }
164 static String toHexString(final long i) { return "0x"+Long.toHexString(i); }
165
166}
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...
Utility class for querying platform specific properties.
Definition: Platform.java:58
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
static OSType getOSType()
Returns the OS type.
Definition: Platform.java:401
static void main(final String args[])