Patchwork [trans-mem,darwin] PR/52042 find tm_clone_table with PIE

login
register
mail settings
Submitter Patrick Marlier
Date Feb. 15, 2012, 7:18 p.m.
Message ID <4F3C0517.4040507@gmail.com>
Download mbox | patch
Permalink /patch/141393/
State New
Headers show

Comments

Patrick Marlier - Feb. 15, 2012, 7:18 p.m.
Here the committed patch approved off-list by Mike Stump which fixed 
PR/52042.
Tested on darwin10/11 with x86_64.

PS: this is my first commit so I hope I get it right. Otherwise do not 
hesitate to yell at me.

Thanks.
--
Patrick.

2012-02-15  Iain Sandoe  <iains@gcc.gnu.org>
	    Patrick Marlier  <patrick.marlier@gmail.com>

	PR libitm/52042
	* config/darwin-crt-tm.c (getTMCloneTable): New function.
	(__doTMRegistrations): Call it.
	(__doTMdeRegistrations): Likewise.



On 02/07/2012 10:36 PM, Patrick Marlier wrote:
> Hi,
>
> The problem in this PR is that with PIE, getsectdata does not return the
> position of tm_clone_table after the relocation.
> While _dyld_get_image_vmaddr_slide(0) is enough for PIE, this is not
> enough for dylib.
> I did not find an easy API function to get position of the
> tm_clone_table for a shared library (dylib). So the only way I found is
> to get the mach_header address of the current dylib (via
> _dyld_get_image_header_containing_address), iterate over loaded binaries
> to find the current shared library and use _dyld_get_image_vmaddr_slide
> to find the position.
> Any other proposal (my knowledge of darwin is really limited)?
>
> Can someone do a bootstrap and test libitm on darwin (I have a limited
> access to a darwin machine, at least libitm tests pass)? Thanks!
>
> If tests passed, ok for 4.7?
> --
> Patrick Marlier.
> libgcc:
>
> PR libitm/52042
> * config/darwin-crt-tm.c: Changes for PIE and shared library.
>
Mike Stump - Feb. 15, 2012, 10:30 p.m.
On Feb 15, 2012, at 11:18 AM, Patrick Marlier wrote:
> PS: this is my first commit so I hope I get it right. Otherwise do not hesitate to yell at me.

Thanks.  I know of two people that will test and yell, if anything goes wrong.  I'll spin up a build...

Patch

Index: config/darwin-crt-tm.c
===================================================================
--- config/darwin-crt-tm.c	(revision 184280)
+++ config/darwin-crt-tm.c	(working copy)
@@ -23,33 +23,67 @@  a copy of the GCC Runtime Library Exception along
 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 <http://www.gnu.org/licenses/>.  */
 
+#include "tsystem.h"
+#include <stddef.h>
+#include <dlfcn.h>
 #include <mach-o/dyld.h>
+#include <mach-o/getsect.h>
 
-/* not listed in mach-o/dyld.h for some reason.  */
-extern char * getsectdata (const char*,const char*,unsigned long*); 
+#ifdef __LP64__
+#define GET_DATA_TMCT(mh,size) \
+  getsectdatafromheader_64 ((struct mach_header_64*) mh, \
+			    "__DATA", "__tm_clone_table", (uint64_t *)size)
+#else
+#define GET_DATA_TMCT(mh,size) \
+  getsectdatafromheader (mh, "__DATA", "__tm_clone_table", (uint32_t *)size)
+#endif
 
 #define WEAK __attribute__((weak))
 
 extern void _ITM_registerTMCloneTable (void *, size_t) WEAK;
 extern void _ITM_deregisterTMCloneTable (void *) WEAK;
 
+#if defined(START) || defined(END)
+static inline void *getTMCloneTable (const void *f, size_t *tmct_siz)
+{
+  char *tmct_fixed, *tmct = NULL;
+  unsigned int i, img_count; 
+  struct mach_header *mh;
+  Dl_info info;
+  
+  if (! dladdr (f, &info) || info.dli_fbase == NULL)
+    abort ();
+  
+  mh = (struct mach_header *) info.dli_fbase;
+  tmct_fixed = GET_DATA_TMCT (mh, tmct_siz);
+  *tmct_siz /= (sizeof (size_t) * 2);
+  /* No tm_clone_table or no clones. */
+  if (tmct_fixed == NULL || *tmct_siz == 0)
+    return NULL; 
+
+  img_count = _dyld_image_count();
+  for (i = 0; i < img_count && tmct == NULL; i++)
+    {
+      if (mh == _dyld_get_image_header(i))
+	tmct = tmct_fixed + (unsigned long)_dyld_get_image_vmaddr_slide(i); 
+    }
+
+  return tmct;
+}
+#endif
+
 #ifdef START
 
 void __doTMRegistrations (void) __attribute__ ((constructor));
 
 void __doTMRegistrations (void)
 {
-  char * tm_clone_table_sect_data;
-  unsigned long tmct_siz;
-  
-  tm_clone_table_sect_data = getsectdata ("__DATA",
-					  "__tm_clone_table",
-					  &tmct_siz);
-  tmct_siz /= (sizeof (size_t) * 2);
-  if (_ITM_registerTMCloneTable != NULL
-      && tm_clone_table_sect_data != NULL
-      && tmct_siz > 0)
-    _ITM_registerTMCloneTable (tm_clone_table_sect_data, (size_t)tmct_siz);
+  size_t tmct_siz;
+  void *tmct;
+
+  tmct = getTMCloneTable ((const void *)&__doTMRegistrations, &tmct_siz);
+  if (_ITM_registerTMCloneTable != NULL && tmct != NULL)
+    _ITM_registerTMCloneTable (tmct, (size_t)tmct_siz);
 }
 
 #endif
@@ -60,18 +94,12 @@  void __doTMdeRegistrations (void) __attribute__ ((
 
 void __doTMdeRegistrations (void)
 {
-  char * tm_clone_table_sect_data;
-  unsigned long tmct_siz;
-  
-  tm_clone_table_sect_data = getsectdata ("__DATA",
-					  "__tm_clone_table",
-					  &tmct_siz);
-  
-  if (_ITM_deregisterTMCloneTable != NULL
-      && tm_clone_table_sect_data != NULL
-      && tmct_siz > 0)
-    _ITM_deregisterTMCloneTable (tm_clone_table_sect_data);
+  size_t tmct_siz;
+  void *tmct;
 
+  tmct = getTMCloneTable ((const void *)&__doTMdeRegistrations, &tmct_siz);
+  if (_ITM_deregisterTMCloneTable != NULL && tmct != NULL)
+    _ITM_deregisterTMCloneTable (tmct);
 }
 
 #endif