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 7356 2007-02-14 11:00:28Z 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_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_library_loader_entry *le;
451 hashtable_library_name_entry *ne; /* library name */
452 u4 key; /* hashkey */
453 u4 slot; /* slot in hashtable */
455 LOCK_MONITOR_ENTER(hashtable_library->header);
457 /* normally addresses are aligned to 4, 8 or 16 bytes */
459 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
460 slot = key & (hashtable_library->size - 1);
461 le = hashtable_library->ptr[slot];
463 /* search external hash chain for the entry */
466 if (le->loader == loader)
469 le = le->hashlink; /* next element in external chain */
472 /* no loader found? create a new entry */
475 le = NEW(hashtable_library_loader_entry);
480 /* insert entry into hashtable */
483 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
484 hashtable_library->ptr[slot] = le;
486 /* update number of hashtable-entries */
488 hashtable_library->entries++;
492 /* search for library name */
497 if (ne->name == filename) {
498 LOCK_MONITOR_EXIT(hashtable_library->header);
503 ne = ne->hashlink; /* next element in external chain */
506 /* not found? add the library name to the classloader */
508 ne = NEW(hashtable_library_name_entry);
513 /* insert entry into external chain */
515 ne->hashlink = le->namelink;
518 LOCK_MONITOR_EXIT(hashtable_library->header);
520 #endif /* !defined(WITH_STATIC_CLASSPATH) */
523 /* native_hashtable_library_find ***********************************************
525 Find an entry in the native library hashtable.
527 *******************************************************************************/
529 #if !defined(WITH_STATIC_CLASSPATH)
530 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
531 java_objectheader *loader)
533 hashtable_library_loader_entry *le;
534 hashtable_library_name_entry *ne; /* library name */
535 u4 key; /* hashkey */
536 u4 slot; /* slot in hashtable */
538 /* normally addresses are aligned to 4, 8 or 16 bytes */
540 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
541 slot = key & (hashtable_library->size - 1);
542 le = hashtable_library->ptr[slot];
544 /* search external hash chain for the entry */
547 if (le->loader == loader)
550 le = le->hashlink; /* next element in external chain */
553 /* no loader found? return NULL */
558 /* search for library name */
563 if (ne->name == filename)
566 ne = ne->hashlink; /* next element in external chain */
569 /* return entry, if no entry was found, ne is NULL */
573 #endif /* !defined(WITH_STATIC_CLASSPATH) */
576 /* native_findfunction *********************************************************
578 Looks up a method (must have the same class name, method name,
579 descriptor and 'static'ness) and returns a function pointer to it.
580 Returns: function pointer or NULL (if there is no such method)
582 Remark: For faster operation, the names/descriptors are converted
583 from C strings to Unicode the first time this function is called.
585 *******************************************************************************/
587 #if defined(WITH_STATIC_CLASSPATH)
588 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
591 /* entry of table for fast string comparison */
592 struct nativecompref *n;
595 isstatic = isstatic ? true : false;
597 if (!nativecompdone) {
598 for (i = 0; i < NATIVETABLESIZE; i++) {
599 nativecomptable[i].classname =
600 utf_new_char(nativetable[i].classname);
602 nativecomptable[i].methodname =
603 utf_new_char(nativetable[i].methodname);
605 nativecomptable[i].descriptor =
606 utf_new_char(nativetable[i].descriptor);
608 nativecomptable[i].isstatic = nativetable[i].isstatic;
609 nativecomptable[i].func = nativetable[i].func;
612 nativecompdone = true;
615 for (i = 0; i < NATIVETABLESIZE; i++) {
616 n = &(nativecomptable[i]);
618 if (cname == n->classname && mname == n->methodname &&
619 desc == n->descriptor && isstatic == n->isstatic)
623 /* no function was found, throw exception */
626 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
631 #endif /* defined(WITH_STATIC_CLASSPATH) */
634 /* native_make_overloaded_function *********************************************
638 *******************************************************************************/
640 #if !defined(WITH_STATIC_CLASSPATH)
641 static char *native_make_overloaded_function(char *name, utf *desc)
649 utf_ptr = desc->text;
650 namelen = strlen(name) + strlen("__") + strlen("0");
652 /* calculate additional length */
654 while ((c = utf_nextu2(&utf_ptr)) != ')') {
671 while (utf_nextu2(&utf_ptr) != ';')
683 /* reallocate memory */
687 newname = DMNEW(char, namelen);
688 MCOPY(newname, name, char, i);
690 utf_ptr = desc->text;
695 while ((c = utf_nextu2(&utf_ptr)) != ')') {
713 while ((c = utf_nextu2(&utf_ptr)) != ';')
714 if (((c >= 'a') && (c <= 'z')) ||
715 ((c >= 'A') && (c <= 'Z')) ||
716 ((c >= '0') && (c <= '9')))
738 /* native_insert_char **********************************************************
740 Inserts the passed UTF character into the native method name. If
741 necessary it is escaped properly.
743 *******************************************************************************/
745 static s4 native_insert_char(char *name, u4 pos, u2 c)
753 /* replace '/' or '.' with '_' */
758 /* escape sequence for '_' is '_1' */
764 /* escape sequence for ';' is '_2' */
770 /* escape sequence for '[' is '_1' */
779 /* unicode character */
783 for (i = 0; i < 4; ++i) {
785 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
794 /* return the new buffer index */
800 /* native_resolve_function *****************************************************
802 Resolves a native function, maybe from a dynamic library.
804 *******************************************************************************/
806 functionptr native_resolve_function(methodinfo *m)
817 hashtable_library_loader_entry *le;
818 hashtable_library_name_entry *ne;
819 u4 key; /* hashkey */
820 u4 slot; /* slot in hashtable */
824 if (opt_verbosejni) {
825 printf("[Dynamic-linking native method ");
826 utf_display_printable_ascii_classname(m->class->name);
828 utf_display_printable_ascii(m->name);
832 /* Calculate length of native function name. We multiply the
833 class and method name length by 6 as this is the maxium
834 escape-sequence that can be generated (unicode). */
836 namelen = strlen("Java_") +
837 utf_get_number_of_u2s(m->class->name) * 6 +
839 utf_get_number_of_u2s(m->name) * 6 +
842 /* allocate memory */
844 dumpsize = dump_size();
846 name = DMNEW(char, namelen);
848 /* generate name of native functions */
850 strcpy(name, "Java_");
851 pos = strlen("Java_");
853 utf_ptr = m->class->name->text;
854 utf_endptr = UTF_END(m->class->name);
856 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
858 pos = native_insert_char(name, pos, c);
861 /* seperator between class and method */
865 utf_ptr = m->name->text;
866 utf_endptr = UTF_END(m->name);
868 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
870 pos = native_insert_char(name, pos, c);
877 /* check for an buffer overflow */
879 assert(pos <= namelen);
881 /* generate overloaded function (having the types in it's name) */
883 newname = native_make_overloaded_function(name, m->descriptor);
885 /* check the library hash entries of the classloader of the
890 /* normally addresses are aligned to 4, 8 or 16 bytes */
892 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
893 slot = key & (hashtable_library->size - 1);
894 le = hashtable_library->ptr[slot];
896 /* iterate through loaders in this hash slot */
898 while ((le != NULL) && (sym == NULL)) {
899 /* iterate through names in this loader */
903 while ((ne != NULL) && (sym == NULL)) {
904 sym = lt_dlsym(ne->handle, name);
907 sym = lt_dlsym(ne->handle, newname);
920 /* If not found, try to find the native function symbol in the
924 sym = lt_dlsym(mainhandle, name);
927 sym = lt_dlsym(mainhandle, newname);
931 printf("internal ]\n");
935 #if defined(ENABLE_JVMTI)
936 /* fire Native Method Bind event */
937 if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
940 /* no symbol found? throw exception */
944 printf("failed ]\n");
946 exceptions_throw_unsatisfiedlinkerror(m->name);
951 dump_release(dumpsize);
953 return (functionptr) (ptrint) sym;
955 #endif /* !defined(WITH_STATIC_CLASSPATH) */
958 /* native_new_and_init *********************************************************
960 Creates a new object on the heap and calls the initializer.
961 Returns the object pointer or NULL if memory is exhausted.
963 *******************************************************************************/
965 java_objectheader *native_new_and_init(classinfo *c)
968 java_objectheader *o;
971 vm_abort("native_new_and_init: c == NULL");
980 /* try to find the initializer */
982 m = class_findmethod(c, utf_init, utf_void__void);
984 /* ATTENTION: returning the object here is ok, since the class may
985 not have an initializer */
990 /* call initializer */
992 (void) vm_call_method(m, o);
998 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
1001 java_objectheader *o;
1004 vm_abort("native_new_and_init_string: c == NULL");
1013 /* find initializer */
1015 m = class_resolveclassmethod(c,
1017 utf_java_lang_String__void,
1021 /* initializer not found */
1026 /* call initializer */
1028 (void) vm_call_method(m, o, s);
1034 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1037 java_objectheader *o;
1040 vm_abort("native_new_and_init_int: c == NULL");
1049 /* find initializer */
1051 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
1053 /* initializer not found */
1058 /* call initializer */
1060 (void) vm_call_method(m, o, i);
1066 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
1068 java_objectheader *o;
1072 vm_abort("native_new_and_init_throwable: c == NULL");
1081 /* find initializer */
1083 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
1085 /* initializer not found */
1090 /* call initializer */
1092 (void) vm_call_method(m, o, t);
1098 /* native_class_getdeclaredannotations *****************************************
1101 java.lang.Class.getDeclaredAnnotations(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;
1103 *******************************************************************************/
1105 #if defined(ENABLE_JAVASE)
1106 java_objectarray *native_class_getdeclaredannotations(classinfo *c)
1108 java_objectarray *oa;
1112 classinfo *class_java_lang_annotation_Annotation;
1114 /* create Annotation-array */
1116 /* XXX should we cache that class? */
1117 if (!(class_java_lang_annotation_Annotation =
1118 load_class_bootstrap(utf_new_char("java/lang/annotation/Annotation"))))
1121 count = c->runtimevisibleannotationscount;
1123 oa = builtin_anewarray(count, class_java_lang_annotation_Annotation);
1128 /* fill the annotations */
1130 for (i = 0; i < count; i++) {
1138 /* native_get_parametertypes ***************************************************
1140 Use the descriptor of a method to generate a java/lang/Class array
1141 which contains the classes of the parametertypes of the method.
1143 *******************************************************************************/
1145 java_objectarray *native_get_parametertypes(methodinfo *m)
1148 typedesc *paramtypes;
1150 java_objectarray *oa;
1155 /* is the descriptor fully parsed? */
1157 if (m->parseddesc->params == NULL)
1158 if (!descriptor_params_from_paramtypes(md, m->flags))
1161 paramtypes = md->paramtypes;
1162 paramcount = md->paramcount;
1164 /* skip `this' pointer */
1166 if (!(m->flags & ACC_STATIC)) {
1171 /* create class-array */
1173 oa = builtin_anewarray(paramcount, class_java_lang_Class);
1180 for (i = 0; i < paramcount; i++)
1181 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
1182 (classinfo **) &oa->data[i]))
1189 /* native_get_exceptiontypes ***************************************************
1191 Get the exceptions which can be thrown by a method.
1193 *******************************************************************************/
1195 java_objectarray *native_get_exceptiontypes(methodinfo *m)
1197 java_objectarray *oa;
1201 /* create class-array */
1203 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
1208 for (i = 0; i < m->thrownexceptionscount; i++) {
1209 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
1210 resolveEager, true, false, &c))
1213 oa->data[i] = (java_objectheader *) c;
1221 * These are local overrides for various environment variables in Emacs.
1222 * Please do not remove this and leave it at the end of the file, where
1223 * Emacs will automagically detect them.
1224 * ---------------------------------------------------------------------
1227 * indent-tabs-mode: t