* Removed all Id tags.
[cacao.git] / src / native / native.c
index 6f1941613ce33c042dde91eb7a4a79fe58892b9c..37f86f45c72fbd3da3442479f1440b929d0d0271 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.
-
-   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 2010 2005-03-07 09:50:57Z twisti $
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
 */
 
 
-#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(ENABLE_LTDL) && defined(HAVE_LTDL_H)
+# include <ltdl.h>
 #endif
-#include <sys/stat.h>
 
-#include "config.h"
-#include "cacao/cacao.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/stringlocal.h"
-#include "vm/tables.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 "nativetable.inc"
 
+/* include table of native functions ******************************************/
 
-/** Create a new ProtectionDomain object for a classpath_info structure. 
-       This object will be used for every java.lang.class object who represents a 
-       class which has been loaded from the location this classpath_info structure 
-       points to. **/
-static inline void create_ProtectionDomain(struct classpath_info *cpi) {
-       jmethodID mid;
-       jobject obj;
+#if defined(WITH_STATIC_CLASSPATH)
+# include "native/nativetable.inc"
+#endif
 
-       jobject cs;
-       jobject pc;
-       jobject url;
 
-       classinfo *c;  
-       classinfo *classpc;
+/* tables for methods *********************************************************/
 
-       /* create a new java.io.File object */ 
-       c = class_new(utf_new_char_classname("java/io/File"));
-       
-       if (!class_load(c) || !class_link(c)) {
-               log_text("unable to find java.io.File");
-               throw_main_exception_exit();
-       }
+#if defined(WITH_STATIC_CLASSPATH)
+#define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
 
-       mid = class_resolvemethod(c, utf_new_char("<init>"), utf_new_char("(Ljava/lang/String;)V"));
+/* table for fast string comparison */
+static nativecompref nativecomptable[NATIVETABLESIZE];
 
-       if (mid == NULL) {
-               log_text("unable to find constructor in java.io.File");
-               cacao_exit(1);
-       }
+/* string comparsion table initialized */
+static bool nativecompdone = false;
+#endif
 
-       obj = builtin_new (c);
-       asm_calljavafunction(mid,obj,javastring_new(utf_new_char(cpi->path)),NULL,NULL);
 
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+/* global variables ***********************************************************/
 
-       /* get URL of CodeSource from java.io.File object */ 
-       mid = class_resolvemethod(c, utf_new_char("toURL"), utf_new_char("()Ljava/net/URL;"));
+static avl_tree_t *tree_native_methods;
 
-       if (mid == NULL) {
-               log_text("unable to find toURL in java.io.File");
-               cacao_exit(1);
-       }
+#if defined(ENABLE_LTDL)
+static hashtable *hashtable_library;
+#endif
 
-       url = asm_calljavafunction(mid,obj,url,NULL,NULL);
 
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+/* prototypes *****************************************************************/
 
-       /* create a new java.security.CodeSource object */ 
-       c = class_new(utf_new_char_classname("java/security/CodeSource"));
-       
-       if (!class_load(c) || !class_link(c)) {
-               log_text("unable to find java.security.CodeSource");
-               throw_main_exception_exit();
-       }
-       
-       mid = class_resolvemethod(c, 
-                                                         utf_new_char("<init>"), 
-                                                         utf_new_char("(Ljava/net/URL;[Ljava/security/cert/Certificate;)V"));
+static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
 
-       if (mid == NULL) {
-               log_text("unable to find constructor in java.security.CodeSource");
-               cacao_exit(1);
-       }
 
-       cs = builtin_new (c);
-       asm_calljavafunction(mid,cs,url,NULL,NULL);
+/* native_init *****************************************************************
 
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+   Initializes the native subsystem.
 
-       /* create a new java.security.PermissionCollection object */ 
+*******************************************************************************/
 
+bool native_init(void)
+{
+#if defined(ENABLE_LTDL)
+       /* initialize libltdl */
 
-       classpc = class_new(utf_new_char_classname("java/security/PermissionCollection"));
-       
-       if (!class_load(c) || !class_link(c)) {
-               log_text("unable to find java.security.PermissionCollection");
-               throw_main_exception_exit();
-       }
+       if (lt_dlinit())
+               vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
 
-       mid = class_resolvemethod(classpc, utf_new_char("<init>"), 
-                                                         utf_new_char("()V"));
+       /* initialize library hashtable, 10 entries should be enough */
 
-       if (mid == NULL) {
-               log_text("unable to find constructor in java.security.PermissionCollection");
-               cacao_exit(1);
-       }
+       hashtable_library = NEW(hashtable);
 
-       pc = builtin_new (classpc);
-       asm_calljavafunction(mid,pc,NULL,NULL,NULL);
+       hashtable_create(hashtable_library, 10);
+#endif
 
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+       /* initialize the native methods table */
 
-    /* add AllPermission - todo: this should not be the default behaviour */
-       c = class_new(utf_new_char_classname("java/security/AllPermission"));
-       
-       if (!class_load(c) || !class_link(c)) {
-               log_text("unable to find java.security.AllPermission");
-               throw_main_exception_exit();
-       }
+       tree_native_methods = avl_create(&native_tree_native_methods_comparator);
 
-       mid = class_resolvemethod(c, 
-                                                         utf_new_char("<init>"), 
-                                                         utf_new_char("()V"));
+       /* everything's ok */
 
-       if (mid == NULL) {
-               log_text("unable to find constructor in java.security.AllPermission");
-               cacao_exit(1);
-       }
+       return true;
+}
 
 
-       obj = builtin_new (c);
-       asm_calljavafunction(mid,obj,NULL,NULL,NULL);
+/* native_tree_native_methods_comparator ***************************************
 
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+   Comparison function for AVL tree of native methods.
 
+   IN:
+       treenode....node in the tree
+          node........node to compare with tree-node
 
-       mid = class_resolvemethod(classpc, 
-                                                         utf_new_char("add"), 
-                                                         utf_new_char("(Ljava/security/Permission;)V"));
+   RETURN VALUE:
+       -1, 0, +1
 
-       if (mid == NULL) {
-               log_text("unable to find add in java.security.PermissionCollection");
-               cacao_exit(1);
-       }
+*******************************************************************************/
 
-       asm_calljavafunction(mid,pc,obj,NULL,NULL);
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
+{
+       const native_methods_node_t *treenmn;
+       const native_methods_node_t *nmn;
 
-       /* create a new java.security.ProtectionDomain object */ 
-       c = class_new(utf_new_char_classname("java/security/ProtectionDomain"));
-       
-       if (!class_load(c) || !class_link(c)) {
-               log_text("unable to find java.security.ProtectionDomain");
-               throw_main_exception_exit();
-       }
+       treenmn = treenode;
+       nmn     = node;
 
-       mid = class_resolvemethod(c, 
-                                                         utf_new_char("<init>"), 
-                                                         utf_new_char("(Ljava/security/CodeSource;Ljava/security/PermissionCollection;)V"));
+       /* these are for walking the tree */
 
-       if (mid == NULL) {
-               log_text("unable to find constructor in java.security.ProtectionDomain");
-               cacao_exit(1);
-       }
-       
-       obj = builtin_new (c);
-       asm_calljavafunction(mid,obj,cs,pc,NULL);
-
-       if (*exceptionptr != NULL) {
-               utf_display((*exceptionptr)->vftbl->class->name);
-               printf ("\n");
-               fflush (stdout);        
-               cacao_exit(1);
-       }
+       if (treenmn->classname < nmn->classname)
+               return -1;
+       else if (treenmn->classname > nmn->classname)
+               return 1;
 
-       cpi->pd = (struct java_security_ProtectionDomain*) obj;
-}
+       if (treenmn->name < nmn->name)
+               return -1;
+       else if (treenmn->name > nmn->name)
+               return 1;
 
+       if (treenmn->descriptor < nmn->descriptor)
+               return -1;
+       else if (treenmn->descriptor > nmn->descriptor)
+               return 1;
 
-/************* use classinfo structure as java.lang.Class object **************/
+       /* all pointers are equal, we have found the entry */
 
-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;
-
-
-               /* set a real, java object ProtectionDomain and CodeSource.
-                  Only one ProtectionDomain-object per classpath_info is needed.*/
-               if (c->pd != NULL) { 
-            /* only set ProtectionDomain for non primitive type classes*/
-                       if (((struct classpath_info*)c->pd)->pd == NULL) 
-                               create_ProtectionDomain((struct classpath_info *)c->pd);
-               
-                       c->pd = ((struct classpath_info*)c->pd)->pd;
-               }
-       }
-            
+       return 0;
 }
 
 
