* configure.ac,
[cacao.git] / src / native / native.c
index 84d967d76d2093f847840779b80dfb5d9d306fc8..5cc8cc0247df3bd18c91878190daecb2349099a0 100644 (file)
@@ -1,9 +1,9 @@
-/* native/native.c - table of native functions
+/* src/native/native.c - table of native functions
 
-   Copyright (C) 1996-2005 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
+   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
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
-
-   Authors: Reinhard Grafl
-            Roman Obermaisser
-            Andreas Krall
-
-   The .hh files created with the header file generator are all
-   included here as are the C functions implementing these methods.
-
-   $Id: native.c 1875 2005-01-21 09:37:33Z twisti $
+   $Id: native.c 8132 2007-06-22 11:15:47Z twisti $
 
 */
 
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-#include <math.h>
-#include <string.h>
-#include <assert.h>
-#include <sys/time.h>
-#include <utime.h>
+#include "config.h"
 
-/* Include files for IO functions */
+#include <assert.h>
+#include <ctype.h>
 
-#include <fcntl.h>
-#include <dirent.h>
-#include <sys/types.h>
-#ifdef _OSF_SOURCE 
-#include <sys/mode.h>
+#if !defined(WITH_STATIC_CLASSPATH)
+# include <ltdl.h>
 #endif
-#include <sys/stat.h>
 
-#include "config.h"
+#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/loader.h"
-#include "vm/options.h"
-#include "vm/tables.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 ******************************************/
-
-/* XXX quick hack? */
-#if defined(USE_GTK)
-#include "native/vm/GtkComponentPeer.c"
-#include "native/vm/GtkScrollPanePeer.c"
-#include "native/vm/GtkFileDialogPeer.c"
+#if defined(ENABLE_JVMTI)
+#include "native/jvmti/cacaodbg.h"
 #endif
 
-#include "nativetable.inc"
-
-
-/* for java-string to char conversion */
-#define MAXSTRINGSIZE 1000                          
-
-
-/******************** systemclasses required for native methods ***************/
 
-classinfo *class_java_lang_Class;
-classinfo *class_java_lang_VMClass;
-classinfo *class_java_lang_System;
-classinfo *class_java_lang_ClassLoader;
-classinfo *class_gnu_java_lang_SystemClassLoader;
-classinfo *class_java_lang_SecurityManager;
-classinfo *class_java_lang_Double;
-classinfo *class_java_lang_Float;
-classinfo *class_java_lang_Long;
-classinfo *class_java_lang_Byte;
-classinfo *class_java_lang_Short;
-classinfo *class_java_lang_Boolean;
-classinfo *class_java_lang_Void;
-classinfo *class_java_lang_Character;
-classinfo *class_java_lang_Integer;
-
-methodinfo *method_vmclass_init;
-
-
-/* the system classloader object */
-struct java_lang_ClassLoader *SystemClassLoader = NULL;
+/* include table of native functions ******************************************/
 
-/* for raising exceptions from native methods */
-#if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
-java_objectheader* _exceptionptr = NULL;
+#if defined(WITH_STATIC_CLASSPATH)
+# include "native/nativetable.inc"
 #endif
 
-/************* use classinfo structure as java.lang.Class object **************/
 
-void use_class_as_object(classinfo *c) 
-{
-       if (!c->classvftbl) {
-               /* is the class loaded */
-               if (!c->loaded)
-                       if (!class_load(c))
-                               panic("Class could not be loaded in use_class_as_object");
-               /* is the class linked */
-               if (!c->linked)
-                       if (!class_link(c))
-                               panic("Class could not be linked in use_class_as_object");
-
-               /*if (class_java_lang_Class ==0) panic("java/lang/Class not loaded in use_class_as_object");
-               if (class_java_lang_Class->vftbl ==0) panic ("vftbl == 0 in use_class_as_object");*/
-               c->header.vftbl = class_java_lang_Class->vftbl;
-               c->classvftbl = true;
-               /*printf("use_class_as_object: %s\n",c->name->text);*/
-       }
-            
-}
+/* tables for methods *********************************************************/
 
-
-/************************** tables for methods ********************************/
-
-#undef JOWENN_DEBUG
-#undef JOWENN_DEBUG1
-
-#ifdef STATIC_CLASSPATH
+#if defined(WITH_STATIC_CLASSPATH)
 #define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
 
 /* table for fast string comparison */
@@ -156,1349 +91,862 @@ static bool nativecompdone = false;
 #endif
 
 
-/* XXX don't define this in a header file!!! */
-
-static struct nativeCall nativeCalls[] =
-{
-#include "nativecalls.inc"
-};
+/* global variables ***********************************************************/
 
-#define NATIVECALLSSIZE    (sizeof(nativeCalls) / sizeof(struct nativeCall))
+static avl_tree_t *tree_native_methods;
+static hashtable *hashtable_library;
 
-struct nativeCompCall nativeCompCalls[NATIVECALLSSIZE];
 
-/******************************************************************************/
+/* prototypes *****************************************************************/
 
-/**include "natcalls.h" **/
+static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
 
 
-/*********************** function: native_loadclasses **************************
+/* native_init *****************************************************************
 
-       load classes required for native methods        
+   Initializes the native subsystem.
 
 *******************************************************************************/
 
-void native_loadclasses()
+bool native_init(void)
 {
-       static int classesLoaded = 0; /*temporary hack JoWenn*/
-       void *p;
+#if !defined(WITH_STATIC_CLASSPATH)
+       /* initialize libltdl */
 
-       if (classesLoaded)
-               return;
+       if (lt_dlinit())
+               vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
 
-       classesLoaded = 1;
+       /* initialize library hashtable, 10 entries should be enough */
 
-#if !defined(STATIC_CLASSPATH)
-       /* 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;
-#endif
-
-       class_java_lang_Cloneable =
-               class_new(utf_new_char("java/lang/Cloneable"));
-       class_load(class_java_lang_Cloneable);
-       class_link(class_java_lang_Cloneable);
-
-       class_java_lang_Class =
-               class_new(utf_new_char("java/lang/Class"));
-       class_load(class_java_lang_Class);
-       class_link(class_java_lang_Class);
-
-       class_java_lang_VMClass =
-               class_new(utf_new_char("java/lang/VMClass"));
-       class_load(class_java_lang_VMClass);
-       class_link(class_java_lang_VMClass);
-
-       class_java_lang_ClassLoader =
-               class_new(utf_new_char("java/lang/ClassLoader"));
-       class_load(class_java_lang_ClassLoader);
-       class_link(class_java_lang_ClassLoader);
-
-       /* load classes for wrapping primitive types */
-       class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
-       class_load(class_java_lang_Double);
-       class_link(class_java_lang_Double);
-
-       class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
-       class_load(class_java_lang_Float);
-       class_link(class_java_lang_Float);
-
-       class_java_lang_Character =     class_new(utf_new_char("java/lang/Character"));
-       class_load(class_java_lang_Character);
-       class_link(class_java_lang_Character);
-
-       class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
-       class_load(class_java_lang_Integer);
-       class_link(class_java_lang_Integer);
-
-       class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
-       class_load(class_java_lang_Long);
-       class_link(class_java_lang_Long);
-
-       class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
-       class_load(class_java_lang_Byte);
-       class_link(class_java_lang_Byte);
-
-       class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
-       class_load(class_java_lang_Short);
-       class_link(class_java_lang_Short);
-
-       class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
-       class_load(class_java_lang_Boolean);
-       class_link(class_java_lang_Boolean);
-
-       class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
-       class_load(class_java_lang_Void);
-       class_link(class_java_lang_Void);
-}
-
-
-/*****************************************************************************
+       hashtable_library = NEW(hashtable);
 
-       create systemclassloader object and initialize instance fields  
+       hashtable_create(hashtable_library, 10);
+#endif
 
-******************************************************************************/
+       /* initialize the native methods table */
 
