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] Add __attribute((tls_model("initial-exec")))


Hi!

ATM the TLS model is determined on a per-file basis and is basically
centered around who references the TLS variables, not who provides them.
E.g. suppose glibc exports errno as STT_TLS symbol. For libc.so we can
assume the variable will resolve into the initial TLS block, so although
random shared library using TLS needs to be compiled with global-dynamic
model so that it can be dlopened, references to errno can be using
initial-exec model and resolved once at dlopen time, not every time
they are referenced.
Similarly, if a dynamically linked program references errno, unless
we go for TLS COPY relocs which IMHO we don't want and is compiled with
-ftls-model=local-exec, it will not link, since local-exec model cannot
reference variables outside of the binary.

With the patch below, glibc can declare errno as:
extern __thread int errno __attribute__ ((tls_model ("initial-exec")));
and optimize -ftls-model={global,local}-dynamic libs and
make -ftls-model=local-exec binaries work at the same time.

2002-09-11  Jakub Jelinek  <jakub@redhat.com>

	* doc/extend.texi (tls_model): Document.
	* varasm.c (decl_tls_model): New.
	* c-common.c (handle_tls_model_attribute): New.
	(c_common_attribute_table): Add tls_model.
	* config/alpha/alpha.c (alpha_encode_section_info): Use
	decl_tls_model.
	* flags.h (enum tls_model, flag_tls_default): Move...
	* tree.h (enum tls_model, flag_tls_default): ...here.
	(decl_tls_model): New prototype.
	* config/ia64/ia64.c (ia64_encode_section_info): Likewise.
	* config/i386/i386.c (ix86_encode_section_info): Likewise.
	* config/i386/i386.md (tls_global_dynamic, tls_local_dynamic_base):
	Allow !flag_pic.

--- gcc/doc/extend.texi.jj	2002-09-10 13:47:53.000000000 +0200
+++ gcc/doc/extend.texi	2002-09-11 15:50:09.000000000 +0200
@@ -2332,6 +2332,15 @@ since it is known that the calling funct
 
 Not all ELF targets support this attribute.
 
+@item tls_model ("@var{tls_model}")
+@cindex @code{tls_model} attribute
+The @code{tls_model} attribute sets thread-local storage model
+(@pxref{Thread-Local}) of a particular @code{__thread} variable,
+overriding @code{-ftls-model=} command line switch on a per-variable
+basis.
+The @var{tls_model} argument should be one of @code{global-dynamic},
+@code{local-dynamic}, @code{initial-exec} or @code{local-exec}.
+
 @item regparm (@var{number})
 @cindex functions that are passed arguments in registers on the 386
 On the Intel 386, the @code{regparm} attribute causes the compiler to
--- gcc/config/alpha/alpha.c.jj	2002-08-28 21:13:12.000000000 +0200
+++ gcc/config/alpha/alpha.c	2002-09-11 14:33:12.000000000 +0200
@@ -1872,22 +1872,7 @@ alpha_encode_section_info (decl, first)
   /* Care for TLS variables.  */
   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
     {
-      enum tls_model kind;
-      if (!flag_pic)
-	{
-	  if (is_local)
-	    kind = TLS_MODEL_LOCAL_EXEC;
-	  else
-	    kind = TLS_MODEL_INITIAL_EXEC;
-	}
-      else if (is_local)
-	kind = TLS_MODEL_LOCAL_DYNAMIC;
-      else
-	kind = TLS_MODEL_GLOBAL_DYNAMIC;
-      if (kind < flag_tls_default)
-	kind = flag_tls_default;
-
-      switch (kind)
+      switch (decl_tls_model (decl))
 	{
 	case TLS_MODEL_GLOBAL_DYNAMIC:
 	  encoding = 'G';
--- gcc/config/i386/i386.c.jj	2002-09-10 14:40:49.000000000 +0200
+++ gcc/config/i386/i386.c	2002-09-11 14:37:48.000000000 +0200
@@ -5493,23 +5493,7 @@ ix86_encode_section_info (decl, first)
       const char *symbol_str;
       char *newstr;
       size_t len;
-      enum tls_model kind;
-
-      if (!flag_pic)
-	{
-	  if (local_p)
-	    kind = TLS_MODEL_LOCAL_EXEC;
-	  else
-	    kind = TLS_MODEL_INITIAL_EXEC;
-	}
-      /* Local dynamic is inefficient when we're not combining the
-	 parts of the address.  */
-      else if (optimize && local_p)
-	kind = TLS_MODEL_LOCAL_DYNAMIC;
-      else
-	kind = TLS_MODEL_GLOBAL_DYNAMIC;
-      if (kind < flag_tls_default)
-	kind = flag_tls_default;
+      enum tls_model kind = decl_tls_model (decl);
 
       symbol_str = XSTR (symbol, 0);
 
--- gcc/config/i386/i386.md.jj	2002-09-10 13:47:40.000000000 +0200
+++ gcc/config/i386/i386.md	2002-09-11 16:01:38.000000000 +0200
@@ -13824,9 +13824,13 @@
 	      (clobber (reg:CC 17))])]
   ""
 {
-  if (!flag_pic)
-    abort ();
-  operands[2] = pic_offset_table_rtx;
+  if (flag_pic)
+    operands[2] = pic_offset_table_rtx;
+  else
+    {
+      operands[2] = gen_reg_rtx (Pmode);
+      emit_insn (gen_set_got (operands[2]));
+    }
   operands[3] = ix86_tls_get_addr ();
 })
 