-/************************** tables for methods ********************************/
+/* native_make_overloaded_function *********************************************
 
-#undef JOWENN_DEBUG
-#undef JOWENN_DEBUG1
-
-#ifdef STATIC_CLASSPATH
-#define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
-
-/* table for fast string comparison */
-static nativecompref nativecomptable[NATIVETABLESIZE];
+   XXX
 
-/* string comparsion table initialized */
-static bool nativecompdone = false;
-#endif
-
-
-/* XXX don't define this in a header file!!! */
+*******************************************************************************/
 
-static struct nativeCall nativeCalls[] =
+#if !defined(WITH_STATIC_CLASSPATH)
+static utf *native_make_overloaded_function(utf *name, utf *descriptor)
 {
-#include "nativecalls.inc"
-};
-
-#define NATIVECALLSSIZE    (sizeof(nativeCalls) / sizeof(struct nativeCall))
+       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);
+               }
+       }
 
-struct nativeCompCall nativeCompCalls[NATIVECALLSSIZE];
+       /* 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);
+               }
+       }
 
+       /* close string */
 
-/* native_loadclasses **********************************************************
+       newname[i] = '\0';
 
-   Load classes required for native methods.
+       /* make a utf-string */
 
-*******************************************************************************/
+       u = utf_new_char(newname);
 
