diff mbox

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

Message ID DC22F074-027A-4ED7-B040-89F7FBCD5A70@sandoe-acoustics.co.uk
State New
Headers show

Commit Message

Iain Sandoe Feb. 9, 2012, 12:26 p.m. UTC
Hi Patrick,

apologies for
(a) the extra loop - I missed a deprecation warning when I built your  
last version ...
(b) rolling two things into one mail ...
... (point 1 is related the pie issue, point 2 to linkage hassles).

On 9 Feb 2012, at 08:36, Iain Sandoe wrote:
>>
>> I think refs (from libstdc++) should be satisfied at link time with  
>> -lstdc++ but probably I am missing something?
>
> when you link c++ code, that is what is required .. but obv. we  
> don't want lstdc++ on the link line for c (because the library  
> *will* be found @ runtime - and thus the routines would be called).
>
> I have an idea ... perhaps we supply (yet another) crt that is  
> linked last on the line and provides the dummies.  That way the  
> linkage will be satisfied from libstd++ for c++ and the dummies for c.
>
> will try and see if I can fit a trial of that idea in on D9 & D10.

Point 2 below... now I look at this again, I wonder why the current  
scheme doesn't cause a problem for Rainer on c++ as well?

===

anyway.

1/ one of the routines you had in the fix was deprecated, can we try:

+  Dl_info info;
+
+  if (! dladdr (f, &info) || info.dli_fbase == NULL)
+    abort ();
+
+  mh = (struct mach_header *) info.dli_fbase;

(and the additional header include) instead?

2/ I've moved the dummy routines to the [Darwin] tm crt endfile  ..

+/* Provide dummy functions to satisfy linkage for versions of the  
Darwin tool-chain that
+   that can't handle undefined weak refs at the link stage.  */
+
+extern void *__cxa_allocate_exception (size_t) WEAK;
+extern void __cxa_throw (void *, void *, void *) WEAK;
+extern void *__cxa_begin_catch (void *) WEAK;
+extern void *__cxa_end_catch (void) WEAK;
+extern void __cxa_tm_cleanup (void *, void *, unsigned int) WEAK;
+
+void *__cxa_allocate_exception (size_t s UNUSED) { return NULL; }
+void __cxa_throw (void * a UNUSED, void * b UNUSED, void * c UNUSED)
+{ return; }
+void *__cxa_begin_catch (void * a UNUSED) { return NULL; }
+void *__cxa_end_catch (void) { return NULL; }
+void __cxa_tm_cleanup (void * a UNUSED, void * b UNUSED,  unsigned  
int c UNUSED)
+{ return; }
+

Rainer, would that work for you too ? ... it would allow removal from  
libitm/eh_cpp.cc

for now, for Darwin:

-#if !defined (HAVE_ELF_STYLE_WEAKREF)
+#if !defined (HAVE_ELF_STYLE_WEAKREF) && ! defined (__MACH__)

====

The attached works with/without -fpie on Darwin 9, I won't get a  
chance to test on D10 for a while, that machine is fully tied up ...  
and I don't have D11 .... could people try it, if this seems reasonable?

Iain
libgcc/config/darwin-crt-tm.c |   96 ++++++++++++++++++++++++++++++-----------
 libgcc/config/t-darwin        |    3 +
 libitm/eh_cpp.cc              |    2 +-
 3 files changed, 75 insertions(+), 26 deletions(-)
diff mbox

Patch

diff --git a/libgcc/config/darwin-crt-tm.c b/libgcc/config/darwin-crt-tm.c
index 2311337..380767b 100644
--- a/libgcc/config/darwin-crt-tm.c
+++ b/libgcc/config/darwin-crt-tm.c
@@ -23,33 +23,68 @@  a copy of the GCC Runtime Library Exception along with this program;
 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>
-
-/* not listed in mach-o/dyld.h for some reason.  */
-extern char * getsectdata (const char*,const char*,unsigned long*); 
+#include <mach-o/getsect.h>
+
+#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))
+#define UNUSED __attribute__((unused))
 
 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 +95,29 @@  void __doTMdeRegistrations (void) __attribute__ ((destructor));
 
 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);
 }
 
+/* Provide dummy functions to satisfy linkage for versions of the Darwin tool-chain that
+   that can't handle undefined weak refs at the link stage.  */
+
+extern void *__cxa_allocate_exception (size_t) WEAK;
+extern void __cxa_throw (void *, void *, void *) WEAK;
+extern void *__cxa_begin_catch (void *) WEAK;
+extern void *__cxa_end_catch (void) WEAK;
+extern void __cxa_tm_cleanup (void *, void *, unsigned int) WEAK;
+
+void *__cxa_allocate_exception (size_t s UNUSED) { return NULL; }
+void __cxa_throw (void * a UNUSED, void * b UNUSED, void * c UNUSED)
+{ return; }
+void *__cxa_begin_catch (void * a UNUSED) { return NULL; }
+void *__cxa_end_catch (void) { return NULL; }
+void __cxa_tm_cleanup (void * a UNUSED, void * b UNUSED,  unsigned int c UNUSED)
+{ return; }
+
 #endif
diff --git a/libitm/eh_cpp.cc b/libitm/eh_cpp.cc
index 352a313..4dd97bf 100644
--- a/libitm/eh_cpp.cc
+++ b/libitm/eh_cpp.cc
@@ -39,7 +39,7 @@  extern void *__cxa_begin_catch (void *) WEAK;
 extern void *__cxa_end_catch (void) WEAK;
 extern void __cxa_tm_cleanup (void *, void *, unsigned int) WEAK;
 
-#if !defined (HAVE_ELF_STYLE_WEAKREF)
+#if !defined (HAVE_ELF_STYLE_WEAKREF) && ! defined (__MACH__)
 void *__cxa_allocate_exception (size_t) { return NULL; }
 void __cxa_throw (void *, void *, void *) { return; }
 void *__cxa_begin_catch (void *) { return NULL; }