aboutsummaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/runtime
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2003-08-28 22:17:37 +0000
committerTom Tromey <tromey@gcc.gnu.org>2003-08-28 22:17:37 +0000
commit91edd042ff3b7b58be7c20bf7483445f60c43210 (patch)
treea5d84d77e14c8eed9bc348be5e03ab4e6490a077 /libjava/gnu/gcj/runtime
parentff2865f2dde326797f4061c32c19fc75dcd4fec5 (diff)
downloadgcc-91edd042ff3b7b58be7c20bf7483445f60c43210.zip
gcc-91edd042ff3b7b58be7c20bf7483445f60c43210.tar.gz
gcc-91edd042ff3b7b58be7c20bf7483445f60c43210.tar.bz2
Makefile.in: Rebuilt.
* Makefile.in: Rebuilt. * Makefile.am (ordinary_java_source_files): Added new files. * java/lang/Class.h (_Jv_sharedlib_register_hook): Declare as friend. * java/net/URLClassLoader.java (findClass): Don't use findURLResource. Use loader's getClass method. (URLLoader.getClass): New method. (addURL): Handle `gcjlib' URLs. (SoURLLoader): New class. (SoResource): Likewise. * gnu/gcj/protocol/gcjlib/Connection.java: New file. * gnu/gcj/protocol/gcjlib/Handler.java: New file. * include/jvm.h (struct _Jv_core_chain): Moved from natCore.cc. (_Jv_RegisterCoreHook): Declare. (_Jv_FindCore): Declare. * gnu/gcj/runtime/SharedLibHelper.java: New file. * gnu/gcj/runtime/natSharedLibLoader.cc (CoreHookFunc): New typedef. (core_hook): New function. (struct SharedLibDummy) [saved_core]: New field. (init): Set _Jv_RegisterCoreHook. Throw exception on failure. (register_hook): Set protection domain and class loader on new class. (finalize): Free core chain. * gnu/gcj/Core.java (Core): New constructor. * gnu/gcj/runtime/SharedLibLoader.java: Rewrote to use SharedLibHelper. * gnu/gcj/natCore.cc (_Jv_RegisterResource): Indentation fixlet. (_Jv_create_core): New function. (create): Use it. (default_register_resource): New function. (_Jv_RegisterCoreHook): New global. (_Jv_RegisterResource): Use it. (core_chain_struct): Removed. (_Jv_FindCore): New function. (_Jv_FreeCoreChain): New function. From-SVN: r70892
Diffstat (limited to 'libjava/gnu/gcj/runtime')
-rw-r--r--libjava/gnu/gcj/runtime/SharedLibHelper.java144
-rw-r--r--libjava/gnu/gcj/runtime/SharedLibLoader.java65
-rw-r--r--libjava/gnu/gcj/runtime/natSharedLibLoader.cc79
3 files changed, 240 insertions, 48 deletions
diff --git a/libjava/gnu/gcj/runtime/SharedLibHelper.java b/libjava/gnu/gcj/runtime/SharedLibHelper.java
new file mode 100644
index 0000000..9e170a1
--- /dev/null
+++ b/libjava/gnu/gcj/runtime/SharedLibHelper.java
@@ -0,0 +1,144 @@
+/* Copyright (C) 2001, 2003 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+package gnu.gcj.runtime;
+import java.lang.ref.WeakReference;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.util.HashMap;
+import java.security.*;
+import gnu.gcj.Core;
+
+public class SharedLibHelper
+{
+ /** Load a shared library, and associate a ClassLoader with it.
+ * @param libname named of shared library (passed to dlopen)
+ * @param parent the parent ClassLoader
+ * @parem flags passed to dlopen
+ */
+ SharedLibHelper(String libname, ClassLoader parent, CodeSource source,
+ int flags)
+ {
+ // FIXME: ask security manager first.
+ loader = parent;
+ baseName = libname;
+ domain = new ProtectionDomain(source,
+ Policy.getPolicy().getPermissions(source));
+ this.flags = flags;
+ }
+
+ public static SharedLibHelper findHelper (String libname)
+ {
+ synchronized (map)
+ {
+ WeakReference ref = (WeakReference) map.get(libname);
+ if (ref != null)
+ return (SharedLibHelper) ref.get();
+ return null;
+ }
+ }
+
+ public static SharedLibHelper findHelper (ClassLoader loader, String libname,
+ CodeSource source)
+ {
+ synchronized (map)
+ {
+ SharedLibHelper result;
+ WeakReference ref = (WeakReference) map.get(libname);
+ if (ref != null)
+ {
+ result = (SharedLibHelper) ref.get();
+ if (result != null)
+ {
+ if (result.loader != loader)
+ // FIXME
+ throw new UnknownError();
+ return result;
+ }
+ }
+
+ result = new SharedLibHelper(libname, loader, source, 0);
+ map.put(libname, new WeakReference(result));
+ return result;
+ }
+ }
+
+ public native void finalize ();
+
+ public Class findClass(String name)
+ {
+ ensureInit();
+ return (Class) classMap.get(name);
+ }
+
+ public URL findResource (String name)
+ {
+ ensureInit();
+ if (! hasResource(name))
+ return null;
+ try
+ {
+ return new URL("gcjlib", "", -1, baseName + "!/" + name);
+ }
+ catch (MalformedURLException _)
+ {
+ }
+ return null;
+ }
+
+ public native Core findCore (String name);
+
+ void ensureInit()
+ {
+ synchronized (classMap)
+ {
+ if (initialized)
+ return;
+ init();
+ initialized = true;
+ }
+ }
+
+ native boolean hasResource(String name);
+ native void init();
+
+ /** Called during dlopen's processing of the init section. */
+ void registerClass(String name, Class cls)
+ {
+ classMap.put(name, cls);
+ }
+
+ /** The handle returned by dlopen. */
+ gnu.gcj.RawData handler;
+
+ /** Holds a _Jv_core_chain for the loader. */
+ gnu.gcj.RawData core_chain;
+
+ /** Map classnames to Classes. */
+ HashMap classMap = new HashMap(20);
+
+ /** Class loader we're helping. */
+ ClassLoader loader;
+
+ /** Name of base file. */
+ String baseName;
+
+ /** Protection domain for loaded classes. */
+ ProtectionDomain domain;
+
+ /** Flags to pass to dlopen. FIXME: platform dependent.
+ 0 is always "sensible" (defined by us). */
+ int flags;
+
+ /** True if we've been initialized. */
+ boolean initialized = false;
+
+ /** Map shared library names to a helper object. This uses weak
+ references in the values so we don't prevent collection. */
+ static HashMap map = new HashMap ();
+}
diff --git a/libjava/gnu/gcj/runtime/SharedLibLoader.java b/libjava/gnu/gcj/runtime/SharedLibLoader.java
index cfcd04e..1f80bbc 100644
--- a/libjava/gnu/gcj/runtime/SharedLibLoader.java
+++ b/libjava/gnu/gcj/runtime/SharedLibLoader.java
@@ -7,7 +7,12 @@ Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
package gnu.gcj.runtime;
-import java.util.Hashtable;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.CodeSource;
+import java.util.Enumeration;
+import java.util.Vector;
/**
* A ClassLoader backed by a gcj-compiled shared library.
@@ -16,55 +21,59 @@ import java.util.Hashtable;
public class SharedLibLoader extends ClassLoader
{
- public native void finalize ();
-
- /** Called during dlopen's processing of the init section. */
- void registerClass(String name, Class cls)
- {
- classMap.put(name, cls);
- }
-
/** Load a shared library, and associate a ClassLoader with it.
* @param libname named of shared library (passed to dlopen)
* @param parent the parent ClassLoader
- * @param flags passed to dlopen
+ * @parem flags passed to dlopen
*/
public SharedLibLoader(String libname, ClassLoader parent, int flags)
{
super(parent);
- init(libname, flags);
+ URL url;
+ try
+ {
+ url = new URL("file", "", libname);
+ }
+ catch (MalformedURLException _)
+ {
+ url = null;
+ }
+ helper = SharedLibHelper.findHelper(this, libname,
+ new CodeSource(url, null));
}
-
/** Load a shared library, and asociate a ClassLoader with it.
* @param libname named of shared library (passed to dlopen)
*/
public SharedLibLoader(String libname)
{
- super(getSystemClassLoader());
- init(libname, 0);
- }
-
- native void init(String libname, int flags);
-
- public Class loadClass(String name)
- throws ClassNotFoundException
- {
- return super.loadClass(name);
+ this(libname, getSystemClassLoader(), 0);
}
public Class findClass(String name)
throws ClassNotFoundException
{
- Object cls = classMap.get(name);
+ Class cls = helper.findClass(name);
if (cls == null)
throw new ClassNotFoundException(name);
- return (Class) cls;
+ return cls;
}
- /** The handle returned by dlopen. */
- gnu.gcj.RawData handler;
+ public URL findResource (String name)
+ {
+ return helper.findResource(name);
+ }
+
+ public Enumeration findResources (String name) throws IOException
+ {
+ URL url = findResource(name);
+ if (url == null)
+ return null;
+ Vector v = new Vector(1);
+ v.add(url);
+ return v.elements();
+ }
- /** Map classnames to Classes. */
- Hashtable classMap = new Hashtable(20);
+ /** The helper that does the work for us. */
+ SharedLibHelper helper;
}
diff --git a/libjava/gnu/gcj/runtime/natSharedLibLoader.cc b/libjava/gnu/gcj/runtime/natSharedLibLoader.cc
index 49bcfb4..46eef75 100644
--- a/libjava/gnu/gcj/runtime/natSharedLibLoader.cc
+++ b/libjava/gnu/gcj/runtime/natSharedLibLoader.cc
@@ -1,4 +1,4 @@
-// natSharedLibLoader.cc - Implementation of FirstThread native methods.
+// natSharedLibLoader.cc - Implementation of SharedLibHelper native methods.
/* Copyright (C) 2001, 2003 Free Software Foundation
@@ -11,74 +11,113 @@ details. */
#include <config.h>
#include <gcj/cni.h>
-#include <gnu/gcj/runtime/SharedLibLoader.h>
+#include <jvm.h>
+#include <gnu/gcj/runtime/SharedLibHelper.h>
#include <java/io/IOException.h>
#include <java/lang/UnsupportedOperationException.h>
-#include <java/lang/UnsatisfiedLinkError.h>
+#include <java/lang/UnknownError.h>
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
/* Only used during dlopen, while having a lock on Class.class. */
-static gnu::gcj::runtime::SharedLibLoader* curLoader;
+static java::lang::ClassLoader *curLoader;
+static gnu::gcj::runtime::SharedLibHelper *curHelper;
typedef void (*ClassHookFunc) (jclass);
+typedef void (*CoreHookFunc) (_Jv_core_chain *);
+
+void
+_Jv_sharedlib_register_hook (jclass cls)
+{
+ curHelper->registerClass(cls->getName(), cls);
+ cls->protectionDomain = curHelper->domain;
+ cls->loader = curLoader;
+}
static void
-::register_hook(jclass cls)
+core_hook (_Jv_core_chain *chain)
{
- curLoader->registerClass(cls->getName(), cls);
+ chain->next = (_Jv_core_chain *) curHelper->core_chain;
+ curHelper->core_chain = (gnu::gcj::RawData *) chain;
}
struct SharedLibDummy
{
ClassHookFunc saved;
+ CoreHookFunc saved_core;
SharedLibDummy()
{
saved = _Jv_RegisterClassHook;
+ saved_core = _Jv_RegisterCoreHook;
}
~SharedLibDummy()
{
_Jv_RegisterClassHook = saved;
+ _Jv_RegisterCoreHook = saved_core;
curLoader = NULL;
}
};
#endif
void
-gnu::gcj::runtime::SharedLibLoader::init(jstring libname, jint flags)
+gnu::gcj::runtime::SharedLibHelper::init(void)
{
#ifdef HAVE_DLOPEN
- jint len = _Jv_GetStringUTFLength (libname);
- char lname[len + 1];
- JvGetStringUTFRegion (libname, 0, libname->length(), lname);
- lname[len] = '\0';
+ char *lname = (char *) __builtin_alloca (JvGetStringUTFLength (baseName)
+ + 1);
+ jsize total = JvGetStringUTFRegion (baseName, 0, baseName->length(), lname);
+ lname[total] = '\0';
if (flags==0)
- flags = RTLD_LAZY;
+ flags = RTLD_GLOBAL | RTLD_LAZY;
JvSynchronize dummy1(&java::lang::Class::class$);
SharedLibDummy dummy2;
- curLoader = this;
- _Jv_RegisterClassHook = ::register_hook;
+ curLoader = loader;
+ curHelper = this;
+ _Jv_RegisterClassHook = _Jv_sharedlib_register_hook;
+ _Jv_RegisterCoreHook = core_hook;
void *h = dlopen(lname, flags);
if (h == NULL)
{
const char *msg = dlerror();
- jstring str = JvNewStringLatin1 (lname);
- str = str->concat (JvNewStringLatin1 (": "));
- str = str->concat (JvNewStringLatin1 (msg));
- throw new java::lang::UnsatisfiedLinkError (str);
+ throw new java::lang::UnknownError(JvNewStringLatin1(msg));
}
handler = (gnu::gcj::RawData*) h;
#else
- const char *msg = "SharedLibLoader is not supported on this platform";
+ const char *msg
+ = "shared library class loading is not supported on this platform";
throw new java::lang::UnsupportedOperationException(JvNewStringLatin1(msg));
#endif
}
+jboolean
+gnu::gcj::runtime::SharedLibHelper::hasResource (jstring name)
+{
+#ifdef HAVE_DLOPEN
+ _Jv_core_chain *node = _Jv_FindCore ((_Jv_core_chain *) core_chain, name);
+ return node != NULL;
+#else
+ return false;
+#endif
+}
+
+gnu::gcj::Core *
+gnu::gcj::runtime::SharedLibHelper::findCore (jstring name)
+{
+#ifdef HAVE_DLOPEN
+ extern gnu::gcj::Core *_Jv_create_core (_Jv_core_chain *node, jstring name);
+ ensureInit();
+ return _Jv_create_core ((_Jv_core_chain *) core_chain, name);
+#else
+ return NULL;
+#endif
+}
+
void
-gnu::gcj::runtime::SharedLibLoader::finalize()
+gnu::gcj::runtime::SharedLibHelper::finalize()
{
+ _Jv_FreeCoreChain ((_Jv_core_chain *) core_chain);
#ifdef HAVE_DLOPEN
dlclose (handler);
#endif