-bool native_init(void)
-{
-       static int classesLoaded = 0; /*temporary hack JoWenn*/
-#if !defined(STATIC_CLASSPATH)
-       void *p;
-#endif
+       /* release memory */
 
-       if (classesLoaded)
-               return true;
+       dump_release(dumpsize);
 
-       classesLoaded = 1;
+       return u;
+}
 
-#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
 
-       /* load classes for wrapping primitive types */
+/* native_insert_char **********************************************************
 
-       if (!class_load(class_java_lang_Void) ||
-               !class_link(class_java_lang_Void))
-               return false;
+   Inserts the passed UTF character into the native method name.  If
+   necessary it is escaped properly.
 
-       if (!class_load(class_java_lang_Boolean) ||
-               !class_link(class_java_lang_Boolean))
-               return false;
+*******************************************************************************/
 
-       if (!class_load(class_java_lang_Byte) ||
-               !class_link(class_java_lang_Byte))
-               return false;
+static s4 native_insert_char(char *name, u4 pos, u2 c)
+{
+       s4 val;
+       s4 i;
 
-       if (!class_load(class_java_lang_Character) ||
-               !class_link(class_java_lang_Character))
-               return false;
+       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;
+                       }
 
-       if (!class_load(class_java_lang_Short) ||
-               !class_link(class_java_lang_Short))
-               return false;
+                       pos += 4;
+               }
+               break;
+       }
 
-       if (!class_load(class_java_lang_Integer) ||
-               !class_link(class_java_lang_Integer))
-               return false;
+       /* return the new buffer index */
 
-       if (!class_load(class_java_lang_Long) ||
-               !class_link(class_java_lang_Long))
-               return false;
+       return pos;
+}
 
-       if (!class_load(class_java_lang_Float) ||
-               !class_link(class_java_lang_Float))
-               return false;
 
-       if (!class_load(class_java_lang_Double) ||
-               !class_link(class_java_lang_Double))
-               return false;
+/* native_method_symbol ********************************************************
 
-       /* everything's ok */
+   Generate a method-symbol string out of the class name and the
+   method name.
 
-       return true;
-}
+*******************************************************************************/
 
+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;
 
-/*********************** Function: native_findfunction *************************
+       /* mark memory */
 
-       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)
+       dumpsize = dump_size();
 
-       Remark: For faster operation, the names/descriptors are converted from C
-               strings to Unicode the first time this function is called.
+       /* 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). */
 
-*******************************************************************************/
+       namelen =
+               strlen("Java_") +
+               utf_get_number_of_u2s(classname) * 6 +
+               strlen("_") +
+               utf_get_number_of_u2s(methodname) * 6 +
+               strlen("0");
 
-functionptr native_findfunction(utf *cname, utf *mname, 
-                                                               utf *desc, bool isstatic)
-{
-#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;
+       /* allocate memory */
 
-       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;
-       }
+       name = DMNEW(char, namelen);
 
-#ifdef JOWENN_DEBUG
-       buffer_len = 
-               utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
-       
-       buffer = MNEW(char, buffer_len);
+       /* generate name of native functions */
 
-       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);
+       strcpy(name, "Java_");
+       pos = strlen("Java_");
 
