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 7667 2007-04-05 00:16:05Z michi $
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 "vm/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"
106 #include "native/include/sun_misc_Unsafe.h"
108 #if defined(ENABLE_JVMTI)
109 #include "native/include/gnu_classpath_jdwp_event_EventRequest.h"
110 #include "native/include/java_nio_ByteBuffer.h"
111 #include "native/include/gnu_classpath_jdwp_VMVirtualMachine.h"
112 #include "native/include/gnu_classpath_jdwp_VMFrame.h"
113 #include "native/include/gnu_classpath_jdwp_VMMethod.h"
116 #elif defined(ENABLE_JAVAME_CLDC1_1)
118 #include "native/include/com_sun_cldchi_io_ConsoleOutputStream.h"
119 #include "native/include/java_lang_Class.h"
120 #include "native/include/java_lang_Double.h"
121 #include "native/include/java_lang_Float.h"
122 #include "native/include/java_lang_Math.h"
123 #include "native/include/java_lang_Runtime.h"
124 #include "native/include/java_lang_System.h"
125 #include "native/include/java_lang_Thread.h"
129 #if defined(WITH_STATIC_CLASSPATH)
131 /* these are required to prevent compiler warnings */
133 #include "native/include/java_net_DatagramPacket.h"
134 #include "native/include/java_net_InetAddress.h"
135 #include "native/include/java_net_SocketImpl.h"
137 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
138 #include "native/include/gnu_java_net_PlainSocketImpl.h"
139 #include "native/include/gnu_java_nio_PipeImpl.h"
140 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
141 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
142 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
143 #include "native/include/java_lang_VMProcess.h"
144 #include "native/include/java_nio_MappedByteBufferImpl.h"
145 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
147 /* now include the native table */
149 #include "native/nativetable.inc"
151 #elif !defined(ENABLE_LIBJVM)
153 /* dummynativetable ************************************************************
155 Ensure that symbols for functions implemented within CACAO are used
156 and exported to dlopen.
158 ATTENTION: Don't make this table static!!! Otherwise the compiler
159 may optimize it away!
161 *******************************************************************************/
163 functionptr dummynativetable[] = {
164 #if defined(ENABLE_JAVASE)
165 (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
166 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClass,
167 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClassLoader,
168 (functionptr) Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader,
170 (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
172 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getLoadedClassCount,
173 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getUnloadedClassCount,
174 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_isVerbose,
175 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_setVerbose,
177 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getHeapMemoryUsage,
178 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getNonHeapMemoryUsage,
179 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getObjectPendingFinalizationCount,
180 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_isVerbose,
181 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_setVerbose,
183 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getInputArguments,
184 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getStartTime,
186 (functionptr) Java_java_lang_VMClass_isInstance,
187 (functionptr) Java_java_lang_VMClass_isAssignableFrom,
188 (functionptr) Java_java_lang_VMClass_isInterface,
189 (functionptr) Java_java_lang_VMClass_isPrimitive,
190 (functionptr) Java_java_lang_VMClass_getName,
191 (functionptr) Java_java_lang_VMClass_getSuperclass,
192 (functionptr) Java_java_lang_VMClass_getInterfaces,
193 (functionptr) Java_java_lang_VMClass_getComponentType,
194 (functionptr) Java_java_lang_VMClass_getModifiers,
195 (functionptr) Java_java_lang_VMClass_getDeclaringClass,
196 (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
197 (functionptr) Java_java_lang_VMClass_getDeclaredFields,
198 (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
199 (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
200 (functionptr) Java_java_lang_VMClass_getClassLoader,
201 (functionptr) Java_java_lang_VMClass_forName,
202 (functionptr) Java_java_lang_VMClass_isArray,
203 (functionptr) Java_java_lang_VMClass_throwException,
205 (functionptr) Java_java_lang_VMClassLoader_defineClass,
206 (functionptr) Java_java_lang_VMClassLoader_resolveClass,
207 (functionptr) Java_java_lang_VMClassLoader_loadClass,
208 (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
209 (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
210 (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
212 (functionptr) Java_java_lang_VMObject_getClass,
213 (functionptr) Java_java_lang_VMObject_clone,
214 (functionptr) Java_java_lang_VMObject_notify,
215 (functionptr) Java_java_lang_VMObject_notifyAll,
216 (functionptr) Java_java_lang_VMObject_wait,
218 (functionptr) Java_java_lang_VMRuntime_availableProcessors,
219 (functionptr) Java_java_lang_VMRuntime_freeMemory,
220 (functionptr) Java_java_lang_VMRuntime_totalMemory,
221 (functionptr) Java_java_lang_VMRuntime_maxMemory,
222 (functionptr) Java_java_lang_VMRuntime_gc,
223 (functionptr) Java_java_lang_VMRuntime_runFinalization,
224 (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
225 (functionptr) Java_java_lang_VMRuntime_traceInstructions,
226 (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
227 (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
228 (functionptr) Java_java_lang_VMRuntime_exit,
229 (functionptr) Java_java_lang_VMRuntime_nativeLoad,
230 (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
232 (functionptr) Java_java_lang_VMString_intern,
234 (functionptr) Java_java_lang_VMSystem_arraycopy,
235 (functionptr) Java_java_lang_VMSystem_identityHashCode,
237 (functionptr) Java_java_lang_VMThread_start,
238 (functionptr) Java_java_lang_VMThread_interrupt,
239 (functionptr) Java_java_lang_VMThread_isInterrupted,
240 (functionptr) Java_java_lang_VMThread_suspend,
241 (functionptr) Java_java_lang_VMThread_resume,
242 (functionptr) Java_java_lang_VMThread_nativeSetPriority,
243 (functionptr) Java_java_lang_VMThread_nativeStop,
244 (functionptr) Java_java_lang_VMThread_currentThread,
245 (functionptr) Java_java_lang_VMThread_yield,
246 (functionptr) Java_java_lang_VMThread_interrupted,
247 (functionptr) Java_java_lang_VMThread_holdsLock,
249 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
250 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
252 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryPoolNames,
253 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryManagerNames,
254 (functionptr) Java_java_lang_management_VMManagementFactory_getGarbageCollectorNames,
256 (functionptr) Java_java_lang_reflect_Constructor_getModifiersInternal,
257 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
259 (functionptr) Java_java_lang_reflect_Field_getModifiersInternal,
260 (functionptr) Java_java_lang_reflect_Field_getType,
261 (functionptr) Java_java_lang_reflect_Field_get,
262 (functionptr) Java_java_lang_reflect_Field_getBoolean,
263 (functionptr) Java_java_lang_reflect_Field_getByte,
264 (functionptr) Java_java_lang_reflect_Field_getChar,
265 (functionptr) Java_java_lang_reflect_Field_getShort,
266 (functionptr) Java_java_lang_reflect_Field_getInt,
267 (functionptr) Java_java_lang_reflect_Field_getLong,
268 (functionptr) Java_java_lang_reflect_Field_getFloat,
269 (functionptr) Java_java_lang_reflect_Field_getDouble,
270 (functionptr) Java_java_lang_reflect_Field_set,
271 (functionptr) Java_java_lang_reflect_Field_setBoolean,
272 (functionptr) Java_java_lang_reflect_Field_setByte,
273 (functionptr) Java_java_lang_reflect_Field_setChar,
274 (functionptr) Java_java_lang_reflect_Field_setShort,
275 (functionptr) Java_java_lang_reflect_Field_setInt,
276 (functionptr) Java_java_lang_reflect_Field_setLong,
277 (functionptr) Java_java_lang_reflect_Field_setFloat,
278 (functionptr) Java_java_lang_reflect_Field_setDouble,
280 (functionptr) Java_java_lang_reflect_Method_getModifiersInternal,
281 (functionptr) Java_java_lang_reflect_Method_getReturnType,
282 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
283 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
284 (functionptr) Java_java_lang_reflect_Method_invokeNative,
286 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
287 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
288 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
290 (functionptr) Java_java_security_VMAccessController_getStack,
292 (functionptr) Java_sun_misc_Unsafe_objectFieldOffset,
293 (functionptr) Java_sun_misc_Unsafe_compareAndSwapInt,
295 #if defined(ENABLE_JVMTI)
296 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread,
297 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread,
298 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount,
299 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount,
300 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus,
301 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods,
302 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod,
303 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames,
304 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame,
305 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount,
306 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus,
307 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests,
308 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod,
309 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile,
310 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent,
311 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent,
312 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents,
313 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses,
314 (functionptr) Java_gnu_classpath_jdwp_VMFrame_setValue,
315 (functionptr) Java_gnu_classpath_jdwp_VMFrame_getValue,
316 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getName,
317 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getSignature,
318 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getModifiers,
319 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getLineTable,
320 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
323 #elif defined(ENABLE_JAVAME_CLDC1_1)
324 (functionptr) Java_com_sun_cldc_io_ResourceInputStream_open,
326 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_open0,
327 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf,
328 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte,
330 (functionptr) Java_com_sun_cldchi_io_ConsoleOutputStream_write,
332 (functionptr) Java_java_lang_Class_forName,
333 (functionptr) Java_java_lang_Class_newInstance,
334 (functionptr) Java_java_lang_Class_isInstance,
335 (functionptr) Java_java_lang_Class_isAssignableFrom,
336 (functionptr) Java_java_lang_Class_isInterface,
337 (functionptr) Java_java_lang_Class_isArray,
338 (functionptr) Java_java_lang_Class_getName,
340 (functionptr) Java_java_lang_Double_doubleToLongBits,
342 (functionptr) Java_java_lang_Float_floatToIntBits,
344 (functionptr) Java_java_lang_Math_ceil,
345 (functionptr) Java_java_lang_Math_cos,
346 (functionptr) Java_java_lang_Math_floor,
347 (functionptr) Java_java_lang_Math_sin,
348 (functionptr) Java_java_lang_Math_sqrt,
349 (functionptr) Java_java_lang_Math_tan,
351 (functionptr) Java_java_lang_Object_hashCode,
352 (functionptr) Java_java_lang_Object_notify,
353 (functionptr) Java_java_lang_Object_wait,
355 (functionptr) Java_java_lang_Runtime_exitInternal,
356 (functionptr) Java_java_lang_Runtime_freeMemory,
357 (functionptr) Java_java_lang_Runtime_totalMemory,
358 (functionptr) Java_java_lang_Runtime_gc,
360 (functionptr) Java_java_lang_String_hashCode,
361 (functionptr) Java_java_lang_String_indexOf__I,
362 (functionptr) Java_java_lang_String_indexOf__II,
363 (functionptr) Java_java_lang_String_lastIndexOf__II,
364 (functionptr) Java_java_lang_String_intern,
366 (functionptr) Java_java_lang_System_getProperty0,
368 (functionptr) Java_java_lang_Thread_currentThread,
369 (functionptr) Java_java_lang_Thread_setPriority0,
370 (functionptr) Java_java_lang_Thread_start0,
371 (functionptr) Java_java_lang_Thread_yield,
373 (functionptr) Java_java_lang_Throwable_printStackTrace,
374 (functionptr) Java_java_lang_Throwable_fillInStackTrace
378 #endif /* defined(ENABLE_LIBJVM) */
381 /* tables for methods *********************************************************/
383 #if defined(WITH_STATIC_CLASSPATH)
384 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
386 /* table for fast string comparison */
387 static nativecompref nativecomptable[NATIVETABLESIZE];
389 /* string comparsion table initialized */
390 static bool nativecompdone = false;
394 /* global variables ***********************************************************/
396 #if !defined(WITH_STATIC_CLASSPATH)
397 static hashtable *hashtable_library;
398 static lt_dlhandle mainhandle;
402 /* native_init *****************************************************************
404 Initializes the native subsystem.
406 *******************************************************************************/
408 bool native_init(void)
410 #if !defined(WITH_STATIC_CLASSPATH)
411 /* initialize libltdl */
414 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
416 /* Get the handle for the main program or for the libjvm.so,
417 depends on the configuration. */
419 # if defined(ENABLE_LIBJVM)
420 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
421 If not found, try the absolute path. */
423 if (!(mainhandle = lt_dlopenext("libjvm")))
424 if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
425 vm_abort("native_init: lt_dlopenext failed: %s\n", lt_dlerror());
427 if (!(mainhandle = lt_dlopen(NULL)))
428 vm_abort("native_init: lt_dlopen failed: %s\n", lt_dlerror());
431 /* initialize library hashtable, 10 entries should be enough */
433 hashtable_library = NEW(hashtable);
435 hashtable_create(hashtable_library, 10);
438 /* everything's ok */
444 /* native_hashtable_library_add ************************************************
446 Adds an entry to the native library hashtable.
448 *******************************************************************************/
450 #if !defined(WITH_STATIC_CLASSPATH)
451 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
454 hashtable_classloader_entry *cle;
455 hashtable_library_loader_entry *le;
456 hashtable_library_name_entry *ne; /* library name */
457 u4 key; /* hashkey */
458 u4 slot; /* slot in hashtable */
460 LOCK_MONITOR_ENTER(hashtable_library->header);
462 /* insert loader into the classloader hashtable */
464 cle = loader_hashtable_classloader_add(loader);
466 /* normally addresses are aligned to 4, 8 or 16 bytes */
468 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
469 slot = key & (hashtable_library->size - 1);
470 le = hashtable_library->ptr[slot];
472 /* search external hash chain for the entry */
478 le = le->hashlink; /* next element in external chain */
481 /* no loader found? create a new entry */
484 le = NEW(hashtable_library_loader_entry);
489 /* insert entry into hashtable */
492 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
493 hashtable_library->ptr[slot] = le;
495 /* update number of hashtable-entries */
497 hashtable_library->entries++;
501 /* search for library name */
506 if (ne->name == filename) {
507 LOCK_MONITOR_EXIT(hashtable_library->header);
512 ne = ne->hashlink; /* next element in external chain */
515 /* not found? add the library name to the classloader */
517 ne = NEW(hashtable_library_name_entry);
522 /* insert entry into external chain */
524 ne->hashlink = le->namelink;
527 LOCK_MONITOR_EXIT(hashtable_library->header);
529 #endif /* !defined(WITH_STATIC_CLASSPATH) */
532 /* native_hashtable_library_find ***********************************************
534 Find an entry in the native library hashtable.
536 *******************************************************************************/
538 #if !defined(WITH_STATIC_CLASSPATH)
539 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
540 java_objectheader *loader)
542 hashtable_classloader_entry *cle;
543 hashtable_library_loader_entry *le;
544 hashtable_library_name_entry *ne; /* library name */
545 u4 key; /* hashkey */
546 u4 slot; /* slot in hashtable */
548 /* search loader in the classloader hashtable */
550 cle = loader_hashtable_classloader_find(loader);
555 /* normally addresses are aligned to 4, 8 or 16 bytes */
557 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
558 slot = key & (hashtable_library->size - 1);
559 le = hashtable_library->ptr[slot];
561 /* search external hash chain for the entry */
567 le = le->hashlink; /* next element in external chain */
570 /* no loader found? return NULL */
575 /* search for library name */
580 if (ne->name == filename)
583 ne = ne->hashlink; /* next element in external chain */
586 /* return entry, if no entry was found, ne is NULL */
590 #endif /* !defined(WITH_STATIC_CLASSPATH) */
593 /* native_findfunction *********************************************************
595 Looks up a method (must have the same class name, method name,
596 descriptor and 'static'ness) and returns a function pointer to it.
597 Returns: function pointer or NULL (if there is no such method)
599 Remark: For faster operation, the names/descriptors are converted
600 from C strings to Unicode the first time this function is called.
602 *******************************************************************************/
604 #if defined(WITH_STATIC_CLASSPATH)
605 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
608 /* entry of table for fast string comparison */
609 struct nativecompref *n;
612 isstatic = isstatic ? true : false;
614 if (!nativecompdone) {
615 for (i = 0; i < NATIVETABLESIZE; i++) {
616 nativecomptable[i].classname =
617 utf_new_char(nativetable[i].classname);
619 nativecomptable[i].methodname =
620 utf_new_char(nativetable[i].methodname);
622 nativecomptable[i].descriptor =
623 utf_new_char(nativetable[i].descriptor);
625 nativecomptable[i].isstatic = nativetable[i].isstatic;
626 nativecomptable[i].func = nativetable[i].func;
629 nativecompdone = true;
632 for (i = 0; i < NATIVETABLESIZE; i++) {
633 n = &(nativecomptable[i]);
635 if (cname == n->classname && mname == n->methodname &&
636 desc == n->descriptor && isstatic == n->isstatic)
640 /* no function was found, throw exception */
643 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
648 #endif /* defined(WITH_STATIC_CLASSPATH) */
651 /* native_make_overloaded_function *********************************************
655 *******************************************************************************/
657 #if !defined(WITH_STATIC_CLASSPATH)
658 static char *native_make_overloaded_function(char *name, utf *desc)
666 utf_ptr = desc->text;
667 namelen = strlen(name) + strlen("__") + strlen("0");
669 /* calculate additional length */
671 while ((c = utf_nextu2(&utf_ptr)) != ')') {
688 while (utf_nextu2(&utf_ptr) != ';')
700 /* reallocate memory */
704 newname = DMNEW(char, namelen);
705 MCOPY(newname, name, char, i);
707 utf_ptr = desc->text;
712 while ((c = utf_nextu2(&utf_ptr)) != ')') {
730 while ((c = utf_nextu2(&utf_ptr)) != ';')
731 if (((c >= 'a') && (c <= 'z')) ||
732 ((c >= 'A') && (c <= 'Z')) ||
733 ((c >= '0') && (c <= '9')))
755 /* native_insert_char **********************************************************
757 Inserts the passed UTF character into the native method name. If
758 necessary it is escaped properly.
760 *******************************************************************************/
762 static s4 native_insert_char(char *name, u4 pos, u2 c)
770 /* replace '/' or '.' with '_' */
775 /* escape sequence for '_' is '_1' */
781 /* escape sequence for ';' is '_2' */
787 /* escape sequence for '[' is '_1' */
796 /* unicode character */
800 for (i = 0; i < 4; ++i) {
802 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
811 /* return the new buffer index */
817 /* native_resolve_function *****************************************************
819 Resolves a native function, maybe from a dynamic library.
821 *******************************************************************************/
823 functionptr native_resolve_function(methodinfo *m)
834 hashtable_library_loader_entry *le;
835 hashtable_library_name_entry *ne;
836 u4 key; /* hashkey */
837 u4 slot; /* slot in hashtable */
841 if (opt_verbosejni) {
842 printf("[Dynamic-linking native method ");
843 utf_display_printable_ascii_classname(m->class->name);
845 utf_display_printable_ascii(m->name);
849 /* Calculate length of native function name. We multiply the
850 class and method name length by 6 as this is the maxium
851 escape-sequence that can be generated (unicode). */
853 namelen = strlen("Java_") +
854 utf_get_number_of_u2s(m->class->name) * 6 +
856 utf_get_number_of_u2s(m->name) * 6 +
859 /* allocate memory */
861 dumpsize = dump_size();
863 name = DMNEW(char, namelen);
865 /* generate name of native functions */
867 strcpy(name, "Java_");
868 pos = strlen("Java_");
870 utf_ptr = m->class->name->text;
871 utf_endptr = UTF_END(m->class->name);
873 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
875 pos = native_insert_char(name, pos, c);
878 /* seperator between class and method */
882 utf_ptr = m->name->text;
883 utf_endptr = UTF_END(m->name);
885 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
887 pos = native_insert_char(name, pos, c);
894 /* check for an buffer overflow */
896 assert(pos <= namelen);
898 /* generate overloaded function (having the types in it's name) */
900 newname = native_make_overloaded_function(name, m->descriptor);
902 /* check the library hash entries of the classloader of the
907 /* normally addresses are aligned to 4, 8 or 16 bytes */
909 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
910 slot = key & (hashtable_library->size - 1);
911 le = hashtable_library->ptr[slot];
913 /* iterate through loaders in this hash slot */
915 while ((le != NULL) && (sym == NULL)) {
916 /* iterate through names in this loader */
920 while ((ne != NULL) && (sym == NULL)) {
921 sym = lt_dlsym(ne->handle, name);
924 sym = lt_dlsym(ne->handle, newname);
937 /* If not found, try to find the native function symbol in the
941 sym = lt_dlsym(mainhandle, name);
944 sym = lt_dlsym(mainhandle, newname);
948 printf("internal ]\n");
952 #if defined(ENABLE_JVMTI)
953 /* fire Native Method Bind event */
954 if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
957 /* no symbol found? throw exception */
961 printf("failed ]\n");
963 exceptions_throw_unsatisfiedlinkerror(m->name);
968 dump_release(dumpsize);
970 return (functionptr) (ptrint) sym;
972 #endif /* !defined(WITH_STATIC_CLASSPATH) */
975 /* native_new_and_init *********************************************************
977 Creates a new object on the heap and calls the initializer.
978 Returns the object pointer or NULL if memory is exhausted.
980 *******************************************************************************/
982 java_objectheader *native_new_and_init(classinfo *c)
985 java_objectheader *o;
988 vm_abort("native_new_and_init: c == NULL");
997 /* try to find the initializer */
999 m = class_findmethod(c, utf_init, utf_void__void);
1001 /* ATTENTION: returning the object here is ok, since the class may
1002 not have an initializer */
1007 /* call initializer */
1009 (void) vm_call_method(m, o);
1015 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
1018 java_objectheader *o;
1021 vm_abort("native_new_and_init_string: c == NULL");
1030 /* find initializer */
1032 m = class_resolveclassmethod(c,
1034 utf_java_lang_String__void,
1038 /* initializer not found */
1043 /* call initializer */
1045 (void) vm_call_method(m, o, s);
1051 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1054 java_objectheader *o;
1057 vm_abort("native_new_and_init_int: c == NULL");
1066 /* find initializer */
1068 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
1070 /* initializer not found */
1075 /* call initializer */
1077 (void) vm_call_method(m, o, i);
1083 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
1085 java_objectheader *o;
1089 vm_abort("native_new_and_init_throwable: c == NULL");
1098 /* find initializer */
1100 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
1102 /* initializer not found */
1107 /* call initializer */
1109 (void) vm_call_method(m, o, t);
1115 /* native_class_getdeclaredannotations *****************************************
1118 java.lang.Class.getDeclaredAnnotations(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;
1120 *******************************************************************************/
1122 #if defined(ENABLE_JAVASE)
1123 java_objectarray *native_class_getdeclaredannotations(classinfo *c)
1125 java_objectarray *oa;
1129 classinfo *class_java_lang_annotation_Annotation;
1131 /* create Annotation-array */
1133 /* XXX should we cache that class? */
1134 if (!(class_java_lang_annotation_Annotation =
1135 load_class_bootstrap(utf_new_char("java/lang/annotation/Annotation"))))
1138 count = c->runtimevisibleannotationscount;
1140 oa = builtin_anewarray(count, class_java_lang_annotation_Annotation);
1145 /* fill the annotations */
1147 for (i = 0; i < count; i++) {
1156 * These are local overrides for various environment variables in Emacs.
1157 * Please do not remove this and leave it at the end of the file, where
1158 * Emacs will automagically detect them.
1159 * ---------------------------------------------------------------------
1162 * indent-tabs-mode: t