28import java.io.IOException;
29import java.io.RandomAccessFile;
30import java.nio.ByteBuffer;
31import java.nio.ByteOrder;
32import java.nio.IntBuffer;
33import java.nio.ShortBuffer;
34import java.nio.file.FileSystems;
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.security.PrivilegedAction;
40import org.jau.io.Buffers;
41import org.jau.sec.SecurityUtil;
42import org.jau.sys.PlatformTypes.ABIType;
43import org.jau.sys.PlatformTypes.CPUType;
44import org.jau.sys.PlatformTypes.OSType;
45import org.jau.sys.elf.ElfHeaderPart1;
46import org.jau.sys.elf.ElfHeaderPart2;
47import org.jau.sys.elf.SectionArmAttributes;
48import org.jau.util.VersionNumber;
90 public static final boolean DEBUG;
135 NEWLINE = System.getProperty(
"line.separator");
144 final String os_name_prop;
145 final String os_arch_prop;
147 final String[] props =
150 public String[] run() {
151 final String[] props =
new String[4];
153 props[i++] = System.getProperty(
"java.runtime.name");
154 props[i++] = System.getProperty(
"os.name").toLowerCase();
155 props[i++] = System.getProperty(
"os.arch").toLowerCase();
156 props[i++] = System.getProperty(
"os.version");
162 os_name_prop = props[i++];
163 os_arch_prop = props[i++];
166 OS = OSType.
query(os_name_prop);
169 final String elfCpuName;
170 final CPUType elfCpuType;
171 final ABIType elfABIType;
172 final int elfLittleEndian;
173 final boolean elfValid;
175 final String[] _elfCpuName = {
null };
176 final CPUType[] _elfCpuType = {
null };
177 final ABIType[] _elfAbiType = {
null };
178 final int[] _elfLittleEndian = { 0 };
179 final boolean[] _elfValid = {
false };
180 SecurityUtil.doPrivileged(
new PrivilegedAction<Object>() {
182 public Object run() {
183 RandomAccessFile in =
null;
185 final File file = queryElfFile(
OS);
187 System.err.println(
"ELF-1: Using "+file);
189 in =
new RandomAccessFile(file,
"r");
190 final ElfHeaderPart1 eh1 = readElfHeaderPart1(
OS, in);
192 System.err.println(
"ELF-1: Got "+eh1);
195 final ElfHeaderPart2 eh2 = readElfHeaderPart2(eh1, in);
197 System.err.println(
"ELF-2: Got "+eh2);
200 _elfCpuName[0] = eh2.cpuName;
201 _elfCpuType[0] = eh2.cpuType;
202 _elfAbiType[0] = eh2.abiType;
203 if( eh1.isLittleEndian() ) {
204 _elfLittleEndian[0] = 1;
205 }
else if( eh1.isBigEndian() ) {
206 _elfLittleEndian[0] = 2;
211 }
catch (
final Throwable t) {
219 }
catch (
final IOException e) { }
224 elfCpuName = _elfCpuName[0];
225 elfCpuType = _elfCpuType[0];
226 elfABIType = _elfAbiType[0];
227 elfLittleEndian = _elfLittleEndian[0];
228 elfValid = _elfValid[0];
230 System.err.println(
"Platform.Elf: valid "+elfValid+
", elfCpuName "+elfCpuName+
", cpuType "+elfCpuType+
", abiType "+elfABIType+
", elfLittleEndian "+elfLittleEndian);
236 switch( elfLittleEndian ) {
251 System.err.println(
"Platform.Endian: test-little "+queryIsLittleEndianImpl()+
", elf[valid "+elfValid+
", val "+elfLittleEndian+
"] -> LITTLE_ENDIAN "+
LITTLE_ENDIAN);
257 final CPUType propCpuType = CPUType.query(os_arch_prop);
258 final ABIType propABIType = ABIType.query(propCpuType, os_arch_prop);
260 System.err.println(
"Platform.Property: ARCH "+os_arch_prop+
", CpuType "+propCpuType+
", ABIType "+propABIType);
265 if( isCompatible(elfCpuType, elfABIType, propCpuType, propABIType) ) {
283 final String _os_name2 = PlatformTypes.getOSName(
OS);
284 os_name =
null != _os_name2 ? _os_name2 : os_name_prop;
288 os_arch =
null != _os_arch2 ? _os_arch2 : os_arch_prop;
296 System.err.println(
"Platform.Hard: CPU_ARCH "+
CPU+
", ABI_TYPE "+
ABI+
" - strategy "+strategy+
"(elfValid "+elfValid+
"), little "+
LITTLE_ENDIAN);
316 private static final File queryElfFile(
final OSType osType) {
319 if( OSType.LINUX == osType ) {
332 final Path exe_symlink = FileSystems.getDefault().getPath(
"/proc/self/exe");
333 if( Files.isSymbolicLink(exe_symlink) ) {
334 final Path exe_path = Files.readSymbolicLink(exe_symlink);
335 file = exe_path.toFile();
336 if( !checkFileReadAccess(file) ) {
340 System.err.println(
"ElfFile: "+exe_symlink+
" -> "+exe_path+
" -> "+file);
343 System.err.println(
"ElfFile: "+exe_symlink+
" -> NULL");
345 }
catch(
final Throwable t) {
352 file = findSysLib(
"java");
355 file = findSysLib(
"jvm");
357 }
catch(
final Throwable t) {
364 private static final ElfHeaderPart1 readElfHeaderPart1(
final OSType osType,
final RandomAccessFile in) {
365 ElfHeaderPart1 res =
null;
367 res = ElfHeaderPart1.read(osType, in);
368 }
catch(
final Throwable t) {
370 System.err.println(
"Caught: "+t.getMessage());
376 private static final ElfHeaderPart2 readElfHeaderPart2(
final ElfHeaderPart1 eh1,
final RandomAccessFile in) {
377 ElfHeaderPart2 res =
null;
379 res = ElfHeaderPart2.read(eh1, in);
380 }
catch(
final Throwable t) {
382 System.err.println(
"Caught: "+t.getMessage());
389 private static boolean checkFileReadAccess(
final File file) {
391 return file.isFile() && file.canRead();
392 }
catch (
final Throwable t) { }
396 private static File findSysLib(
final String libName) {
397 final ClassLoader cl = PlatformProps.class.getClassLoader();
398 final List<String> possibleLibPaths = JNILibrary.enumerateLibraryPaths(libName,
400 for(
int i=0; i<possibleLibPaths.size(); i++) {
401 final String libPath = possibleLibPaths.get(i);
402 final File lib =
new File(libPath);
404 System.err.println(
"findSysLib #"+i+
": test "+lib);
406 if( checkFileReadAccess(lib) ) {
410 System.err.println(
"findSysLib #"+i+
": "+lib+
" not readable");
416 private static final boolean queryIsLittleEndianImpl() {
417 final ByteBuffer tst_b = ByteBuffer.allocate(Buffers.SIZEOF_INT).order(ByteOrder.nativeOrder());
419 final IntBuffer tst_i = tst_b.asIntBuffer();
420 final ShortBuffer tst_s = tst_b.asShortBuffer();
421 tst_i.put(0, 0x0A0B0C0D);
422 return 0x0C0D == tst_s.get(0);
428 private static final boolean isCompatible(
final CPUType cpu1,
final ABIType abi1,
final CPUType cpu2,
final ABIType abi2) {
429 return cpu1.isCompatible(cpu2) && abi1.isCompatible(abi2);
static< T > T doPrivileged(final PrivilegedAction< T > o)
Call wrapper for java.security.AccessController#doPrivileged(PrivilegedAction).
Helper routines for logging and debugging.
static final boolean debug(final String subcomponent)
Machine data description for alignment and size onle, see com.jogamp.gluegen.
static StaticConfig guessStaticMachineDataInfo(final PlatformTypes.OSType osType, final PlatformTypes.CPUType cpuType)
Simple version number class containing a version number either being defined explicit or derived from...
final int compareTo(final Object o)