This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch 2/2] Make java compiled resources public.


This is the meat of the patch.  As mentioned in the first part,  it is
part of:

...a two part patch to make embedded java resources public symbols in
libgcj.a.

The patch is motivated by the fact that there are no references to the resource files.  So when doing static linking there is no easy way to make the linker include them in the compiled object.

The patches give the resource files public visibility with a mangled name, this allows them to be forced into the output object.  The names are given hidden visibility so that they do not pollute the namespace of libgcj.so.

The mangling is as follows:

The resource name is broken into path components by '/' characters. Each component then has an '_' prepended and all '.' -> "$_" and '$' -> "$$".  The length of each component is then prepended to this and all are concatenated together and preceeded by "_ZGr".  "Gr" being an unused special-name designator that could be thought of as representing GNU-resource.  For example:

"java/util/iso4217.properties" mangles as:
"_ZGr5_java5_util20_iso4217$_properties"

This patch is nearly identical to http://gcc.gnu.org/ml/java-patches/2007-q4/msg00100.html , but incorporates Jakub's suggestions.

As before tested on x86-64-pc-linux-gnu with make check in libjava with no FAILures.

OK to commit?

2008-01-14  David Daney  <ddaney@avtrex.com>

	* class.c (hide)  Rename to...
	(java_hide_decl) ... this throughout, and make public.
	* resource.c (Jr_count): Remove.
	(compile_resource_data): Call java_mangle_resource_name to generate
	decl name.  Make resource decl public and hidden.
	* mangle.c (append_resource_name_hunk): New function.
	(java_mangle_resource_name): Same.
	* java-tree.h (java_hide_decl, java_mangle_resource_name): Declare
	functions.



Index: class.c
===================================================================
--- class.c	(revision 131121)
+++ class.c	(working copy)
@@ -742,8 +742,8 @@ build_java_method_type (tree fntype, tre
   return fntype;
 }
 