-void init_systemclassloader() 
-{
-       if (!SystemClassLoader) {
-               native_loadclasses();
+       tree_native_methods = avl_create(&native_tree_native_methods_comparator);
 
-               /* create object and call initializer */
-               SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
+       /* everything's ok */
 
-               /* systemclassloader has no parent */
-               SystemClassLoader->parent      = NULL;
-               SystemClassLoader->initialized = true;
-       }
+       return true;
 }
 
 
-/*********************** Function: native_findfunction *************************
+/* native_tree_native_methods_comparator ***************************************
+
+   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
 
 *******************************************************************************/
 
-functionptr native_findfunction(utf *cname, utf *mname, 
-                                                               utf *desc, bool isstatic)
+static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
 {
-#ifdef STATIC_CLASSPATH
-       int i;
-       /* entry of table for fast string comparison */
-       struct nativecompref *n;
-       /* for warning message if no function is found */
-       char *buffer;                   
-       int buffer_len;
+       const native_methods_node_t *treenmn;
+       const native_methods_node_t *nmn;
 
-       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);
-                       nativecomptable[i].isstatic   = 
-                               nativetable[i].isstatic;
-                       nativecomptable[i].func       = 
-                               nativetable[i].func;
-               }
-               nativecompdone = true;
-       }
+       treenmn = treenode;
+       nmn     = node;
 
-#ifdef JOWENN_DEBUG
-       buffer_len = 
-               utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
-       
-       buffer = MNEW(char, buffer_len);
+       /* these are for walking the tree */
 
-       strcpy(buffer, "searching matching function in native table:");
-       utf_sprint(buffer+strlen(buffer), mname);
-       strcpy(buffer+strlen(buffer), ": ");
-       utf_sprint(buffer+strlen(buffer), desc);
-       strcpy(buffer+strlen(buffer), " for class ");
-       utf_sprint(buffer+strlen(buffer), cname);
+       if (treenmn->classname < nmn->classname)
+               return -1;
+       else if (treenmn->classname > nmn->classname)
+               return 1;
 
-       log_text(buffer);       
+       if (treenmn->name < nmn->name)
+               return -1;
+       else if (treenmn->name > nmn->name)
+               return 1;
 
-       MFREE(buffer, char, buffer_len);
-#endif
-               
-       for (i = 0; i < NATIVETABLESIZE; i++) {
-               n = &(nativecomptable[i]);
+       if (treenmn->descriptor < nmn->descriptor)
+               return -1;
+       else if (treenmn->descriptor > nmn->descriptor)
+               return 1;
 
-               if (cname == n->classname && mname == n->methodname &&
-                   desc == n->descriptor && isstatic == n->isstatic)
-                       return n->func;
-#ifdef JOWENN_DEBUG
-                       else {
-                               if (cname == n->classname && mname == n->methodname )  log_text("static and descriptor mismatch");
-                       
-                               else {
-                                       buffer_len = 
-                                         utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
-       
-                                       buffer = MNEW(char, buffer_len);
-
-                                       strcpy(buffer, "comparing with:");
-                                       utf_sprint(buffer+strlen(buffer), n->methodname);
-                                       strcpy (buffer+strlen(buffer), ": ");
-                                       utf_sprint(buffer+strlen(buffer), n->descriptor);
-                                       strcpy(buffer+strlen(buffer), " for class ");
-                                       utf_sprint(buffer+strlen(buffer), n->classname);
-
-                                       log_text(buffer);       
-
-                                       MFREE(buffer, char, buffer_len);
-                               }
-                       } 
-#endif
-       }
-
-               
-       /* no function was found, display warning */
-
-       buffer_len = 
-               utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
-
-       buffer = MNEW(char, buffer_len);
+       /* all pointers are equal, we have found the entry */
 
-       strcpy(buffer, "warning: native function ");
-       utf_sprint(buffer + strlen(buffer), mname);
-       strcpy(buffer + strlen(buffer), ": ");
-       utf_sprint(buffer + strlen(buffer), desc);
-       strcpy(buffer + strlen(buffer), " not found in class ");
-       utf_sprint(buffer + strlen(buffer), cname);
-
-       log_text(buffer);       
-
-       MFREE(buffer, char, buffer_len);
-
-/*     exit(1); */
-
-       /* keep compiler happy */
-       return NULL;
-#else
-/* dynamic classpath */
-  return 0;
-#endif
+       return 0;
 }
 
 
-/********************** function: javastring_new *******************************
-
-       creates a new object of type java/lang/String with the text of 
-       the specified utf8-string
+/* native_make_overloaded_function *********************************************
 
-       return: pointer to the string or NULL if memory is exhausted.   
+   XXX
 
 *******************************************************************************/
 