@@ -13866,8 +13870,13 @@
 	      (clobber (reg:CC 17))])]
   ""
 {
-  if (!flag_pic)
-    abort ();
+  if (flag_pic)
+    operands[2] = pic_offset_table_rtx;
+  else
+    {
+      operands[2] = gen_reg_rtx (Pmode);
+      emit_insn (gen_set_got (operands[2]));
+    }
   operands[1] = pic_offset_table_rtx;
   operands[2] = ix86_tls_get_addr ();
 })
--- gcc/config/ia64/ia64.c.jj	2002-09-10 13:47:41.000000000 +0200
+++ gcc/config/ia64/ia64.c	2002-09-11 14:32:13.000000000 +0200
@@ -7134,24 +7134,7 @@ ia64_encode_section_info (decl, first)
   is_local = (*targetm.binds_local_p) (decl);
 
   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
-    {
-      enum tls_model kind;
-      if (!flag_pic)
-	{
-	  if (is_local)
-	    kind = TLS_MODEL_LOCAL_EXEC;
-	  else
-	    kind = TLS_MODEL_INITIAL_EXEC;
-	}
-      else if (is_local)
-	kind = TLS_MODEL_LOCAL_DYNAMIC;
-      else
-	kind = TLS_MODEL_GLOBAL_DYNAMIC;
-      if (kind < flag_tls_default)
-	kind = flag_tls_default;
-
-      encoding = " GLil"[kind];
-    }
+    encoding = " GLil"[decl_tls_model (decl)];
   /* Determine if DECL will wind up in .sdata/.sbss.  */
   else if (is_local && ia64_in_small_data_p (decl))
     encoding = 's';
--- gcc/varasm.c.jj	2002-09-10 13:47:31.000000000 +0200
+++ gcc/varasm.c	2002-09-11 15:30:31.000000000 +0200
@@ -4680,6 +4680,52 @@ init_varasm_once ()
   const_alias_set = new_alias_set ();
 }
 
