]>
Commit | Line | Data |
---|---|---|
ea00486e | 1 | /* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. |
3cfe49da | 2 | |
54a7b573 | 3 | This file is part of GCC. |
3cfe49da | 4 | |
54a7b573 | 5 | GCC is free software; you can redistribute it and/or modify |
3cfe49da GK |
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 | ||
54a7b573 | 10 | GCC is distributed in the hope that it will be useful, |
3cfe49da GK |
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 | |
54a7b573 | 16 | along with GCC; see the file COPYING. If not, write to |
3cfe49da GK |
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" | |
335c9de7 ZW |
30 | #include <string.h> |
31 | #include <stdlib.h> | |
3cfe49da GK |
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 | ||
f1518966 AJ |
44 | static const fde * _Unwind_Find_registered_FDE (void *pc, |
45 | struct dwarf_eh_bases *bases); | |
3cfe49da GK |
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 | extern char *getsectdatafromheader (struct mach_header*, const char*, | |
61 | const char *, unsigned long *); | |
62 | ||
63 | /* This is referenced from KEYMGR_GCC3_DW2_OBJ_LIST. */ | |
64 | struct km_object_info { | |
65 | struct object *seen_objects; | |
66 | struct object *unseen_objects; | |
67 | unsigned spare[2]; | |
68 | }; | |
69 | ||
70 | /* Node of KEYMGR_GCC3_LIVE_IMAGE_LIST. Info about each resident image. */ | |
71 | struct live_images { | |
72 | unsigned long this_size; /* sizeof (live_images) */ | |
73 | struct mach_header *mh; /* the image info */ | |
74 | unsigned long vm_slide; | |
75 | void (*destructor)(struct live_images *); /* destructor for this */ | |
76 | struct live_images *next; | |
77 | unsigned int examined_p; | |
78 | void *fde; | |
79 | void *object_info; | |
80 | unsigned long info[2]; /* Future use. */ | |
81 | }; | |
82 | ||
83 | /* Bits in the examined_p field of struct live_images. */ | |
84 | enum { | |
85 | EXAMINED_IMAGE_MASK = 1, /* We've seen this one. */ | |
86 | ALLOCED_IMAGE_MASK = 2, /* The FDE entries were allocated by | |
87 | malloc, and must be freed. This isn't | |
88 | used by newer libgcc versions. */ | |
481a8181 GK |
89 | IMAGE_IS_TEXT_MASK = 4, /* This image is in the TEXT segment. */ |
90 | DESTRUCTOR_MAY_BE_CALLED_LIVE = 8 /* The destructor may be called on an | |
91 | object that's part of the live | |
92 | image list. */ | |
3cfe49da GK |
93 | }; |
94 | \f | |
481a8181 GK |
95 | /* Delete any data we allocated on a live_images structure. Either |
96 | IMAGE has already been removed from the | |
97 | KEYMGR_GCC3_LIVE_IMAGE_LIST and the struct will be deleted | |
98 | after we return, or that list is locked and we're being called | |
99 | because this object might be about to be unloaded. Called by | |
100 | KeyMgr. */ | |
3cfe49da | 101 | |
f1518966 | 102 | static void |
3cfe49da GK |
103 | live_image_destructor (struct live_images *image) |
104 | { | |
105 | if (image->object_info) | |
106 | { | |
ea00486e GK |
107 | struct km_object_info *the_obj_info; |
108 | ||
109 | the_obj_info = | |
110 | _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST); | |
111 | if (the_obj_info) | |
112 | { | |
113 | seen_objects = the_obj_info->seen_objects; | |
114 | unseen_objects = the_obj_info->unseen_objects; | |
115 | ||
116 | /* Free any sorted arrays. */ | |
117 | __deregister_frame_info_bases (image->fde); | |
118 | ||
119 | the_obj_info->seen_objects = seen_objects; | |
120 | the_obj_info->unseen_objects = unseen_objects; | |
121 | } | |
122 | _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST, | |
123 | the_obj_info); | |
3cfe49da GK |
124 | |
125 | free (image->object_info); | |
126 | image->object_info = NULL; | |
127 | if (image->examined_p & ALLOCED_IMAGE_MASK) | |
128 | free (image->fde); | |
481a8181 | 129 | image->fde = NULL; |
3cfe49da | 130 | } |
481a8181 GK |
131 | image->examined_p = 0; |
132 | image->destructor = NULL; | |
3cfe49da GK |
133 | } |
134 | ||
135 | /* Run through the list of live images. If we can allocate memory, | |
136 | give each unseen image a new `struct object'. Even if we can't, | |
137 | check whether the PC is inside the FDE of each unseen image. | |
138 | */ | |
f1518966 AJ |
139 | |
140 | static inline const fde * | |
3cfe49da GK |
141 | examine_objects (void *pc, struct dwarf_eh_bases *bases, int dont_alloc) |
142 | { | |
f1518966 | 143 | const fde *result = NULL; |
3cfe49da GK |
144 | struct live_images *image; |
145 | ||
146 | image = _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST); | |
147 | ||
148 | for (; image != NULL; image = image->next) | |
149 | if ((image->examined_p & EXAMINED_IMAGE_MASK) == 0) | |
150 | { | |
151 | char *fde; | |
152 | unsigned long sz; | |
f1518966 | 153 | |
3cfe49da GK |
154 | fde = getsectdatafromheader (image->mh, "__DATA", "__eh_frame", &sz); |
155 | if (fde == NULL) | |
156 | { | |
157 | fde = getsectdatafromheader (image->mh, "__TEXT", | |
158 | "__eh_frame", &sz); | |
159 | if (fde != NULL) | |
160 | image->examined_p |= IMAGE_IS_TEXT_MASK; | |
161 | } | |
f1518966 | 162 | |
3cfe49da GK |
163 | /* If .eh_frame is empty, don't register at all. */ |
164 | if (fde != NULL && sz > 0) | |
165 | { | |
166 | char *real_fde = (fde + image->vm_slide); | |
167 | struct object *ob = NULL; | |
168 | struct object panicob; | |
f1518966 | 169 | |
3cfe49da GK |
170 | if (! dont_alloc) |
171 | ob = calloc (1, sizeof (struct object)); | |
172 | dont_alloc |= ob == NULL; | |
173 | if (dont_alloc) | |
174 | ob = &panicob; | |
f1518966 | 175 | |
3cfe49da GK |
176 | ob->pc_begin = (void *)-1; |
177 | ob->tbase = 0; | |
178 | ob->dbase = 0; | |
179 | ob->u.single = (struct dwarf_fde *)real_fde; | |
180 | ob->s.i = 0; | |
181 | ob->s.b.encoding = DW_EH_PE_omit; | |
182 | ob->fde_end = real_fde + sz; | |
f1518966 | 183 | |
ea00486e | 184 | image->fde = real_fde; |
f1518966 | 185 | |
ea00486e | 186 | result = search_object (ob, pc); |
f1518966 | 187 | |
3cfe49da GK |
188 | if (! dont_alloc) |
189 | { | |
ea00486e GK |
190 | struct object **p; |
191 | ||
3cfe49da GK |
192 | image->destructor = live_image_destructor; |
193 | image->object_info = ob; | |
f1518966 AJ |
194 | |
195 | image->examined_p |= (EXAMINED_IMAGE_MASK | |
481a8181 | 196 | | DESTRUCTOR_MAY_BE_CALLED_LIVE); |
ea00486e GK |
197 | |
198 | /* Insert the object into the classified list. */ | |
199 | for (p = &seen_objects; *p ; p = &(*p)->next) | |
200 | if ((*p)->pc_begin < ob->pc_begin) | |
201 | break; | |
202 | ob->next = *p; | |
203 | *p = ob; | |
3cfe49da | 204 | } |
ea00486e | 205 | |
3cfe49da GK |
206 | if (result) |
207 | { | |
208 | int encoding; | |
f1518966 | 209 | |
3cfe49da GK |
210 | bases->tbase = ob->tbase; |
211 | bases->dbase = ob->dbase; | |
f1518966 | 212 | |
3cfe49da GK |
213 | encoding = ob->s.b.encoding; |
214 | if (ob->s.b.mixed_encoding) | |
215 | encoding = get_fde_encoding (result); | |
f1518966 | 216 | read_encoded_value_with_base (encoding, |
3cfe49da | 217 | base_from_object (encoding, ob), |
f1518966 | 218 | result->pc_begin, |
3cfe49da GK |
219 | (_Unwind_Ptr *)&bases->func); |
220 | break; | |
221 | } | |
222 | } | |
223 | else | |
224 | image->examined_p |= EXAMINED_IMAGE_MASK; | |
225 | } | |
226 | ||
227 | _keymgr_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST); | |
228 | ||
229 | return result; | |
230 | } | |
231 | ||
f1518966 | 232 | const fde * |
3cfe49da GK |
233 | _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases) |
234 | { | |
235 | struct km_object_info *the_obj_info; | |
f1518966 | 236 | const fde *ret = NULL; |
3cfe49da | 237 | |
f1518966 | 238 | the_obj_info = |
3cfe49da GK |
239 | _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST); |
240 | if (! the_obj_info) | |
241 | the_obj_info = calloc (1, sizeof (*the_obj_info)); | |
f1518966 | 242 | |
3cfe49da GK |
243 | if (the_obj_info != NULL) |
244 | { | |
245 | seen_objects = the_obj_info->seen_objects; | |
246 | unseen_objects = the_obj_info->unseen_objects; | |
f1518966 | 247 | |
3cfe49da GK |
248 | ret = _Unwind_Find_registered_FDE (pc, bases); |
249 | } | |
f1518966 | 250 | |
3cfe49da GK |
251 | /* OK, didn't find it in the list of FDEs we've seen before, |
252 | so go through and look at the new ones. */ | |
253 | if (ret == NULL) | |
254 | ret = examine_objects (pc, bases, the_obj_info == NULL); | |
255 | ||
256 | if (the_obj_info != NULL) | |
257 | { | |
258 | the_obj_info->seen_objects = seen_objects; | |
259 | the_obj_info->unseen_objects = unseen_objects; | |
3cfe49da | 260 | } |
ea00486e GK |
261 | _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST, |
262 | the_obj_info); | |
3cfe49da GK |
263 | return ret; |
264 | } |