* configure.ac,
[cacao.git] / src / native / native.c
index 715dc0f019dffe921f17ffb19fcaf847fbcdda2c..5cc8cc0247df3bd18c91878190daecb2349099a0 100644 (file)
@@ -1,6 +1,6 @@
 /* src/native/native.c - table of native functions
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
    J. Wenninger, Institut f. Computersprachen - TU Wien
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Reinhard Grafl
-            Roman Obermaisser
-            Andreas Krall
-
-   Changes: Christian Thalinger
-
-   $Id: native.c 4541 2006-02-21 13:37:48Z twisti $
+   $Id: native.c 8132 2007-06-22 11:15:47Z twisti $
 
 */
 
 #include "config.h"
 
 #include <assert.h>
+#include <ctype.h>
 
 #if !defined(WITH_STATIC_CLASSPATH)
 # include <ltdl.h>
 #endif
 
+#include <stdint.h>
+
 #include "vm/types.h"
 
 #include "mm/memory.h"
+
 #include "native/jni.h"
 #include "native/native.h"
-#include "native/include/java_lang_Throwable.h"
+
+#include "native/vm/nativevm.h"
+
+#include "threads/lock-common.h"
+
+#include "toolbox/avl.h"
+#include "toolbox/hashtable.h"
 #include "toolbox/logging.h"
+
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
-#include "vm/hashtable.h"
-#include "vm/loader.h"
-#include "vm/options.h"
-#include "vm/resolve.h"
 #include "vm/stringlocal.h"
+#include "vm/vm.h"
+
 #include "vm/jit/asmpart.h"
 #include "vm/jit/jit.h"
 
+#include "vmcore/loader.h"
+#include "vmcore/options.h"
+#include "vm/resolve.h"
 
-/* include table of native functions ******************************************/
+#if defined(ENABLE_JVMTI)
+#include "native/jvmti/cacaodbg.h"
+#endif
 
-#include "native/include/java_lang_Cloneable.h"
-#include "native/include/java_util_Properties.h"
-#include "native/include/java_io_InputStream.h"
-#include "native/include/java_io_PrintStream.h"
-
-#include "native/include/gnu_classpath_VMStackWalker.h"
-#include "native/include/gnu_classpath_VMSystemProperties.h"
-#include "native/include/java_lang_Class.h"
-#include "native/include/java_lang_Object.h"
-#include "native/include/java_lang_VMClass.h"
-#include "native/include/java_lang_VMClassLoader.h"
-#include "native/include/java_lang_VMObject.h"
-#include "native/include/java_lang_VMRuntime.h"
-#include "native/include/java_lang_VMString.h"
-#include "native/include/java_lang_VMSystem.h"
-#include "native/include/java_lang_VMThread.h"
-#include "native/include/java_lang_VMThrowable.h"
-#include "native/include/java_lang_reflect_Constructor.h"
-#include "native/include/java_lang_reflect_Field.h"
-#include "native/include/java_lang_reflect_Method.h"
-#include "native/include/java_lang_reflect_VMProxy.h"
-#include "native/include/java_security_VMAccessController.h"
 
+/* include table of native functions ******************************************/
 
 #if defined(WITH_STATIC_CLASSPATH)