-       log_text(buffer);       
+       utf_ptr    = classname->text;
+       utf_endptr = UTF_END(classname);
 
-       MFREE(buffer, char, buffer_len);
-#endif
-               
-       for (i = 0; i < NATIVETABLESIZE; i++) {
-               n = &(nativecomptable[i]);
+       for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
+               c   = *utf_ptr;
+               pos = native_insert_char(name, pos, c);
+       }
 
-               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);
+       /* seperator between class and method */
 
-                                       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);
+       name[pos++] = '_';
 
-                                       log_text(buffer);       
+       utf_ptr    = methodname->text;
+       utf_endptr = UTF_END(methodname);
 
-                                       MFREE(buffer, char, buffer_len);
-                               }
-                       } 
-#endif
+       for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
+               c   = *utf_ptr;
+               pos = native_insert_char(name, pos, c);
        }
 
-               
-       /* no function was found, display warning */
+       /* close string */
 
-       buffer_len = 
-               utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
+       name[pos] = '\0';
 
-       buffer = MNEW(char, buffer_len);
+       /* check for an buffer overflow */
 
-       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);
+       assert(pos <= namelen);
 
-       log_text(buffer);       
+       /* make a utf-string */
 
-       MFREE(buffer, char, buffer_len);
+       u = utf_new_char(name);
 
-/*     exit(1); */
+       /* release memory */
 
-       /* keep compiler happy */
-       return NULL;
-#else
-/* dynamic classpath */
-  return 0;
-#endif
+       dump_release(dumpsize);
+
+       return u;
 }
 
 
-/****************** function class_findfield_approx ****************************
-       
-       searches in 'classinfo'-structure for a field with the
-       specified name
+/* native_method_register ******************************************************
+
+   Register a native method in the native method table.
 
 *******************************************************************************/
-fieldinfo *class_findfield_approx(classinfo *c, utf *name)
+
+void native_method_register(utf *classname, const JNINativeMethod *methods,
+                                                       int32_t count)
 {
-       s4 i;
+       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);
+               }
 
-       for (i = 0; i < c->fieldscount; i++) {
-               /* compare field names */
-               if ((c->fields[i].name == name))
-                       return &(c->fields[i]);
-       }
+               /* generate the utf8 names */
 
-       /* field was not found, raise exception */      
-       *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
+               name       = utf_new_char(methods[i].name);
+               descriptor = utf_new_char(methods[i].signature);
 
-       return NULL;
-}
+               /* allocate a new tree node */
 
+               nmn = NEW(native_methods_node_t);
 
-s4 class_findfield_index_approx(classinfo *c, utf *name)
-{
-       s4 i;
-
-       for (i = 0; i < c->fieldscount; i++) {
-               /* compare field names */
-               if ((c->fields[i].name == name))
-                       return i;
-       }
+               nmn->classname  = classname;
+               nmn->name       = name;
+               nmn->descriptor = descriptor;
+               nmn->function   = (functionptr) (ptrint) methods[i].fnPtr;
 
-       /* field was not found, raise exception */      
-       *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
+               /* insert the method into the tree */
 
-       return -1;
+               avl_insert(tree_native_methods, nmn);
+       }
 }
 
 
-/* native_new_and_init *********************************************************
+/* native_method_find **********************************************************
+
+   Find a native method in the native method table.
 
-   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)
+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 */
+       tmpnmn.classname  = m->class->name;
+       tmpnmn.name       = m->name;
+       tmpnmn.descriptor = m->descriptor;
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       /* find the entry in the AVL-tree */
 
-       /* find initializer */
+       nmn = avl_find(tree_native_methods, &tmpnmn);
 
-       m = class_findmethod(c, utf_init, utf_void__void);
-                                                     
-       /* initializer not found */
+       if (nmn == NULL)
+               return NULL;
 
-       if (!m)
-               return o;
+       return nmn->function;
+}
 
-       /* call initializer */
 
-       asm_calljavafunction(m, o, NULL, NULL, NULL);
+/* native_library_open *********************************************************
 
-       return o;
-}
+   Open a native library with the given utf8 name.
 
+*******************************************************************************/
 
-java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
+#if defined(ENABLE_LTDL)
+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);
+       handle = lt_dlopen(filename->text);
 
-       if (!o)
-               return NULL;
+       if (handle == NULL) {
+               if (opt_verbose) {
+                       log_start();
+                       log_print("native_library_load: lt_dlopen failed: ");
+                       log_print(lt_dlerror());
+                       log_finish();
+               }
 
-       /* find initializer */
+               return NULL;
+       }
 
