1 /* src/native/native.c - table of native functions
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: native.c 7246 2007-01-29 18:49:05Z twisti $
35 #if !defined(WITH_STATIC_CLASSPATH)
41 #include "mm/memory.h"
42 #include "native/jni.h"
43 #include "native/native.h"
44 #include "native/include/java_lang_String.h"
45 #include "native/include/java_lang_Throwable.h"
47 #if defined(ENABLE_THREADS)
48 # include "threads/native/lock.h"
50 # include "threads/none/lock.h"
53 #include "toolbox/hashtable.h"
54 #include "toolbox/logging.h"
56 #include "vm/builtin.h"
57 #include "vm/exceptions.h"
58 #include "vm/global.h"
59 #include "vm/stringlocal.h"
62 #include "vm/jit/asmpart.h"
63 #include "vm/jit/jit.h"
65 #include "vmcore/loader.h"
66 #include "vmcore/options.h"
67 #include "vmcore/resolve.h"
69 #if defined(ENABLE_JVMTI)
70 #include "native/jvmti/cacaodbg.h"
74 /* include table of native functions ******************************************/
76 #if defined(ENABLE_JAVASE)
78 #include "native/include/java_io_InputStream.h"
79 #include "native/include/java_io_PrintStream.h"
81 #include "native/include/java_lang_Cloneable.h"
82 #include "native/include/java_util_Properties.h"
84 #include "native/include/java_lang_Object.h"
86 #include "native/include/gnu_classpath_VMStackWalker.h"
87 #include "native/include/gnu_classpath_VMSystemProperties.h"
88 #include "native/include/gnu_java_lang_management_VMClassLoadingMXBeanImpl.h"
89 #include "native/include/gnu_java_lang_management_VMMemoryMXBeanImpl.h"
90 #include "native/include/gnu_java_lang_management_VMRuntimeMXBeanImpl.h"
91 #include "native/include/java_lang_VMClass.h"
92 #include "native/include/java_security_ProtectionDomain.h" /* required by... */
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 #elif defined(ENABLE_JAVAME_CLDC1_1)
117 #include "native/include/com_sun_cldchi_io_ConsoleOutputStream.h"
118 #include "native/include/java_lang_Class.h"
119 #include "native/include/java_lang_Double.h"
120 #include "native/include/java_lang_Float.h"
121 #include "native/include/java_lang_Math.h"
122 #include "native/include/java_lang_Runtime.h"
123 #include "native/include/java_lang_System.h"
124 #include "native/include/java_lang_Thread.h"
128 #if defined(WITH_STATIC_CLASSPATH)
130 /* these are required to prevent compiler warnings */
132 #include "native/include/java_net_DatagramPacket.h"
133 #include "native/include/java_net_InetAddress.h"
134 #include "native/include/java_net_SocketImpl.h"
136 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
137 #include "native/include/gnu_java_net_PlainSocketImpl.h"
138 #include "native/include/gnu_java_nio_PipeImpl.h"
139 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
140 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
141 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
142 #include "native/include/java_lang_VMProcess.h"
143 #include "native/include/java_nio_MappedByteBufferImpl.h"
144 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
146 /* now include the native table */
148 #include "native/nativetable.inc"
150 #elif !defined(ENABLE_LIBJVM)
152 /* dummynativetable ************************************************************
154 Ensure that symbols for functions implemented within CACAO are used
155 and exported to dlopen.
157 ATTENTION: Don't make this table static!!! Otherwise the compiler
158 may optimize it away!
160 *******************************************************************************/
162 functionptr dummynativetable[] = {
163 #if defined(ENABLE_JAVASE)
164 (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
165 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClass,
166 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClassLoader,
167 (functionptr) Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader,
169 (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
171 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getLoadedClassCount,
172 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getUnloadedClassCount,
173 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_isVerbose,
174 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_setVerbose,
176 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getHeapMemoryUsage,
177 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getNonHeapMemoryUsage,
178 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getObjectPendingFinalizationCount,
179 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_isVerbose,
180 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_setVerbose,
182 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getInputArguments,
183 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getStartTime,
185 (functionptr) Java_java_lang_VMClass_isInstance,
186 (functionptr) Java_java_lang_VMClass_isAssignableFrom,
187 (functionptr) Java_java_lang_VMClass_isInterface,
188 (functionptr) Java_java_lang_VMClass_isPrimitive,
189 (functionptr) Java_java_lang_VMClass_getName,
190 (functionptr) Java_java_lang_VMClass_getSuperclass,
191 (functionptr) Java_java_lang_VMClass_getInterfaces,
192 (functionptr) Java_java_lang_VMClass_getComponentType,
193 (functionptr) Java_java_lang_VMClass_getModifiers,
194 (functionptr) Java_java_lang_VMClass_getDeclaringClass,
195 (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
196 (functionptr) Java_java_lang_VMClass_getDeclaredFields,
197 (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
198 (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
199 (functionptr) Java_java_lang_VMClass_getClassLoader,
200 (functionptr) Java_java_lang_VMClass_forName,
201 (functionptr) Java_java_lang_VMClass_isArray,
202 (functionptr) Java_java_lang_VMClass_throwException,
204 (functionptr) Java_java_lang_VMClassLoader_defineClass,
205 (functionptr) Java_java_lang_VMClassLoader_resolveClass,
206 (functionptr) Java_java_lang_VMClassLoader_loadClass,
207 (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
208 (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
209 (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
211 (functionptr) Java_java_lang_VMObject_getClass,
212 (functionptr) Java_java_lang_VMObject_clone,
213 (functionptr) Java_java_lang_VMObject_notify,
214 (functionptr) Java_java_lang_VMObject_notifyAll,
215 (functionptr) Java_java_lang_VMObject_wait,
217 (functionptr) Java_java_lang_VMRuntime_availableProcessors,
218 (functionptr) Java_java_lang_VMRuntime_freeMemory,
219 (functionptr) Java_java_lang_VMRuntime_totalMemory,
220 (functionptr) Java_java_lang_VMRuntime_maxMemory,
221 (functionptr) Java_java_lang_VMRuntime_gc,
222 (functionptr) Java_java_lang_VMRuntime_runFinalization,
223 (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
224 (functionptr) Java_java_lang_VMRuntime_traceInstructions,
225 (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
226 (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
227 (functionptr) Java_java_lang_VMRuntime_exit,
228 (functionptr) Java_java_lang_VMRuntime_nativeLoad,
229 (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
231 (functionptr) Java_java_lang_VMString_intern,
233 (functionptr) Java_java_lang_VMSystem_arraycopy,
234 (functionptr) Java_java_lang_VMSystem_identityHashCode,
236 (functionptr) Java_java_lang_VMThread_start,
237 (functionptr) Java_java_lang_VMThread_interrupt,
238 (functionptr) Java_java_lang_VMThread_isInterrupted,
239 (functionptr) Java_java_lang_VMThread_suspend,
240 (functionptr) Java_java_lang_VMThread_resume,
241 (functionptr) Java_java_lang_VMThread_nativeSetPriority,
242 (functionptr) Java_java_lang_VMThread_nativeStop,
243 (functionptr) Java_java_lang_VMThread_currentThread,
244 (functionptr) Java_java_lang_VMThread_yield,
245 (functionptr) Java_java_lang_VMThread_interrupted,
246 (functionptr) Java_java_lang_VMThread_holdsLock,
248 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
249 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
251 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryPoolNames,
252 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryManagerNames,
253 (functionptr) Java_java_lang_management_VMManagementFactory_getGarbageCollectorNames,
255 (functionptr) Java_java_lang_reflect_Constructor_getModifiersInternal,
256 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
258 (functionptr) Java_java_lang_reflect_Field_getModifiersInternal,
259 (functionptr) Java_java_lang_reflect_Field_getType,
260 (functionptr) Java_java_lang_reflect_Field_get,
261 (functionptr) Java_java_lang_reflect_Field_getBoolean,
262 (functionptr) Java_java_lang_reflect_Field_getByte,
263 (functionptr) Java_java_lang_reflect_Field_getChar,
264 (functionptr) Java_java_lang_reflect_Field_getShort,
265 (functionptr) Java_java_lang_reflect_Field_getInt,
266 (functionptr) Java_java_lang_reflect_Field_getLong,
267 (functionptr) Java_java_lang_reflect_Field_getFloat,
268 (functionptr) Java_java_lang_reflect_Field_getDouble,
269 (functionptr) Java_java_lang_reflect_Field_set,
270 (functionptr) Java_java_lang_reflect_Field_setBoolean,
271 (functionptr) Java_java_lang_reflect_Field_setByte,
272 (functionptr) Java_java_lang_reflect_Field_setChar,
273 (functionptr) Java_java_lang_reflect_Field_setShort,
274 (functionptr) Java_java_lang_reflect_Field_setInt,
275 (functionptr) Java_java_lang_reflect_Field_setLong,
276 (functionptr) Java_java_lang_reflect_Field_setFloat,
277 (functionptr) Java_java_lang_reflect_Field_setDouble,
279 (functionptr) Java_java_lang_reflect_Method_getModifiersInternal,
280 (functionptr) Java_java_lang_reflect_Method_getReturnType,
281 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
282 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
283 (functionptr) Java_java_lang_reflect_Method_invokeNative,
285 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
286 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
287 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
289 (functionptr) Java_java_security_VMAccessController_getStack,
291 #if defined(ENABLE_JVMTI)
292 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread,
293 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread,
294 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount,
295 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount,
296 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus,
297 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods,
298 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod,
299 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames,
300 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame,
301 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount,
302 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus,
303 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests,
304 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod,
305 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile,
306 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent,
307 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent,
308 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents,
309 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses,
310 (functionptr) Java_gnu_classpath_jdwp_VMFrame_setValue,
311 (functionptr) Java_gnu_classpath_jdwp_VMFrame_getValue,
312 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getName,
313 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getSignature,
314 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getModifiers,
315 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getLineTable,
316 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
319 #elif defined(ENABLE_JAVAME_CLDC1_1)
320 (functionptr) Java_com_sun_cldchi_io_ConsoleOutputStream_write,
322 (functionptr) Java_java_lang_Class_forName,
323 (functionptr) Java_java_lang_Class_newInstance,
324 (functionptr) Java_java_lang_Class_getName,
326 (functionptr) Java_java_lang_Double_doubleToLongBits,
328 (functionptr) Java_java_lang_Float_floatToIntBits,
330 (functionptr) Java_java_lang_Math_ceil,
331 (functionptr) Java_java_lang_Math_cos,
332 (functionptr) Java_java_lang_Math_floor,
333 (functionptr) Java_java_lang_Math_sin,
334 (functionptr) Java_java_lang_Math_sqrt,
335 (functionptr) Java_java_lang_Math_tan,
337 (functionptr) Java_java_lang_Runtime_exitInternal,
339 (functionptr) Java_java_lang_System_getProperty0,
341 (functionptr) Java_java_lang_Thread_currentThread,
342 (functionptr) Java_java_lang_Thread_setPriority0,
343 (functionptr) Java_java_lang_Thread_start0,
344 (functionptr) Java_java_lang_Thread_yield,
346 (functionptr) Java_java_lang_Throwable_printStackTrace,
347 (functionptr) Java_java_lang_Throwable_fillInStackTrace
351 #endif /* defined(ENABLE_LIBJVM) */
354 /* tables for methods *********************************************************/
356 #if defined(WITH_STATIC_CLASSPATH)
357 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
359 /* table for fast string comparison */
360 static nativecompref nativecomptable[NATIVETABLESIZE];
362 /* string comparsion table initialized */
363 static bool nativecompdone = false;
367 /* global variables ***********************************************************/
369 #if !defined(WITH_STATIC_CLASSPATH)
370 static hashtable *hashtable_library;
371 static lt_dlhandle mainhandle;
375 /* native_init *****************************************************************
377 Initializes the native subsystem.
379 *******************************************************************************/
381 bool native_init(void)
383 #if !defined(WITH_STATIC_CLASSPATH)
384 /* initialize libltdl */
387 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
389 /* Get the handle for the main program or for the libjvm.so,
390 depends on the configuration. */
392 # if defined(ENABLE_LIBJVM)
393 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
394 If not found, try the absolute path. */
396 if (!(mainhandle = lt_dlopenext("libjvm")))
397 if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
398 vm_abort("native_init: lt_dlopenext failed: %s\n", lt_dlerror());
400 if (!(mainhandle = lt_dlopen(NULL)))
401 vm_abort("native_init: lt_dlopen failed: %s\n", lt_dlerror());
404 /* initialize library hashtable, 10 entries should be enough */
406 hashtable_library = NEW(hashtable);
408 hashtable_create(hashtable_library, 10);
411 /* everything's ok */
417 /* native_hashtable_library_add ************************************************
419 Adds an entry to the native library hashtable.
421 *******************************************************************************/
423 #if !defined(WITH_STATIC_CLASSPATH)
424 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
427 hashtable_library_loader_entry *le;
428 hashtable_library_name_entry *ne; /* library name */
429 u4 key; /* hashkey */
430 u4 slot; /* slot in hashtable */
432 LOCK_MONITOR_ENTER(hashtable_library->header);
434 /* normally addresses are aligned to 4, 8 or 16 bytes */
436 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
437 slot = key & (hashtable_library->size - 1);
438 le = hashtable_library->ptr[slot];
440 /* search external hash chain for the entry */
443 if (le->loader == loader)
446 le = le->hashlink; /* next element in external chain */
449 /* no loader found? create a new entry */
452 le = NEW(hashtable_library_loader_entry);
457 /* insert entry into hashtable */
460 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
461 hashtable_library->ptr[slot] = le;
463 /* update number of hashtable-entries */
465 hashtable_library->entries++;
469 /* search for library name */
474 if (ne->name == filename) {
475 LOCK_MONITOR_EXIT(hashtable_library->header);
480 ne = ne->hashlink; /* next element in external chain */
483 /* not found? add the library name to the classloader */
485 ne = NEW(hashtable_library_name_entry);
490 /* insert entry into external chain */
492 ne->hashlink = le->namelink;
495 LOCK_MONITOR_EXIT(hashtable_library->header);
497 #endif /* !defined(WITH_STATIC_CLASSPATH) */
500 /* native_hashtable_library_find ***********************************************
502 Find an entry in the native library hashtable.
504 *******************************************************************************/
506 #if !defined(WITH_STATIC_CLASSPATH)
507 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
508 java_objectheader *loader)
510 hashtable_library_loader_entry *le;
511 hashtable_library_name_entry *ne; /* library name */
512 u4 key; /* hashkey */
513 u4 slot; /* slot in hashtable */
515 /* normally addresses are aligned to 4, 8 or 16 bytes */
517 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
518 slot = key & (hashtable_library->size - 1);
519 le = hashtable_library->ptr[slot];
521 /* search external hash chain for the entry */
524 if (le->loader == loader)
527 le = le->hashlink; /* next element in external chain */
530 /* no loader found? return NULL */
535 /* search for library name */
540 if (ne->name == filename)
543 ne = ne->hashlink; /* next element in external chain */
546 /* return entry, if no entry was found, ne is NULL */
550 #endif /* !defined(WITH_STATIC_CLASSPATH) */
553 /* native_findfunction *********************************************************
555 Looks up a method (must have the same class name, method name,
556 descriptor and 'static'ness) and returns a function pointer to it.
557 Returns: function pointer or NULL (if there is no such method)
559 Remark: For faster operation, the names/descriptors are converted
560 from C strings to Unicode the first time this function is called.
562 *******************************************************************************/
564 #if defined(WITH_STATIC_CLASSPATH)
565 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
568 /* entry of table for fast string comparison */
569 struct nativecompref *n;
572 isstatic = isstatic ? true : false;
574 if (!nativecompdone) {
575 for (i = 0; i < NATIVETABLESIZE; i++) {
576 nativecomptable[i].classname =
577 utf_new_char(nativetable[i].classname);
579 nativecomptable[i].methodname =
580 utf_new_char(nativetable[i].methodname);
582 nativecomptable[i].descriptor =
583 utf_new_char(nativetable[i].descriptor);
585 nativecomptable[i].isstatic = nativetable[i].isstatic;
586 nativecomptable[i].func = nativetable[i].func;
589 nativecompdone = true;
592 for (i = 0; i < NATIVETABLESIZE; i++) {
593 n = &(nativecomptable[i]);
595 if (cname == n->classname && mname == n->methodname &&
596 desc == n->descriptor && isstatic == n->isstatic)
600 /* no function was found, throw exception */
603 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
608 #endif /* defined(WITH_STATIC_CLASSPATH) */
611 /* native_make_overloaded_function *********************************************
615 *******************************************************************************/
617 #if !defined(WITH_STATIC_CLASSPATH)
618 static char *native_make_overloaded_function(char *name, utf *desc)
626 utf_ptr = desc->text;
627 namelen = strlen(name) + strlen("__") + strlen("0");
629 /* calculate additional length */
631 while ((c = utf_nextu2(&utf_ptr)) != ')') {
648 while (utf_nextu2(&utf_ptr) != ';')
660 /* reallocate memory */
664 newname = DMNEW(char, namelen);
665 MCOPY(newname, name, char, i);
667 utf_ptr = desc->text;
672 while ((c = utf_nextu2(&utf_ptr)) != ')') {
690 while ((c = utf_nextu2(&utf_ptr)) != ';')
691 if (((c >= 'a') && (c <= 'z')) ||
692 ((c >= 'A') && (c <= 'Z')) ||
693 ((c >= '0') && (c <= '9')))
715 /* native_insert_char **********************************************************
717 Inserts the passed UTF character into the native method name. If
718 necessary it is escaped properly.
720 *******************************************************************************/
722 static s4 native_insert_char(char *name, u4 pos, u2 c)
730 /* replace '/' or '.' with '_' */
735 /* escape sequence for '_' is '_1' */
741 /* escape sequence for ';' is '_2' */
747 /* escape sequence for '[' is '_1' */
756 /* unicode character */
760 for (i = 0; i < 4; ++i) {
762 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
771 /* return the new buffer index */
777 /* native_resolve_function *****************************************************
779 Resolves a native function, maybe from a dynamic library.
781 *******************************************************************************/
783 functionptr native_resolve_function(methodinfo *m)
794 hashtable_library_loader_entry *le;
795 hashtable_library_name_entry *ne;
796 u4 key; /* hashkey */
797 u4 slot; /* slot in hashtable */
801 if (opt_verbosejni) {
802 printf("[Dynamic-linking native method ");
803 utf_display_printable_ascii_classname(m->class->name);
805 utf_display_printable_ascii(m->name);
809 /* Calculate length of native function name. We multiply the
810 class and method name length by 6 as this is the maxium
811 escape-sequence that can be generated (unicode). */
813 namelen = strlen("Java_") +
814 utf_get_number_of_u2s(m->class->name) * 6 +
816 utf_get_number_of_u2s(m->name) * 6 +
819 /* allocate memory */
821 dumpsize = dump_size();
823 name = DMNEW(char, namelen);
825 /* generate name of native functions */
827 strcpy(name, "Java_");
828 pos = strlen("Java_");
830 utf_ptr = m->class->name->text;
831 utf_endptr = UTF_END(m->class->name);
833 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
835 pos = native_insert_char(name, pos, c);
838 /* seperator between class and method */
842 utf_ptr = m->name->text;
843 utf_endptr = UTF_END(m->name);
845 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
847 pos = native_insert_char(name, pos, c);
854 /* check for an buffer overflow */
856 assert(pos <= namelen);
858 /* generate overloaded function (having the types in it's name) */
860 newname = native_make_overloaded_function(name, m->descriptor);
862 /* check the library hash entries of the classloader of the
867 /* normally addresses are aligned to 4, 8 or 16 bytes */
869 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
870 slot = key & (hashtable_library->size - 1);
871 le = hashtable_library->ptr[slot];
873 /* iterate through loaders in this hash slot */
875 while ((le != NULL) && (sym == NULL)) {
876 /* iterate through names in this loader */
880 while ((ne != NULL) && (sym == NULL)) {
881 sym = lt_dlsym(ne->handle, name);
884 sym = lt_dlsym(ne->handle, newname);
897 /* If not found, try to find the native function symbol in the
901 sym = lt_dlsym(mainhandle, name);
904 sym = lt_dlsym(mainhandle, newname);
908 printf("internal ]\n");
912 #if defined(ENABLE_JVMTI)
913 /* fire Native Method Bind event */
914 if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
917 /* no symbol found? throw exception */
921 printf("failed ]\n");
923 exceptions_throw_unsatisfiedlinkerror(m->name);
928 dump_release(dumpsize);
930 return (functionptr) (ptrint) sym;
932 #endif /* !defined(WITH_STATIC_CLASSPATH) */
935 /* native_new_and_init *********************************************************
937 Creates a new object on the heap and calls the initializer.
938 Returns the object pointer or NULL if memory is exhausted.
940 *******************************************************************************/
942 java_objectheader *native_new_and_init(classinfo *c)
945 java_objectheader *o;
948 vm_abort("native_new_and_init: c == NULL");
957 /* try to find the initializer */
959 m = class_findmethod(c, utf_init, utf_void__void);
961 /* ATTENTION: returning the object here is ok, since the class may
962 not have an initializer */
967 /* call initializer */
969 (void) vm_call_method(m, o);
975 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
978 java_objectheader *o;
981 vm_abort("native_new_and_init_string: c == NULL");
990 /* find initializer */
992 m = class_resolveclassmethod(c,
994 utf_java_lang_String__void,
998 /* initializer not found */
1003 /* call initializer */
1005 (void) vm_call_method(m, o, s);
1011 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1014 java_objectheader *o;
1017 vm_abort("native_new_and_init_int: c == NULL");
1026 /* find initializer */
1028 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
1030 /* initializer not found */
1035 /* call initializer */
1037 (void) vm_call_method(m, o, i);
1043 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
1045 java_objectheader *o;
1049 vm_abort("native_new_and_init_throwable: c == NULL");
1058 /* find initializer */
1060 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
1062 /* initializer not found */
1067 /* call initializer */
1069 (void) vm_call_method(m, o, t);
1075 /* native_class_getdeclaredannotations *****************************************
1078 java.lang.Class.getDeclaredAnnotations(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;
1080 *******************************************************************************/
1082 #if defined(ENABLE_JAVASE)
1083 java_objectarray *native_class_getdeclaredannotations(classinfo *c)
1085 java_objectarray *oa;
1089 classinfo *class_java_lang_annotation_Annotation;
1091 /* create Annotation-array */
1093 /* XXX should we cache that class? */
1094 if (!(class_java_lang_annotation_Annotation =
1095 load_class_bootstrap(utf_new_char("java/lang/annotation/Annotation"))))
1098 count = c->runtimevisibleannotationscount;
1100 oa = builtin_anewarray(count, class_java_lang_annotation_Annotation);
1105 /* fill the annotations */
1107 for (i = 0; i < count; i++) {
1115 /* native_get_parametertypes ***************************************************
1117 Use the descriptor of a method to generate a java/lang/Class array
1118 which contains the classes of the parametertypes of the method.
1120 *******************************************************************************/
1122 java_objectarray *native_get_parametertypes(methodinfo *m)
1125 typedesc *paramtypes;
1127 java_objectarray *oa;
1132 /* is the descriptor fully parsed? */
1134 if (!m->parseddesc->params)
1135 if (!descriptor_params_from_paramtypes(md, m->flags))
1138 paramtypes = md->paramtypes;
1139 paramcount = md->paramcount;
1141 /* skip `this' pointer */
1143 if (!(m->flags & ACC_STATIC)) {
1148 /* create class-array */
1150 oa = builtin_anewarray(paramcount, class_java_lang_Class);
1157 for (i = 0; i < paramcount; i++)
1158 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
1159 (classinfo **) &oa->data[i]))
1166 /* native_get_exceptiontypes ***************************************************
1168 Get the exceptions which can be thrown by a method.
1170 *******************************************************************************/
1172 java_objectarray *native_get_exceptiontypes(methodinfo *m)
1174 java_objectarray *oa;
1178 /* create class-array */
1180 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
1185 for (i = 0; i < m->thrownexceptionscount; i++) {
1186 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
1187 resolveEager, true, false, &c))
1190 oa->data[i] = (java_objectheader *) c;
1197 /* native_get_returntype *******************************************************
1199 Get the returntype class of a method.
1201 *******************************************************************************/
1203 classinfo *native_get_returntype(methodinfo *m)
1207 if (!resolve_class_from_typedesc(&(m->parseddesc->returntype), true, false,
1216 * These are local overrides for various environment variables in Emacs.
1217 * Please do not remove this and leave it at the end of the file, where
1218 * Emacs will automagically detect them.
1219 * ---------------------------------------------------------------------
1222 * indent-tabs-mode: t