-java_lang_String *javastring_new(utf *u)
+#if !defined(WITH_STATIC_CLASSPATH)
+static utf *native_make_overloaded_function(utf *name, utf *descriptor)
 {
-       char *utf_ptr;                  /* current utf character in utf string    */
-       u4 utflength;                   /* length of utf-string if uncompressed   */
-       java_lang_String *s;                /* result-string                          */
-       java_chararray *a;
-       s4 i;
+       char *newname;
+       s4    namelen;
+       char *utf_ptr;
+       u2    c;
+       s4    i;
+       s4    dumpsize;
+       utf  *u;
+
+       /* mark memory */
+
+       dumpsize = dump_size();
+
+       utf_ptr = descriptor->text;
+       namelen = strlen(name->text) + strlen("__") + strlen("0");
+
+       /* calculate additional length */
+
+       while ((c = utf_nextu2(&utf_ptr)) != ')') {
+               switch (c) {
+               case 'Z':
+               case 'B':
+               case 'C':
+               case 'S':
+               case 'I':
+               case 'J':
+               case 'F':
+               case 'D':
+                       namelen++;
+                       break;
+               case '[':
+                       namelen += 2;
+                       break;
+               case 'L':
+                       namelen++;
+                       while (utf_nextu2(&utf_ptr) != ';')
+                               namelen++;
+                       namelen += 2;
+                       break;
+               case '(':
+                       break;
+               default:
+                       assert(0);
+               }
+       }
 
-       if (!u) {
-               *exceptionptr = new_nullpointerexception();
-               return NULL;
+       /* reallocate memory */
+
+       i = strlen(name->text);
+
+       newname = DMNEW(char, namelen);
+       MCOPY(newname, name->text, char, i);
+
+       utf_ptr = descriptor->text;
+
+       newname[i++] = '_';
+       newname[i++] = '_';
+
+       while ((c = utf_nextu2(&utf_ptr)) != ')') {
+               switch (c) {
+               case 'Z':
+               case 'B':
+               case 'C':
+               case 'S':
+               case 'J':
+               case 'I':
+               case 'F':
+               case 'D':
+                       newname[i++] = c;
+                       break;
+               case '[':
+                       newname[i++] = '_';
+                       newname[i++] = '3';
+                       break;
+               case 'L':
+                       newname[i++] = 'L';
+                       while ((c = utf_nextu2(&utf_ptr)) != ';')
+                               if (((c >= 'a') && (c <= 'z')) ||
+                                       ((c >= 'A') && (c <= 'Z')) ||
+                                       ((c >= '0') && (c <= '9')))
+                                       newname[i++] = c;
+                               else
+                                       newname[i++] = '_';
+                       newname[i++] = '_';
+                       newname[i++] = '2';
+                       break;
+               case '(':
+                       break;
+               default:
+                       assert(0);
+               }
        }
 
-       utf_ptr = u->text;
-       utflength = utf_strlen(u);
+       /* close string */
 
-       s = (java_lang_String *) builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(utflength);
+       newname[i] = '\0';
 
-       /* javastring or character-array could not be created */
-       if (!a || !s)
-               return NULL;
+       /* make a utf-string */
 
-       /* decompress utf-string */
-       for (i = 0; i < utflength; i++)
-               a->data[i] = utf_nextu2(&utf_ptr);
-       
-       /* set fields of the javastring-object */
-       s->value  = a;
-       s->offset = 0;
-       s->count  = utflength;
+       u = utf_new_char(newname);
 
-       return s;
-}
+       /* release memory */
 
+       dump_release(dumpsize);
 
-/********************** function: javastring_new_char **************************
+       return u;
+}
 
-       creates a new java/lang/String object which contains the convertet
-       C-string passed via text.
 
-       return: the object pointer or NULL if memory is exhausted.
+/* native_insert_char **********************************************************
+
+   Inserts the passed UTF character into the native method name.  If
+   necessary it is escaped properly.
 
 *******************************************************************************/
 
-java_lang_String *javastring_new_char(const char *text)
+static s4 native_insert_char(char *name, u4 pos, u2 c)
 {
+       s4 val;
        s4 i;
-       s4 len;                /* length of the string */
-       java_lang_String *s;   /* result-string */
-       java_chararray *a;
-
-       if (!text) {
-               *exceptionptr = new_nullpointerexception();
-               return NULL;
-       }
-
-       len = strlen(text);
 
-       s = (java_lang_String *) builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(len);
+       switch (c) {
+       case '/':
+       case '.':
+               /* replace '/' or '.' with '_' */
+               name[pos] = '_';
+               break;
+
+       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;
+                       }
 
-       /* javastring or character-array could not be created */
-       if (!a || !s)
-               return NULL;
+                       pos += 4;
+               }
+               break;
+       }
 
-       /* copy text */
-       for (i = 0; i < len; i++)
-               a->data[i] = text[i];
-       
-       /* set fields of the javastring-object */
-       s->value  = a;
-       s->offset = 0;
-       s->count  = len;
+       /* return the new buffer index */
 
-       return s;
+       return pos;
 }
 
 
-/************************* function javastring_tochar **************************
+/* native_method_symbol ********************************************************
 
-       converts a Java string into a C string.
-       
-       return: pointer to C string
-       
-       Caution: every call of this function overwrites the previous string !!!
-       
-*******************************************************************************/
+   Generate a method-symbol string out of the class name and the
+   method name.
 
-static char stringbuffer[MAXSTRINGSIZE];
+*******************************************************************************/
 
-char *javastring_tochar(java_objectheader *so) 
+static utf *native_method_symbol(utf *classname, utf *methodname)
 {
-       java_lang_String *s = (java_lang_String *) so;
-       java_chararray *a;
-       s4 i;
-       
-       if (!s)
-               return "";
+       char *name;
+       s4    namelen;
+       char *utf_ptr;
+       char *utf_endptr;
+       u2    c;
+       u4    pos;
+       s4    dumpsize;
+       utf  *u;
 
-       a = s->value;
+       /* mark memory */
 
-       if (!a)
-               return "";
+       dumpsize = dump_size();
 
-       if (s->count > MAXSTRINGSIZE)
-               return "";
+       /* 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). */
 
-       for (i = 0; i < s->count; i++)
-               stringbuffer[i] = a->data[s->offset + i];
+       namelen =
+               strlen("Java_") +
+               utf_get_number_of_u2s(classname) * 6 +
+               strlen("_") +
+               utf_get_number_of_u2s(methodname) * 6 +
+               strlen("0");
 
-       stringbuffer[i] = '\0';
+       /* allocate memory */
 
-       return stringbuffer;
-}
+       name = DMNEW(char, namelen);
 
+       /* generate name of native functions */
 
-/****************** function class_findfield_approx ****************************
-       
-       searches in 'classinfo'-structure for a field with the
-       specified name
+       strcpy(name, "Java_");
+       pos = strlen("Java_");
 
-*******************************************************************************/
-fieldinfo *class_findfield_approx(classinfo *c, utf *name)
-{
-       s4 i;
+       utf_ptr    = classname->text;
+       utf_endptr = UTF_END(classname);
 
-       for (i = 0; i < c->fieldscount; i++) {
-               /* compare field names */
-               if ((c->fields[i].name == name))
-                       return &(c->fields[i]);
+       for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
+               c   = *utf_ptr;
+               pos = native_insert_char(name, pos, c);
        }
 
-       /* field was not found, raise exception */      
-       *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
-
-       return NULL;
-}
+       /* seperator between class and method */
 
+       name[pos++] = '_';
 
