0

I need to call standard os functions from within Java code. I managed to successfully load the lib using the following construct:

interface CStdLib extends Library {

    /**
     * C STDLIB instance.
     */
    CStdLib CSTDLIB = CStdLib.class.cast(
        Native.load("c", CStdLib.class)
    );

    int syscall(int cid, Object... args);
}

I also can call a function (e.g. gettimeofday) if explicitly define relevant parameter structures as Java interfaces.

But I would like to call any system function without modifications in Java code.
So the question is: is there a way to dynamically find out signature and required structures for arbitrary function from a given native library (libc in this case)?
And/or can I find out somehow the list of available functions for the lib?

  • It's not really possible to dynamically find out the signatures. Even a brute force test of all possible names will only get you as far as function names and number of arguments but won't know the types. Closest thing you can get is jlink in newer JDKs, part of Project Panama. But if you're doing that you don't need JNA. Best thing to do? Read the header file. – Daniel Widdis Sep 19 '22 at 15:39
  • I was thinking more towards some generic structure (or Object pointers) which would be accepted by system call. Something like array of objects.. But I didn't manage to find such a structure so far. – Max Petrov Sep 20 '22 at 08:09
  • Pointer manipulations is exactly what the Project Panama implementation is about. If you want to do that, JDK19 comes out today and have fun. No JNA. No JNI. Use jlink to get the libraries. – Daniel Widdis Sep 20 '22 at 15:17
  • But you specifically asked "is there a way to dynamically find out signature and required structures for arbitrary function from a given native library" and the answer is no. – Daniel Widdis Sep 20 '22 at 15:18
  • [JEP 424](https://openjdk.org/jeps/424) has examples. – Daniel Widdis Sep 21 '22 at 15:09

0 Answers0