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 7393 2007-02-23 20:28:35Z 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 "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_cldc_io_ResourceInputStream_open,
322 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_open0,
323 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf,
324 (functionptr) Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte,
326 (functionptr) Java_com_sun_cldchi_io_ConsoleOutputStream_write,
328 (functionptr) Java_java_lang_Class_forName,
329 (functionptr) Java_java_lang_Class_newInstance,
330 (functionptr) Java_java_lang_Class_isInstance,
331 (functionptr) Java_java_lang_Class_isAssignableFrom,
332 (functionptr) Java_java_lang_Class_isInterface,
333 (functionptr) Java_java_lang_Class_isArray,
334 (functionptr) Java_java_lang_Class_getName,
336 (functionptr) Java_java_lang_Double_doubleToLongBits,
338 (functionptr) Java_java_lang_Float_floatToIntBits,
340 (functionptr) Java_java_lang_Math_ceil,
341 (functionptr) Java_java_lang_Math_cos,
342 (functionptr) Java_java_lang_Math_floor,
343 (functionptr) Java_java_lang_Math_sin,
344 (functionptr) Java_java_lang_Math_sqrt,
345 (functionptr) Java_java_lang_Math_tan,
347 (functionptr) Java_java_lang_Object_hashCode,
348 (functionptr) Java_java_lang_Object_notify,
349 (functionptr) Java_java_lang_Object_wait,
351 (functionptr) Java_java_lang_Runtime_exitInternal,
352 (functionptr) Java_java_lang_Runtime_freeMemory,
353 (functionptr) Java_java_lang_Runtime_totalMemory,
354 (functionptr) Java_java_lang_Runtime_gc,
356 (functionptr) Java_java_lang_String_hashCode,
357 (functionptr) Java_java_lang_String_indexOf__I,
358 (functionptr) Java_java_lang_String_indexOf__II,
359 (functionptr) Java_java_lang_String_lastIndexOf__II,
360 (functionptr) Java_java_lang_String_intern,
362 (functionptr) Java_java_lang_System_getProperty0,
364 (functionptr) Java_java_lang_Thread_currentThread,
365 (functionptr) Java_java_lang_Thread_setPriority0,
366 (functionptr) Java_java_lang_Thread_start0,
367 (functionptr) Java_java_lang_Thread_yield,
369 (functionptr) Java_java_lang_Throwable_printStackTrace,
370 (functionptr) Java_java_lang_Throwable_fillInStackTrace
374 #endif /* defined(ENABLE_LIBJVM) */
377 /* tables for methods *********************************************************/
379 #if defined(WITH_STATIC_CLASSPATH)
380 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
382 /* table for fast string comparison */
383 static nativecompref nativecomptable[NATIVETABLESIZE];
385 /* string comparsion table initialized */
386 static bool nativecompdone = false;
390 /* global variables ***********************************************************/
392 #if !defined(WITH_STATIC_CLASSPATH)
393 static hashtable *hashtable_library;
394 static lt_dlhandle mainhandle;
398 /* native_init *****************************************************************
400 Initializes the native subsystem.
402 *******************************************************************************/
404 bool native_init(void)
406 #if !defined(WITH_STATIC_CLASSPATH)
407 /* initialize libltdl */
410 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
412 /* Get the handle for the main program or for the libjvm.so,
413 depends on the configuration. */
415 # if defined(ENABLE_LIBJVM)
416 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
417 If not found, try the absolute path. */
419 if (!(mainhandle = lt_dlopenext("libjvm")))
420 if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
421 vm_abort("native_init: lt_dlopenext failed: %s\n", lt_dlerror());
423 if (!(mainhandle = lt_dlopen(NULL)))
424 vm_abort("native_init: lt_dlopen failed: %s\n", lt_dlerror());
427 /* initialize library hashtable, 10 entries should be enough */
429 hashtable_library = NEW(hashtable);
431 hashtable_create(hashtable_library, 10);
434 /* everything's ok */
440 /* native_hashtable_library_add ************************************************
442 Adds an entry to the native library hashtable.
444 *******************************************************************************/
446 #if !defined(WITH_STATIC_CLASSPATH)
447 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
450 hashtable_classloader_entry *cle;
451 hashtable_library_loader_entry *le;
452 hashtable_library_name_entry *ne; /* library name */
453 u4 key; /* hashkey */
454 u4 slot; /* slot in hashtable */
456 LOCK_MONITOR_ENTER(hashtable_library->header);
458 /* insert loader into the classloader hashtable */
460 cle = loader_hashtable_classloader_add(loader);
462 /* normally addresses are aligned to 4, 8 or 16 bytes */
464 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
465 slot = key & (hashtable_library->size - 1);
466 le = hashtable_library->ptr[slot];
468 /* search external hash chain for the entry */
474 le = le->hashlink; /* next element in external chain */
477 /* no loader found? create a new entry */
480 le = NEW(hashtable_library_loader_entry);
485 /* insert entry into hashtable */
488 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
489 hashtable_library->ptr[slot] = le;
491 /* update number of hashtable-entries */
493 hashtable_library->entries++;
497 /* search for library name */
502 if (ne->name == filename) {
503 LOCK_MONITOR_EXIT(hashtable_library->header);
508 ne = ne->hashlink; /* next element in external chain */
511 /* not found? add the library name to the classloader */
513 ne = NEW(hashtable_library_name_entry);
518 /* insert entry into external chain */
520 ne->hashlink = le->namelink;
523 LOCK_MONITOR_EXIT(hashtable_library->header);
525 #endif /* !defined(WITH_STATIC_CLASSPATH) */
528 /* native_hashtable_library_find ***********************************************
530 Find an entry in the native library hashtable.
532 *******************************************************************************/
534 #if !defined(WITH_STATIC_CLASSPATH)
535 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
536 java_objectheader *loader)
538 hashtable_classloader_entry *cle;
539 hashtable_library_loader_entry *le;
540 hashtable_library_name_entry *ne; /* library name */
541 u4 key; /* hashkey */
542 u4 slot; /* slot in hashtable */
544 /* search loader in the classloader hashtable */
546 cle = loader_hashtable_classloader_find(loader);
551 /* normally addresses are aligned to 4, 8 or 16 bytes */
553 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
554 slot = key & (hashtable_library->size - 1);
555 le = hashtable_library->ptr[slot];
557 /* search external hash chain for the entry */
563 le = le->hashlink; /* next element in external chain */
566 /* no loader found? return NULL */
571 /* search for library name */
576 if (ne->name == filename)
579 ne = ne->hashlink; /* next element in external chain */
582 /* return entry, if no entry was found, ne is NULL */
586 #endif /* !defined(WITH_STATIC_CLASSPATH) */
589 /* native_findfunction *********************************************************
591 Looks up a method (must have the same class name, method name,
592 descriptor and 'static'ness) and returns a function pointer to it.
593 Returns: function pointer or NULL (if there is no such method)
595 Remark: For faster operation, the names/descriptors are converted
596 from C strings to Unicode the first time this function is called.
598 *******************************************************************************/
600 #if defined(WITH_STATIC_CLASSPATH)
601 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
604 /* entry of table for fast string comparison */
605 struct nativecompref *n;
608 isstatic = isstatic ? true : false;
610 if (!nativecompdone) {
611 for (i = 0; i < NATIVETABLESIZE; i++) {
612 nativecomptable[i].classname =
613 utf_new_char(nativetable[i].classname);
615 nativecomptable[i].methodname =
616 utf_new_char(nativetable[i].methodname);
618 nativecomptable[i].descriptor =
619 utf_new_char(nativetable[i].descriptor);
621 nativecomptable[i].isstatic = nativetable[i].isstatic;
622 nativecomptable[i].func = nativetable[i].func;
625 nativecompdone = true;
628 for (i = 0; i < NATIVETABLESIZE; i++) {
629 n = &(nativecomptable[i]);
631 if (cname == n->classname && mname == n->methodname &&
632 desc == n->descriptor && isstatic == n->isstatic)
636 /* no function was found, throw exception */
639 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
644 #endif /* defined(WITH_STATIC_CLASSPATH) */
647 /* native_make_overloaded_function *********************************************
651 *******************************************************************************/
653 #if !defined(WITH_STATIC_CLASSPATH)
654 static char *native_make_overloaded_function(char *name, utf *desc)
662 utf_ptr = desc->text;
663 namelen = strlen(name) + strlen("__") + strlen("0");
665 /* calculate additional length */
667 while ((c = utf_nextu2(&utf_ptr)) != ')') {
684 while (utf_nextu2(&utf_ptr) != ';')
696 /* reallocate memory */
700 newname = DMNEW(char, namelen);
701 MCOPY(newname, name, char, i);
703 utf_ptr = desc->text;
708 while ((c = utf_nextu2(&utf_ptr)) != ')') {
726 while ((c = utf_nextu2(&utf_ptr)) != ';')
727 if (((c >= 'a') && (c <= 'z')) ||
728 ((c >= 'A') && (c <= 'Z')) ||
729 ((c >= '0') && (c <= '9')))
751 /* native_insert_char **********************************************************
753 Inserts the passed UTF character into the native method name. If
754 necessary it is escaped properly.
756 *******************************************************************************/
758 static s4 native_insert_char(char *name, u4 pos, u2 c)
766 /* replace '/' or '.' with '_' */
771 /* escape sequence for '_' is '_1' */
777 /* escape sequence for ';' is '_2' */
783 /* escape sequence for '[' is '_1' */
792 /* unicode character */
796 for (i = 0; i < 4; ++i) {
798 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
807 /* return the new buffer index */
813 /* native_resolve_function *****************************************************
815 Resolves a native function, maybe from a dynamic library.
817 *******************************************************************************/
819 functionptr native_resolve_function(methodinfo *m)
830 hashtable_library_loader_entry *le;
831 hashtable_library_name_entry *ne;
832 u4 key; /* hashkey */
833 u4 slot; /* slot in hashtable */
837 if (opt_verbosejni) {
838 printf("[Dynamic-linking native method ");
839 utf_display_printable_ascii_classname(m->class->name);
841 utf_display_printable_ascii(m->name);
845 /* Calculate length of native function name. We multiply the
846 class and method name length by 6 as this is the maxium
847 escape-sequence that can be generated (unicode). */
849 namelen = strlen("Java_") +
850 utf_get_number_of_u2s(m->class->name) * 6 +
852 utf_get_number_of_u2s(m->name) * 6 +
855 /* allocate memory */
857 dumpsize = dump_size();
859 name = DMNEW(char, namelen);
861 /* generate name of native functions */
863 strcpy(name, "Java_");
864 pos = strlen("Java_");
866 utf_ptr = m->class->name->text;
867 utf_endptr = UTF_END(m->class->name);
869 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
871 pos = native_insert_char(name, pos, c);
874 /* seperator between class and method */
878 utf_ptr = m->name->text;
879 utf_endptr = UTF_END(m->name);
881 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
883 pos = native_insert_char(name, pos, c);
890 /* check for an buffer overflow */
892 assert(pos <= namelen);
894 /* generate overloaded function (having the types in it's name) */
896 newname = native_make_overloaded_function(name, m->descriptor);
898 /* check the library hash entries of the classloader of the
903 /* normally addresses are aligned to 4, 8 or 16 bytes */
906 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
908 /* XXX this is only a hack until classinfo->classloade is of type cle!!! */
909 key = ((u4) (ptrint) loader_hashtable_classloader_find(m->class->classloader)) >> 4;
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++) {
1156 /* native_get_parametertypes ***************************************************
1158 Use the descriptor of a method to generate a java/lang/Class array
1159 which contains the classes of the parametertypes of the method.
1161 *******************************************************************************/
1163 java_objectarray *native_get_parametertypes(methodinfo *m)
1166 typedesc *paramtypes;
1168 java_objectarray *oa;
1173 /* is the descriptor fully parsed? */
1175 if (m->parseddesc->params == NULL)
1176 if (!descriptor_params_from_paramtypes(md, m->flags))
1179 paramtypes = md->paramtypes;
1180 paramcount = md->paramcount;
1182 /* skip `this' pointer */
1184 if (!(m->flags & ACC_STATIC)) {
1189 /* create class-array */
1191 oa = builtin_anewarray(paramcount, class_java_lang_Class);
1198 for (i = 0; i < paramcount; i++)
1199 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
1200 (classinfo **) &oa->data[i]))
1207 /* native_get_exceptiontypes ***************************************************
1209 Get the exceptions which can be thrown by a method.
1211 *******************************************************************************/
1213 java_objectarray *native_get_exceptiontypes(methodinfo *m)
1215 java_objectarray *oa;
1219 /* create class-array */
1221 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
1226 for (i = 0; i < m->thrownexceptionscount; i++) {
1227 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
1228 resolveEager, true, false, &c))
1231 oa->data[i] = (java_objectheader *) c;
1239 * These are local overrides for various environment variables in Emacs.
1240 * Please do not remove this and leave it at the end of the file, where
1241 * Emacs will automagically detect them.
1242 * ---------------------------------------------------------------------
1245 * indent-tabs-mode: t