-s4 class_findfield_index_approx(classinfo *c, utf *name)
-{
-       s4 i;
+       utf_ptr    = methodname->text;
+       utf_endptr = UTF_END(methodname);
 
-       for (i = 0; i < c->fieldscount; i++) {
-               /* compare field names */
-               if ((c->fields[i].name == name))
-                       return i;
+       for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
+               c   = *utf_ptr;
+               pos = native_insert_char(name, pos, c);
        }
 
-       /* field was not found, raise exception */      
-       *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
-
-       return -1;
-}
-
-
-/********************** function: 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_objectheader *native_new_and_init(classinfo *c)
-{
-       methodinfo *m;
-       java_objectheader *o;
+       /* close string */
 
-       if (!c)
-               return *exceptionptr;
+       name[pos] = '\0';
 
-       /* create object */
+       /* check for an buffer overflow */
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       assert(pos <= namelen);
 
-       /* find initializer */
+       /* make a utf-string */
 
-       m = class_findmethod(c, utf_new_char("<init>"), utf_new_char("()V"));
-                                                     
-       /* initializer not found */
+       u = utf_new_char(name);
 
-       if (!m)
-               return o;
-
-       /* call initializer */
+       /* release memory */
 
-       asm_calljavafunction(m, o, NULL, NULL, NULL);
+       dump_release(dumpsize);
 
-       return o;
+       return u;
 }
 
 
-java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
-{
-       methodinfo *m;
-       java_objectheader *o;
-
-       if (!c)
-               return *exceptionptr;
+/* native_method_register ******************************************************
 
-       /* create object */
+   Register a native method in the native method table.
 
-       o = builtin_new(c);
+*******************************************************************************/
 
-       if (!o)
-               return NULL;
+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);
+               }
 
-       /* find initializer */
+               /* generate the utf8 names */
 
-       m = class_resolveclassmethod(c,
-                                                                utf_new_char("<init>"),
-                                                                utf_new_char("(Ljava/lang/String;)V"),
-                                                                NULL,
-                                                                true);
+               name       = utf_new_char(methods[i].name);
+               descriptor = utf_new_char(methods[i].signature);
 
-       /* initializer not found */
+               /* allocate a new tree node */
 
-       if (!m)
-               return NULL;
+               nmn = NEW(native_methods_node_t);
 
-       /* call initializer */
+               nmn->classname  = classname;
+               nmn->name       = name;
+               nmn->descriptor = descriptor;
+               nmn->function   = (functionptr) (ptrint) methods[i].fnPtr;
 
-       asm_calljavafunction(m, o, s, NULL, NULL);
+               /* insert the method into the tree */
 
-       return o;
+               avl_insert(tree_native_methods, nmn);
+       }
 }
 
 
-java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
-{
-       methodinfo *m;
-       java_objectheader *o;
-
-       if (!c)
-               return *exceptionptr;
-
-       /* create object */
+/* native_method_find **********************************************************
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
-
-       /* find initializer */
-
-       m = class_resolveclassmethod(c,
-                                                                utf_new_char("<init>"),
-                                                                utf_new_char("(I)V"),
-                                                                NULL,
-                                                                true);
-
-       /* initializer not found  */                                                  
-       if (!m)
-               return NULL;
-
-       /* call initializer */
-
-       asm_calljavafunction(m, o, (void *) (ptrint) i, NULL, NULL);
-
-       return o;
-}
+   Find a native method in the native method table.
 
+*******************************************************************************/
 
-java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
+static functionptr native_method_find(methodinfo *m)
 {
-       methodinfo *m;
-       java_objectheader *o;
+       native_methods_node_t  tmpnmn;
+       native_methods_node_t *nmn;
 
-       if (!c)
-               return *exceptionptr;
+       /* fill the temporary structure used for searching the tree */
 
-       /* create object */
-
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       tmpnmn.classname  = m->class->name;
+       tmpnmn.name       = m->name;
+       tmpnmn.descriptor = m->descriptor;
 
-       /* find initializer */
+       /* find the entry in the AVL-tree */
 
-       m = class_findmethod(c,
-                                                utf_new_char("<init>"),
-                                                utf_new_char("(Ljava/lang/Throwable;)V"));
-                                                     
-       /* initializer not found */
+       nmn = avl_find(tree_native_methods, &tmpnmn);
 
-       if (!m)
+       if (nmn == NULL)
                return NULL;
 
-       /* call initializer */
-
-       asm_calljavafunction(m, o, t, NULL, NULL);
-
-       return o;
+       return nmn->function;
 }
 
 
-/******************** function: stringtable_update ****************************
+/* native_library_open *********************************************************
 
-       traverses the javastring hashtable and sets the vftbl-entries of
-       javastrings which were temporarily set to NULL, because 
-       java.lang.Object was not yet loaded
+   Open a native library with the given utf8 name.
 
 *******************************************************************************/
-void stringtable_update ()
+
+#if !defined(WITH_STATIC_CLASSPATH)
+lt_dlhandle native_library_open(utf *filename)
 {
-       java_lang_String *js;   
-       java_chararray *a;
-       literalstring *s;       /* hashtable entry */
-       int i;
-
-       for (i = 0; i < string_hash.size; i++) {
-               s = string_hash.ptr[i];
-               if (s) {
-                       while (s) {
-                                                               
-                               js = (java_lang_String *) s->string;
-                               
-                               if (!js || !js->value) 
-                                       /* error in hashtable found */
-                                       panic("invalid literalstring in hashtable");
-
-                               a = js->value;
-
-                               if (!js->header.vftbl) 
-                                       /* vftbl of javastring is NULL */ 
-                                       js->header.vftbl = class_java_lang_String->vftbl;
-
-                               if (!a->header.objheader.vftbl) 
-                                       /* vftbl of character-array is NULL */ 
-                                       a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
-
-                               /* follow link in external hash chain */
-                               s = s->hashlink;
-                       }       
-               }               
-       }
-}
+       lt_dlhandle handle;
 
+       if (opt_verbosejni) {
+               printf("[Loading native library ");
+               utf_display_printable_ascii(filename);
+               printf(" ... ");
+       }
 
-/************************* function: u2_utflength ***************************
+       /* try to open the library */
 
-       returns the utf length in bytes of a u2 array 
+       handle = lt_dlopen(filename->text);
 
