OSDN Git Service

gcc/fortran/
[pf3gnuchains/gcc-fork.git] / gcc / unwind-dw2-fde-darwin.c
1 /* Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
2
3    This file is part of GCC.
4
5    GCC is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    GCC is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GCC; see the file COPYING.  If not, write to
17    the Free Software Foundation, 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 /* As a special exception, if you link this library with other files,
21    some of which are compiled with GCC, to produce an executable,
22    this library does not by itself cause the resulting executable
23    to be covered by the GNU General Public License.
24    This exception does not however invalidate any other reasons why
25    the executable file might be covered by the GNU General Public License.  */
26
27 /* Locate the FDE entry for a given address, using Darwin's keymgr support.  */
28
29 #include "tconfig.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include "dwarf2.h"
33 #include "unwind.h"
34 #define NO_BASE_OF_ENCODED_VALUE
35 #define DWARF2_OBJECT_END_PTR_EXTENSION
36 #include "unwind-pe.h"
37 #include "unwind-dw2-fde.h"
38 /* Carefully don't include gthr.h.  */
39
40 typedef int __gthread_mutex_t;
41 #define __gthread_mutex_lock(x)  (void)(x)
42 #define __gthread_mutex_unlock(x) (void)(x)
43
44 static const fde * _Unwind_Find_registered_FDE (void *pc,
45                                                 struct dwarf_eh_bases *bases);
46
47 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
48 #include "unwind-dw2-fde.c"
49 #undef _Unwind_Find_FDE
50
51 /* KeyMgr stuff.  */
52 #define KEYMGR_GCC3_LIVE_IMAGE_LIST     301     /* loaded images  */
53 #define KEYMGR_GCC3_DW2_OBJ_LIST        302     /* Dwarf2 object list  */
54
55 extern void *_keymgr_get_and_lock_processwide_ptr (int);
56 extern void _keymgr_set_and_unlock_processwide_ptr (int, void *);
57 extern void _keymgr_unlock_processwide_ptr (int);
58
59 struct mach_header;
60 struct mach_header_64;
61 extern char *getsectdatafromheader (struct mach_header*, const char*,
62                         const char *, unsigned long *);
63 extern char *getsectdatafromheader_64 (struct mach_header*, const char*,
64                         const char *, unsigned long *);
65
66 /* This is referenced from KEYMGR_GCC3_DW2_OBJ_LIST.  */
67 struct km_object_info {
68   struct object *seen_objects;
69   struct object *unseen_objects;
70   unsigned spare[2];
71 };
72
73 /* Node of KEYMGR_GCC3_LIVE_IMAGE_LIST.  Info about each resident image.  */
74 struct live_images {
75   unsigned long this_size;                      /* sizeof (live_images)  */
76   struct mach_header *mh;                       /* the image info  */
77   unsigned long vm_slide;
78   void (*destructor)(struct live_images *);     /* destructor for this  */
79   struct live_images *next;
80   unsigned int examined_p;
81   void *fde;
82   void *object_info;
83   unsigned long info[2];                        /* Future use.  */
84 };
85
86 /* Bits in the examined_p field of struct live_images.  */
87 enum {
88   EXAMINED_IMAGE_MASK = 1,      /* We've seen this one.  */
89   ALLOCED_IMAGE_MASK = 2,       /* The FDE entries were allocated by
90                                    malloc, and must be freed.  This isn't
91                                    used by newer libgcc versions.  */
92   IMAGE_IS_TEXT_MASK = 4,       /* This image is in the TEXT segment.  */
93   DESTRUCTOR_MAY_BE_CALLED_LIVE = 8  /* The destructor may be called on an
94                                         object that's part of the live
95                                         image list.  */
96 };
97 \f
98 /* Delete any data we allocated on a live_images structure.  Either
99    IMAGE has already been removed from the
100    KEYMGR_GCC3_LIVE_IMAGE_LIST and the struct will be deleted
101    after we return, or that list is locked and we're being called
102    because this object might be about to be unloaded.  Called by
103    KeyMgr.  */
104
105 static void
106 live_image_destructor (struct live_images *image)
107 {
108   if (image->object_info)
109     {
110       struct km_object_info *the_obj_info;
111
112       the_obj_info =
113         _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
114       if (the_obj_info)
115         {
116           seen_objects = the_obj_info->seen_objects;
117           unseen_objects = the_obj_info->unseen_objects;
118
119           /* Free any sorted arrays.  */
120           __deregister_frame_info_bases (image->fde);
121
122           the_obj_info->seen_objects = seen_objects;
123           the_obj_info->unseen_objects = unseen_objects;
124         }
125       _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
126                                               the_obj_info);
127
128       free (image->object_info);
129       image->object_info = NULL;
130       if (image->examined_p & ALLOCED_IMAGE_MASK)
131         free (image->fde);
132       image->fde = NULL;
133     }
134   image->examined_p = 0;
135   image->destructor = NULL;
136 }
137
138 /* Run through the list of live images.  If we can allocate memory,
139    give each unseen image a new `struct object'.  Even if we can't,
140    check whether the PC is inside the FDE of each unseen image.
141  */
142
143 static inline const fde *
144 examine_objects (void *pc, struct dwarf_eh_bases *bases, int dont_alloc)
145 {
146   const fde *result = NULL;
147   struct live_images *image;
148
149   image = _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
150
151   for (; image != NULL; image = image->next)
152     if ((image->examined_p & EXAMINED_IMAGE_MASK) == 0)
153       {
154         char *fde;
155         unsigned long sz;
156
157 #ifdef __ppc64__
158         fde = getsectdatafromheader_64 ((struct mach_header_64 *) image->mh,
159                                      "__DATA", "__eh_frame", &sz);
160 #else
161         fde = getsectdatafromheader (image->mh, "__DATA", "__eh_frame", &sz);
162 #endif
163         if (fde == NULL)
164           {
165 #ifdef __ppc64__
166             fde = getsectdatafromheader_64 ((struct mach_header_64 *) image->mh,
167                                          "__TEXT", "__eh_frame", &sz);
168 #else
169             fde = getsectdatafromheader (image->mh, "__TEXT",
170                                          "__eh_frame", &sz);
171 #endif
172             if (fde != NULL)
173               image->examined_p |= IMAGE_IS_TEXT_MASK;
174           }
175
176         /* If .eh_frame is empty, don't register at all.  */
177         if (fde != NULL && sz > 0)
178           {
179             char *real_fde = (fde + image->vm_slide);
180             struct object *ob = NULL;
181             struct object panicob;
182
183             if (! dont_alloc)
184               ob = calloc (1, sizeof (struct object));
185             dont_alloc |= ob == NULL;
186             if (dont_alloc)
187               ob = &panicob;
188
189             ob->pc_begin = (void *)-1;
190             ob->tbase = 0;
191             ob->dbase = 0;
192             ob->u.single = (struct dwarf_fde *)real_fde;
193             ob->s.i = 0;
194             ob->s.b.encoding = DW_EH_PE_omit;
195             ob->fde_end = real_fde + sz;
196
197             image->fde = real_fde;
198
199             result = search_object (ob, pc);
200
201             if (! dont_alloc)
202               {
203                 struct object **p;
204
205                 image->destructor = live_image_destructor;
206                 image->object_info = ob;
207
208                 image->examined_p |= (EXAMINED_IMAGE_MASK
209                                       | DESTRUCTOR_MAY_BE_CALLED_LIVE);
210
211                 /* Insert the object into the classified list.  */
212                 for (p = &seen_objects; *p ; p = &(*p)->next)
213                   if ((*p)->pc_begin < ob->pc_begin)
214                     break;
215                 ob->next = *p;
216                 *p = ob;
217               }
218
219             if (result)
220               {
221                 int encoding;
222
223                 bases->tbase = ob->tbase;
224                 bases->dbase = ob->dbase;
225
226                 encoding = ob->s.b.encoding;
227                 if (ob->s.b.mixed_encoding)
228                   encoding = get_fde_encoding (result);
229                 read_encoded_value_with_base (encoding,
230                                               base_from_object (encoding, ob),
231                                               result->pc_begin,
232                                               (_Unwind_Ptr *)&bases->func);
233                 break;
234               }
235           }
236         else
237           image->examined_p |= EXAMINED_IMAGE_MASK;
238       }
239
240   _keymgr_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
241
242   return result;
243 }
244
245 const fde *
246 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
247 {
248   struct km_object_info *the_obj_info;
249   const fde *ret = NULL;
250
251   the_obj_info =
252     _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
253   if (! the_obj_info)
254     the_obj_info = calloc (1, sizeof (*the_obj_info));
255
256   if (the_obj_info != NULL)
257     {
258       seen_objects = the_obj_info->seen_objects;
259       unseen_objects = the_obj_info->unseen_objects;
260
261       ret = _Unwind_Find_registered_FDE (pc, bases);
262     }
263
264   /* OK, didn't find it in the list of FDEs we've seen before,
265      so go through and look at the new ones.  */
266   if (ret == NULL)
267     ret = examine_objects (pc, bases, the_obj_info == NULL);
268
269   if (the_obj_info != NULL)
270     {
271       the_obj_info->seen_objects = seen_objects;
272       the_obj_info->unseen_objects = unseen_objects;
273     }
274   _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
275                                           the_obj_info);
276   return ret;
277 }