xref: /dflybsd-src/contrib/gcc-4.7/libgcc/config/darwin-crt-tm.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Provide the runtime infrastructure for the transactional memory lib.
2*e4b17023SJohn Marino    Copyright (C) 2011 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    Contributed by Iain Sandoe <iains@gcc.gnu.org>
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino    This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional
18*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version
19*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation.
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and
22*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
23*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
25*e4b17023SJohn Marino 
26*e4b17023SJohn Marino #include "tsystem.h"
27*e4b17023SJohn Marino #include <stddef.h>
28*e4b17023SJohn Marino #include <dlfcn.h>
29*e4b17023SJohn Marino #include <mach-o/dyld.h>
30*e4b17023SJohn Marino #include <mach-o/getsect.h>
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino #ifdef __LP64__
33*e4b17023SJohn Marino #define GET_DATA_TMCT(mh,size) \
34*e4b17023SJohn Marino   getsectdatafromheader_64 ((struct mach_header_64*) mh, \
35*e4b17023SJohn Marino 			    "__DATA", "__tm_clone_table", (uint64_t *)size)
36*e4b17023SJohn Marino #else
37*e4b17023SJohn Marino #define GET_DATA_TMCT(mh,size) \
38*e4b17023SJohn Marino   getsectdatafromheader (mh, "__DATA", "__tm_clone_table", (uint32_t *)size)
39*e4b17023SJohn Marino #endif
40*e4b17023SJohn Marino 
41*e4b17023SJohn Marino #define WEAK __attribute__((weak))
42*e4b17023SJohn Marino #define UNUSED __attribute__((unused))
43*e4b17023SJohn Marino 
44*e4b17023SJohn Marino extern void _ITM_registerTMCloneTable (void *, size_t) WEAK;
45*e4b17023SJohn Marino extern void _ITM_deregisterTMCloneTable (void *) WEAK;
46*e4b17023SJohn Marino 
47*e4b17023SJohn Marino #if defined(START) || defined(END)
getTMCloneTable(const void * f,size_t * tmct_siz)48*e4b17023SJohn Marino static inline void *getTMCloneTable (const void *f, size_t *tmct_siz)
49*e4b17023SJohn Marino {
50*e4b17023SJohn Marino   char *tmct_fixed, *tmct = NULL;
51*e4b17023SJohn Marino   unsigned int i, img_count;
52*e4b17023SJohn Marino   struct mach_header *mh;
53*e4b17023SJohn Marino   Dl_info info;
54*e4b17023SJohn Marino 
55*e4b17023SJohn Marino   if (! dladdr (f, &info) || info.dli_fbase == NULL)
56*e4b17023SJohn Marino     abort ();
57*e4b17023SJohn Marino 
58*e4b17023SJohn Marino   mh = (struct mach_header *) info.dli_fbase;
59*e4b17023SJohn Marino   tmct_fixed = GET_DATA_TMCT (mh, tmct_siz);
60*e4b17023SJohn Marino   *tmct_siz /= (sizeof (size_t) * 2);
61*e4b17023SJohn Marino   /* No tm_clone_table or no clones. */
62*e4b17023SJohn Marino   if (tmct_fixed == NULL || *tmct_siz == 0)
63*e4b17023SJohn Marino     return NULL;
64*e4b17023SJohn Marino 
65*e4b17023SJohn Marino   img_count = _dyld_image_count();
66*e4b17023SJohn Marino   for (i = 0; i < img_count && tmct == NULL; i++)
67*e4b17023SJohn Marino     {
68*e4b17023SJohn Marino       if (mh == _dyld_get_image_header(i))
69*e4b17023SJohn Marino 	tmct = tmct_fixed + (unsigned long)_dyld_get_image_vmaddr_slide(i);
70*e4b17023SJohn Marino     }
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino   return tmct;
73*e4b17023SJohn Marino }
74*e4b17023SJohn Marino #endif
75*e4b17023SJohn Marino 
76*e4b17023SJohn Marino #ifdef START
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino void __doTMRegistrations (void) __attribute__ ((constructor));
79*e4b17023SJohn Marino 
__doTMRegistrations(void)80*e4b17023SJohn Marino void __doTMRegistrations (void)
81*e4b17023SJohn Marino {
82*e4b17023SJohn Marino   size_t tmct_siz;
83*e4b17023SJohn Marino   void *tmct;
84*e4b17023SJohn Marino 
85*e4b17023SJohn Marino   tmct = getTMCloneTable ((const void *)&__doTMRegistrations, &tmct_siz);
86*e4b17023SJohn Marino   if (_ITM_registerTMCloneTable != NULL && tmct != NULL)
87*e4b17023SJohn Marino     _ITM_registerTMCloneTable (tmct, (size_t)tmct_siz);
88*e4b17023SJohn Marino }
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino #endif
91*e4b17023SJohn Marino 
92*e4b17023SJohn Marino #ifdef END
93*e4b17023SJohn Marino 
94*e4b17023SJohn Marino void __doTMdeRegistrations (void) __attribute__ ((destructor));
95*e4b17023SJohn Marino 
__doTMdeRegistrations(void)96*e4b17023SJohn Marino void __doTMdeRegistrations (void)
97*e4b17023SJohn Marino {
98*e4b17023SJohn Marino   size_t tmct_siz;
99*e4b17023SJohn Marino   void *tmct;
100*e4b17023SJohn Marino 
101*e4b17023SJohn Marino   tmct = getTMCloneTable ((const void *)&__doTMdeRegistrations, &tmct_siz);
102*e4b17023SJohn Marino   if (_ITM_deregisterTMCloneTable != NULL && tmct != NULL)
103*e4b17023SJohn Marino     _ITM_deregisterTMCloneTable (tmct);
104*e4b17023SJohn Marino }
105*e4b17023SJohn Marino 
106*e4b17023SJohn Marino /* Provide dummy functions to satisfy linkage for versions of the Darwin
107*e4b17023SJohn Marino    tool-chain that that can't handle undefined weak refs at the link stage.
108*e4b17023SJohn Marino    ??? Define these dummy functions only when !HAVE_ELF_STYLE_WEAKREF. */
109*e4b17023SJohn Marino 
110*e4b17023SJohn Marino extern void *__cxa_allocate_exception (size_t) WEAK;
111*e4b17023SJohn Marino extern void __cxa_throw (void *, void *, void *) WEAK;
112*e4b17023SJohn Marino extern void *__cxa_begin_catch (void *) WEAK;
113*e4b17023SJohn Marino extern void *__cxa_end_catch (void) WEAK;
114*e4b17023SJohn Marino extern void __cxa_tm_cleanup (void *, void *, unsigned int) WEAK;
115*e4b17023SJohn Marino 
116*e4b17023SJohn Marino extern void *_ZnwX (size_t) WEAK;
117*e4b17023SJohn Marino extern void _ZdlPv (void *) WEAK;
118*e4b17023SJohn Marino extern void *_ZnaX (size_t) WEAK;
119*e4b17023SJohn Marino extern void _ZdaPv (void *) WEAK;
120*e4b17023SJohn Marino 
121*e4b17023SJohn Marino typedef const struct nothrow_t { } *c_nothrow_p;
122*e4b17023SJohn Marino 
123*e4b17023SJohn Marino extern void *_ZnwXRKSt9nothrow_t (size_t, c_nothrow_p) WEAK;
124*e4b17023SJohn Marino extern void _ZdlPvRKSt9nothrow_t (void *, c_nothrow_p) WEAK;
125*e4b17023SJohn Marino extern void *_ZnaXRKSt9nothrow_t (size_t, c_nothrow_p) WEAK;
126*e4b17023SJohn Marino extern void _ZdaPvRKSt9nothrow_t (void *, c_nothrow_p) WEAK;
127*e4b17023SJohn Marino 
__cxa_allocate_exception(size_t s UNUSED)128*e4b17023SJohn Marino void *__cxa_allocate_exception (size_t s UNUSED) { return NULL; }
__cxa_throw(void * a UNUSED,void * b UNUSED,void * c UNUSED)129*e4b17023SJohn Marino void __cxa_throw (void * a UNUSED, void * b UNUSED, void * c UNUSED)
130*e4b17023SJohn Marino   { return; }
__cxa_begin_catch(void * a UNUSED)131*e4b17023SJohn Marino void *__cxa_begin_catch (void * a UNUSED) { return NULL; }
__cxa_end_catch(void)132*e4b17023SJohn Marino void *__cxa_end_catch (void) { return NULL; }
__cxa_tm_cleanup(void * a UNUSED,void * b UNUSED,unsigned int c UNUSED)133*e4b17023SJohn Marino void __cxa_tm_cleanup (void * a UNUSED, void * b UNUSED, unsigned int c UNUSED)
134*e4b17023SJohn Marino   { return; }
135*e4b17023SJohn Marino 
_ZnwX(size_t s UNUSED)136*e4b17023SJohn Marino void *_ZnwX (size_t s UNUSED) { return NULL; }
_ZdlPv(void * a UNUSED)137*e4b17023SJohn Marino void _ZdlPv (void * a UNUSED) { return; }
_ZnaX(size_t s UNUSED)138*e4b17023SJohn Marino void *_ZnaX (size_t s UNUSED) { return NULL; }
_ZdaPv(void * a UNUSED)139*e4b17023SJohn Marino void _ZdaPv (void * a UNUSED) { return; }
140*e4b17023SJohn Marino 
_ZnwXRKSt9nothrow_t(size_t s UNUSED,c_nothrow_p b UNUSED)141*e4b17023SJohn Marino void *_ZnwXRKSt9nothrow_t (size_t s UNUSED, c_nothrow_p b UNUSED)
142*e4b17023SJohn Marino   { return NULL; }
_ZdlPvRKSt9nothrow_t(void * a UNUSED,c_nothrow_p b UNUSED)143*e4b17023SJohn Marino void _ZdlPvRKSt9nothrow_t (void * a UNUSED, c_nothrow_p b UNUSED)  { return; }
_ZnaXRKSt9nothrow_t(size_t s UNUSED,c_nothrow_p b UNUSED)144*e4b17023SJohn Marino void *_ZnaXRKSt9nothrow_t (size_t s UNUSED, c_nothrow_p b UNUSED)
145*e4b17023SJohn Marino   { return NULL; }
_ZdaPvRKSt9nothrow_t(void * a UNUSED,c_nothrow_p b UNUSED)146*e4b17023SJohn Marino void _ZdaPvRKSt9nothrow_t (void * a UNUSED, c_nothrow_p b UNUSED) { return; }
147*e4b17023SJohn Marino 
148*e4b17023SJohn Marino #endif
149