2005-12-13 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / unwind-dw2-fde-darwin.c
blob0e16371f6c8abd21fc9d90280f478d883bf8c4ee
1 /* Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
3 This file is part of GCC.
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.
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.
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, 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA. */
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. */
27 /* Locate the FDE entry for a given address, using Darwin's keymgr support. */
29 #include "tconfig.h"
30 #include "tsystem.h"
31 #include <string.h>
32 #include <stdlib.h>
33 #include "dwarf2.h"
34 #include "unwind.h"
35 #define NO_BASE_OF_ENCODED_VALUE
36 #define DWARF2_OBJECT_END_PTR_EXTENSION
37 #include "unwind-pe.h"
38 #include "unwind-dw2-fde.h"
39 /* Carefully don't include gthr.h. */
41 typedef int __gthread_mutex_t;
42 #define __gthread_mutex_lock(x) (void)(x)
43 #define __gthread_mutex_unlock(x) (void)(x)
45 static const fde * _Unwind_Find_registered_FDE (void *pc,
46 struct dwarf_eh_bases *bases);
48 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
49 #include "unwind-dw2-fde.c"
50 #undef _Unwind_Find_FDE
52 /* KeyMgr stuff. */
53 #define KEYMGR_GCC3_LIVE_IMAGE_LIST 301 /* loaded images */
54 #define KEYMGR_GCC3_DW2_OBJ_LIST 302 /* Dwarf2 object list */
56 extern void *_keymgr_get_and_lock_processwide_ptr (int);
57 extern void _keymgr_set_and_unlock_processwide_ptr (int, void *);
58 extern void _keymgr_unlock_processwide_ptr (int);
60 struct mach_header;
61 struct mach_header_64;
62 extern char *getsectdatafromheader (struct mach_header*, const char*,
63 const char *, unsigned long *);
64 extern char *getsectdatafromheader_64 (struct mach_header*, const char*,
65 const char *, unsigned long *);
67 /* This is referenced from KEYMGR_GCC3_DW2_OBJ_LIST. */
68 struct km_object_info {
69 struct object *seen_objects;
70 struct object *unseen_objects;
71 unsigned spare[2];
74 /* Node of KEYMGR_GCC3_LIVE_IMAGE_LIST. Info about each resident image. */
75 struct live_images {
76 unsigned long this_size; /* sizeof (live_images) */
77 struct mach_header *mh; /* the image info */
78 unsigned long vm_slide;
79 void (*destructor)(struct live_images *); /* destructor for this */
80 struct live_images *next;
81 unsigned int examined_p;
82 void *fde;
83 void *object_info;
84 unsigned long info[2]; /* Future use. */
87 /* Bits in the examined_p field of struct live_images. */
88 enum {
89 EXAMINED_IMAGE_MASK = 1, /* We've seen this one. */
90 ALLOCED_IMAGE_MASK = 2, /* The FDE entries were allocated by
91 malloc, and must be freed. This isn't
92 used by newer libgcc versions. */
93 IMAGE_IS_TEXT_MASK = 4, /* This image is in the TEXT segment. */
94 DESTRUCTOR_MAY_BE_CALLED_LIVE = 8 /* The destructor may be called on an
95 object that's part of the live
96 image list. */
99 /* Delete any data we allocated on a live_images structure. Either
100 IMAGE has already been removed from the
101 KEYMGR_GCC3_LIVE_IMAGE_LIST and the struct will be deleted
102 after we return, or that list is locked and we're being called
103 because this object might be about to be unloaded. Called by
104 KeyMgr. */
106 static void
107 live_image_destructor (struct live_images *image)
109 if (image->object_info)
111 struct km_object_info *the_obj_info;
113 the_obj_info =
114 _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
115 if (the_obj_info)
117 seen_objects = the_obj_info->seen_objects;
118 unseen_objects = the_obj_info->unseen_objects;
120 /* Free any sorted arrays. */
121 __deregister_frame_info_bases (image->fde);
123 the_obj_info->seen_objects = seen_objects;
124 the_obj_info->unseen_objects = unseen_objects;
126 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
127 the_obj_info);
129 free (image->object_info);
130 image->object_info = NULL;
131 if (image->examined_p & ALLOCED_IMAGE_MASK)
132 free (image->fde);
133 image->fde = NULL;
135 image->examined_p = 0;
136 image->destructor = NULL;
139 /* Run through the list of live images. If we can allocate memory,
140 give each unseen image a new `struct object'. Even if we can't,
141 check whether the PC is inside the FDE of each unseen image.
144 static inline const fde *
145 examine_objects (void *pc, struct dwarf_eh_bases *bases, int dont_alloc)
147 const fde *result = NULL;
148 struct live_images *image;
150 image = _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
152 for (; image != NULL; image = image->next)
153 if ((image->examined_p & EXAMINED_IMAGE_MASK) == 0)
155 char *fde;
156 unsigned long sz;
158 #ifdef __ppc64__
159 fde = getsectdatafromheader_64 ((struct mach_header_64 *) image->mh,
160 "__DATA", "__eh_frame", &sz);
161 #else
162 fde = getsectdatafromheader (image->mh, "__DATA", "__eh_frame", &sz);
163 #endif
164 if (fde == NULL)
166 #ifdef __ppc64__
167 fde = getsectdatafromheader_64 ((struct mach_header_64 *) image->mh,
168 "__TEXT", "__eh_frame", &sz);
169 #else
170 fde = getsectdatafromheader (image->mh, "__TEXT",
171 "__eh_frame", &sz);
172 #endif
173 if (fde != NULL)
174 image->examined_p |= IMAGE_IS_TEXT_MASK;
177 /* If .eh_frame is empty, don't register at all. */
178 if (fde != NULL && sz > 0)
180 char *real_fde = (fde + image->vm_slide);
181 struct object *ob = NULL;
182 struct object panicob;
184 if (! dont_alloc)
185 ob = calloc (1, sizeof (struct object));
186 dont_alloc |= ob == NULL;
187 if (dont_alloc)
188 ob = &panicob;
190 ob->pc_begin = (void *)-1;
191 ob->tbase = 0;
192 ob->dbase = 0;
193 ob->u.single = (struct dwarf_fde *)real_fde;
194 ob->s.i = 0;
195 ob->s.b.encoding = DW_EH_PE_omit;
196 ob->fde_end = real_fde + sz;
198 image->fde = real_fde;
200 result = search_object (ob, pc);
202 if (! dont_alloc)
204 struct object **p;
206 image->destructor = live_image_destructor;
207 image->object_info = ob;
209 image->examined_p |= (EXAMINED_IMAGE_MASK
210 | DESTRUCTOR_MAY_BE_CALLED_LIVE);
212 /* Insert the object into the classified list. */
213 for (p = &seen_objects; *p ; p = &(*p)->next)
214 if ((*p)->pc_begin < ob->pc_begin)
215 break;
216 ob->next = *p;
217 *p = ob;
220 if (result)
222 int encoding;
223 _Unwind_Ptr func;
225 bases->tbase = ob->tbase;
226 bases->dbase = ob->dbase;
228 encoding = ob->s.b.encoding;
229 if (ob->s.b.mixed_encoding)
230 encoding = get_fde_encoding (result);
231 read_encoded_value_with_base (encoding,
232 base_from_object (encoding, ob),
233 result->pc_begin, &func);
234 bases->func = (void *) func;
235 break;
238 else
239 image->examined_p |= EXAMINED_IMAGE_MASK;
242 _keymgr_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
244 return result;
247 const fde *
248 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
250 struct km_object_info *the_obj_info;
251 const fde *ret = NULL;
253 the_obj_info =
254 _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
255 if (! the_obj_info)
256 the_obj_info = calloc (1, sizeof (*the_obj_info));
258 if (the_obj_info != NULL)
260 seen_objects = the_obj_info->seen_objects;
261 unseen_objects = the_obj_info->unseen_objects;
263 ret = _Unwind_Find_registered_FDE (pc, bases);
266 /* OK, didn't find it in the list of FDEs we've seen before,
267 so go through and look at the new ones. */
268 if (ret == NULL)
269 ret = examine_objects (pc, bases, the_obj_info == NULL);
271 if (the_obj_info != NULL)
273 the_obj_info->seen_objects = seen_objects;
274 the_obj_info->unseen_objects = unseen_objects;
276 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
277 the_obj_info);
278 return ret;