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 7674 2007-04-05 13:27:11Z 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,
248 (functionptr) Java_java_lang_VMThread_getState,
250 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
251 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
253 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryPoolNames,
254 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryManagerNames,
255 (functionptr) Java_java_lang_management_VMManagementFactory_getGarbageCollectorNames,
257 (functionptr) Java_java_lang_reflect_Constructor_getModifiersInternal,
258 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
260 (functionptr) Java_java_lang_reflect_Field_getModifiersInternal,
261 (functionptr) Java_java_lang_reflect_Field_getType,
262 (functionptr) Java_java_lang_reflect_Field_get,
263 (functionptr) Java_java_lang_reflect_Field_getBoolean,
264 (functionptr) Java_java_lang_reflect_Field_getByte,
265 (functionptr) Java_java_lang_reflect_Field_getChar,
266 (functionptr) Java_java_lang_reflect_Field_getShort,
267 (functionptr) Java_java_lang_reflect_Field_getInt,
268 (functionptr) Java_java_lang_reflect_Field_getLong,
269 (functionptr) Java_java_lang_reflect_Field_getFloat,
270 (functionptr) Java_java_lang_reflect_Field_getDouble,
271 (functionptr) Java_java_lang_reflect_Field_set,
272 (functionptr) Java_java_lang_reflect_Field_setBoolean,
273 (functionptr) Java_java_lang_reflect_Field_setByte,
274 (functionptr) Java_java_lang_reflect_Field_setChar,
275 (functionptr) Java_java_lang_reflect_Field_setShort,
276 (functionptr) Java_java_lang_reflect_Field_setInt,
277 (functionptr) Java_java_lang_reflect_Field_setLong,
278 (functionptr) Java_java_lang_reflect_Field_setFloat,
279 (functionptr) Java_java_lang_reflect_Field_setDouble,
281 (functionptr) Java_java_lang_reflect_Method_getModifiersInternal,
282 (functionptr) Java_java_lang_reflect_Method_getReturnType,
283 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
284 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
285 (functionptr) Java_java_lang_reflect_Method_invokeNative,
287 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
288 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
289 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
291 (functionptr) Java_java_security_VMAccessController_getStack,
293 (functionptr) Java_sun_misc_Unsafe_objectFieldOffset,
294 (functionptr) Java_sun_misc_Unsafe_compareAndSwapInt,
296 #if defined(ENABLE_JVMTI)
297 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread,
298 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread,
299 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount,
300 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount,
301 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus,
302 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods,
303 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod,
304 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames,
305 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame,
306 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount,
307 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus,
308 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests,
309 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod,
310 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile,
311 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent,
312 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent,
313 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents,
314 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses,
315 (functionptr) Java_gnu_classpath_jdwp_VMFrame_setValue,
316 (functionptr) Java_gnu_classpath_jdwp_VMFrame_getValue,
317 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getName,
318 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getSignature,
319 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getModifiers,
320 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getLineTable,
321 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
324 #elif defined(ENABLE_JAVAME_CLDC1_1)
325 (functionptr) Java_com_sun_cldc_io_ResourceInputStream_open,
327 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_open0,
328 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf,
329 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte,
331 (functionptr) Java_com_sun_cldchi_io_ConsoleOutputStream_write,
333 (functionptr) Java_java_lang_Class_forName,
334 (functionptr) Java_java_lang_Class_newInstance,
335 (functionptr) Java_java_lang_Class_isInstance,
336 (functionptr) Java_java_lang_Class_isAssignableFrom,
337 (functionptr) Java_java_lang_Class_isInterface,
338 (functionptr) Java_java_lang_Class_isArray,
339 (functionptr) Java_java_lang_Class_getName,
341 (functionptr) Java_java_lang_Double_doubleToLongBits,
343 (functionptr) Java_java_lang_Float_floatToIntBits,
345 (functionptr) Java_java_lang_Math_ceil,
346 (functionptr) Java_java_lang_Math_cos,
347 (functionptr) Java_java_lang_Math_floor,
348 (functionptr) Java_java_lang_Math_sin,
349 (functionptr) Java_java_lang_Math_sqrt,
350 (functionptr) Java_java_lang_Math_tan,
352 (functionptr) Java_java_lang_Object_hashCode,
353 (functionptr) Java_java_lang_Object_notify,
354 (functionptr) Java_java_lang_Object_wait,
356 (functionptr) Java_java_lang_Runtime_exitInternal,
357 (functionptr) Java_java_lang_Runtime_freeMemory,
358 (functionptr) Java_java_lang_Runtime_totalMemory,
359 (functionptr) Java_java_lang_Runtime_gc,
361 (functionptr) Java_java_lang_String_hashCode,
362 (functionptr) Java_java_lang_String_indexOf__I,
363 (functionptr) Java_java_lang_String_indexOf__II,
364 (functionptr) Java_java_lang_String_lastIndexOf__II,
365 (functionptr) Java_java_lang_String_intern,
367 (functionptr) Java_java_lang_System_getProperty0,
369 (functionptr) Java_java_lang_Thread_currentThread,
370 (functionptr) Java_java_lang_Thread_setPriority0,
371 (functionptr) Java_java_lang_Thread_start0,
372 (functionptr) Java_java_lang_Thread_yield,
374 (functionptr) Java_java_lang_Throwable_printStackTrace,
375 (functionptr) Java_java_lang_Throwable_fillInStackTrace
379 #endif /* defined(ENABLE_LIBJVM) */
382 /* tables for methods *********************************************************/
384 #if defined(WITH_STATIC_CLASSPATH)
385 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
387 /* table for fast string comparison */
388 static nativecompref nativecomptable[NATIVETABLESIZE];
390 /* string comparsion table initialized */
391 static bool nativecompdone = false;
395 /* global variables ***********************************************************/
397 #if !defined(WITH_STATIC_CLASSPATH)
398 static hashtable *hashtable_library;
399 static lt_dlhandle mainhandle;
403 /* native_init *****************************************************************
405 Initializes the native subsystem.
407 *******************************************************************************/
409 bool native_init(void)
411 #if !defined(WITH_STATIC_CLASSPATH)
412 /* initialize libltdl */
415 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
417 /* Get the handle for the main program or for the libjvm.so,
418 depends on the configuration. */
420 # if defined(ENABLE_LIBJVM)
421 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
422 If not found, try the absolute path. */
424 if (!(mainhandle = lt_dlopenext("libjvm")))
425 if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
426 vm_abort("native_init: lt_dlopenext failed: %s\n", lt_dlerror());
428 if (!(mainhandle = lt_dlopen(NULL)))
429 vm_abort("native_init: lt_dlopen failed: %s\n", lt_dlerror());
432 /* initialize library hashtable, 10 entries should be enough */
434 hashtable_library = NEW(hashtable);
436 hashtable_create(hashtable_library, 10);
439 /* everything's ok */
445 /* native_hashtable_library_add ************************************************
447 Adds an entry to the native library hashtable.
449 *******************************************************************************/
451 #if !defined(WITH_STATIC_CLASSPATH)
452 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
455 hashtable_classloader_entry *cle;
456 hashtable_library_loader_entry *le;
457 hashtable_library_name_entry *ne; /* library name */
458 u4 key; /* hashkey */
459 u4 slot; /* slot in hashtable */
461 LOCK_MONITOR_ENTER(hashtable_library->header);
463 /* insert loader into the classloader hashtable */
465 cle = loader_hashtable_classloader_add(loader);
467 /* normally addresses are aligned to 4, 8 or 16 bytes */
469 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
470 slot = key & (hashtable_library->size - 1);
471 le = hashtable_library->ptr[slot];
473 /* search external hash chain for the entry */
479 le = le->hashlink; /* next element in external chain */
482 /* no loader found? create a new entry */
485 le = NEW(hashtable_library_loader_entry);
490 /* insert entry into hashtable */
493 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
494 hashtable_library->ptr[slot] = le;
496 /* update number of hashtable-entries */
498 hashtable_library->entries++;
502 /* search for library name */
507 if (ne->name == filename) {
508 LOCK_MONITOR_EXIT(hashtable_library->header);
513 ne = ne->hashlink; /* next element in external chain */
516 /* not found? add the library name to the classloader */
518 ne = NEW(hashtable_library_name_entry);
523 /* insert entry into external chain */
525 ne->hashlink = le->namelink;
528 LOCK_MONITOR_EXIT(hashtable_library->header);
530 #endif /* !defined(WITH_STATIC_CLASSPATH) */
533 /* native_hashtable_library_find ***********************************************
535 Find an entry in the native library hashtable.
537 *******************************************************************************/
539 #if !defined(WITH_STATIC_CLASSPATH)
540 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
541 java_objectheader *loader)
543 hashtable_classloader_entry *cle;
544 hashtable_library_loader_entry *le;
545 hashtable_library_name_entry *ne; /* library name */
546 u4 key; /* hashkey */
547 u4 slot; /* slot in hashtable */
549 /* search loader in the classloader hashtable */
551 cle = loader_hashtable_classloader_find(loader);
556 /* normally addresses are aligned to 4, 8 or 16 bytes */
558 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
559 slot = key & (hashtable_library->size - 1);
560 le = hashtable_library->ptr[slot];
562 /* search external hash chain for the entry */
568 le = le->hashlink; /* next element in external chain */
571 /* no loader found? return NULL */
576 /* search for library name */
581 if (ne->name == filename)
584 ne = ne->hashlink; /* next element in external chain */
587 /* return entry, if no entry was found, ne is NULL */
591 #endif /* !defined(WITH_STATIC_CLASSPATH) */
594 /* native_findfunction *********************************************************
596 Looks up a method (must have the same class name, method name,
597 descriptor and 'static'ness) and returns a function pointer to it.
598 Returns: function pointer or NULL (if there is no such method)
600 Remark: For faster operation, the names/descriptors are converted
601 from C strings to Unicode the first time this function is called.
603 *******************************************************************************/
605 #if defined(WITH_STATIC_CLASSPATH)
606 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
609 /* entry of table for fast string comparison */
610 struct nativecompref *n;
613 isstatic = isstatic ? true : false;
615 if (!nativecompdone) {
616 for (i = 0; i < NATIVETABLESIZE; i++) {
617 nativecomptable[i].classname =
618 utf_new_char(nativetable[i].classname);
620 nativecomptable[i].methodname =
621 utf_new_char(nativetable[i].methodname);
623 nativecomptable[i].descriptor =
624 utf_new_char(nativetable[i].descriptor);
626 nativecomptable[i].isstatic = nativetable[i].isstatic;
627 nativecomptable[i].func = nativetable[i].func;
630 nativecompdone = true;
633 for (i = 0; i < NATIVETABLESIZE; i++) {
634 n = &(nativecomptable[i]);
636 if (cname == n->classname && mname == n->methodname &&
637 desc == n->descriptor && isstatic == n->isstatic)
641 /* no function was found, throw exception */
644 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
649 #endif /* defined(WITH_STATIC_CLASSPATH) */
652 /* native_make_overloaded_function *********************************************
656 *******************************************************************************/
658 #if !defined(WITH_STATIC_CLASSPATH)
659 static char *native_make_overloaded_function(char *name, utf *desc)
667 utf_ptr = desc->text;
668 namelen = strlen(name) + strlen("__") + strlen("0");
670 /* calculate additional length */
672 while ((c = utf_nextu2(&utf_ptr)) != ')') {
689 while (utf_nextu2(&utf_ptr) != ';')
701 /* reallocate memory */
705 newname = DMNEW(char, namelen);
706 MCOPY(newname, name, char, i);
708 utf_ptr = desc->text;
713 while ((c = utf_nextu2(&utf_ptr)) != ')') {
731 while ((c = utf_nextu2(&utf_ptr)) != ';')
732 if (((c >= 'a') && (c <= 'z')) ||
733 ((c >= 'A') && (c <= 'Z')) ||
734 ((c >= '0') && (c <= '9')))
756 /* native_insert_char **********************************************************
758 Inserts the passed UTF character into the native method name. If
759 necessary it is escaped properly.
761 *******************************************************************************/
763 static s4 native_insert_char(char *name, u4 pos, u2 c)
771 /* replace '/' or '.' with '_' */
776 /* escape sequence for '_' is '_1' */
782 /* escape sequence for ';' is '_2' */
788 /* escape sequence for '[' is '_1' */
797 /* unicode character */
801 for (i = 0; i < 4; ++i) {
803 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
812 /* return the new buffer index */
818 /* native_resolve_function *****************************************************
820 Resolves a native function, maybe from a dynamic library.
822 *******************************************************************************/
824 functionptr native_resolve_function(methodinfo *m)
835 hashtable_library_loader_entry *le;
836 hashtable_library_name_entry *ne;
837 u4 key; /* hashkey */
838 u4 slot; /* slot in hashtable */
842 if (opt_verbosejni) {
843 printf("[Dynamic-linking native method ");
844 utf_display_printable_ascii_classname(m->class->name);
846 utf_display_printable_ascii(m->name);
850 /* Calculate length of native function name. We multiply the
851 class and method name length by 6 as this is the maxium
852 escape-sequence that can be generated (unicode). */
854 namelen = strlen("Java_") +
855 utf_get_number_of_u2s(m->class->name) * 6 +
857 utf_get_number_of_u2s(m->name) * 6 +
860 /* allocate memory */
862 dumpsize = dump_size();
864 name = DMNEW(char, namelen);
866 /* generate name of native functions */
868 strcpy(name, "Java_");
869 pos = strlen("Java_");
871 utf_ptr = m->class->name->text;
872 utf_endptr = UTF_END(m->class->name);
874 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
876 pos = native_insert_char(name, pos, c);
879 /* seperator between class and method */
883 utf_ptr = m->name->text;
884 utf_endptr = UTF_END(m->name);
886 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
888 pos = native_insert_char(name, pos, c);
895 /* check for an buffer overflow */
897 assert(pos <= namelen);
899 /* generate overloaded function (having the types in it's name) */
901 newname = native_make_overloaded_function(name, m->descriptor);
903 /* check the library hash entries of the classloader of the
908 /* normally addresses are aligned to 4, 8 or 16 bytes */
910 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
911 slot = key & (hashtable_library->size - 1);
912 le = hashtable_library->ptr[slot];
914 /* iterate through loaders in this hash slot */
916 while ((le != NULL) && (sym == NULL)) {
917 /* iterate through names in this loader */
921 while ((ne != NULL) && (sym == NULL)) {
922 sym = lt_dlsym(ne->handle, name);
925 sym = lt_dlsym(ne->handle, newname);
938 /* If not found, try to find the native function symbol in the
942 sym = lt_dlsym(mainhandle, name);
945 sym = lt_dlsym(mainhandle, newname);
949 printf("internal ]\n");
953 #if defined(ENABLE_JVMTI)
954 /* fire Native Method Bind event */
955 if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
958 /* no symbol found? throw exception */
962 printf("failed ]\n");
964 exceptions_throw_unsatisfiedlinkerror(m->name);
969 dump_release(dumpsize);
971 return (functionptr) (ptrint) sym;
973 #endif /* !defined(WITH_STATIC_CLASSPATH) */
976 /* native_new_and_init *********************************************************
978 Creates a new object on the heap and calls the initializer.
979 Returns the object pointer or NULL if memory is exhausted.
981 *******************************************************************************/
983 java_objectheader *native_new_and_init(classinfo *c)
986 java_objectheader *o;
989 vm_abort("native_new_and_init: c == NULL");
998 /* try to find the initializer */
1000 m = class_findmethod(c, utf_init, utf_void__void);
1002 /* ATTENTION: returning the object here is ok, since the class may
1003 not have an initializer */
1008 /* call initializer */
1010 (void) vm_call_method(m, o);
1016 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
1019 java_objectheader *o;
1022 vm_abort("native_new_and_init_string: c == NULL");
1031 /* find initializer */
1033 m = class_resolveclassmethod(c,
1035 utf_java_lang_String__void,
1039 /* initializer not found */
1044 /* call initializer */
1046 (void) vm_call_method(m, o, s);
1052 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1055 java_objectheader *o;
1058 vm_abort("native_new_and_init_int: c == NULL");
1067 /* find initializer */
1069 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
1071 /* initializer not found */
1076 /* call initializer */
1078 (void) vm_call_method(m, o, i);
1084 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
1086 java_objectheader *o;
1090 vm_abort("native_new_and_init_throwable: c == NULL");
1099 /* find initializer */
1101 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
1103 /* initializer not found */
1108 /* call initializer */
1110 (void) vm_call_method(m, o, t);
1116 /* native_class_getdeclaredannotations *****************************************
1119 java.lang.Class.getDeclaredAnnotations(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;
1121 *******************************************************************************/
1123 #if defined(ENABLE_JAVASE)
1124 java_objectarray *native_class_getdeclaredannotations(classinfo *c)
1126 java_objectarray *oa;
1130 classinfo *class_java_lang_annotation_Annotation;
1132 /* create Annotation-array */
1134 /* XXX should we cache that class? */
1135 if (!(class_java_lang_annotation_Annotation =
1136 load_class_bootstrap(utf_new_char("java/lang/annotation/Annotation"))))
1139 count = c->runtimevisibleannotationscount;
1141 oa = builtin_anewarray(count, class_java_lang_annotation_Annotation);
1146 /* fill the annotations */
1148 for (i = 0; i < count; i++) {
1157 * These are local overrides for various environment variables in Emacs.
1158 * Please do not remove this and leave it at the end of the file, where
1159 * Emacs will automagically detect them.
1160 * ---------------------------------------------------------------------
1163 * indent-tabs-mode: t