-       m = class_resolveclassmethod(c,
-                                                                utf_init,
-                                                                utf_java_lang_String__void,
-                                                                NULL,
-                                                                true);
+       return handle;
+}
+#endif
 
-       /* initializer not found */
 
-       if (!m)
-               return NULL;
+/* native_library_add **********************************************************
 
-       /* call initializer */
+   Adds an entry to the native library hashtable.
 
-       asm_calljavafunction(m, o, s, NULL, NULL);
+*******************************************************************************/
 
-       return o;
-}
+#if defined(ENABLE_LTDL)
+void native_library_add(utf *filename, classloader *loader, lt_dlhandle handle)
+{
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne; /* library name                       */
+       u4   key;                           /* hashkey                            */
+       u4   slot;                          /* slot in hashtable                  */
 
+       LOCK_MONITOR_ENTER(hashtable_library->header);
 
-java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
-{
-       methodinfo *m;
-       java_objectheader *o;
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
 
-       if (!c)
-               return *exceptionptr;
+       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
-       /* create object */
+       /* search external hash chain for the entry */
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       while (le) {
+               if (le->loader == loader)
+                       break;
 
-       /* find initializer */
+               le = le->hashlink;                  /* next element in external chain */
+       }
 
-       m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
+       /* no loader found? create a new entry */
 
-       /* initializer not found  */
+       if (le == NULL) {
+               le = NEW(hashtable_library_loader_entry);
 
-       if (!m)
-               return NULL;
+               le->loader   = loader;
+               le->namelink = NULL;
 
-       /* call initializer */
+               /* insert entry into hashtable */
 
-       asm_calljavafunction(m, o, (void *) (ptrint) i, NULL, NULL);
+               le->hashlink =
+                       (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
+               hashtable_library->ptr[slot] = le;
 
-       return o;
-}
+               /* update number of hashtable-entries */
 
+               hashtable_library->entries++;
+       }
 
-java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
-{
-       methodinfo *m;
-       java_objectheader *o;
 
-       if (!c)
-               return *exceptionptr;
+       /* search for library name */
 
-       /* create object */
+       ne = le->namelink;
 
-       o = builtin_new(c);
-       
-       if (!o)
-               return NULL;
+       while (ne) {
+               if (ne->name == filename) {
+                       LOCK_MONITOR_EXIT(hashtable_library->header);
 
-       /* find initializer */
+                       return;
+               }
 
-       m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
-                                                     
-       /* initializer not found */
+               ne = ne->hashlink;                  /* next element in external chain */
+       }
 
-       if (!m)
-               return NULL;
+       /* not found? add the library name to the classloader */
 
-       /* call initializer */
+       ne = NEW(hashtable_library_name_entry);
 
-       asm_calljavafunction(m, o, t, NULL, NULL);
+       ne->name   = filename;
+       ne->handle = handle;
 
-       return o;
-}
+       /* insert entry into external chain */
 
+       ne->hashlink = le->namelink;
+       le->namelink = ne;
 
-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
+       LOCK_MONITOR_EXIT(hashtable_library->header);
 }
+#endif
 
 
-/******************************************************************************************                                                                                                            
-
-       creates method signature (excluding return type) from array of 
-       class-objects representing the parameters of the method 
+/* native_library_find *********************************************************
 
-*******************************************************************************************/
+   Find an entry in the native library hashtable.
 
+*******************************************************************************/
 
-utf *create_methodsig(java_objectarray* types, char *retType)
+#if defined(ENABLE_LTDL)
+hashtable_library_name_entry *native_library_find(utf *filename,
+                                                                                                 classloader *loader)
 {
-    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 */
-                       }
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne; /* library name                       */
+       u4   key;                           /* hashkey                            */
+       u4   slot;                          /* slot in hashtable                  */
 
-           } else {            
-                       /* check for primitive types */
-                       for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
-                               char *utf_pos   = utf_ptr - 1;
-                               char *primitive = primitivetype_table[j].wrapname;
+       /* normally addresses are aligned to 4, 8 or 16 bytes */
 
-                               /* compare text */
-                               while (utf_pos < utf_end(c->name)) {
-                                       if (*utf_pos++ != *primitive++) goto nomatch;
-                               }
+       key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
-                               /* primitive type found */
-                               *pos++ = primitivetype_table[j].typesig;
-                               goto next_type;
+       /* search external hash chain for the entry */
 
-                       nomatch:
-                               ;
-                       }
+       while (le) {
+               if (le->loader == loader)
+                       break;
 
-                       /* no primitive type and no arrayclass, so must be object */
-                       *pos++ = 'L';
+               le = le->hashlink;                  /* next element in external chain */
+       }
 
