public inbox for prelink@sourceware.org
 help / color / mirror / Atom feed
From: "Joseph S. Myers" <joseph@codesourcery.com>
To: prelink@sourceware.org
Subject: [ARM prelink 2/3] TLS support
Date: Wed, 04 Mar 2009 01:44:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.64.0903040143370.15240@digraph.polyomino.org.uk> (raw)

This is the second of three patches with various fixes to the
prelinker's ARM support (and is relative to a tree with the first
patch applied).  This one adds TLS support.  Please commit if OK.

2009-03-04  Daniel Jacobowitz  <dan@codesourcery.com>
            Joseph Myers  <joseph@codesourcery.com>

	* src/arch-arm.c (R_ARM_TLS_DTPMOD32, R_ARM_TLS_DTPOFF32,
	R_ARM_TLS_TPOFF32): Define if R_ARM_TLS_DTPMOD32 not already
	defined.
	(arm_prelink_rel, arm_prelink_rela, arm_prelink_conflict_rel,
	arm_prelink_conflict_rela, arm_rel_to_rela, arm_rela_to_rel,
	arm_need_rel_to_rela, arm_undo_prelink_rel, arm_reloc_class):
	Handle TLS relocations.

diff -rupN prelink.ne32/src/arch-arm.c prelink.tls/src/arch-arm.c
--- prelink.ne32/src/arch-arm.c	2009-03-03 17:15:05.000000000 -0800
+++ prelink.tls/src/arch-arm.c	2009-03-03 17:25:57.000000000 -0800
@@ -28,6 +28,12 @@
 
 #include "prelink.h"
 
+#ifndef R_ARM_TLS_DTPMOD32
+#define R_ARM_TLS_DTPMOD32      17      /* ID of module containing symbol */
+#define R_ARM_TLS_DTPOFF32      18      /* Offset in TLS block */
+#define R_ARM_TLS_TPOFF32       19      /* Offset in static TLS block */
+#endif
+
 static int
 arm_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
 		 GElf_Addr adjust)
@@ -151,6 +157,25 @@ arm_prelink_rel (struct prelink_info *in
 	return 0;
       error (0, 0, "%s: R_ARM_COPY reloc in shared library?", dso->filename);
       return 1;
+    case R_ARM_TLS_DTPOFF32:
+      write_ne32 (dso, rel->r_offset, value);
+      break;
+    /* DTPMOD32 and TPOFF32 is impossible to predict in shared libraries
+       unless prelink sets the rules.  */
+    case R_ARM_TLS_DTPMOD32:
+      if (dso->ehdr.e_type == ET_EXEC)
+        {
+          error (0, 0, "%s: R_ARM_TLS_DTPMOD32 reloc in executable?",
+                 dso->filename);
+          return 1;
+        }
+      break;
+    case R_ARM_TLS_TPOFF32:
+      if (dso->ehdr.e_type == ET_EXEC)
+	error (0, 0, "%s: R_ARM_TLS_TPOFF32 relocs should not be present in "
+	       "prelinked ET_EXEC REL sections",
+	       dso->filename);
+      break;
     default:
       error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
 	     (int) GELF_R_TYPE (rel->r_info));
@@ -201,6 +226,24 @@ arm_prelink_rela (struct prelink_info *i
 	return 0;
       error (0, 0, "%s: R_ARM_COPY reloc in shared library?", dso->filename);
       return 1;
+    case R_ARM_TLS_DTPOFF32:
+      write_ne32 (dso, rela->r_offset, value + rela->r_addend);
+      break;
+    /* DTPMOD32 and TPOFF32 is impossible to predict in shared libraries
+       unless prelink sets the rules.  */
+    case R_ARM_TLS_DTPMOD32:
+      if (dso->ehdr.e_type == ET_EXEC)
+        {
+          error (0, 0, "%s: R_ARM_TLS_DTPMOD32 reloc in executable?",
+                 dso->filename);
+          return 1;
+        }
+      break;
+    case R_ARM_TLS_TPOFF32:
+      if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+        write_ne32 (dso, rela->r_offset,
+                    value + rela->r_addend + info->resolvetls->offset);
+      break;
     default:
       error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
 	     (int) GELF_R_TYPE (rela->r_info));
