1 /* src/native/native.c - table of native functions
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
31 Changes: Christian Thalinger
33 $Id: native.c 5810 2006-10-20 13:54:54Z twisti $
43 #if !defined(WITH_STATIC_CLASSPATH)
49 #include "mm/memory.h"
50 #include "native/jni.h"
51 #include "native/native.h"
52 #include "native/include/java_lang_Throwable.h"
54 #if defined(ENABLE_THREADS)
55 # include "threads/native/lock.h"
57 # include "threads/none/lock.h"
60 #include "toolbox/logging.h"
61 #include "vm/builtin.h"
62 #include "vm/exceptions.h"
63 #include "vm/global.h"
64 #include "vm/hashtable.h"
65 #include "vm/loader.h"
66 #include "vm/options.h"
67 #include "vm/resolve.h"
68 #include "vm/stringlocal.h"
70 #include "vm/jit/asmpart.h"
71 #include "vm/jit/jit.h"
73 #if defined(ENABLE_JVMTI)
74 #include "native/jvmti/cacaodbg.h"
78 /* include table of native functions ******************************************/
80 #include "native/include/java_lang_Cloneable.h"
81 #include "native/include/java_util_Properties.h"
82 #include "native/include/java_io_InputStream.h"
83 #include "native/include/java_io_PrintStream.h"
85 #include "native/include/gnu_classpath_VMStackWalker.h"
86 #include "native/include/gnu_classpath_VMSystemProperties.h"
87 #include "native/include/gnu_java_lang_management_VMClassLoadingMXBeanImpl.h"
88 #include "native/include/gnu_java_lang_management_VMMemoryMXBeanImpl.h"
89 #include "native/include/gnu_java_lang_management_VMRuntimeMXBeanImpl.h"
90 #include "native/include/java_lang_Class.h"
91 #include "native/include/java_lang_Object.h"
92 #include "native/include/java_lang_VMClass.h"
93 #include "native/include/java_lang_VMClassLoader.h"
94 #include "native/include/java_lang_VMObject.h"
95 #include "native/include/java_lang_VMRuntime.h"
96 #include "native/include/java_lang_VMString.h"
97 #include "native/include/java_lang_VMSystem.h"
98 #include "native/include/java_lang_VMThread.h"
99 #include "native/include/java_lang_VMThrowable.h"
100 #include "native/include/java_lang_management_VMManagementFactory.h"
101 #include "native/include/java_lang_reflect_Constructor.h"
102 #include "native/include/java_lang_reflect_Field.h"
103 #include "native/include/java_lang_reflect_Method.h"
104 #include "native/include/java_lang_reflect_VMProxy.h"
105 #include "native/include/java_security_VMAccessController.h"
107 #if defined(ENABLE_JVMTI)
108 #include "native/include/gnu_classpath_jdwp_event_EventRequest.h"
109 #include "native/include/java_nio_ByteBuffer.h"
110 #include "native/include/gnu_classpath_jdwp_VMVirtualMachine.h"
111 #include "native/include/gnu_classpath_jdwp_VMFrame.h"
112 #include "native/include/gnu_classpath_jdwp_VMMethod.h"
115 #if defined(WITH_STATIC_CLASSPATH)
117 /* these are required to prevent compiler warnings */
119 #include "native/include/java_net_DatagramPacket.h"
120 #include "native/include/java_net_InetAddress.h"
121 #include "native/include/java_net_SocketImpl.h"
123 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
124 #include "native/include/gnu_java_net_PlainSocketImpl.h"
125 #include "native/include/gnu_java_nio_PipeImpl.h"
126 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
127 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
128 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
129 #include "native/include/java_lang_VMProcess.h"
130 #include "native/include/java_nio_MappedByteBufferImpl.h"
131 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
133 /* now include the native table */
135 #include "native/nativetable.inc"
137 #elif !defined(ENABLE_LIBJVM)
139 /* dummynativetable ************************************************************
141 Ensure that symbols for functions implemented within CACAO are used
142 and exported to dlopen.
144 ATTENTION: Don't make this table static!!! Otherwise the compiler
145 may optimize it away!
147 *******************************************************************************/
149 functionptr dummynativetable[] = {
150 (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
151 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClass,
152 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClassLoader,
153 (functionptr) Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader,
155 (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
157 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getLoadedClassCount,
158 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getUnloadedClassCount,
159 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_isVerbose,
160 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_setVerbose,
162 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getHeapMemoryUsage,
163 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getNonHeapMemoryUsage,
164 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getObjectPendingFinalizationCount,
165 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_isVerbose,
166 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_setVerbose,
168 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getInputArguments,
169 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getStartTime,
171 (functionptr) Java_java_lang_VMClass_isInstance,
172 (functionptr) Java_java_lang_VMClass_isAssignableFrom,
173 (functionptr) Java_java_lang_VMClass_isInterface,
174 (functionptr) Java_java_lang_VMClass_isPrimitive,
175 (functionptr) Java_java_lang_VMClass_getName,
176 (functionptr) Java_java_lang_VMClass_getSuperclass,
177 (functionptr) Java_java_lang_VMClass_getInterfaces,
178 (functionptr) Java_java_lang_VMClass_getComponentType,
179 (functionptr) Java_java_lang_VMClass_getModifiers,
180 (functionptr) Java_java_lang_VMClass_getDeclaringClass,
181 (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
182 (functionptr) Java_java_lang_VMClass_getDeclaredFields,
183 (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
184 (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
185 (functionptr) Java_java_lang_VMClass_getClassLoader,
186 (functionptr) Java_java_lang_VMClass_forName,
187 (functionptr) Java_java_lang_VMClass_isArray,
188 (functionptr) Java_java_lang_VMClass_throwException,
190 (functionptr) Java_java_lang_VMClassLoader_defineClass,
191 (functionptr) Java_java_lang_VMClassLoader_resolveClass,
192 (functionptr) Java_java_lang_VMClassLoader_loadClass,
193 (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
194 (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
195 (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
197 (functionptr) Java_java_lang_VMObject_getClass,
198 (functionptr) Java_java_lang_VMObject_clone,
199 (functionptr) Java_java_lang_VMObject_notify,
200 (functionptr) Java_java_lang_VMObject_notifyAll,
201 (functionptr) Java_java_lang_VMObject_wait,
203 (functionptr) Java_java_lang_VMRuntime_availableProcessors,
204 (functionptr) Java_java_lang_VMRuntime_freeMemory,
205 (functionptr) Java_java_lang_VMRuntime_totalMemory,
206 (functionptr) Java_java_lang_VMRuntime_maxMemory,
207 (functionptr) Java_java_lang_VMRuntime_gc,
208 (functionptr) Java_java_lang_VMRuntime_runFinalization,
209 (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
210 (functionptr) Java_java_lang_VMRuntime_traceInstructions,
211 (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
212 (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
213 (functionptr) Java_java_lang_VMRuntime_exit,
214 (functionptr) Java_java_lang_VMRuntime_nativeLoad,
215 (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
217 (functionptr) Java_java_lang_VMString_intern,
219 (functionptr) Java_java_lang_VMSystem_arraycopy,
220 (functionptr) Java_java_lang_VMSystem_identityHashCode,
222 (functionptr) Java_java_lang_VMThread_start,
223 (functionptr) Java_java_lang_VMThread_interrupt,
224 (functionptr) Java_java_lang_VMThread_isInterrupted,
225 (functionptr) Java_java_lang_VMThread_suspend,
226 (functionptr) Java_java_lang_VMThread_resume,
227 (functionptr) Java_java_lang_VMThread_nativeSetPriority,
228 (functionptr) Java_java_lang_VMThread_nativeStop,
229 (functionptr) Java_java_lang_VMThread_currentThread,
230 (functionptr) Java_java_lang_VMThread_yield,
231 (functionptr) Java_java_lang_VMThread_interrupted,
232 (functionptr) Java_java_lang_VMThread_holdsLock,
234 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
235 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
237 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryPoolNames,
238 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryManagerNames,
239 (functionptr) Java_java_lang_management_VMManagementFactory_getGarbageCollectorNames,
241 (functionptr) Java_java_lang_reflect_Constructor_getModifiersInternal,
242 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
244 (functionptr) Java_java_lang_reflect_Field_getModifiersInternal,
245 (functionptr) Java_java_lang_reflect_Field_getType,
246 (functionptr) Java_java_lang_reflect_Field_get,
247 (functionptr) Java_java_lang_reflect_Field_getBoolean,
248 (functionptr) Java_java_lang_reflect_Field_getByte,
249 (functionptr) Java_java_lang_reflect_Field_getChar,
250 (functionptr) Java_java_lang_reflect_Field_getShort,
251 (functionptr) Java_java_lang_reflect_Field_getInt,
252 (functionptr) Java_java_lang_reflect_Field_getLong,
253 (functionptr) Java_java_lang_reflect_Field_getFloat,
254 (functionptr) Java_java_lang_reflect_Field_getDouble,
255 (functionptr) Java_java_lang_reflect_Field_set,
256 (functionptr) Java_java_lang_reflect_Field_setBoolean,
257 (functionptr) Java_java_lang_reflect_Field_setByte,
258 (functionptr) Java_java_lang_reflect_Field_setChar,
259 (functionptr) Java_java_lang_reflect_Field_setShort,
260 (functionptr) Java_java_lang_reflect_Field_setInt,
261 (functionptr) Java_java_lang_reflect_Field_setLong,
262 (functionptr) Java_java_lang_reflect_Field_setFloat,
263 (functionptr) Java_java_lang_reflect_Field_setDouble,
265 (functionptr) Java_java_lang_reflect_Method_getModifiersInternal,
266 (functionptr) Java_java_lang_reflect_Method_getReturnType,
267 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
268 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
269 (functionptr) Java_java_lang_reflect_Method_invokeNative,
271 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
272 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
273 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
275 (functionptr) Java_java_security_VMAccessController_getStack,
277 #if defined(ENABLE_JVMTI)
278 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread,
279 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread,
280 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount,
281 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount,
282 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus,
283 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods,
284 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod,
285 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames,
286 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame,
287 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount,
288 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus,
289 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests,
290 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod,
291 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile,
292 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent,
293 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent,
294 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents,
295 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses,
296 (functionptr) Java_gnu_classpath_jdwp_VMFrame_setValue,
297 (functionptr) Java_gnu_classpath_jdwp_VMFrame_getValue,
298 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getName,
299 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getSignature,
300 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getModifiers,
301 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getLineTable,
302 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
307 #endif /* defined(ENABLE_LIBJVM) */
310 /* tables for methods *********************************************************/
312 #if defined(WITH_STATIC_CLASSPATH)
313 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
315 /* table for fast string comparison */
316 static nativecompref nativecomptable[NATIVETABLESIZE];
318 /* string comparsion table initialized */
319 static bool nativecompdone = false;
323 /* global variables ***********************************************************/
325 #if !defined(WITH_STATIC_CLASSPATH)
326 static hashtable *hashtable_library;
327 static lt_dlhandle mainhandle;
331 /* native_init *****************************************************************
333 Initializes the native subsystem.
335 *******************************************************************************/
337 bool native_init(void)
339 #if !defined(WITH_STATIC_CLASSPATH)
340 /* initialize libltdl */
343 vm_abort("lt_dlinit failed: %s\n", lt_dlerror());
345 /* Get the handle for the main program or for the libjvm.so,
346 depends on the configuration. */
348 # if defined(ENABLE_LIBJVM)
349 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
350 If not found, try the absolute path. */
352 if (!(mainhandle = lt_dlopenext("libjvm")))
353 if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
354 vm_abort("lt_dlopenext failed: %s\n", lt_dlerror());
356 if (!(mainhandle = lt_dlopen(NULL)))
357 vm_abort("lt_dlopen failed: %s\n", lt_dlerror());
360 /* initialize library hashtable, 10 entries should be enough */
362 hashtable_library = NEW(hashtable);
364 hashtable_create(hashtable_library, 10);
367 /* everything's ok */
373 /* native_hashtable_library_add ************************************************
375 Adds an entry to the native library hashtable.
377 *******************************************************************************/
379 #if !defined(WITH_STATIC_CLASSPATH)
380 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
383 hashtable_library_loader_entry *le;
384 hashtable_library_name_entry *ne; /* library name */
385 u4 key; /* hashkey */
386 u4 slot; /* slot in hashtable */
388 LOCK_MONITOR_ENTER(hashtable_library->header);
390 /* normally addresses are aligned to 4, 8 or 16 bytes */
392 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
393 slot = key & (hashtable_library->size - 1);
394 le = hashtable_library->ptr[slot];
396 /* search external hash chain for the entry */
399 if (le->loader == loader)
402 le = le->hashlink; /* next element in external chain */
405 /* no loader found? create a new entry */
408 le = NEW(hashtable_library_loader_entry);
413 /* insert entry into hashtable */
416 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
417 hashtable_library->ptr[slot] = le;
419 /* update number of hashtable-entries */
421 hashtable_library->entries++;
425 /* search for library name */
430 if (ne->name == filename) {
431 LOCK_MONITOR_EXIT(hashtable_library->header);
436 ne = ne->hashlink; /* next element in external chain */
439 /* not found? add the library name to the classloader */
441 ne = NEW(hashtable_library_name_entry);
446 /* insert entry into external chain */
448 ne->hashlink = le->namelink;
451 LOCK_MONITOR_EXIT(hashtable_library->header);
453 #endif /* !defined(WITH_STATIC_CLASSPATH) */
456 /* native_hashtable_library_find ***********************************************
458 Find an entry in the native library hashtable.
460 *******************************************************************************/
462 #if !defined(WITH_STATIC_CLASSPATH)
463 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
464 java_objectheader *loader)
466 hashtable_library_loader_entry *le;
467 hashtable_library_name_entry *ne; /* library name */
468 u4 key; /* hashkey */
469 u4 slot; /* slot in hashtable */
471 /* normally addresses are aligned to 4, 8 or 16 bytes */
473 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
474 slot = key & (hashtable_library->size - 1);
475 le = hashtable_library->ptr[slot];
477 /* search external hash chain for the entry */
480 if (le->loader == loader)
483 le = le->hashlink; /* next element in external chain */
486 /* no loader found? return NULL */
491 /* search for library name */
496 if (ne->name == filename)
499 ne = ne->hashlink; /* next element in external chain */
502 /* return entry, if no entry was found, ne is NULL */
506 #endif /* !defined(WITH_STATIC_CLASSPATH) */
509 /* native_findfunction *********************************************************
511 Looks up a method (must have the same class name, method name,
512 descriptor and 'static'ness) and returns a function pointer to it.
513 Returns: function pointer or NULL (if there is no such method)
515 Remark: For faster operation, the names/descriptors are converted
516 from C strings to Unicode the first time this function is called.
518 *******************************************************************************/
520 #if defined(WITH_STATIC_CLASSPATH)
521 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
524 /* entry of table for fast string comparison */
525 struct nativecompref *n;
528 isstatic = isstatic ? true : false;
530 if (!nativecompdone) {
531 for (i = 0; i < NATIVETABLESIZE; i++) {
532 nativecomptable[i].classname =
533 utf_new_char(nativetable[i].classname);
535 nativecomptable[i].methodname =
536 utf_new_char(nativetable[i].methodname);
538 nativecomptable[i].descriptor =
539 utf_new_char(nativetable[i].descriptor);
541 nativecomptable[i].isstatic = nativetable[i].isstatic;
542 nativecomptable[i].func = nativetable[i].func;
545 nativecompdone = true;
548 for (i = 0; i < NATIVETABLESIZE; i++) {
549 n = &(nativecomptable[i]);
551 if (cname == n->classname && mname == n->methodname &&
552 desc == n->descriptor && isstatic == n->isstatic)
557 /* no function was found, throw exception */
560 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
565 #endif /* defined(WITH_STATIC_CLASSPATH) */
568 /* native_make_overloaded_function *********************************************
572 *******************************************************************************/
574 #if !defined(WITH_STATIC_CLASSPATH)
575 static char *native_make_overloaded_function(char *name, utf *desc)
583 utf_ptr = desc->text;
584 namelen = strlen(name) + strlen("__") + strlen("0");
586 /* calculate additional length */
588 while ((c = utf_nextu2(&utf_ptr)) != ')') {
605 while (utf_nextu2(&utf_ptr) != ';')
617 /* reallocate memory */
621 newname = DMNEW(char, namelen);
622 MCOPY(newname, name, char, i);
624 utf_ptr = desc->text;
629 while ((c = utf_nextu2(&utf_ptr)) != ')') {
647 while ((c = utf_nextu2(&utf_ptr)) != ';')
648 if (((c >= 'a') && (c <= 'z')) ||
649 ((c >= 'A') && (c <= 'Z')) ||
650 ((c >= '0') && (c <= '9')))
672 /* native_insert_char **********************************************************
674 Inserts the passed UTF character into the native method name. If
675 necessary it is escaped properly.
677 *******************************************************************************/
679 static s4 native_insert_char(char *name, u4 pos, u2 c)
687 /* replace '/' or '.' with '_' */
692 /* escape sequence for '_' is '_1' */
698 /* escape sequence for ';' is '_2' */
704 /* escape sequence for '[' is '_1' */
713 /* unicode character */
717 for (i = 0; i < 4; ++i) {
719 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
728 /* return the new buffer index */
734 /* native_resolve_function *****************************************************
736 Resolves a native function, maybe from a dynamic library.
738 *******************************************************************************/
740 functionptr native_resolve_function(methodinfo *m)
751 hashtable_library_loader_entry *le;
752 hashtable_library_name_entry *ne;
753 u4 key; /* hashkey */
754 u4 slot; /* slot in hashtable */
758 if (opt_verbosejni) {
759 printf("[Dynamic-linking native method ");
760 utf_display_printable_ascii_classname(m->class->name);
762 utf_display_printable_ascii(m->name);
766 /* Calculate length of native function name. We multiply the
767 class and method name length by 6 as this is the maxium
768 escape-sequence that can be generated (unicode). */
770 namelen = strlen("Java_") +
771 utf_get_number_of_u2s(m->class->name) * 6 +
773 utf_get_number_of_u2s(m->name) * 6 +
776 /* allocate memory */
778 dumpsize = dump_size();
780 name = DMNEW(char, namelen);
782 /* generate name of native functions */
784 strcpy(name, "Java_");
785 pos = strlen("Java_");
787 utf_ptr = m->class->name->text;
788 utf_endptr = UTF_END(m->class->name);
790 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
792 pos = native_insert_char(name, pos, c);
795 /* seperator between class and method */
799 utf_ptr = m->name->text;
800 utf_endptr = UTF_END(m->name);
802 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
804 pos = native_insert_char(name, pos, c);
811 /* check for an buffer overflow */
813 assert(pos <= namelen);
815 /* generate overloaded function (having the types in it's name) */
817 newname = native_make_overloaded_function(name, m->descriptor);
819 /* check the library hash entries of the classloader of the
824 /* normally addresses are aligned to 4, 8 or 16 bytes */
826 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
827 slot = key & (hashtable_library->size - 1);
828 le = hashtable_library->ptr[slot];
830 /* iterate through loaders in this hash slot */
832 while ((le != NULL) && (sym == NULL)) {
833 /* iterate through names in this loader */
837 while ((ne != NULL) && (sym == NULL)) {
838 sym = lt_dlsym(ne->handle, name);
841 sym = lt_dlsym(ne->handle, newname);
854 /* If not found, try to find the native function symbol in the
858 sym = lt_dlsym(mainhandle, name);
861 sym = lt_dlsym(mainhandle, newname);
865 printf("internal ]\n");
869 #if defined(ENABLE_JVMTI)
870 /* fire Native Method Bind event */
871 if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
874 /* no symbol found? throw exception */
878 printf("failed ]\n");
881 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
887 dump_release(dumpsize);
889 return (functionptr) (ptrint) sym;
891 #endif /* !defined(WITH_STATIC_CLASSPATH) */
894 /* native_new_and_init *********************************************************
896 Creates a new object on the heap and calls the initializer.
897 Returns the object pointer or NULL if memory is exhausted.
899 *******************************************************************************/
901 java_objectheader *native_new_and_init(classinfo *c)
904 java_objectheader *o;
907 return *exceptionptr;
916 /* try to find the initializer */
918 m = class_findmethod(c, utf_init, utf_void__void);
920 /* ATTENTION: returning the object here is ok, since the class may
921 not have an initializer */
926 /* call initializer */
928 (void) vm_call_method(m, o);
934 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
937 java_objectheader *o;
940 return *exceptionptr;
949 /* find initializer */
951 m = class_resolveclassmethod(c,
953 utf_java_lang_String__void,
957 /* initializer not found */
962 /* call initializer */
964 (void) vm_call_method(m, o, s);
970 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
973 java_objectheader *o;
976 return *exceptionptr;
985 /* find initializer */
987 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
989 /* initializer not found */
994 /* call initializer */
996 (void) vm_call_method(m, o, i);
1002 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
1005 java_objectheader *o;
1008 return *exceptionptr;
1017 /* find initializer */
1019 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
1021 /* initializer not found */
1026 /* call initializer */
1028 (void) vm_call_method(m, o, t);
1034 /* native_get_parametertypes ***************************************************
1036 Use the descriptor of a method to generate a java/lang/Class array
1037 which contains the classes of the parametertypes of the method.
1039 *******************************************************************************/
1041 java_objectarray *native_get_parametertypes(methodinfo *m)
1044 typedesc *paramtypes;
1046 java_objectarray *oa;
1051 /* is the descriptor fully parsed? */
1053 if (!m->parseddesc->params)
1054 if (!descriptor_params_from_paramtypes(md, m->flags))
1057 paramtypes = md->paramtypes;
1058 paramcount = md->paramcount;
1060 /* skip `this' pointer */
1062 if (!(m->flags & ACC_STATIC)) {
1067 /* create class-array */
1069 oa = builtin_anewarray(paramcount, class_java_lang_Class);
1076 for (i = 0; i < paramcount; i++)
1077 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
1078 (classinfo **) &oa->data[i]))
1085 /* native_get_exceptiontypes ***************************************************
1087 Get the exceptions which can be thrown by a method.
1089 *******************************************************************************/
1091 java_objectarray *native_get_exceptiontypes(methodinfo *m)
1093 java_objectarray *oa;
1097 /* create class-array */
1099 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
1104 for (i = 0; i < m->thrownexceptionscount; i++) {
1105 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
1106 resolveEager, true, false, &c))
1109 oa->data[i] = (java_objectheader *) c;
1116 /* native_get_returntype *******************************************************
1118 Get the returntype class of a method.
1120 *******************************************************************************/
1122 classinfo *native_get_returntype(methodinfo *m)
1126 if (!resolve_class_from_typedesc(&(m->parseddesc->returntype), true, false,
1135 * These are local overrides for various environment variables in Emacs.
1136 * Please do not remove this and leave it at the end of the file, where
1137 * Emacs will automagically detect them.
1138 * ---------------------------------------------------------------------
1141 * indent-tabs-mode: t