-                       /* copy text */
-                       for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
-                               *pos++ = *utf_ptr;
-                       }
+       /* no loader found? return NULL */
 
-                       *pos++ = ';';
+       if (le == NULL)
+               return NULL;
 
-               next_type:
-                       ;
-               }  
-    }      
+       /* search for library name */
 
-    *pos++ = ')';
+       ne = le->namelink;
 
-    if (retType) {
-               for (i = 0; i < strlen(retType); i++) {
-                       *pos++ = retType[i];
-               }
-    }
+       while (ne) {
+               if (ne->name == filename)
+                       return ne;
 
-    /* create utf-string */
-    result = utf_new(buffer, (pos - buffer));
-    MFREE(buffer, char, buffer_size);
+               ne = ne->hashlink;                  /* next element in external chain */
+       }
+
+       /* return entry, if no entry was found, ne is NULL */
 
-    return result;
+       return ne;
 }
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
-/******************************************************************************************
+/* native_findfunction *********************************************************
 
-       retrieve the next argument or returntype from a descriptor
-       and return the corresponding class 
+   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.
 
-classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
+*******************************************************************************/
+
+#if defined(WITH_STATIC_CLASSPATH)
+functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
+                                                               bool isstatic)
 {
-    classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
-                                         (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
-    if (!c)
-       /* unknown type */
-       panic("illegal descriptor");
+       /* entry of table for fast string comparison */
+       struct nativecompref *n;
+       s4 i;
 
-    if (skip) return NULL;
+       isstatic = isstatic ? true : false;
+       
+       if (!nativecompdone) {
+               for (i = 0; i < NATIVETABLESIZE; i++) {
+                       nativecomptable[i].classname  = 
+                               utf_new_char(nativetable[i].classname);
 
-    use_class_as_object(c);
-    return c;
-}
+                       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;
+       }
 
-/* get_parametertypes **********************************************************
+       for (i = 0; i < NATIVETABLESIZE; i++) {
+               n = &(nativecomptable[i]);
 
-   use the descriptor of a method to generate a java/lang/Class array
-   which contains the classes of the parametertypes of the method
+               if (cname == n->classname && mname == n->methodname &&
+                   desc == n->descriptor && isstatic == n->isstatic)
+                       return n->func;
+       }
 
-*******************************************************************************/
+       /* no function was found, throw exception */
 
-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;
+       exceptions_throw_unsatisfiedlinkerror(mname);
+
+       return NULL;
 }
+#endif /* defined(WITH_STATIC_CLASSPATH) */
 
 
-/* get_exceptiontypes **********************************************************
+/* native_resolve_function *****************************************************
 
-   get the exceptions which can be thrown by a method
+   Resolves a native function, maybe from a dynamic library.
 
 *******************************************************************************/
 
-java_objectarray* get_exceptiontypes(methodinfo *m)
+functionptr native_resolve_function(methodinfo *m)
 {
-    u2 excount;
-    u2 i;
-    java_objectarray *result;
+       classloader                    *cl;
+       utf                            *name;
+       utf                            *newname;
+       functionptr                     f;
+#if defined(ENABLE_LTDL)
+       hashtable_library_loader_entry *le;
+       hashtable_library_name_entry   *ne;
+       u4                              key;    /* hashkey                        */
+       u4                              slot;   /* slot in hashtable              */
+#endif
+#if defined(WITH_CLASSPATH_SUN)
+       methodinfo                     *method_findNative;
+       java_handle_t                  *s;
+#endif
 
-       excount = m->thrownexceptionscount;
+       cl = m->class->classloader;
 
-    /* create class-array */
-    result = builtin_anewarray(excount, class_java_lang_Class);
+       /* verbose output */
 
-    for (i = 0; i < excount; i++) {
-               java_objectheader *o = (java_objectheader *) (m->thrownexceptions[i]);
-               use_class_as_object((classinfo *) o);
-               result->data[i] = o;
-    }
+       if (opt_verbosejni) {
+               printf("[Dynamic-linking native method ");
+               utf_display_printable_ascii_classname(m->class->name);
+               printf(".");
+               utf_display_printable_ascii(m->name);
+               printf(" ... ");
+       }
 
-    return result;
-}
+       /* 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;
 
-       get the returntype class of a method
+#if defined(ENABLE_LTDL)
+       /* 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];
 
-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  */
+       /* iterate through loaders in this hash slot */
 
-       utf_ptr  = desc->text;
-       desc_end = utf_end(desc);
+       while ((le != NULL) && (f == NULL)) {
+               /* iterate through names in this loader */
 
-       /* ignore parametertypes */
-        while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
-               /* skip */ ;
+               ne = le->namelink;
+                       
+               while ((ne != NULL) && (f == NULL)) {
+                       f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
 
-       return get_type(&utf_ptr,desc_end, false);
-}
+                       if (f == NULL)
+                               f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
 
+                       ne = ne->hashlink;
+               }
 
-/*****************************************************************************/
-/*****************************************************************************/
+               le = le->hashlink;
+       }
 