-static void
-hide (tree decl ATTRIBUTE_UNUSED)
+void
+java_hide_decl (tree decl ATTRIBUTE_UNUSED)
 {
 #ifdef HAVE_GAS_HIDDEN
   DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
@@ -872,7 +872,7 @@ add_field (tree class, tree name, tree f
       /* Hide everything that shouldn't be visible outside a DSO.  */
       if (flag_indirect_classes
 	  || (FIELD_PRIVATE (field)))
-	hide (field);
+	java_hide_decl (field);
       /* Considered external unless we are compiling it into this
 	 object file.  */
       DECL_EXTERNAL (field) = (is_compiled_class (class) != 2);
@@ -1031,7 +1031,7 @@ build_static_class_ref (tree type)
 	{
 	  TREE_PUBLIC (decl) = 1;
 	  if (CLASS_PRIVATE (TYPE_NAME (type)))
-	    hide (decl);
+	    java_hide_decl (decl);
 	}
       DECL_IGNORED_P (decl) = 1;
       DECL_ARTIFICIAL (decl) = 1;
@@ -1071,7 +1071,7 @@ build_classdollar_field (tree type)
       TREE_CONSTANT (decl) = 1;
       TREE_READONLY (decl) = 1;
       TREE_PUBLIC (decl) = 1;
-      hide (decl);
+      java_hide_decl (decl);
       DECL_IGNORED_P (decl) = 1;
       DECL_ARTIFICIAL (decl) = 1;
       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
@@ -1760,7 +1760,7 @@ make_class_data (tree type)
       /* The only dispatch table exported from a DSO is the dispatch
 	 table for java.lang.Class.  */
       if (DECL_NAME (type_decl) != id_class)
-	hide (dtable_decl);
+	java_hide_decl (dtable_decl);
       if (! flag_indirect_classes)
 	rest_of_decl_compilation (dtable_decl, 1, 0);
       /* Maybe we're compiling Class as the first class.  If so, set
@@ -2613,7 +2613,7 @@ layout_class_method (tree this_class, tr
       || (METHOD_PRIVATE (method_decl) && METHOD_STATIC (method_decl)
 	  && ! METHOD_NATIVE (method_decl)
 	  && ! special_method_p (method_decl)))
-    hide (method_decl);
+    java_hide_decl (method_decl);
 
   /* Considered external unless it is being compiled into this object
      file, or it was already flagged as external.  */
Index: resource.c
===================================================================
--- resource.c	(revision 131121)
+++ resource.c	(working copy)
@@ -51,14 +51,10 @@ The Free Software Foundation is independ
 /* A list of all the resources files.  */
 static GTY(()) tree resources = NULL;
 
-/* Count of all the resources compiled in this invocation.  */
-static int Jr_count = 0;
-
 void
 compile_resource_data (const char *name, const char *buffer, int length)
 {
   tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
-  char buf[60];
 
   data_type = build_prim_array_type (unsigned_byte_type_node,
 				     strlen (name) + length);
@@ -79,11 +75,10 @@ compile_resource_data (const char *name,
   TREE_CONSTANT (rinit) = 1;
   TREE_INVARIANT (rinit) = 1;
 
-  /* Generate a unique-enough identifier.  */
-  sprintf (buf, "_Jr%d", ++Jr_count);
-
-  decl = build_decl (VAR_DECL, get_identifier (buf), rtype);
+  decl = build_decl (VAR_DECL, java_mangle_resource_name (name), rtype);
   TREE_STATIC (decl) = 1;
+  TREE_PUBLIC (decl) = 1;
+  java_hide_decl (decl);
   DECL_ARTIFICIAL (decl) = 1;
   DECL_IGNORED_P (decl) = 1;
   TREE_READONLY (decl) = 1;
Index: mangle.c
===================================================================
--- mangle.c	(revision 131121)
+++ mangle.c	(working copy)
@@ -796,6 +796,86 @@ compression_table_add (tree type)
   TREE_VEC_ELT (compression_table, compression_next++) = type;
 }
 
+/* Prepend '_' so leading digits are encoded properly.  Also replace
+   '.' with "$_" and '$' with "$$".  The result is encoded with
+   append_gpp_mangled_name.  */
+
+static void
+append_resource_name_hunk(const unsigned char *hunk, int len)
+{
+  /* We need twice the length if all characters are escaped.  */
+  unsigned char *n = (unsigned char *)alloca (2 * len + 1);
+  unsigned char *d;
+  const unsigned char *w1;
+  const unsigned char *w2;
+  const unsigned char *limit;
+
+  d = n;
+
+  *d++ = '_';
+  memcpy (n + 1, hunk, len);
+
+  w1 = hunk;
+  limit = hunk + len;
+  while (w1 < limit)
+    {
+      int ch;
+      w2 = w1;
+      ch = UTF8_GET(w1, limit);
+      gcc_assert (ch > 0);
+      if (ch == '.')
+	{
+	  *d++ = '$';
+	  *d++ = '_';
+	}
+      else if (ch == '$')
+	{
+	  *d++ = '$';
+	  *d++ = '$';
+	}
+      else
+	{
+	  memcpy (d, w2, w1 - w2);
+	  d += w1 - w2;
+	}
+    }
+  append_gpp_mangled_name ((char *)n, d - n);
+}
+
+/* Mangle an embedded resource file name.  Each path component is
+   encoded with append_resource_name_hunk.  */
+
+tree
+java_mangle_resource_name (const char *name)
+{
+  const unsigned char *ptr;
+  const unsigned char *start_of_hunk;
+  const unsigned char *end_of_hunk;
+  const unsigned char *limit = (const unsigned char *)name + strlen(name);
+
+  init_mangling ();
+  MANGLE_RAW_STRING ("Gr");
+
+  ptr = (const unsigned char *)name;
+
+  while (ptr < limit)
+    {
+      start_of_hunk = ptr;
+      end_of_hunk = ptr;
+      while (ptr < limit)
+	{
+	  int ch;
+	  ch = UTF8_GET(ptr, limit);
+	  if (ch == '/')
+	    break;
+	  end_of_hunk = ptr;
+	}
+      append_resource_name_hunk (start_of_hunk, end_of_hunk - start_of_hunk);
+    }
+
+  return finish_mangling ();
+}
+
 /* Mangling initialization routine.  */
 
 static void
Index: java-tree.h
===================================================================
--- java-tree.h	(revision 131121)
+++ java-tree.h	(working copy)
@@ -1026,6 +1026,7 @@ extern tree parse_signature (struct JCF 
 extern tree add_field (tree, tree, tree, int);
 extern tree add_method (tree, int, tree, tree);
 extern tree add_method_1 (tree, int, tree, tree);
+extern void java_hide_decl (tree);
 extern tree make_class (void);
 extern tree push_class (tree, tree);
 extern tree unmangle_classname (const char *name, int name_length);
@@ -1205,6 +1206,7 @@ extern void java_check_methods (tree);
 extern void java_mangle_decl (tree);
 extern tree java_mangle_class_field (struct obstack *, tree);
 extern tree java_mangle_vtable (struct obstack *, tree);
+extern tree java_mangle_resource_name (const char *);
 extern void append_gpp_mangled_name (const char *, int);
 
 extern void add_predefined_file (tree);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]