/cp
[official-gcc.git] / libgcc / config / darwin-crt-tm.c
blob4f4d44449b46cf17ae179f2a1189c8118a245909
1 /* Provide the runtime infrastructure for the transactional memory lib.
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 Contributed by Iain Sandoe <iains@gcc.gnu.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "tsystem.h"
27 #include <stddef.h>
28 #include <dlfcn.h>
29 #include <mach-o/dyld.h>
30 #include <mach-o/getsect.h>
32 #ifdef __LP64__
33 #define GET_DATA_TMCT(mh,size) \
34 getsectdatafromheader_64 ((struct mach_header_64*) mh, \
35 "__DATA", "__tm_clone_table", (uint64_t *)size)
36 #else
37 #define GET_DATA_TMCT(mh,size) \
38 getsectdatafromheader (mh, "__DATA", "__tm_clone_table", (uint32_t *)size)
39 #endif
41 #define WEAK __attribute__((weak))
43 extern void _ITM_registerTMCloneTable (void *, size_t) WEAK;
44 extern void _ITM_deregisterTMCloneTable (void *) WEAK;
46 #if defined(START) || defined(END)
47 static inline void *getTMCloneTable (const void *f, size_t *tmct_siz)
49 char *tmct_fixed, *tmct = NULL;
50 unsigned int i, img_count;
51 struct mach_header *mh;
52 Dl_info info;
54 if (! dladdr (f, &info) || info.dli_fbase == NULL)
55 abort ();
57 mh = (struct mach_header *) info.dli_fbase;
58 tmct_fixed = GET_DATA_TMCT (mh, tmct_siz);
59 *tmct_siz /= (sizeof (size_t) * 2);
60 /* No tm_clone_table or no clones. */
61 if (tmct_fixed == NULL || *tmct_siz == 0)
62 return NULL;
64 img_count = _dyld_image_count();
65 for (i = 0; i < img_count && tmct == NULL; i++)
67 if (mh == _dyld_get_image_header(i))
68 tmct = tmct_fixed + (unsigned long)_dyld_get_image_vmaddr_slide(i);
71 return tmct;
73 #endif
75 #ifdef START
77 void __doTMRegistrations (void) __attribute__ ((constructor));
79 void __doTMRegistrations (void)
81 size_t tmct_siz;
82 void *tmct;
84 tmct = getTMCloneTable ((const void *)&__doTMRegistrations, &tmct_siz);
85 if (_ITM_registerTMCloneTable != NULL && tmct != NULL)
86 _ITM_registerTMCloneTable (tmct, (size_t)tmct_siz);
89 #endif
91 #ifdef END
93 void __doTMdeRegistrations (void) __attribute__ ((destructor));
95 void __doTMdeRegistrations (void)
97 size_t tmct_siz;
98 void *tmct;
100 tmct = getTMCloneTable ((const void *)&__doTMdeRegistrations, &tmct_siz);
101 if (_ITM_deregisterTMCloneTable != NULL && tmct != NULL)
102 _ITM_deregisterTMCloneTable (tmct);
105 #endif