-*****************************************************************************/
+       if (handle == NULL) {
+               if (opt_verbose) {
+                       log_start();
+                       log_print("native_library_load: lt_dlopen failed: ");
+                       log_print(lt_dlerror());
+                       log_finish();
+               }
 
-u4 u2_utflength(u2 *text, u4 u2_length)
-{
-       u4 result_len =  0;  /* utf length in bytes  */
-       u2 ch;               /* current unicode character */
-       u4 len;
-       
-       for (len = 0; len < u2_length; len++) {
-               /* next unicode character */
-               ch = *text++;
-         
-               /* determine bytes required to store unicode character as utf */
-               if (ch && (ch < 0x80)) 
-                       result_len++;
-               else if (ch < 0x800)
-                       result_len += 2;        
-               else 
-                       result_len += 3;        
+               return NULL;
        }
 
-    return result_len;
+       return handle;
 }
+#endif
 
 
-/********************* function: utf_new_u2 ***********************************
+/* native_library_add **********************************************************
 
-       make utf symbol from u2 array, 
-       if isclassname is true '.' is replaced by '/'
+   Adds an entry to the native library hashtable.
 
 *******************************************************************************/
 
-utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
+#if !defined(WITH_STATIC_CLASSPATH)
+void native_library_add(utf *filename, java_objectheader *loader,
+                                               lt_dlhandle handle)
 {
-       char *buffer; /* memory buffer for  unicode characters */
-       char *pos;    /* pointer to current position in buffer */
-       u4 left;      /* unicode characters left */
-       u4 buflength; /* utf length in bytes of the u2 array  */
-       utf *result;  /* resulting utf-string */
-       int i;          
-
-       /* determine utf length in bytes and allocate memory */
-       /* printf("utf_new_u2: unicode_length=%d\n",unicode_length);            */
-       buflength = u2_utflength(unicode_pos, unicode_length); 
-       buffer    = MNEW(char, buflength);
-       left = buflength;
-       pos  = buffer;
-
-       for (i = 0; i++ < unicode_length; unicode_pos++) {
-               /* next unicode character */
-               u2 c = *unicode_pos;
-               
-               if ((c != 0) && (c < 0x80)) {
-                       /* 1 character */       
-                       left--;
-               if ((int) left < 0) break;
-                       /* convert classname */
-                       if (isclassname && c == '.')
-                               *pos++ = '/';
-                       else
-                               *pos++ = (char) c;
-
-               } else if (c < 0x800) {             
-                       /* 2 characters */                              
-               unsigned char high = c >> 6;
-               unsigned char low  = c & 0x3F;
-                       left = left - 2;
-               if ((int) left < 0) break;
-               *pos++ = high | 0xC0; 
-               *pos++ = low  | 0x80;     
-
-               } else {         
-               /* 3 characters */                              
-               char low  = c & 0x3f;
-               char mid  = (c >> 6) & 0x3F;
-               char high = c >> 12;
-                       left = left - 3;
-               if ((int) left < 0) break;
-               *pos++ = high | 0xE0; 
-               *pos++ = mid  | 0x80;  
-               *pos++ = low  | 0x80;   
-               }
-       }
-       
-       /* insert utf-string into symbol-table */
-       result = utf_new(buffer,buflength);
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne; /* library name                       */
+       u4   key;                           /* hashkey                            */
+       u4   slot;                          /* slot in hashtable                  */
 
-       MFREE(buffer, char, buflength);
+       LOCK_MONITOR_ENTER(hashtable_library->header);
 
-       return result;
-}
+       /* 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];
 
-/********************* function: javastring_toutf *****************************
+       /* search external hash chain for the entry */
 
-       make utf symbol from javastring
+       while (le) {
+               if (le->loader == loader)
+                       break;
 
-*******************************************************************************/
+               le = le->hashlink;                  /* next element in external chain */
+       }
 
-utf *javastring_toutf(java_lang_String *string, bool isclassname)
-{
-       java_lang_String *str = (java_lang_String *) string;
+       /* no loader found? create a new entry */
 
-/*     printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
-/*     fflush(stdout); */
+       if (le == NULL) {
+               le = NEW(hashtable_library_loader_entry);
 
-       return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
-}
+               le->loader   = loader;
+               le->namelink = NULL;
 
+               /* insert entry into hashtable */
 
-/********************* function: literalstring_u2 *****************************
+               le->hashlink =
+                       (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
+               hashtable_library->ptr[slot] = le;
 
-    searches for the javastring with the specified u2-array in 
-    the string hashtable, if there is no such string a new one is 
-    created 
+               /* update number of hashtable-entries */
 
-    if copymode is true a copy of the u2-array is made
+               hashtable_library->entries++;
+       }
 
-*******************************************************************************/
 
-java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
-                                                                       bool copymode)
-{
-    literalstring *s;                /* hashtable element */
-    java_lang_String *js;            /* u2-array wrapped in javastring */
-    java_chararray *stringdata;      /* copy of u2-array */      
-    u4 key;
-    u4 slot;
-    u2 i;
-
-/* #define DEBUG_LITERALSTRING_U2 */
-#ifdef DEBUG_LITERALSTRING_U2
-    printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
-       fflush(stdout);
-#endif
-    
-    /* find location in hashtable */
-    key  = unicode_hashkey(a->data + offset, length);
-    slot = key & (string_hash.size - 1);
-    s    = string_hash.ptr[slot];
-
-    while (s) {
-               js = (java_lang_String *) s->string;
-
-               if (length == js->count) {
-                       /* compare text */
-                       for (i = 0; i < length; i++) {
-                               if (a->data[offset + i] != js->value->data[i])
-                                       goto nomatch;
-                       }
+       /* search for library name */
 
-                       /* string already in hashtable, free memory */
-                       if (!copymode)
-                               mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
+       ne = le->namelink;
 
-#ifdef DEBUG_LITERALSTRING_U2
-                       printf("literalstring_u2: foundentry at %p\n", js);
-                       utf_display(javastring_toutf(js, 0));
-                       printf("\n\n");
-                       fflush(stdout);
-#endif
-                       return (java_objectheader *) js;
+       while (ne) {
+               if (ne->name == filename) {
+                       LOCK_MONITOR_EXIT(hashtable_library->header);
+
+                       return;
                }
 
-       nomatch:
-               /* follow link in external hash chain */
-               s = s->hashlink;
-    }
-
-    if (copymode) {
-               /* create copy of u2-array for new javastring */
-               u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
-               stringdata = mem_alloc(arraysize);
-/*             memcpy(stringdata, a, arraysize); */
-               memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
-               memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
-
-    } else {
-               stringdata = a;
+               ne = ne->hashlink;                  /* next element in external chain */
        }
 
-    /* location in hashtable found, complete arrayheader */
-    stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
-    stringdata->header.size = length;
+       /* not found? add the library name to the classloader */
 
-       /* if we use eager loading, we have to check loaded String class */
-       if (opt_eager) {
-               class_java_lang_String =
-                       class_new_intern(utf_new_char("java/lang/String"));
+       ne = NEW(hashtable_library_name_entry);
 
-               if (!class_load(class_java_lang_String))
-                       return NULL;
+       ne->name   = filename;
+       ne->handle = handle;
 
-               list_addfirst(&unlinkedclasses, class_java_lang_String);
-       }
+       /* insert entry into external chain */
 
-       /* create new javastring */
-       js = NEW(java_lang_String);
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-       initObjectLock(&js->header);
-#endif
-       js->header.vftbl = class_java_lang_String->vftbl;
-       js->value  = stringdata;
-       js->offset = 0;
-       js->count  = length;
-
-#ifdef DEBUG_LITERALSTRING_U2
-       printf("literalstring_u2: newly created at %p\n", js);
-       utf_display(javastring_toutf(js, 0));
-       printf("\n\n");
-       fflush(stdout);
-#endif
-                       
-       /* create new literalstring */
-       s = NEW(literalstring);
-       s->hashlink = string_hash.ptr[slot];
-       s->string   = (java_objectheader *) js;
-       string_hash.ptr[slot] = s;
-
-       /* update number of hashtable entries */
-       string_hash.entries++;
-
-       /* reorganization of hashtable */       
-       if (string_hash.entries > (string_hash.size * 2)) {
-               /* reorganization of hashtable, average length of 
-                  the external chains is approx. 2                */  
-
-               u4 i;
-               literalstring *s;
-               hashtable newhash; /* the new hashtable */
-      
-               /* create new hashtable, double the size */
-               init_hashtable(&newhash, string_hash.size * 2);
-               newhash.entries = string_hash.entries;
-      
-               /* transfer elements to new hashtable */
-               for (i = 0; i < string_hash.size; i++) {
-                       s = string_hash.ptr[i];
-                       while (s) {
-                               literalstring *nexts = s->hashlink;
-                               js   = (java_lang_String *) s->string;
-                               slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
-         
-                               s->hashlink = newhash.ptr[slot];
-                               newhash.ptr[slot] = s;
-       
-                               /* follow link in external hash chain */  
-                               s = nexts;
-                       }
-               }
-       
-               /* dispose old table */ 
-               MFREE(string_hash.ptr, void*, string_hash.size);
-               string_hash = newhash;
-       }
+       ne->hashlink = le->namelink;
+       le->namelink = ne;
 
-       return (java_objectheader *) js;
+       LOCK_MONITOR_EXIT(hashtable_library->header);
 }
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
-/******************** Function: literalstring_new *****************************
+/* native_library_find *********************************************************
 
-    creates a new javastring with the text of the utf-symbol
-    and inserts it into the string hashtable
+   Find an entry in the native library hashtable.
 
 *******************************************************************************/
 
-java_objectheader *literalstring_new(utf *u)
+#if !defined(WITH_STATIC_CLASSPATH)
+hashtable_library_name_entry *native_library_find(utf *filename,
+                                                                                                 java_objectheader *loader)
 {
-    char *utf_ptr = u->text;         /* pointer to current unicode character in utf string */
-    u4 utflength  = utf_strlen(u);   /* length of utf-string if uncompressed */
-    java_chararray *a;               /* u2-array constructed from utf string */
-    u4 i;
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne; /* library name                       */
+       u4   key;                           /* hashkey                            */
+       u4   slot;                          /* slot in hashtable                  */
 
-    /* allocate memory */ 
-    a = mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
 
-    /* convert utf-string to u2-array */
-    for (i = 0; i < utflength; i++)
-               a->data[i] = utf_nextu2(&utf_ptr);
+       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
-    return literalstring_u2(a, utflength, 0, false);
-}
+       /* search external hash chain for the entry */
 
+       while (le) {
+               if (le->loader == loader)
+                       break;
 
-/********************** function: literalstring_free **************************
+               le = le->hashlink;                  /* next element in external chain */
+       }
 
-        removes a javastring from memory                      
+       /* no loader found? return NULL */
 
-******************************************************************************/
+       if (le == NULL)
+               return NULL;
 
-void literalstring_free(java_objectheader* sobj)
-{
-       java_lang_String *s = (java_lang_String *) sobj;
-       java_chararray *a = s->value;
+       /* search for library name */
 
-       /* dispose memory of java.lang.String object */
-       FREE(s, java_lang_String);
+       ne = le->namelink;
 
-       /* dispose memory of java-characterarray */
-       FREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
-}
+       while (ne) {
+               if (ne->name == filename)
+                       return ne;
 
+               ne = ne->hashlink;                  /* next element in external chain */
+       }
 
-void copy_vftbl(vftbl_t **dest, vftbl_t *src)
-{
-    *dest = src;
-#if 0
-    /* XXX this kind of copying does not work (in the general
-     * case). The interface tables would have to be copied, too. I
-     * don't see why we should make a copy anyway. -Edwin
-     */
-       *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
-       memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
-       memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
-#endif
+       /* return entry, if no entry was found, ne is NULL */
+
+       return ne;
 }
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
-/******************************************************************************************                                                                                                            
+/* native_findfunction *********************************************************
 
-       creates method signature (excluding return type) from array of 
-       class-objects representing the parameters of the method 
+   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)
 
-*******************************************************************************************/
+   Remark: For faster operation, the names/descriptors are converted
+   from C strings to Unicode the first time this function is called.
 
+*******************************************************************************/
 
-utf *create_methodsig(java_objectarray* types, char *retType)
+#if defined(WITH_STATIC_CLASSPATH)
+functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
+                                                               bool isstatic)
 {
-    char *buffer;       /* buffer for building the desciptor */
-    char *pos;          /* current position in buffer */
-    utf *result;        /* the method signature */
-    u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
-    u4 i, j;
-    if (!types) return NULL;
-
-    /* determine required buffer-size */    
-    for (i = 0; i < types->header.size; i++) {
-               classinfo *c = (classinfo *) types->data[i];
-               buffer_size  = buffer_size + c->name->blength + 2;
-    }
-
-    if (retType) buffer_size += strlen(retType);
-
-    /* allocate buffer */
-    buffer = MNEW(char, buffer_size);
-    pos    = buffer;
-    
-    /* method-desciptor starts with parenthesis */
-    *pos++ = '(';
-
-    for (i = 0; i < types->header.size; i++) {
-               char ch;           
-
-               /* current argument */
-           classinfo *c = (classinfo *) types->data[i];
-
-           /* current position in utf-text */
-           char *utf_ptr = c->name->text; 
-           
-           /* determine type of argument */
-           if ((ch = utf_nextu2(&utf_ptr)) == '[') {
-               /* arrayclass */
-               for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
-                               *pos++ = *utf_ptr; /* copy text */
-                       }
-
-           } else {            
-                       /* check for primitive types */
-                       for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
-                               char *utf_pos   = utf_ptr - 1;
-                               char *primitive = primitivetype_table[j].wrapname;
-
-                               /* compare text */
-                               while (utf_pos < utf_end(c->name)) {
-                                       if (*utf_pos++ != *primitive++) goto nomatch;
-                               }
+       /* entry of table for fast string comparison */
+       struct nativecompref *n;
+       s4 i;
 
-                               /* primitive type found */
-                               *pos++ = primitivetype_table[j].typesig;
-                               goto next_type;
+       isstatic = isstatic ? true : false;
+       
+       if (!nativecompdone) {
+               for (i = 0; i < NATIVETABLESIZE; i++) {
+                       nativecomptable[i].classname  = 
+                               utf_new_char(nativetable[i].classname);
 
-                       nomatch:
-                               ;
-                       }
+                       nativecomptable[i].methodname = 
+                               utf_new_char(nativetable[i].methodname);
 
-                       /* no primitive type and no arrayclass, so must be object */
-                       *pos++ = 'L';
+                       nativecomptable[i].descriptor =
+                               utf_new_char(nativetable[i].descriptor);
 
-                       /* copy text */
-                       for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
-                               *pos++ = *utf_ptr;
-                       }
+                       nativecomptable[i].isstatic   = nativetable[i].isstatic;
+                       nativecomptable[i].func       = nativetable[i].func;
+               }
 
-                       *pos++ = ';';
+               nativecompdone = true;
+       }
 
-               next_type:
-                       ;
-               }  
-    }      
+       for (i = 0; i < NATIVETABLESIZE; i++) {
+               n = &(nativecomptable[i]);
 
-    *pos++ = ')';
+               if (cname == n->classname && mname == n->methodname &&
+                   desc == n->descriptor && isstatic == n->isstatic)
+                       return n->func;
+       }
 