-
-/* these are required to prevent compiler warnings */
-
-#include "native/include/java_net_DatagramPacket.h"
-#include "native/include/java_net_InetAddress.h"
-#include "native/include/java_net_SocketImpl.h"
-
-#include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
-#include "native/include/gnu_java_net_PlainSocketImpl.h"
-#include "native/include/gnu_java_nio_PipeImpl.h"
-#include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
-#include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
-#include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
-#include "native/include/java_lang_VMProcess.h"
-#include "native/include/java_nio_MappedByteBufferImpl.h"
-#include "native/include/java_nio_channels_spi_SelectorProvider.h"
-
-/* now include the native table */
-
-#include "native/nativetable.inc"
-
-#else /* defined(WITH_STATIC_CLASSPATH) */
-
-/* Ensure that symbols for functions implemented within CACAO are used
-   and exported to dlopen. */
-
-static functionptr dummynativetable[] = {
-       (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
-
-       (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
-
-       (functionptr) Java_java_lang_VMClass_isInstance,
-       (functionptr) Java_java_lang_VMClass_isAssignableFrom,
-       (functionptr) Java_java_lang_VMClass_isInterface,
-       (functionptr) Java_java_lang_VMClass_isPrimitive,
-       (functionptr) Java_java_lang_VMClass_getName,
-       (functionptr) Java_java_lang_VMClass_getSuperclass,
-       (functionptr) Java_java_lang_VMClass_getInterfaces,
-       (functionptr) Java_java_lang_VMClass_getComponentType,
-       (functionptr) Java_java_lang_VMClass_getModifiers,
-       (functionptr) Java_java_lang_VMClass_getDeclaringClass,
-       (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
-       (functionptr) Java_java_lang_VMClass_getDeclaredFields,
-       (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
-       (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
-       (functionptr) Java_java_lang_VMClass_getClassLoader,
-       (functionptr) Java_java_lang_VMClass_forName,
-       (functionptr) Java_java_lang_VMClass_isArray,
-       (functionptr) Java_java_lang_VMClass_throwException,
-
-       (functionptr) Java_java_lang_VMClassLoader_defineClass,
-       (functionptr) Java_java_lang_VMClassLoader_resolveClass,
-       (functionptr) Java_java_lang_VMClassLoader_loadClass,
-       (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
-       (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
-       (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
-
-       (functionptr) Java_java_lang_VMObject_getClass,
-       (functionptr) Java_java_lang_VMObject_clone,
-       (functionptr) Java_java_lang_VMObject_notify,
-       (functionptr) Java_java_lang_VMObject_notifyAll,
-       (functionptr) Java_java_lang_VMObject_wait,
-
-       (functionptr) Java_java_lang_VMRuntime_availableProcessors,
-       (functionptr) Java_java_lang_VMRuntime_freeMemory,
-       (functionptr) Java_java_lang_VMRuntime_totalMemory,
-       (functionptr) Java_java_lang_VMRuntime_maxMemory,
-       (functionptr) Java_java_lang_VMRuntime_gc,
-       (functionptr) Java_java_lang_VMRuntime_runFinalization,
-       (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
-       (functionptr) Java_java_lang_VMRuntime_traceInstructions,
-       (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
-       (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
-       (functionptr) Java_java_lang_VMRuntime_exit,
-       (functionptr) Java_java_lang_VMRuntime_nativeLoad,
-       (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
-
-       (functionptr) Java_java_lang_VMString_intern,
-
-       (functionptr) Java_java_lang_VMSystem_arraycopy,
-       (functionptr) Java_java_lang_VMSystem_identityHashCode,
-
-       (functionptr) Java_java_lang_VMThread_start,
-       (functionptr) Java_java_lang_VMThread_interrupt,
-       (functionptr) Java_java_lang_VMThread_isInterrupted,
-       (functionptr) Java_java_lang_VMThread_suspend,
-       (functionptr) Java_java_lang_VMThread_resume,
-       (functionptr) Java_java_lang_VMThread_nativeSetPriority,
-       (functionptr) Java_java_lang_VMThread_nativeStop,
-       (functionptr) Java_java_lang_VMThread_currentThread,
-       (functionptr) Java_java_lang_VMThread_yield,
-       (functionptr) Java_java_lang_VMThread_interrupted,
-       (functionptr) Java_java_lang_VMThread_holdsLock,
-
-       (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
-       (functionptr) Java_java_lang_VMThrowable_getStackTrace,
-
-       (functionptr) Java_java_lang_reflect_Constructor_getModifiers,
-       (functionptr) Java_java_lang_reflect_Constructor_constructNative,
-
-       (functionptr) Java_java_lang_reflect_Field_getModifiers,
-       (functionptr) Java_java_lang_reflect_Field_getType,
-       (functionptr) Java_java_lang_reflect_Field_get,
-       (functionptr) Java_java_lang_reflect_Field_getBoolean,
-       (functionptr) Java_java_lang_reflect_Field_getByte,
-       (functionptr) Java_java_lang_reflect_Field_getChar,
-       (functionptr) Java_java_lang_reflect_Field_getShort,
-       (functionptr) Java_java_lang_reflect_Field_getInt,
-       (functionptr) Java_java_lang_reflect_Field_getLong,
-       (functionptr) Java_java_lang_reflect_Field_getFloat,
-       (functionptr) Java_java_lang_reflect_Field_getDouble,
-       (functionptr) Java_java_lang_reflect_Field_set,
-       (functionptr) Java_java_lang_reflect_Field_setBoolean,
-       (functionptr) Java_java_lang_reflect_Field_setByte,
-       (functionptr) Java_java_lang_reflect_Field_setChar,
-       (functionptr) Java_java_lang_reflect_Field_setShort,
-       (functionptr) Java_java_lang_reflect_Field_setInt,
-       (functionptr) Java_java_lang_reflect_Field_setLong,
-       (functionptr) Java_java_lang_reflect_Field_setFloat,
-       (functionptr) Java_java_lang_reflect_Field_setDouble,
-
-       (functionptr) Java_java_lang_reflect_Method_getModifiers,
-       (functionptr) Java_java_lang_reflect_Method_getReturnType,
-       (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
-       (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
-       (functionptr) Java_java_lang_reflect_Method_invokeNative,
-
-       (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
-       (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
-       (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
-
-       (functionptr) Java_java_security_VMAccessController_getStack,
-};
-
-#endif /* defined(WITH_STATIC_CLASSPATH) */
+# include "native/nativetable.inc"
+#endif
 
 
 /* tables for methods *********************************************************/
@@ -241,47 +93,39 @@ static bool nativecompdone = false;
 
 /* global variables ***********************************************************/
 
-#if !defined(WITH_STATIC_CLASSPATH)
-static hashtable hashtable_library;
-static lt_dlhandle mainhandle;
-#endif
+static avl_tree_t *tree_native_methods;
+static hashtable *hashtable_library;
+
+
+/* prototypes *****************************************************************/
+
+static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
 
 
-/* native_loadclasses **********************************************************
+/* native_init *****************************************************************
 
-   Load classes required for native methods.
+   Initializes the native subsystem.
 
 *******************************************************************************/
 
 bool native_init(void)
 {
 #if !defined(WITH_STATIC_CLASSPATH)
-       void *p;
-
-       /* We need to access the dummy native table, not only to remove a
-          warning but to be sure that the table is not optimized away
-          (gcc does this since 3.4). */
-
-       p = &dummynativetable;
-
        /* initialize libltdl */
 
-       if (lt_dlinit()) {
-               /* XXX how can we throw an exception here? */
-               log_text(lt_dlerror());
+       if (lt_dlinit())
+               vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
 
-               return false;
-       }
+       /* initialize library hashtable, 10 entries should be enough */
 
-       /* get the handle for the main program */
+       hashtable_library = NEW(hashtable);
 
-       if (!(mainhandle = lt_dlopen(NULL)))
-               return false;
+       hashtable_create(hashtable_library, 10);
+#endif
 
-       /* initialize library hashtable, 10 entries should be enough */
+       /* initialize the native methods table */
 
-       hashtable_create(&hashtable_library, 10);
-#endif
+       tree_native_methods = avl_create(&native_tree_native_methods_comparator);
 
        /* everything's ok */
 
@@ -289,192 +133,48 @@ bool native_init(void)
 }
 
 
-/* native_hashtable_library_add ************************************************
-
-   Adds an entry to the native library hashtable.
-
-*******************************************************************************/
-
-#if !defined(WITH_STATIC_CLASSPATH)
-void native_hashtable_library_add(utf *filename, java_objectheader *loader,
-                                                                 lt_dlhandle handle)
-{
-       hashtable_library_loader_entry *le;
-       hashtable_library_name_entry   *ne; /* library name                       */
-       u4   key;                           /* hashkey                            */
-       u4   slot;                          /* slot in hashtable                  */
-
-       /* normally addresses are aligned to 4, 8 or 16 bytes */
-
-       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
-       slot = key & (hashtable_library.size - 1);
-       le   = hashtable_library.ptr[slot];
-
-       /* search external hash chain for the entry */
-
-       while (le) {
-               if (le->loader == loader)
-                       break;
-
-               le = le->hashlink;                  /* next element in external chain */
-       }
-
-       /* no loader found? create a new entry */
-
-       if (!le) {
-               le = NEW(hashtable_library_loader_entry);
-
-               le->loader = loader;
-               le->namelink = NULL;
-
-               /* insert entry into hashtable */
-
-               le->hashlink =
-                       (hashtable_library_loader_entry *) hashtable_library.ptr[slot];
-               hashtable_library.ptr[slot] = le;
-
-               /* update number of hashtable-entries */
-
-               hashtable_library.entries++;
-       }
-
-
-       /* search for library name */
-
-       ne = le->namelink;
-
-       while (ne) {
-               if (ne->name == filename)
-                       return;
-
-               ne = ne->hashlink;                  /* next element in external chain */
-       }
-
-       /* not found? add the library name to the classloader */
-
-       ne = NEW(hashtable_library_name_entry);
-
-       ne->name = filename;
-       ne->handle = handle;
-
-       /* insert entry into external chain */
-
-       ne->hashlink = le->namelink;
-       le->namelink = ne;
-}
-#endif /* !defined(WITH_STATIC_CLASSPATH) */
-
-
-/* native_hashtable_library_find ***********************************************
-
-   Find an entry in the native library hashtable.
-
-*******************************************************************************/
-
-#if !defined(WITH_STATIC_CLASSPATH)
-hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
-                                                                                                                       java_objectheader *loader)
-{
-       hashtable_library_loader_entry *le;
-       hashtable_library_name_entry   *ne; /* library name                       */
-       u4   key;                           /* hashkey                            */
-       u4   slot;                          /* slot in hashtable                  */
-
-       /* normally addresses are aligned to 4, 8 or 16 bytes */
-
-       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
-       slot = key & (hashtable_library.size - 1);
-       le   = hashtable_library.ptr[slot];
-
-       /* search external hash chain for the entry */
-
-       while (le) {
-               if (le->loader == loader)
-                       break;
-
-               le = le->hashlink;                  /* next element in external chain */
-       }
-
-       /* no loader found? return NULL */
-
-       if (!le)
-               return NULL;
-
-       /* search for library name */
-
-       ne = le->namelink;
-
-       while (ne) {
-               if (ne->name == filename)
-                       return ne;
-
-               ne = ne->hashlink;                  /* next element in external chain */
-       }
-
-       /* return entry, if no entry was found, ne is NULL */
-
-       return ne;
-}
-#endif /* !defined(WITH_STATIC_CLASSPATH) */
-
+/* native_tree_native_methods_comparator ***************************************
 
-/* native_findfunction *********************************************************
+   Comparison function for AVL tree of native methods.
 
-   Looks up a method (must have the same class name, method name,
-   descriptor and 'static'ness) and returns a function pointer to it.
-   Returns: function pointer or NULL (if there is no such method)
+   IN:
+       treenode....node in the tree
+          node........node to compare with tree-node
 
-   Remark: For faster operation, the names/descriptors are converted
-   from C strings to Unicode the first time this function is called.
+   RETURN VALUE:
+       -1, 0, +1
 
 *******************************************************************************/
 
-#if defined(WITH_STATIC_CLASSPATH)
-functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
-                                                               bool isstatic)
+static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
 {
-       /* entry of table for fast string comparison */
-       struct nativecompref *n;
-       s4 i;
-
-       isstatic = isstatic ? true : false;
-       
-       if (!nativecompdone) {
-               for (i = 0; i < NATIVETABLESIZE; i++) {
-                       nativecomptable[i].classname  = 
-                               utf_new_char(nativetable[i].classname);
-
-                       nativecomptable[i].methodname = 
-                               utf_new_char(nativetable[i].methodname);
-
-                       nativecomptable[i].descriptor =
-                               utf_new_char(nativetable[i].descriptor);
+       const native_methods_node_t *treenmn;
+       const native_methods_node_t *nmn;
 
-                       nativecomptable[i].isstatic   = nativetable[i].isstatic;
-                       nativecomptable[i].func       = nativetable[i].func;
-               }
+       treenmn = treenode;
+       nmn     = node;
 
-               nativecompdone = true;
-       }
+       /* these are for walking the tree */
 
-       for (i = 0; i < NATIVETABLESIZE; i++) {
-               n = &(nativecomptable[i]);
+       if (treenmn->classname < nmn->classname)
+               return -1;
+       else if (treenmn->classname > nmn->classname)
+               return 1;
 
-               if (cname == n->classname && mname == n->methodname &&
-                   desc == n->descriptor && isstatic == n->isstatic)
-                       return n->func;
-       }
+       if (treenmn->name < nmn->name)
+               return -1;
+       else if (treenmn->name > nmn->name)
+               return 1;
 
-               
-       /* no function was found, throw exception */
+       if (treenmn->descriptor < nmn->descriptor)
+               return -1;
+       else if (treenmn->descriptor > nmn->descriptor)
+               return 1;
 
-       *exceptionptr =
-                       new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
-                                                                        mname);
+       /* all pointers are equal, we have found the entry */
 
-       return NULL;
+       return 0;
 }
-#endif /* defined(WITH_STATIC_CLASSPATH) */
 
 
 /* native_make_overloaded_function *********************************************
@@ -484,16 +184,22 @@ functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
 *******************************************************************************/
 
 #if !defined(WITH_STATIC_CLASSPATH)
-static char *native_make_overloaded_function(char *name, utf *desc)
+static utf *native_make_overloaded_function(utf *name, utf *descriptor)
 {
        char *newname;
        s4    namelen;
        char *utf_ptr;
        u2    c;
        s4    i;
+       s4    dumpsize;
+       utf  *u;
+
+       /* mark memory */
+
+       dumpsize = dump_size();
 
-       utf_ptr = desc->text;
-       namelen = strlen(name) + strlen("__") + strlen("0");
+       utf_ptr = descriptor->text;
+       namelen = strlen(name->text) + strlen("__") + strlen("0");
 
        /* calculate additional length */
 
@@ -525,15 +231,14 @@ static char *native_make_overloaded_function(char *name, utf *desc)
                }
        }
 
-
        /* reallocate memory */
 
-       i = strlen(name);
+       i = strlen(name->text);
 
        newname = DMNEW(char, namelen);
-       MCOPY(newname, name, char, i);
+       MCOPY(newname, name->text, char, i);
 
-       utf_ptr = desc->text;
+       utf_ptr = descriptor->text;
 
        newname[i++] = '_';
        newname[i++] = '_';
@@ -577,426 +282,671 @@ static char *native_make_overloaded_function(char *name, utf *desc)
 
        newname[i] = '\0';
 
-       return newname;
+       /* make a utf-string */
+
+       u = utf_new_char(newname);
+
+       /* release memory */
+
+       dump_release(dumpsize);
+
+       return u;
 }
 
 
-/* native_resolve_function *****************************************************
+/* native_insert_char **********************************************************
 
-   Resolves a native function, maybe from a dynamic library.
+   Inserts the passed UTF character into the native method name.  If
+   necessary it is escaped properly.
 
 *******************************************************************************/
 
-functionptr native_resolve_function(methodinfo *m)
+static s4 native_insert_char(char *name, u4 pos, u2 c)
 {
-       lt_ptr                     sym;
-       char                      *name;
-       char                      *newname;
-       s4                         namelen;
-       char                      *utf_ptr;
-       char                      *utf_endptr;
-       s4                         dumpsize;
-       hashtable_library_loader_entry *le;
-       hashtable_library_name_entry   *ne;
-       u4                         key;     /* hashkey                            */
-       u4                         slot;    /* slot in hashtable                  */
-       u4                         i;
-
+       s4 val;
+       s4 i;
 
-       /* verbose output */
+       switch (c) {
+       case '/':
+       case '.':
+               /* replace '/' or '.' with '_' */
+               name[pos] = '_';
+               break;
 
-       if (opt_verbosejni) {
-               printf("[Dynamic-linking native method ");
-               utf_display_classname(m->class->name);
-               printf(".");
-               utf_display(m->name);
-               printf(" ... ");
+       case '_':
+               /* escape sequence for '_' is '_1' */
+               name[pos]   = '_';
+               name[++pos] = '1';
+               break;
+
+       case ';':
+               /* escape sequence for ';' is '_2' */
+               name[pos]   = '_';
+               name[++pos] = '2';
+               break;
+
+       case '[':
+               /* escape sequence for '[' is '_1' */
+               name[pos]   = '_';
+               name[++pos] = '3';
+               break;
+
+       default:
+               if (isalnum(c))
+                       name[pos] = c;
+               else {
+                       /* unicode character */
+                       name[pos]   = '_';
+                       name[++pos] = '0';
+
+                       for (i = 0; i < 4; ++i) {
+                               val = c & 0x0f;
+                               name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
+                               c >>= 4;
+                       }
+
+                       pos += 4;
+               }
+               break;
        }
-               
-       /* calculate length of native function name */
 
-       namelen = strlen("Java_") + utf_strlen(m->class->name) + strlen("_") +
-               utf_strlen(m->name) + strlen("0");
+       /* return the new buffer index */
 
-       /* check for underscores in class name */
+       return pos;
+}
 
-       utf_ptr = m->class->name->text;
-       utf_endptr = UTF_END(m->class->name);
 
-       while (utf_ptr < utf_endptr)
-               if (utf_nextu2(&utf_ptr) == '_')
-                       namelen++;
+/* native_method_symbol ********************************************************
 
-       /* check for underscores in method name */
+   Generate a method-symbol string out of the class name and the
+   method name.
 
-       utf_ptr = m->name->text;
-       utf_endptr = UTF_END(m->name);
+*******************************************************************************/
 
-       while (utf_ptr < utf_endptr)
-               if (utf_nextu2(&utf_ptr) == '_')
-                       namelen++;
+static utf *native_method_symbol(utf *classname, utf *methodname)
+{
+       char *name;
+       s4    namelen;
+       char *utf_ptr;
+       char *utf_endptr;
+       u2    c;
+       u4    pos;
+       s4    dumpsize;
+       utf  *u;
 
-       /* allocate memory */
+       /* mark memory */
 
        dumpsize = dump_size();
 
-       name = DMNEW(char, namelen);
-
-
-       /* generate name of native functions */
+       /* Calculate length of native function name.  We multiply the
+          class and method name length by 6 as this is the maxium
+          escape-sequence that can be generated (unicode). */
 
-       strcpy(name, "Java_");
-       i = strlen("Java_");
+       namelen =
+               strlen("Java_") +
+               utf_get_number_of_u2s(classname) * 6 +
+               strlen("_") +
+               utf_get_number_of_u2s(methodname) * 6 +
+               strlen("0");
 
-       utf_ptr = m->class->name->text;
-       utf_endptr = UTF_END(m->class->name);
+       /* allocate memory */
 
-       for (; utf_ptr < utf_endptr; utf_ptr++, i++) {
-               name[i] = *utf_ptr;
+       name = DMNEW(char, namelen);
 
-               /* escape sequence for '_' is '_1' */
+       /* generate name of native functions */
 
-               if (name[i] == '_')
-                       name[++i] = '1';
+       strcpy(name, "Java_");
+       pos = strlen("Java_");
 
-               /* replace '/' with '_' */
+       utf_ptr    = classname->text;
+       utf_endptr = UTF_END(classname);
 
-               if (name[i] == '/')
-                       name[i] = '_';
+       for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
+               c   = *utf_ptr;
+               pos = native_insert_char(name, pos, c);
        }
 
        /* seperator between class and method */
 
-       name[i++] = '_';
-
-       utf_ptr = m->name->text;
-       utf_endptr = UTF_END(m->name);
+       name[pos++] = '_';
 
-       for (; utf_ptr < utf_endptr; utf_ptr++, i++) {
-               name[i] = *utf_ptr;
-
-               /* escape sequence for '_' is '_1' */
+       utf_ptr    = methodname->text;
+       utf_endptr = UTF_END(methodname);
 
-               if (name[i] == '_')
-                       name[++i] = '1';
+       for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
+               c   = *utf_ptr;
+               pos = native_insert_char(name, pos, c);
        }
 
        /* close string */
 
-       name[i] = '\0';
+       name[pos] = '\0';
 
+       /* check for an buffer overflow */
 
-       /* generate overloaded function (having the types in it's name)           */
+       assert(pos <= namelen);
 
-       newname = native_make_overloaded_function(name, m->descriptor);
+       /* make a utf-string */
 
-       /* check the library hash entries of the classloader of the
-          methods's class  */
+       u = utf_new_char(name);
 
-       sym = NULL;
+       /* release memory */
 
-       /* normally addresses are aligned to 4, 8 or 16 bytes */
+       dump_release(dumpsize);
 
-       key  = ((u4) (ptrint) m->class->classloader) >> 4;    /* align to 16-byte */
-       slot = key & (hashtable_library.size - 1);
-       le   = hashtable_library.ptr[slot];
+       return u;
+}
 
-       /* iterate through loaders in this hash slot */
 
-       while ((le != NULL) && (sym == NULL)) {
-               /* iterate through names in this loader */
+/* native_method_register ******************************************************
 
-               ne = le->namelink;
-                       
-               while ((ne != NULL) && (sym == NULL)) {
-                       sym = lt_dlsym(ne->handle, name);
+   Register a native method in the native method table.
 
-                       if (sym == NULL)
-                               sym = lt_dlsym(ne->handle, newname);
+*******************************************************************************/
 
-                       ne = ne->hashlink;
+void native_method_register(utf *classname, const JNINativeMethod *methods,
+                                                       int32_t count)
+{
+       native_methods_node_t *nmn;
+       utf                   *name;
+       utf                   *descriptor;
+       int32_t                i;
+
+       /* insert all methods passed */
+
+       for (i = 0; i < count; i++) {
+               if (opt_verbosejni) {
+                       printf("[Registering JNI native method ");
+                       utf_display_printable_ascii_classname(classname);
+                       printf(".%s]\n", methods[i].name);
                }
 
-               le = le->hashlink;
-       }
+               /* generate the utf8 names */
 
-       if (sym != NULL)
-               if (opt_verbosejni)
-                       printf("JNI ]\n");
+               name       = utf_new_char(methods[i].name);
+               descriptor = utf_new_char(methods[i].signature);
 
+               /* allocate a new tree node */
 
-       /* If not found, try to find the native function symbol in the
-          main program. */
+               nmn = NEW(native_methods_node_t);
 
-       if (sym == NULL) {
-               sym = lt_dlsym(mainhandle, name);
+               nmn->classname  = classname;
+               nmn->name       = name;
+               nmn->descriptor = descriptor;
+               nmn->function   = (functionptr) (ptrint) methods[i].fnPtr;
 
-               if (sym == NULL)
-                       sym = lt_dlsym(mainhandle, newname);
+               /* insert the method into the tree */
 
-               if (sym != NULL)
-                       if (opt_verbosejni)
-                               printf("internal ]\n");
+               avl_insert(tree_native_methods, nmn);
        }
+}
 
 
-       /* no symbol found? throw exception */
+/* native_method_find **********************************************************
 
-       if (sym == NULL) {
-               if (opt_verbosejni)
-                       printf("failed ]\n");
+   Find a native method in the native method table.
 
-               *exceptionptr =
-                       new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
-                                                                        m->name);
-       }
+*******************************************************************************/
 
-       /* release memory */
+static functionptr native_method_find(methodinfo *m)
+{
+       native_methods_node_t  tmpnmn;
+       native_methods_node_t *nmn;
 
-       dump_release(dumpsize);
+       /* fill the temporary structure used for searching the tree */
+
+       tmpnmn.classname  = m->class->name;
+       tmpnmn.name       = m->name;
+       tmpnmn.descriptor = m->descriptor;
 
-       return (functionptr) (ptrint) sym;
+       /* find the entry in the AVL-tree */
+
+       nmn = avl_find(tree_native_methods, &tmpnmn);
+
+       if (nmn == NULL)
+               return NULL;
+
+       return nmn->function;
 }
-#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
-/* native_new_and_init *********************************************************
+/* native_library_open *********************************************************
+
+   Open a native library with the given utf8 name.
 
-   Creates a new object on the heap and calls the initializer.
-   Returns the object pointer or NULL if memory is exhausted.
-                       
 *******************************************************************************/
 
-java_objectheader *native_new_and_init(classinfo *c)
+#if !defined(WITH_STATIC_CLASSPATH)
+lt_dlhandle native_library_open(utf *filename)
 {
-       methodinfo *m;
-       java_objectheader *o;
+       lt_dlhandle handle;
 
-       if (!c)
-               return *exceptionptr;
+       if (opt_verbosejni) {
+               printf("[Loading native library ");
+               utf_display_printable_ascii(filename);
+               printf(" ... ");
+       }
 
-       /* create object */
+       /* try to open the library */
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       handle = lt_dlopen(filename->text);
 
-       /* try to find the initializer */
+       if (handle == NULL) {
+               if (opt_verbose) {
+                       log_start();
+                       log_print("native_library_load: lt_dlopen failed: ");
+                       log_print(lt_dlerror());
+                       log_finish();
+               }
 
-       m = class_findmethod(c, utf_init, utf_void__void);
-                                                     
-       /* ATTENTION: returning the object here is ok, since the class may
-       not have an initializer */
+               return NULL;
+       }
 
-       if (!m)
-               return o;
+       return handle;
+}
+#endif
 
-       /* call initializer */
 
-       ASM_CALLJAVAFUNCTION(m, o, NULL, NULL, NULL);
+/* native_library_add **********************************************************
 
-       return o;
-}
+   Adds an entry to the native library hashtable.
 
+*******************************************************************************/
 
-java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
+#if !defined(WITH_STATIC_CLASSPATH)
+void native_library_add(utf *filename, java_objectheader *loader,
+                                               lt_dlhandle handle)
 {
-       methodinfo *m;
-       java_objectheader *o;
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne; /* library name                       */
+       u4   key;                           /* hashkey                            */
+       u4   slot;                          /* slot in hashtable                  */
 
-       if (!c)
-               return *exceptionptr;
+       LOCK_MONITOR_ENTER(hashtable_library->header);
 
-       /* create object */
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
 
-       o = builtin_new(c);
+       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
-       if (!o)
-               return NULL;
+       /* search external hash chain for the entry */
 
-       /* find initializer */
+       while (le) {
+               if (le->loader == loader)
+                       break;
 
-       m = class_resolveclassmethod(c,
-                                                                utf_init,
-                                                                utf_java_lang_String__void,
-                                                                NULL,
-                                                                true);
+               le = le->hashlink;                  /* next element in external chain */
+       }
 
-       /* initializer not found */
+       /* no loader found? create a new entry */
 
-       if (!m)
-               return NULL;
+       if (le == NULL) {
+               le = NEW(hashtable_library_loader_entry);
 
-       /* call initializer */
+               le->loader   = loader;
+               le->namelink = NULL;
 
-       ASM_CALLJAVAFUNCTION(m, o, s, NULL, NULL);
+               /* insert entry into hashtable */
 
-       return o;
+               le->hashlink =
+                       (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
+               hashtable_library->ptr[slot] = le;
+
+               /* update number of hashtable-entries */
+
+               hashtable_library->entries++;
+       }
+
+
+       /* search for library name */
+
+       ne = le->namelink;
+
+       while (ne) {
+               if (ne->name == filename) {
+                       LOCK_MONITOR_EXIT(hashtable_library->header);
+
+                       return;
+               }
+
+               ne = ne->hashlink;                  /* next element in external chain */
+       }
+
+       /* not found? add the library name to the classloader */
+
+       ne = NEW(hashtable_library_name_entry);
+
+       ne->name   = filename;
+       ne->handle = handle;
+
+       /* insert entry into external chain */
+
+       ne->hashlink = le->namelink;
+       le->namelink = ne;
+
+       LOCK_MONITOR_EXIT(hashtable_library->header);
 }
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
-java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
+/* native_library_find *********************************************************
+
+   Find an entry in the native library hashtable.
+
+*******************************************************************************/
+
+#if !defined(WITH_STATIC_CLASSPATH)
+hashtable_library_name_entry *native_library_find(utf *filename,
+                                                                                                 java_objectheader *loader)
 {
-       methodinfo *m;
-       java_objectheader *o;
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne; /* library name                       */
+       u4   key;                           /* hashkey                            */
+       u4   slot;                          /* slot in hashtable                  */
 
-       if (!c)
-               return *exceptionptr;
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
 
-       /* create object */
+       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       /* search external hash chain for the entry */
 
-       /* find initializer */
+       while (le) {
+               if (le->loader == loader)
+                       break;
 
-       m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
+               le = le->hashlink;                  /* next element in external chain */
+       }
 
-       /* initializer not found  */
+       /* no loader found? return NULL */
 
-       if (!m)
+       if (le == NULL)
                return NULL;
 
-       /* call initializer */
+       /* search for library name */
 
-       ASM_CALLJAVAFUNCTION(m, o, (void *) (ptrint) i, NULL, NULL);
+       ne = le->namelink;
 
-       return o;
+       while (ne) {
+               if (ne->name == filename)
+                       return ne;
+
+               ne = ne->hashlink;                  /* next element in external chain */
+       }
+
+       /* return entry, if no entry was found, ne is NULL */
+
+       return ne;
 }
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
-java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
-{
-       methodinfo *m;
-       java_objectheader *o;
+/* native_findfunction *********************************************************
 
-       if (!c)
-               return *exceptionptr;
+   Looks up a method (must have the same class name, method name,
+   descriptor and 'static'ness) and returns a function pointer to it.
+   Returns: function pointer or NULL (if there is no such method)
 
-       /* create object */
+   Remark: For faster operation, the names/descriptors are converted
+   from C strings to Unicode the first time this function is called.
 
-       o = builtin_new(c);
+*******************************************************************************/
+
+#if defined(WITH_STATIC_CLASSPATH)
+functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
+                                                               bool isstatic)
+{
+       /* entry of table for fast string comparison */
+       struct nativecompref *n;
+       s4 i;
+
+       isstatic = isstatic ? true : false;
        
-       if (!o)
-               return NULL;
+       if (!nativecompdone) {
+               for (i = 0; i < NATIVETABLESIZE; i++) {
+                       nativecomptable[i].classname  = 
+                               utf_new_char(nativetable[i].classname);
 
-       /* find initializer */
+                       nativecomptable[i].methodname = 
+                               utf_new_char(nativetable[i].methodname);
 
-       m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
-                                                     
-       /* initializer not found */
+                       nativecomptable[i].descriptor =
+                               utf_new_char(nativetable[i].descriptor);
 
-       if (!m)
-               return NULL;
+                       nativecomptable[i].isstatic   = nativetable[i].isstatic;
+                       nativecomptable[i].func       = nativetable[i].func;
+               }
 
-       /* call initializer */
+               nativecompdone = true;
+       }
 
-       ASM_CALLJAVAFUNCTION(m, o, t, NULL, NULL);
+       for (i = 0; i < NATIVETABLESIZE; i++) {
+               n = &(nativecomptable[i]);
 
-       return o;
+               if (cname == n->classname && mname == n->methodname &&
+                   desc == n->descriptor && isstatic == n->isstatic)
+                       return n->func;
+       }
+
+       /* no function was found, throw exception */
+
+       exceptions_throw_unsatisfiedlinkerror(mname);
+
+       return NULL;
 }
+#endif /* defined(WITH_STATIC_CLASSPATH) */
 
 
-/* native_get_parametertypes ***************************************************
+/* native_resolve_function *****************************************************
 
-   Use the descriptor of a method to generate a java/lang/Class array
-   which contains the classes of the parametertypes of the method.
+   Resolves a native function, maybe from a dynamic library.
 
 *******************************************************************************/
 
-java_objectarray *native_get_parametertypes(methodinfo *m)
+functionptr native_resolve_function(methodinfo *m)
 {
-       methoddesc       *md;
-       typedesc         *paramtypes;
-       s4                paramcount;
-    java_objectarray *oa;
-       s4                i;
+       java_objectheader              *cl;
+       utf                            *name;
+       utf                            *newname;
+       functionptr                     f;
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne;
+       u4                              key;    /* hashkey                        */
+       u4                              slot;   /* slot in hashtable              */
+#if defined(WITH_CLASSPATH_SUN)
+       methodinfo                     *method_findNative;
+       java_objectheader              *s;
+#endif
+
+       cl = m->class->classloader;
+
+       /* verbose output */
+
+       if (opt_verbosejni) {
+               printf("[Dynamic-linking native method ");
+               utf_display_printable_ascii_classname(m->class->name);
+               printf(".");
+               utf_display_printable_ascii(m->name);
+               printf(" ... ");
+       }
+
+       /* generate method symbol string */
+
+       name = native_method_symbol(m->class->name, m->name);
+
+       /* generate overloaded function (having the types in it's name)           */
+
+       newname = native_make_overloaded_function(name, m->descriptor);
+
+       /* check the library hash entries of the classloader of the
+          methods's class  */
+
+       f = NULL;
+
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
+
+       key  = ((u4) (ptrint) cl) >> 4;                       /* align to 16-byte */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
+
+       /* iterate through loaders in this hash slot */
+
+       while ((le != NULL) && (f == NULL)) {
+               /* iterate through names in this loader */
+
+               ne = le->namelink;
+                       
+               while ((ne != NULL) && (f == NULL)) {
+                       f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
+
+                       if (f == NULL)
+                               f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
 
-       md = m->parseddesc;
+                       ne = ne->hashlink;
+               }
+
+               le = le->hashlink;
+       }
 
-       /* is the descriptor fully parsed? */
+#if defined(WITH_CLASSPATH_SUN)
+       if (f == NULL) {
+               /* We can resolve the function directly from
+                  java.lang.ClassLoader as it's a static function. */
+               /* XXX should be done in native_init */
 
-       if (!m->parseddesc->params)
-               if (!descriptor_params_from_paramtypes(md, m->flags))
+               method_findNative =
+                       class_resolveclassmethod(class_java_lang_ClassLoader,
+                                                                        utf_findNative,
+                                                                        utf_java_lang_ClassLoader_java_lang_String__J,
+                                                                        class_java_lang_ClassLoader,
+                                                                        true);
+
+               if (method_findNative == NULL)
                        return NULL;
 
-       paramtypes = md->paramtypes;
-       paramcount = md->paramcount;
+               /* try the normal name */
+
+               s = javastring_new(name);
+
+               f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
+                                                                                                                NULL, cl, s);
 
-       /* skip `this' pointer */
+               /* if not found, try the mangled name */
 
-       if (!(m->flags & ACC_STATIC)) {
-               paramtypes++;
-               paramcount--;
+               if (f == NULL) {
+                       s = javastring_new(newname);
+
+                       f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
+                                                                                                                        NULL, cl, s);
+               }
        }
+#endif
+
+       if (f != NULL)
+               if (opt_verbosejni)
+                       printf("JNI ]\n");
 
-       /* create class-array */
+       /* If not found, try to find the native function symbol in the
+          main program. */
 
-       oa = builtin_anewarray(paramcount, class_java_lang_Class);
+       if (f == NULL) {
+               f = native_method_find(m);
 
-       if (!oa)
-               return NULL;
+               if (f != NULL)
+                       if (opt_verbosejni)
+                               printf("internal ]\n");
+       }
 
-    /* get classes */
+#if defined(ENABLE_JVMTI)
+       /* fire Native Method Bind event */
+       if (jvmti) jvmti_NativeMethodBind(m, f, &f);
+#endif
 
-       for (i = 0; i < paramcount; i++)
-               if (!resolve_class_from_typedesc(&paramtypes[i], true, false,
-                                                                                (classinfo **) &oa->data[i]))
-                       return NULL;
+       /* no symbol found? throw exception */
 
-       return oa;
-}
+       if (f == NULL) {
+               if (opt_verbosejni)
+                       printf("failed ]\n");
 
+               exceptions_throw_unsatisfiedlinkerror(m->name);
+       }
 
-/* native_get_exceptiontypes ***************************************************
+       return f;
+}
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
-   Get the exceptions which can be thrown by a method.
 
+/* native_new_and_init *********************************************************
+
+   Creates a new object on the heap and calls the initializer.
+   Returns the object pointer or NULL if memory is exhausted.
+                       
 *******************************************************************************/
 
-java_objectarray *native_get_exceptiontypes(methodinfo *m)
+java_objectheader *native_new_and_init(classinfo *c)
 {
-       java_objectarray *oa;
-       classinfo        *c;
-       u2                i;
+       methodinfo *m;
+       java_objectheader *o;
 
-       /* create class-array */
+       if (c == NULL)
+               vm_abort("native_new_and_init: c == NULL");
 
-       oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
+       /* create object */
 
-       if (!oa)
+       o = builtin_new(c);
+       
+       if (o == NULL)
                return NULL;
 
-       for (i = 0; i < m->thrownexceptionscount; i++) {
-               if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
-                                                                                  resolveEager, true, false, &c))
-                       return NULL;
+       /* try to find the initializer */
 
-               oa->data[i] = (java_objectheader *) c;
-       }
+       m = class_findmethod(c, utf_init, utf_void__void);
+                                                     
+       /* ATTENTION: returning the object here is ok, since the class may
+       not have an initializer */
 
-       return oa;
-}
+       if (m == NULL)
+               return o;
 
+       /* call initializer */
 
-/* native_get_returntype *******************************************************
+       (void) vm_call_method(m, o);
 
-   Get the returntype class of a method.
+       return o;
+}
 
-*******************************************************************************/
 
-classinfo *native_get_returntype(methodinfo *m)
+java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
 {
-       classinfo *c;
+       methodinfo        *m;
+       java_objectheader *o;
+
+       if (c == NULL)
+               vm_abort("native_new_and_init_string: c == NULL");
+
+       /* create object */
+
+       o = builtin_new(c);
+
+       if (o == NULL)
+               return NULL;
 
-       if (!resolve_class_from_typedesc(&(m->parseddesc->returntype), true, false,
-                                                                        &c))
+       /* find initializer */
+
+       m = class_findmethod(c, utf_init, utf_java_lang_String__void);
+
+       /* initializer not found */
+
+       if (m == NULL)
                return NULL;
 
-       return c;
+       /* call initializer */
+
+       (void) vm_call_method(m, o, s);
+
+       return o;
 }