+enum tls_model
+decl_tls_model (decl)
+     tree decl;
+{
+  enum tls_model kind;
+  tree attr = lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl));
+  bool is_local;
+
+  if (attr)
+    {
+      attr = TREE_VALUE (TREE_VALUE (attr));
+      if (TREE_CODE (attr) != STRING_CST)
+	abort ();
+      if (!strcmp (TREE_STRING_POINTER (attr), "local-exec"))
+	kind = TLS_MODEL_LOCAL_EXEC;
+      else if (!strcmp (TREE_STRING_POINTER (attr), "initial-exec"))
+	kind = TLS_MODEL_INITIAL_EXEC;
+      else if (!strcmp (TREE_STRING_POINTER (attr), "local-dynamic"))
+	kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
+      else if (!strcmp (TREE_STRING_POINTER (attr), "global-dynamic"))
+	kind = TLS_MODEL_GLOBAL_DYNAMIC;
+      else
+	abort ();
+      return kind;
+    }
+
+  is_local = (*targetm.binds_local_p) (decl);
+  if (!flag_pic)
+    {
+      if (is_local)
+	kind = TLS_MODEL_LOCAL_EXEC;
+      else
+	kind = TLS_MODEL_INITIAL_EXEC;
+    }
+  /* Local dynamic is inefficient when we're not combining the
+     parts of the address.  */
+  else if (optimize && is_local)
+    kind = TLS_MODEL_LOCAL_DYNAMIC;
+  else
+    kind = TLS_MODEL_GLOBAL_DYNAMIC;
+  if (kind < flag_tls_default)
+    kind = flag_tls_default;
+
+  return kind;
+}
+
 /* Select a set of attributes for section NAME based on the properties
    of DECL and whether or not RELOC indicates that DECL's initializer
    might contain runtime relocations.
--- gcc/c-common.c.jj	2002-09-10 13:47:22.000000000 +0200
+++ gcc/c-common.c	2002-09-11 13:39:58.000000000 +0200
@@ -734,6 +734,8 @@ static tree handle_alias_attribute	PARAM
 						 bool *));
 static tree handle_visibility_attribute	PARAMS ((tree *, tree, tree, int,
 						 bool *));
+static tree handle_tls_model_attribute	PARAMS ((tree *, tree, tree, int,
+						 bool *));
 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
 							     tree, int,
 							     bool *));
@@ -833,6 +835,8 @@ const struct attribute_spec c_common_att
 			      handle_vector_size_attribute },
   { "visibility",	      1, 1, true,  false, false,
 			      handle_visibility_attribute },
+  { "tls_model",	      1, 1, true,  false, false,
+			      handle_tls_model_attribute },
   { "nonnull",                0, -1, false, true, true,
 			      handle_nonnull_attribute },
   { "nothrow",                0, 0, true,  false, false,
@@ -6087,6 +6091,49 @@ handle_visibility_attribute (node, name,
   return NULL_TREE;
 }
 
+/* Handle an "tls_model" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_tls_model_attribute (node, name, args, flags, no_add_attrs)
+     tree *node;
+     tree name;
+     tree args;
+     int flags ATTRIBUTE_UNUSED;
+     bool *no_add_attrs;
+{
+  tree decl = *node;
+
+  if (! DECL_THREAD_LOCAL (decl))
+    {
+      warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+      *no_add_attrs = true;
+    }
+  else
+    {
+      tree id;
+
+      id = TREE_VALUE (args);
+      if (TREE_CODE (id) != STRING_CST)
+	{
+	  error ("tls_model arg not a string");
+	  *no_add_attrs = true;
+	  return NULL_TREE;
+	}
+      if (strcmp (TREE_STRING_POINTER (id), "local-exec")
+	  && strcmp (TREE_STRING_POINTER (id), "initial-exec")
+	  && strcmp (TREE_STRING_POINTER (id), "local-dynamic")
+	  && strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
+	{
+	  error ("tls_model arg must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
+	  *no_add_attrs = true;
+	  return NULL_TREE;
+	}
+    }
+
+  return NULL_TREE;
+}
+
 /* Handle a "no_instrument_function" attribute; arguments as in
    struct attribute_spec.handler.  */
 
--- gcc/tree.h.jj	2002-08-19 13:25:17.000000000 +0200
+++ gcc/tree.h	2002-09-11 15:27:17.000000000 +0200
@@ -2098,7 +2098,17 @@ extern GTY(()) tree integer_types[itk_no
 #define long_unsigned_type_node		integer_types[itk_unsigned_long]
 #define long_long_integer_type_node	integer_types[itk_long_long]
 #define long_long_unsigned_type_node	integer_types[itk_unsigned_long_long]
+
+/* Set to the default thread-local storage (tls) model to use.  */
+
+enum tls_model {
+  TLS_MODEL_GLOBAL_DYNAMIC = 1,
+  TLS_MODEL_LOCAL_DYNAMIC,
+  TLS_MODEL_INITIAL_EXEC,
+  TLS_MODEL_LOCAL_EXEC
+};
 
+extern enum tls_model flag_tls_default;
 
 #define NULL_TREE (tree) NULL
 
@@ -2982,6 +2992,7 @@ extern void make_decl_rtl		PARAMS ((tree
 extern void make_decl_one_only		PARAMS ((tree));
 extern int supports_one_only		PARAMS ((void));
 extern void variable_section		PARAMS ((tree, int));
+enum tls_model decl_tls_model		PARAMS ((tree));
 
 /* In fold-const.c */
 extern int div_and_round_double		PARAMS ((enum tree_code, int,
--- gcc/flags.h.jj	2002-08-03 01:21:54.000000000 +0200
+++ gcc/flags.h	2002-09-11 15:26:20.000000000 +0200
@@ -467,17 +467,6 @@ extern int flag_pedantic_errors;
 
 extern int flag_pic;
 
-/* Set to the default thread-local storage (tls) model to use.  */
-
-enum tls_model {
-  TLS_MODEL_GLOBAL_DYNAMIC = 1,
-  TLS_MODEL_LOCAL_DYNAMIC,
-  TLS_MODEL_INITIAL_EXEC,
-  TLS_MODEL_LOCAL_EXEC
-};
-
-extern enum tls_model flag_tls_default;
-
 /* Nonzero means generate extra code for exception handling and enable
    exception handling.  */
 

	Jakub


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