-    if (retType) {
-               for (i = 0; i < strlen(retType); i++) {
-                       *pos++ = retType[i];
-               }
-    }
+       /* no function was found, throw exception */
 
-    /* create utf-string */
-    result = utf_new(buffer, (pos - buffer));
-    MFREE(buffer, char, buffer_size);
+       exceptions_throw_unsatisfiedlinkerror(mname);
 
-    return result;
+       return NULL;
 }
+#endif /* defined(WITH_STATIC_CLASSPATH) */
 
 
-/******************************************************************************************
+/* native_resolve_function *****************************************************
 
-       retrieve the next argument or returntype from a descriptor
-       and return the corresponding class 
+   Resolves a native function, maybe from a dynamic library.
 
-*******************************************************************************************/
+*******************************************************************************/
 
-classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
+functionptr native_resolve_function(methodinfo *m)
 {
-    classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
-                                         (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
-    if (!c)
-       /* unknown type */
-       panic("illegal descriptor");
-
-    if (skip) return NULL;
-
-    use_class_as_object(c);
-    return c;
-}
-
-
-/* get_parametertypes **********************************************************
+       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
 
-   use the descriptor of a method to generate a java/lang/Class array
-   which contains the classes of the parametertypes of the method
+       cl = m->class->classloader;
 
-*******************************************************************************/
+       /* verbose output */
 
-java_objectarray* get_parametertypes(methodinfo *m) 
-{
-    utf  *descr    =  m->descriptor;    /* method-descriptor */ 
-    char *utf_ptr  =  descr->text;      /* current position in utf-text */
-    char *desc_end =  utf_end(descr);   /* points behind utf string     */
-    java_objectarray* result;
-    int parametercount = 0;
-    int i;
-
-    /* skip '(' */
-    utf_nextu2(&utf_ptr);
-  
-    /* determine number of parameters */
-    while (*utf_ptr != ')') {
-       get_type(&utf_ptr, desc_end, true);
-               parametercount++;
-    }
-
-    /* create class-array */
-    result = builtin_anewarray(parametercount, class_java_lang_Class);
-
-    utf_ptr = descr->text;
-    utf_nextu2(&utf_ptr);
-
-    /* get returntype classes */
-    for (i = 0; i < parametercount; i++)
-           result->data[i] =
-                       (java_objectheader *) get_type(&utf_ptr, desc_end, false);
-
-    return result;
-}
+       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 */
 
-/* get_exceptiontypes **********************************************************
+       name = native_method_symbol(m->class->name, m->name);
 
-   get the exceptions which can be thrown by a method
+       /* generate overloaded function (having the types in it's name)           */
 
-*******************************************************************************/
+       newname = native_make_overloaded_function(name, m->descriptor);
 
-java_objectarray* get_exceptiontypes(methodinfo *m)
-{
-    u2 excount;
-    u2 i;
-    java_objectarray *result;
+       /* check the library hash entries of the classloader of the
+          methods's class  */
 
-       excount = m->thrownexceptionscount;
+       f = NULL;
 
-    /* create class-array */
-    result = builtin_anewarray(excount, class_java_lang_Class);
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
 
-    for (i = 0; i < excount; i++) {
-               java_objectheader *o = (java_objectheader *) (m->thrownexceptions[i]);
-               use_class_as_object((classinfo *) o);
-               result->data[i] = o;
-    }
+       key  = ((u4) (ptrint) cl) >> 4;                       /* align to 16-byte */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
-    return result;
-}
+       /* 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);
 
+                       ne = ne->hashlink;
+               }
 
-/******************************************************************************************
+               le = le->hashlink;
+       }
 
-       get the returntype class of a method
+#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 */
 
-*******************************************************************************************/
+               method_findNative =
+                       class_resolveclassmethod(class_java_lang_ClassLoader,
+                                                                        utf_findNative,
+                                                                        utf_java_lang_ClassLoader_java_lang_String__J,
+                                                                        class_java_lang_ClassLoader,
+                                                                        true);
 
-classinfo *get_returntype(methodinfo *m) 
-{
-       char *utf_ptr;   /* current position in utf-text */
-       char *desc_end;  /* points behind utf string     */
-        utf *desc = m->descriptor; /* method-descriptor  */
+               if (method_findNative == NULL)
+                       return NULL;
 
-       utf_ptr  = desc->text;
-       desc_end = utf_end(desc);
+               /* try the normal name */
 
-       /* ignore parametertypes */
-        while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
-               /* skip */ ;
+               s = javastring_new(name);
 
-       return get_type(&utf_ptr,desc_end, false);
-}
+               f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
+                                                                                                                NULL, cl, s);
 
