OSDN Git Service

2006-05-09 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / runtime / natSharedLibLoader.cc
1 // natSharedLibLoader.cc - Implementation of SharedLibHelper native methods.
2
3 /* Copyright (C) 2001, 2003, 2004, 2005, 2006  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12 #include <platform.h>
13
14 #include <gcj/cni.h>
15 #include <jvm.h>
16 #include <execution.h>
17
18 #include <gnu/gcj/runtime/SharedLibHelper.h>
19 #include <java/io/IOException.h>
20 #include <java/lang/UnsupportedOperationException.h>
21 #include <java/lang/UnknownError.h>
22
23 // If we're using the Boehm GC, then we need this include to override dlopen.
24 #ifdef HAVE_BOEHM_GC
25 // Set GC_DEBUG before including gc.h!
26 #ifdef LIBGCJ_GC_DEBUG
27 # define GC_DEBUG
28 #endif
29 #include <gc.h>
30 #endif /* HAVE_BOEHM_GC */
31
32 #ifdef HAVE_DLOPEN
33 #include <dlfcn.h>
34
35 /* Only used during dlopen, while having a lock on Class.class. */
36 static java::lang::ClassLoader *curLoader;
37 static gnu::gcj::runtime::SharedLibHelper *curHelper;
38
39 typedef void (*ClassHookFunc) (jclass);
40 typedef void (*CoreHookFunc) (_Jv_core_chain *);
41
42 void
43 _Jv_sharedlib_register_hook (jclass cls)
44 {
45   cls->protectionDomain = curHelper->domain;
46   cls->loader = curLoader;
47   if (! cls->engine)
48     cls->engine = &_Jv_soleCompiledEngine;
49   curHelper->registerClass(cls->getName(), cls);
50 }
51
52 static void
53 core_hook (_Jv_core_chain *chain)
54 {
55   chain->next = (_Jv_core_chain *) curHelper->core_chain;
56   curHelper->core_chain = (gnu::gcj::RawData *) chain;
57 }
58
59 struct SharedLibDummy
60 {
61   ClassHookFunc saved;
62   CoreHookFunc saved_core;
63   SharedLibDummy()
64   {
65     saved = _Jv_RegisterClassHook;
66     saved_core = _Jv_RegisterCoreHook;
67   }
68   ~SharedLibDummy()
69   {
70     _Jv_RegisterClassHook = saved;
71     _Jv_RegisterCoreHook = saved_core;
72     curLoader = NULL;
73   }
74 };
75 #endif
76
77 void
78 gnu::gcj::runtime::SharedLibHelper::init(void)
79 {
80 #ifdef HAVE_DLOPEN
81   char *lname = (char *) __builtin_alloca (JvGetStringUTFLength (baseName)
82                                            + 1);
83   jsize total = JvGetStringUTFRegion (baseName, 0, baseName->length(), lname);
84   lname[total] = '\0';
85
86   if (flags==0)
87     flags = RTLD_GLOBAL | RTLD_LAZY;
88   JvSynchronize dummy1(&java::lang::Class::class$);
89   SharedLibDummy dummy2;
90   curLoader = loader;
91   curHelper = this;
92   _Jv_RegisterClassHook = _Jv_sharedlib_register_hook;
93   _Jv_RegisterCoreHook = core_hook;
94   void *h = dlopen(lname, flags);
95   if (h == NULL)
96     {
97       const char *msg = dlerror();
98       throw new java::lang::UnknownError(JvNewStringLatin1(msg));
99       fprintf (stderr, "failed loading %s: %s\n", lname, msg);
100     }
101   handler = (gnu::gcj::RawData*) h;
102 #else
103   const char *msg
104     = "shared library class loading is not supported on this platform";
105   throw new java::lang::UnsupportedOperationException(JvNewStringLatin1(msg));
106 #endif
107 }
108
109 jboolean
110 gnu::gcj::runtime::SharedLibHelper::hasResource (jstring name)
111 {
112 #ifdef HAVE_DLOPEN
113   _Jv_core_chain *node = _Jv_FindCore ((_Jv_core_chain *) core_chain, name);
114   return node != NULL;
115 #else
116   return false;
117 #endif
118 }
119
120 gnu::gcj::Core *
121 gnu::gcj::runtime::SharedLibHelper::findCore (jstring name)
122 {
123 #ifdef HAVE_DLOPEN
124   extern gnu::gcj::Core *_Jv_create_core (_Jv_core_chain *node, jstring name);
125   ensureInit();
126   return _Jv_create_core ((_Jv_core_chain *) core_chain, name);
127 #else
128   return NULL;
129 #endif
130 }
131
132 void
133 gnu::gcj::runtime::SharedLibHelper::finalize()
134 {
135   _Jv_FreeCoreChain ((_Jv_core_chain *) core_chain);
136 #ifdef HAVE_DLOPEN
137   if (handler)
138     dlclose (handler);
139 #endif
140 }
141
142 void
143 gnu::gcj::runtime::SharedLibHelper::ensureSupersLinked(jclass k)
144 {
145   _Jv_Linker::wait_for_state (k, JV_STATE_LOADING);
146 }