@@ -315,6 +358,7 @@ arm_prelink_conflict_rel (DSO *dso, stru
 {
   GElf_Addr value;
   struct prelink_conflict *conflict;
+  struct prelink_tls *tls;
   GElf_Rela *ret;
 
   if (GELF_R_TYPE (rel->r_info) == R_ARM_RELATIVE
@@ -324,8 +368,34 @@ arm_prelink_conflict_rel (DSO *dso, stru
   conflict = prelink_conflict (info, GELF_R_SYM (rel->r_info),
 			       GELF_R_TYPE (rel->r_info));
   if (conflict == NULL)
-    return 0;
-  value = conflict_lookup_value (conflict);
+    {
+      if (info->curtls == NULL)
+	return 0;
+
+      switch (GELF_R_TYPE (rel->r_info))
+	{
+	/* Even local DTPMOD and TPOFF relocs need conflicts.  */
+	case R_ARM_TLS_DTPMOD32:
+	case R_ARM_TLS_TPOFF32:
+	  break;
+
+	default:
+	  return 0;
+	}
+      value = 0;
+    }
+  else
+    {
+      /* DTPOFF32 wants to see only real conflicts, not lookups
+	 with reloc_class RTYPE_CLASS_TLS.  */
+      if (GELF_R_TYPE (rel->r_info) == R_ARM_TLS_DTPOFF32
+	  && conflict->lookup.tls == conflict->conflict.tls
+	  && conflict->lookupval == conflict->conflictval)
+        return 0;
+
+      value = conflict_lookup_value (conflict);
+    }
+
   ret = prelink_conflict_add_rela (info);
   if (ret == NULL)
     return 1;
@@ -345,6 +415,33 @@ arm_prelink_conflict_rel (DSO *dso, stru
     case R_ARM_COPY:
       error (0, 0, "R_ARM_COPY should not be present in shared libraries");
       return 1;
+    case R_ARM_TLS_DTPMOD32:
+    case R_ARM_TLS_DTPOFF32:
+    case R_ARM_TLS_TPOFF32:
+      if (conflict != NULL
+	  && (conflict->reloc_class != RTYPE_CLASS_TLS
+	      || conflict->lookup.tls == NULL))
+	{
+	  error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+		 dso->filename);
+	  return 1;
+	}
+      tls = conflict ? conflict->lookup.tls : info->curtls;
+      ret->r_info = GELF_R_INFO (0, R_ARM_ABS32);
+      switch (GELF_R_TYPE (rel->r_info))
+	{
+	case R_ARM_TLS_DTPMOD32:
+	  ret->r_addend = tls->modid;
+	  break;
+	case R_ARM_TLS_DTPOFF32:
+	  ret->r_addend = value;
+	  break;
+	case R_ARM_TLS_TPOFF32:
+	  ret->r_addend = (value + read_une32 (dso, rel->r_offset)
+			   + tls->offset);
+	  break;
+	}
+      break;
     default:
       error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
 	     (int) GELF_R_TYPE (rel->r_info));
@@ -359,6 +456,7 @@ arm_prelink_conflict_rela (DSO *dso, str
 {
   GElf_Addr value;
   struct prelink_conflict *conflict;
+  struct prelink_tls *tls;
   GElf_Rela *ret;
   Elf32_Sword val;
 
@@ -368,9 +466,36 @@ arm_prelink_conflict_rela (DSO *dso, str
     return 0;
   conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
 			       GELF_R_TYPE (rela->r_info));
+
   if (conflict == NULL)
-    return 0;
-  value = conflict_lookup_value (conflict);
+    {
+      if (info->curtls == NULL)
+	return 0;
+
+      switch (GELF_R_TYPE (rela->r_info))
+	{
+	/* Even local DTPMOD and TPOFF relocs need conflicts.  */
+	case R_ARM_TLS_DTPMOD32:
+	case R_ARM_TLS_TPOFF32:
+	  break;
+
+	default:
+	  return 0;
+	}
+      value = 0;
+    }
+  else
+    {
+      /* DTPOFF32 wants to see only real conflicts, not lookups
+	 with reloc_class RTYPE_CLASS_TLS.  */
+      if (GELF_R_TYPE (rela->r_info) == R_ARM_TLS_DTPOFF32
+	  && conflict->lookup.tls == conflict->conflict.tls
+	  && conflict->lookupval == conflict->conflictval)
+        return 0;
+
+      value = conflict_lookup_value (conflict);
+    }
+
   ret = prelink_conflict_add_rela (info);
   if (ret == NULL)
     return 1;
@@ -398,6 +523,32 @@ arm_prelink_conflict_rela (DSO *dso, str
     case R_ARM_COPY:
       error (0, 0, "R_ARM_COPY should not be present in shared libraries");
       return 1;
+    case R_ARM_TLS_DTPMOD32:
+    case R_ARM_TLS_DTPOFF32:
+    case R_ARM_TLS_TPOFF32:
+      if (conflict != NULL
+	  && (conflict->reloc_class != RTYPE_CLASS_TLS
+	      || conflict->lookup.tls == NULL))
+	{
+	  error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+		 dso->filename);
+	  return 1;
+	}
+      tls = conflict ? conflict->lookup.tls : info->curtls;
+      ret->r_info = GELF_R_INFO (0, R_ARM_ABS32);
+      switch (GELF_R_TYPE (rela->r_info))
+	{
+	case R_ARM_TLS_DTPMOD32:
+	  ret->r_addend = tls->modid;
+	  break;
+	case R_ARM_TLS_DTPOFF32:
+	  ret->r_addend = value;
+	  break;
+	case R_ARM_TLS_TPOFF32:
+	  ret->r_addend = value + rela->r_addend + tls->offset;
+	  break;
+	}
+      break;
     default:
       error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
 	     (int) GELF_R_TYPE (rela->r_info));
@@ -418,6 +569,7 @@ arm_rel_to_rela (DSO *dso, GElf_Rel *rel
       abort ();
     case R_ARM_RELATIVE:
     case R_ARM_ABS32:
+    case R_ARM_TLS_TPOFF32:
       rela->r_addend = (Elf32_Sword) read_une32 (dso, rel->r_offset);
       break;
     case R_ARM_PC24:
@@ -426,6 +578,8 @@ arm_rel_to_rela (DSO *dso, GElf_Rel *rel
       break;
     case R_ARM_COPY:
     case R_ARM_GLOB_DAT:
+    case R_ARM_TLS_DTPMOD32:
+    case R_ARM_TLS_DTPOFF32:
       rela->r_addend = 0;
       break;
     }
@@ -445,6 +599,7 @@ arm_rela_to_rel (DSO *dso, GElf_Rela *re
       abort ();
     case R_ARM_RELATIVE:
     case R_ARM_ABS32:
+    case R_ARM_TLS_TPOFF32:
       write_ne32 (dso, rela->r_offset, rela->r_addend);
       break;
     case R_ARM_PC24:
@@ -453,6 +608,8 @@ arm_rela_to_rel (DSO *dso, GElf_Rela *re
 		  | ((rela->r_addend >> 2) & 0xffffff));
       break;
     case R_ARM_GLOB_DAT:
+    case R_ARM_TLS_DTPMOD32:
+    case R_ARM_TLS_DTPOFF32:
       write_ne32 (dso, rela->r_offset, 0);
       break;
     }
@@ -488,6 +645,14 @@ arm_need_rel_to_rela (DSO *dso, int firs
 		/* FALLTHROUGH */
 	      case R_ARM_PC24:
 		return 1;
+	      case R_ARM_TLS_TPOFF32:
+		/* In shared libraries TPOFF is changed always into
+		   conflicts, for executables we need to preserve
+		   original addend.  */
+		if (dso->ehdr.e_type == ET_EXEC)
+		  return 1;
+
+		break;
 	      }
 	}
     }
@@ -614,6 +779,12 @@ arm_undo_prelink_rel (DSO *dso, GElf_Rel
 	return 0;
       error (0, 0, "%s: R_ARM_COPY reloc in shared library?", dso->filename);
       return 1;
+    case R_ARM_TLS_DTPMOD32:
+    case R_ARM_TLS_DTPOFF32:
+      write_ne32 (dso, rel->r_offset, 0);
+      break;
+    case R_ARM_TLS_TPOFF32:
+      break;
     default:
       error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
 	     (int) GELF_R_TYPE (rel->r_info));
@@ -636,6 +807,10 @@ arm_reloc_class (int reloc_type)
     {
     case R_ARM_COPY: return RTYPE_CLASS_COPY;
     case R_ARM_JUMP_SLOT: return RTYPE_CLASS_PLT;
+    case R_ARM_TLS_DTPMOD32:
+    case R_ARM_TLS_DTPOFF32:
+    case R_ARM_TLS_TPOFF32:
+      return RTYPE_CLASS_TLS;
     default: return RTYPE_CLASS_VALID;
     }
 }

-- 
Joseph S. Myers
joseph@codesourcery.com

                 reply	other threads:[~2009-03-04  1:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0903040143370.15240@digraph.polyomino.org.uk \
    --to=joseph@codesourcery.com \
    --cc=prelink@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).