+               /* if not found, try the mangled name */
 
-/*****************************************************************************/
-/*****************************************************************************/
+               if (f == NULL) {
+                       s = javastring_new(newname);
 
+                       f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
+                                                                                                                        NULL, cl, s);
+               }
+       }
+#endif
 
-/*--------------------------------------------------------*/
-void printNativeCall(nativeCall nc) {
-  int i,j;
+       if (f != NULL)
+               if (opt_verbosejni)
+                       printf("JNI ]\n");
 
-  printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
-  for (i=0; i<nc.methCnt; i++) {  
-      printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
+       /* If not found, try to find the native function symbol in the
+          main program. */
 
-    for (j=0; j<nc.callCnt[i]; j++) {  
-        printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
-       nc.methods[i].methodCalls[j].classname, 
-       nc.methods[i].methodCalls[j].methodname, 
-       nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
-      }
-    }
-  printf("-+++++--------------------\n");fflush(stdout);
-}
+       if (f == NULL) {
+               f = native_method_find(m);
 
-/*--------------------------------------------------------*/
-void printCompNativeCall(nativeCompCall nc) {
-  int i,j;
-  printf("printCompNativeCall BEGIN\n");fflush(stdout); 
-  printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
-  utf_display(nc.classname); fflush(stdout);
-  
-  for (i=0; i<nc.methCnt; i++) {  
-    printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
-    utf_display(nc.methods[i].methodname); fflush(stdout);
-    utf_display(nc.methods[i].descriptor);fflush(stdout);
-    printf("\n");fflush(stdout);
-
-    for (j=0; j<nc.callCnt[i]; j++) {  
-      printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
-       utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
-       utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
-       utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
-       printf("\n");fflush(stdout);
-      }
-    }
-printf("---------------------\n");fflush(stdout);
-}
+               if (f != NULL)
+                       if (opt_verbosejni)
+                               printf("internal ]\n");
+       }
 
