26package jau.test.sys.elf;
28import java.io.BufferedOutputStream;
30import java.io.FileOutputStream;
31import java.io.IOException;
32import java.io.OutputStream;
33import java.io.RandomAccessFile;
34import java.nio.file.FileSystems;
35import java.nio.file.Files;
36import java.nio.file.Path;
39import org.jau.sys.JNILibrary;
40import org.jau.sys.PlatformProps;
41import org.jau.sys.RuntimeProps;
42import org.jau.sys.PlatformTypes.OSType;
43import org.jau.sys.elf.ElfHeaderPart1;
44import org.jau.sys.elf.ElfHeaderPart2;
45import org.jau.sys.elf.Section;
46import org.jau.sys.elf.SectionArmAttributes;
47import org.jau.sys.elf.SectionHeader;
48import org.junit.FixMethodOrder;
50import org.junit.runners.MethodSorters;
52import jau.test.junit.util.JunitTracer;
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56 public static String GNU_LINUX_SELF_EXE =
"/proc/self/exe";
57 public static String ARM_HF_EXE =
"tst-exe-armhf";
58 public static String ARM_SF_EXE =
"tst-exe-arm";
59 static File userFile =
null;
61 private static boolean checkFileReadAccess(
final File file) {
63 return file.isFile() && file.canRead();
64 }
catch (
final Throwable t) { }
67 static File findJVMLib(
final String libName) {
69 final List<String> possibleLibPaths = JNILibrary.enumerateLibraryPaths(libName,
71 for(
int i=0; i<possibleLibPaths.size(); i++) {
72 final String libPath = possibleLibPaths.get(i);
73 final File lib =
new File(libPath);
74 System.err.println(
"XXX2 #"+i+
": test "+lib);
75 if( checkFileReadAccess(lib) ) {
78 System.err.println(
"XXX2 #"+i+
": "+lib+
" not readable");
85 if(
null == userFile ) {
86 if( OSType.LINUX == PlatformProps.OS ) {
98 final Path exe_symlink = FileSystems.getDefault().getPath(
"/proc/self/exe");
99 if( Files.isSymbolicLink(exe_symlink) ) {
100 final Path exe_path = Files.readSymbolicLink(exe_symlink);
101 final File f = exe_path.toFile();
102 if( checkFileReadAccess(f) ) {
103 System.err.println(
"ElfFile: "+exe_symlink+
" -> "+exe_path+
" -> "+f);
104 testElfHeaderImpl(f,
false);
107 System.err.println(
"ElfFile: "+exe_symlink+
" -> NULL");
115 if(
null == userFile ) {
116 File jvmLib = findJVMLib(
"java");
117 if(
null == jvmLib ) {
118 jvmLib = findJVMLib(
"jvm");
120 if(
null != jvmLib ) {
121 testElfHeaderImpl(jvmLib,
false);
128 if(
null != userFile ) {
129 testElfHeaderImpl(userFile,
false);
133 void testElfHeaderImpl(
final File file,
final boolean fileOutSections)
throws IOException {
134 RuntimeProps.initSingleton();
135 System.err.println(
"Test file "+file.getAbsolutePath());
136 final RandomAccessFile in =
new RandomAccessFile(file,
"r");
138 final ElfHeaderPart1 eh1;
139 final ElfHeaderPart2 eh2;
141 eh1 = ElfHeaderPart1.read(PlatformProps.OS, in);
142 eh2 = ElfHeaderPart2.read(eh1, in);
143 }
catch (
final Exception e) {
144 System.err.println(
"Probably not an ELF file - or not in current format: (caught) "+e.getMessage());
149 System.err.println(eh1);
150 System.err.println(eh2);
151 System.err.println(
"SH entsz "+eh2.raw.getE_shentsize());
152 System.err.println(
"SH off "+toHexString(eh2.raw.getE_shoff()));
153 System.err.println(
"SH strndx "+eh2.raw.getE_shstrndx());
154 System.err.println(
"SH num "+eh2.sht.length);
155 if( 0 < eh2.sht.length ) {
156 System.err.println(
"SH size "+eh2.sht[0].raw.getBuffer().limit());
159 final SectionHeader sh = eh2.getSectionHeader(SectionHeader.SHT_ARM_ATTRIBUTES);
160 boolean abiVFPArgsAcceptsVFPVariant =
false;
162 final SectionArmAttributes sArmAttrs = (SectionArmAttributes) sh.readSection(in);
163 final SectionArmAttributes.Attribute abiVFPArgsAttr = sArmAttrs.get(SectionArmAttributes.Tag.ABI_VFP_args);
164 if(
null != abiVFPArgsAttr ) {
165 abiVFPArgsAcceptsVFPVariant = SectionArmAttributes.abiVFPArgsAcceptsVFPVariant(abiVFPArgsAttr.getULEB128());
168 System.err.println(
"abiVFPArgsAcceptsVFPVariant "+abiVFPArgsAcceptsVFPVariant);
170 for(i=0; i<eh2.sht.length; i++) {
171 final SectionHeader sh = eh2.sht[i];
172 System.err.println(sh);
173 final int type = sh.getType();
174 if( SectionHeader.SHT_STRTAB == type ) {
175 dumpSection(in, sh,
"SHT_STRTAB", fileOutSections);
176 }
else if( SectionHeader.SHT_ARM_ATTRIBUTES == type ) {
177 dumpSection(in, sh,
"SHT_ARM_ATTRIBUTES", fileOutSections);
185 static void dumpSection(
final RandomAccessFile in,
final SectionHeader sh,
final String name,
final boolean fileOut)
throws IllegalArgumentException, IOException {
186 final Section s = sh.readSection(in);
188 final File outFile =
new File(
"ElfSection-"+sh.getIndex()+
"-"+name);
189 final OutputStream out =
new BufferedOutputStream(
new FileOutputStream(outFile));
191 out.write(s.data, s.offset, s.length);
196 System.err.println(name+
": read "+s.length+
", "+s);
199 public static void main(
final String args[])
throws IOException {
200 for(
int i=0; i<args.length; i++) {
201 if(args[i].equals(
"-file")) {
203 userFile =
new File(args[i]);
207 org.junit.runner.JUnitCore.
main(tstname);
210 static String toHexString(
final int i) {
return "0x"+Integer.toHexString(i); }
211 static String toHexString(
final long i) {
return "0x"+Long.toHexString(i); }
void test01GNULinuxSelfExe()
static void main(final String args[])