+# 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 */
 
-/*--------------------------------------------------------*/
-void printNativeCall(nativeCall nc) {
-  int i,j;
+               method_findNative =
+                       class_resolveclassmethod(class_java_lang_ClassLoader,
+                                                                        utf_findNative,
+                                                                        utf_java_lang_ClassLoader_java_lang_String__J,
+                                                                        class_java_lang_ClassLoader,
+                                                                        true);
 
-  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 (method_findNative == NULL)
+                       return NULL;
 
-    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);
-}
+               /* try the normal name */
 
-/*--------------------------------------------------------*/
-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);
-}
+               s = javastring_new(name);
 
+               f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
+                                                                                                                NULL, cl, s);
 
-/*--------------------------------------------------------*/
-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;
-}
+               /* if not found, try the mangled name */
 
-/*--------------------------------------------------------*/
-nativeCall* findNativeClassCalls(char *aclassname ) {
-int i;
+               if (f == NULL) {
+                       s = javastring_new(newname);
 
-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];
-   }
+                       f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
+                                                                                                                        NULL, cl, s);
+               }
+       }
+# endif
 
-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;
-          }
-      }
-    }
-}
+       if (f != NULL)
+               if (opt_verbosejni)
+                       printf("JNI ]\n");
+#endif
 
+       /* If not found, try to find the native function symbol in the
+          main program. */
 
+       if (f == NULL) {
+               f = native_method_find(m);
 
-/*--------------------------------------------------------*/
+               if (f != NULL)
+                       if (opt_verbosejni)
+                               printf("internal ]\n");
+       }
 
-bool natcall2utf(bool natcallcompdone) {
-int i;
+#if defined(ENABLE_JVMTI)
+       /* fire Native Method Bind event */
+       if (jvmti) jvmti_NativeMethodBind(m, f, &f);
+#endif
 
-if (natcallcompdone) 
-       return true;
+       /* no symbol found? throw exception */
 
-for (i=0;i<NATIVECALLSSIZE; i++) {
-   utfNativeCall  (nativeCalls[i], &nativeCompCalls[i]);  
-   }
+       if (f == NULL) {
+               if (opt_verbosejni)
+                       printf("failed ]\n");
 
-return true;
+               exceptions_throw_unsatisfiedlinkerror(m->name);
+       }
+
+       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.
+                       
+*******************************************************************************/
 
-java_objectarray *builtin_asm_createclasscontextarray(classinfo **end, classinfo **start)
+java_handle_t *native_new_and_init(classinfo *c)
 {
-#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--;
-               }
-       }
+       methodinfo    *m;
+       java_handle_t *o;
 
-       tmpArray =
-               builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
+       if (c == NULL)
+               vm_abort("native_new_and_init: c == NULL");
 
-       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;
-       }
+       /* create object */
+
+       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 */
 
-       return tmpArray;
+       if (m == NULL)
+               return o;
+
+       /* call initializer */
+
+       (void) vm_call_method(m, o);
+
+       return o;
 }
 
 
-java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end, classinfo **start)
+java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s)
 {
-#if defined(__GNUC__)
-#warning platform dependend
-#endif
-       int i;
-       classinfo **current;
-       classinfo *c;
-       classinfo *privilegedAction;
-       size_t size;
+       methodinfo    *m;
+       java_handle_t *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 */
+
+       (void) vm_call_method(m, o, s);
+
+       return o;
 }