+#if defined(ENABLE_JVMTI)
+       /* fire Native Method Bind event */
+       if (jvmti) jvmti_NativeMethodBind(m, f, &f);
+#endif
 
-/*--------------------------------------------------------*/
-classMeth findNativeMethodCalls(utf *c, utf *m, utf *d ) 
-{
-    int i = 0;
-    int j = 0;
-    int cnt = 0;
-    classMeth mc;
-    mc.i_class = i;
-    mc.j_method = j;
-    mc.methCnt = cnt;
-
-    return mc;
-}
+       /* no symbol found? throw exception */
 
-/*--------------------------------------------------------*/
-nativeCall* findNativeClassCalls(char *aclassname ) {
-int i;
+       if (f == NULL) {
+               if (opt_verbosejni)
+                       printf("failed ]\n");
 
-for (i=0;i<NATIVECALLSSIZE; i++) {
-   /* convert table to utf later to speed up search */ 
-   if (strcmp(nativeCalls[i].classname, aclassname) == 0) 
-       return &nativeCalls[i];
-   }
+               exceptions_throw_unsatisfiedlinkerror(m->name);
+       }
 
-return NULL;
-}
-/*--------------------------------------------------------*/
-/*--------------------------------------------------------*/
-void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
-  int i,j;
-
-
-  ncc->classname = utf_new_char(nc.classname); 
-  ncc->methCnt = nc.methCnt;
-  
-  for (i=0; i<nc.methCnt; i++) {  
-    ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
-    ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
-    ncc->callCnt[i] = nc.callCnt[i];
-
-    for (j=0; j<nc.callCnt[i]; j++) {  
-
-       ncc->methods[i].methodCalls[j].classname  = utf_new_char(nc.methods[i].methodCalls[j].classname);
-
-        if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
-          ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
-          ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
-          }
-        else {
-          ncc->methods[i].methodCalls[j].methodname = NULL;
-          ncc->methods[i].methodCalls[j].descriptor = NULL;
-          }
-      }
-    }
+       return f;
 }
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
+/* 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.
+                       
+*******************************************************************************/
 
-bool natcall2utf(bool natcallcompdone) {
-int i;
+java_objectheader *native_new_and_init(classinfo *c)
+{
+       methodinfo *m;
+       java_objectheader *o;
 
-if (natcallcompdone) 
-       return true;
+       if (c == NULL)
+               vm_abort("native_new_and_init: c == NULL");
 
-for (i=0;i<NATIVECALLSSIZE; i++) {
-   utfNativeCall  (nativeCalls[i], &nativeCompCalls[i]);  
-   }
+       /* create object */
 
-return true;
-}
+       o = builtin_new(c);
+       
+       if (o == NULL)
+               return NULL;
 
-/*--------------------------------------------------------*/
+       /* try to find the initializer */
 
+       m = class_findmethod(c, utf_init, utf_void__void);
+                                                     
+       /* ATTENTION: returning the object here is ok, since the class may
+       not have an initializer */
 
-java_objectarray *builtin_asm_createclasscontextarray(classinfo **end, classinfo **start)
-{
-#if defined(__GNUC__)
-#warning platform dependend
-#endif
-       java_objectarray *tmpArray;
-       int i;
-       classinfo **current;
-       classinfo *c;
-       size_t size;
-
-       size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
-
-       /*printf("end %p, start %p, size %ld\n",end,start,size);*/
-       if (!class_java_lang_Class)
-               class_java_lang_Class = class_new(utf_new_char("java/lang/Class"));
-
-       if (!class_java_lang_SecurityManager)
-               class_java_lang_SecurityManager =
-                       class_new(utf_new_char("java/lang/SecurityManager"));
-
-       if (size > 0) {
-               if (start == class_java_lang_SecurityManager) {
-                       size--;
-                       start--;
-               }
-       }
+       if (m == NULL)
+               return o;
 
-       tmpArray =
-               builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
+       /* call initializer */
 
-       for(i = 0, current = start; i < size; i++, current--) {
-               c = *current;
-               /*              printf("%d\n",i);
-                utf_display(c->name);*/
-               use_class_as_object(c);
-               tmpArray->data[i] = (java_objectheader *) c;
-       }
+       (void) vm_call_method(m, o);
 
-       return tmpArray;
+       return o;
 }
 
 
-java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end, classinfo **start)
+java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
 {
-#if defined(__GNUC__)
-#warning platform dependend
-#endif
-       int i;
-       classinfo **current;
-       classinfo *c;
-       classinfo *privilegedAction;
-       size_t size;
+       methodinfo        *m;
+       java_objectheader *o;
 
-       size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
+       if (c == NULL)
+               vm_abort("native_new_and_init_string: c == NULL");
 
-       /*      log_text("builtin_asm_getclassloader");
-        printf("end %p, start %p, size %ld\n",end,start,size);*/
+       /* create object */
 
-       if (!class_java_lang_SecurityManager)
-               class_java_lang_SecurityManager =
-                       class_new(utf_new_char("java/lang/SecurityManager"));
+       o = builtin_new(c);
 
-       if (size > 0) {
-               if (start == class_java_lang_SecurityManager) {
-                       size--;
-                       start--;
-               }
-       }
+       if (o == NULL)
+               return NULL;
 
-       privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
+       /* find initializer */
 
-       for(i = 0, current = start; i < size; i++, current--) {
-               c = *current;
+       m = class_findmethod(c, utf_init, utf_java_lang_String__void);
 
-               if (c == privilegedAction)
-                       return NULL;
+       /* initializer not found */
 
-               if (c->classloader)
-                       return (java_lang_ClassLoader *) c->classloader;
-       }
+       if (m == NULL)
+               return NULL;
 
-       return NULL;
+       /* call initializer */
 
-       /*
-        log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
-        init_systemclassloader();
+       (void) vm_call_method(m, o, s);
 
-        return SystemClassLoader;*/
+       return o;
 }