]>
Commit | Line | Data |
---|---|---|
0021b564 | 1 | /* Subroutines needed for unwinding stack frames for exception handling. */ |
3cfe49da | 2 | /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
0021b564 JM |
3 | Contributed by Jason Merrill <jason@cygnus.com>. |
4 | ||
1322177d | 5 | This file is part of GCC. |
0021b564 | 6 | |
1322177d LB |
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 2, or (at your option) any later | |
10 | version. | |
0021b564 | 11 | |
f7af368f JL |
12 | In addition to the permissions in the GNU General Public License, the |
13 | Free Software Foundation gives you unlimited permission to link the | |
14 | compiled version of this file into combinations with other programs, | |
15 | and to distribute those combinations without any restriction coming | |
16 | from the use of this file. (The General Public License restrictions | |
17 | do apply in other respects; for example, they cover modification of | |
18 | the file, and distribution when not linked into a combine | |
19 | executable.) | |
20 | ||
1322177d LB |
21 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
22 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
24 | for more details. | |
0021b564 JM |
25 | |
26 | You should have received a copy of the GNU General Public License | |
1322177d LB |
27 | along with GCC; see the file COPYING. If not, write to the Free |
28 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
29 | 02111-1307, USA. */ | |
0021b564 | 30 | |
275b60d6 | 31 | #ifndef _Unwind_Find_FDE |
52a11cbf RH |
32 | #include "tconfig.h" |
33 | #include "tsystem.h" | |
4977bab6 ZW |
34 | #include "coretypes.h" |
35 | #include "tm.h" | |
e1f9550a RH |
36 | #include "dwarf2.h" |
37 | #include "unwind.h" | |
bda33a6e | 38 | #define NO_BASE_OF_ENCODED_VALUE |
e1f9550a | 39 | #include "unwind-pe.h" |
52a11cbf RH |
40 | #include "unwind-dw2-fde.h" |
41 | #include "gthr.h" | |
275b60d6 | 42 | #endif |
52a11cbf | 43 | |
e1f9550a RH |
44 | /* The unseen_objects list contains objects that have been registered |
45 | but not yet categorized in any way. The seen_objects list has had | |
46 | it's pc_begin and count fields initialized at minimum, and is sorted | |
47 | by decreasing value of pc_begin. */ | |
48 | static struct object *unseen_objects; | |
49 | static struct object *seen_objects; | |
52a11cbf RH |
50 | |
51 | #ifdef __GTHREAD_MUTEX_INIT | |
52 | static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT; | |
53 | #else | |
54 | static __gthread_mutex_t object_mutex; | |
55 | #endif | |
56 | ||
57 | #ifdef __GTHREAD_MUTEX_INIT_FUNCTION | |
41077ce4 | 58 | static void |
52a11cbf RH |
59 | init_object_mutex (void) |
60 | { | |
61 | __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex); | |
62 | } | |
63 | ||
64 | static void | |
65 | init_object_mutex_once (void) | |
66 | { | |
67 | static __gthread_once_t once = __GTHREAD_ONCE_INIT; | |
68 | __gthread_once (&once, init_object_mutex); | |
69 | } | |
70 | #else | |
71 | #define init_object_mutex_once() | |
72 | #endif | |
73 | ||
74 | /* Called from crtbegin.o to register the unwind info for an object. */ | |
75 | ||
76 | void | |
e1f9550a RH |
77 | __register_frame_info_bases (void *begin, struct object *ob, |
78 | void *tbase, void *dbase) | |
52a11cbf | 79 | { |
55fae1a9 | 80 | /* If .eh_frame is empty, don't register at all. */ |
7008a11b | 81 | if ((uword *) begin == 0 || *(uword *) begin == 0) |
55fae1a9 JJ |
82 | return; |
83 | ||
e1f9550a RH |
84 | ob->pc_begin = (void *)-1; |
85 | ob->tbase = tbase; | |
86 | ob->dbase = dbase; | |
87 | ob->u.single = begin; | |
88 | ob->s.i = 0; | |
89 | ob->s.b.encoding = DW_EH_PE_omit; | |
3cfe49da GK |
90 | #ifdef DWARF2_OBJECT_END_PTR_EXTENSION |
91 | ob->fde_end = NULL; | |
92 | #endif | |
52a11cbf RH |
93 | |
94 | init_object_mutex_once (); | |
95 | __gthread_mutex_lock (&object_mutex); | |
96 | ||
e1f9550a RH |
97 | ob->next = unseen_objects; |
98 | unseen_objects = ob; | |
52a11cbf RH |
99 | |
100 | __gthread_mutex_unlock (&object_mutex); | |
101 | } | |
102 | ||
e1f9550a RH |
103 | void |
104 | __register_frame_info (void *begin, struct object *ob) | |
105 | { | |
106 | __register_frame_info_bases (begin, ob, 0, 0); | |
107 | } | |
108 | ||
52a11cbf RH |
109 | void |
110 | __register_frame (void *begin) | |
111 | { | |
55fae1a9 JJ |
112 | struct object *ob; |
113 | ||
114 | /* If .eh_frame is empty, don't register at all. */ | |
e9d1b155 | 115 | if (*(uword *) begin == 0) |
55fae1a9 JJ |
116 | return; |
117 | ||
703ad42b | 118 | ob = malloc (sizeof (struct object)); |
41077ce4 | 119 | __register_frame_info (begin, ob); |
52a11cbf RH |
120 | } |
121 | ||
122 | /* Similar, but BEGIN is actually a pointer to a table of unwind entries | |
123 | for different translation units. Called from the file generated by | |
124 | collect2. */ | |
125 | ||
126 | void | |
e1f9550a RH |
127 | __register_frame_info_table_bases (void *begin, struct object *ob, |
128 | void *tbase, void *dbase) | |
52a11cbf | 129 | { |
e1f9550a RH |
130 | ob->pc_begin = (void *)-1; |
131 | ob->tbase = tbase; | |
132 | ob->dbase = dbase; | |
133 | ob->u.array = begin; | |
134 | ob->s.i = 0; | |
135 | ob->s.b.from_array = 1; | |
136 | ob->s.b.encoding = DW_EH_PE_omit; | |
52a11cbf RH |
137 | |
138 | init_object_mutex_once (); | |
139 | __gthread_mutex_lock (&object_mutex); | |
140 | ||
e1f9550a RH |
141 | ob->next = unseen_objects; |
142 | unseen_objects = ob; | |
52a11cbf RH |
143 | |
144 | __gthread_mutex_unlock (&object_mutex); | |
145 | } | |
146 | ||
e1f9550a RH |
147 | void |
148 | __register_frame_info_table (void *begin, struct object *ob) | |
149 | { | |
150 | __register_frame_info_table_bases (begin, ob, 0, 0); | |
151 | } | |
152 | ||
52a11cbf RH |
153 | void |
154 | __register_frame_table (void *begin) | |
155 | { | |
703ad42b | 156 | struct object *ob = malloc (sizeof (struct object)); |
52a11cbf RH |
157 | __register_frame_info_table (begin, ob); |
158 | } | |
159 | ||
160 | /* Called from crtbegin.o to deregister the unwind info for an object. */ | |
101fa48c RH |
161 | /* ??? Glibc has for a while now exported __register_frame_info and |
162 | __deregister_frame_info. If we call __register_frame_info_bases | |
163 | from crtbegin (wherein it is declared weak), and this object does | |
164 | not get pulled from libgcc.a for other reasons, then the | |
165 | invocation of __deregister_frame_info will be resolved from glibc. | |
166 | Since the registration did not happen there, we'll abort. | |
167 | ||
168 | Therefore, declare a new deregistration entry point that does the | |
41077ce4 | 169 | exact same thing, but will resolve to the same library as |
101fa48c | 170 | implements __register_frame_info_bases. */ |
52a11cbf RH |
171 | |
172 | void * | |
101fa48c | 173 | __deregister_frame_info_bases (void *begin) |
52a11cbf RH |
174 | { |
175 | struct object **p; | |
e1f9550a | 176 | struct object *ob = 0; |
52a11cbf | 177 | |
55fae1a9 | 178 | /* If .eh_frame is empty, we haven't registered. */ |
7008a11b | 179 | if ((uword *) begin == 0 || *(uword *) begin == 0) |
5d393c8e | 180 | return ob; |
55fae1a9 | 181 | |
52a11cbf RH |
182 | init_object_mutex_once (); |
183 | __gthread_mutex_lock (&object_mutex); | |
184 | ||
e1f9550a RH |
185 | for (p = &unseen_objects; *p ; p = &(*p)->next) |
186 | if ((*p)->u.single == begin) | |
187 | { | |
188 | ob = *p; | |
189 | *p = ob->next; | |
190 | goto out; | |
191 | } | |
192 | ||
193 | for (p = &seen_objects; *p ; p = &(*p)->next) | |
194 | if ((*p)->s.b.sorted) | |
195 | { | |
196 | if ((*p)->u.sort->orig_data == begin) | |
197 | { | |
198 | ob = *p; | |
199 | *p = ob->next; | |
200 | free (ob->u.sort); | |
201 | goto out; | |
202 | } | |
203 | } | |
204 | else | |
205 | { | |
206 | if ((*p)->u.single == begin) | |
207 | { | |
208 | ob = *p; | |
209 | *p = ob->next; | |
210 | goto out; | |
211 | } | |
212 | } | |
52a11cbf RH |
213 | |
214 | __gthread_mutex_unlock (&object_mutex); | |
215 | abort (); | |
e1f9550a RH |
216 | |
217 | out: | |
218 | __gthread_mutex_unlock (&object_mutex); | |
219 | return (void *) ob; | |
52a11cbf RH |
220 | } |
221 | ||
101fa48c RH |
222 | void * |
223 | __deregister_frame_info (void *begin) | |
224 | { | |
225 | return __deregister_frame_info_bases (begin); | |
226 | } | |
101fa48c | 227 | |
52a11cbf RH |
228 | void |
229 | __deregister_frame (void *begin) | |
230 | { | |
55fae1a9 | 231 | /* If .eh_frame is empty, we haven't registered. */ |
e9d1b155 | 232 | if (*(uword *) begin != 0) |
55fae1a9 | 233 | free (__deregister_frame_info (begin)); |
52a11cbf RH |
234 | } |
235 | ||
e1f9550a RH |
236 | \f |
237 | /* Like base_of_encoded_value, but take the base from a struct object | |
238 | instead of an _Unwind_Context. */ | |
239 | ||
240 | static _Unwind_Ptr | |
241 | base_from_object (unsigned char encoding, struct object *ob) | |
242 | { | |
243 | if (encoding == DW_EH_PE_omit) | |
244 | return 0; | |
245 | ||
246 | switch (encoding & 0x70) | |
247 | { | |
248 | case DW_EH_PE_absptr: | |
249 | case DW_EH_PE_pcrel: | |
099c8b17 | 250 | case DW_EH_PE_aligned: |
e1f9550a RH |
251 | return 0; |
252 | ||
253 | case DW_EH_PE_textrel: | |
254 | return (_Unwind_Ptr) ob->tbase; | |
255 | case DW_EH_PE_datarel: | |
256 | return (_Unwind_Ptr) ob->dbase; | |
257 | } | |
258 | abort (); | |
259 | } | |
260 | ||
261 | /* Return the FDE pointer encoding from the CIE. */ | |
262 | /* ??? This is a subset of extract_cie_info from unwind-dw2.c. */ | |
263 | ||
264 | static int | |
265 | get_cie_encoding (struct dwarf_cie *cie) | |
266 | { | |
267 | const unsigned char *aug, *p; | |
268 | _Unwind_Ptr dummy; | |
a9985a92 JM |
269 | _Unwind_Word utmp; |
270 | _Unwind_Sword stmp; | |
e1f9550a RH |
271 | |
272 | aug = cie->augmentation; | |
273 | if (aug[0] != 'z') | |
274 | return DW_EH_PE_absptr; | |
275 | ||
276 | p = aug + strlen (aug) + 1; /* Skip the augmentation string. */ | |
a9985a92 JM |
277 | p = read_uleb128 (p, &utmp); /* Skip code alignment. */ |
278 | p = read_sleb128 (p, &stmp); /* Skip data alignment. */ | |
e1f9550a RH |
279 | p++; /* Skip return address column. */ |
280 | ||
281 | aug++; /* Skip 'z' */ | |
a9985a92 | 282 | p = read_uleb128 (p, &utmp); /* Skip augmentation length. */ |
e1f9550a RH |
283 | while (1) |
284 | { | |
285 | /* This is what we're looking for. */ | |
286 | if (*aug == 'R') | |
287 | return *p; | |
288 | /* Personality encoding and pointer. */ | |
289 | else if (*aug == 'P') | |
099c8b17 RH |
290 | { |
291 | /* ??? Avoid dereferencing indirect pointers, since we're | |
292 | faking the base address. Gotta keep DW_EH_PE_aligned | |
293 | intact, however. */ | |
294 | p = read_encoded_value_with_base (*p & 0x7F, 0, p + 1, &dummy); | |
295 | } | |
e1f9550a RH |
296 | /* LSDA encoding. */ |
297 | else if (*aug == 'L') | |
298 | p++; | |
299 | /* Otherwise end of string, or unknown augmentation. */ | |
300 | else | |
301 | return DW_EH_PE_absptr; | |
302 | aug++; | |
303 | } | |
304 | } | |
305 | ||
306 | static inline int | |
307 | get_fde_encoding (struct dwarf_fde *f) | |
308 | { | |
309 | return get_cie_encoding (get_cie (f)); | |
310 | } | |
311 | ||
52a11cbf | 312 | \f |
72dd050a BH |
313 | /* Sorting an array of FDEs by address. |
314 | (Ideally we would have the linker sort the FDEs so we don't have to do | |
315 | it at run time. But the linkers are not yet prepared for this.) */ | |
316 | ||
e1f9550a RH |
317 | /* Comparison routines. Three variants of increasing complexity. */ |
318 | ||
bde257ff | 319 | static int |
e1f9550a RH |
320 | fde_unencoded_compare (struct object *ob __attribute__((unused)), |
321 | fde *x, fde *y) | |
322 | { | |
2f9ec5e5 HPN |
323 | _Unwind_Ptr x_ptr = *(_Unwind_Ptr *) x->pc_begin; |
324 | _Unwind_Ptr y_ptr = *(_Unwind_Ptr *) y->pc_begin; | |
325 | ||
326 | if (x_ptr > y_ptr) | |
bde257ff | 327 | return 1; |
2f9ec5e5 | 328 | if (x_ptr < y_ptr) |
bde257ff RH |
329 | return -1; |
330 | return 0; | |
e1f9550a RH |
331 | } |
332 | ||
bde257ff | 333 | static int |
e1f9550a RH |
334 | fde_single_encoding_compare (struct object *ob, fde *x, fde *y) |
335 | { | |
336 | _Unwind_Ptr base, x_ptr, y_ptr; | |
337 | ||
338 | base = base_from_object (ob->s.b.encoding, ob); | |
339 | read_encoded_value_with_base (ob->s.b.encoding, base, x->pc_begin, &x_ptr); | |
340 | read_encoded_value_with_base (ob->s.b.encoding, base, y->pc_begin, &y_ptr); | |
341 | ||
bde257ff RH |
342 | if (x_ptr > y_ptr) |
343 | return 1; | |
344 | if (x_ptr < y_ptr) | |
345 | return -1; | |
346 | return 0; | |
e1f9550a RH |
347 | } |
348 | ||
bde257ff | 349 | static int |
e1f9550a RH |
350 | fde_mixed_encoding_compare (struct object *ob, fde *x, fde *y) |
351 | { | |
352 | int x_encoding, y_encoding; | |
353 | _Unwind_Ptr x_ptr, y_ptr; | |
354 | ||
355 | x_encoding = get_fde_encoding (x); | |
356 | read_encoded_value_with_base (x_encoding, base_from_object (x_encoding, ob), | |
357 | x->pc_begin, &x_ptr); | |
358 | ||
359 | y_encoding = get_fde_encoding (y); | |
360 | read_encoded_value_with_base (y_encoding, base_from_object (y_encoding, ob), | |
361 | y->pc_begin, &y_ptr); | |
362 | ||
bde257ff RH |
363 | if (x_ptr > y_ptr) |
364 | return 1; | |
365 | if (x_ptr < y_ptr) | |
366 | return -1; | |
367 | return 0; | |
e1f9550a RH |
368 | } |
369 | ||
bde257ff | 370 | typedef int (*fde_compare_t) (struct object *, fde *, fde *); |
e1f9550a RH |
371 | |
372 | ||
72dd050a BH |
373 | /* This is a special mix of insertion sort and heap sort, optimized for |
374 | the data sets that actually occur. They look like | |
375 | 101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130. | |
376 | I.e. a linearly increasing sequence (coming from functions in the text | |
377 | section), with additionally a few unordered elements (coming from functions | |
378 | in gnu_linkonce sections) whose values are higher than the values in the | |
379 | surrounding linear sequence (but not necessarily higher than the values | |
380 | at the end of the linear sequence!). | |
381 | The worst-case total run time is O(N) + O(n log (n)), where N is the | |
382 | total number of FDEs and n is the number of erratic ones. */ | |
383 | ||
e1f9550a | 384 | struct fde_accumulator |
72dd050a | 385 | { |
e1f9550a RH |
386 | struct fde_vector *linear; |
387 | struct fde_vector *erratic; | |
388 | }; | |
52a11cbf | 389 | |
d593d1d2 | 390 | static inline int |
e1f9550a | 391 | start_fde_sort (struct fde_accumulator *accu, size_t count) |
72dd050a | 392 | { |
e1f9550a RH |
393 | size_t size; |
394 | if (! count) | |
395 | return 0; | |
396 | ||
397 | size = sizeof (struct fde_vector) + sizeof (fde *) * count; | |
703ad42b | 398 | if ((accu->linear = malloc (size))) |
e1f9550a RH |
399 | { |
400 | accu->linear->count = 0; | |
703ad42b | 401 | if ((accu->erratic = malloc (size))) |
e1f9550a RH |
402 | accu->erratic->count = 0; |
403 | return 1; | |
404 | } | |
405 | else | |
41077ce4 | 406 | return 0; |
72dd050a | 407 | } |
0021b564 | 408 | |
72dd050a | 409 | static inline void |
e1f9550a | 410 | fde_insert (struct fde_accumulator *accu, fde *this_fde) |
72dd050a | 411 | { |
e1f9550a RH |
412 | if (accu->linear) |
413 | accu->linear->array[accu->linear->count++] = this_fde; | |
72dd050a BH |
414 | } |
415 | ||
416 | /* Split LINEAR into a linear sequence with low values and an erratic | |
417 | sequence with high values, put the linear one (of longest possible | |
ba540394 | 418 | length) into LINEAR and the erratic one into ERRATIC. This is O(N). |
41077ce4 | 419 | |
ba540394 NS |
420 | Because the longest linear sequence we are trying to locate within the |
421 | incoming LINEAR array can be interspersed with (high valued) erratic | |
422 | entries. We construct a chain indicating the sequenced entries. | |
423 | To avoid having to allocate this chain, we overlay it onto the space of | |
424 | the ERRATIC array during construction. A final pass iterates over the | |
425 | chain to determine what should be placed in the ERRATIC array, and | |
426 | what is the linear sequence. This overlay is safe from aliasing. */ | |
e1f9550a | 427 | |
72dd050a | 428 | static inline void |
e1f9550a RH |
429 | fde_split (struct object *ob, fde_compare_t fde_compare, |
430 | struct fde_vector *linear, struct fde_vector *erratic) | |
72dd050a | 431 | { |
ba540394 | 432 | static fde *marker; |
72dd050a | 433 | size_t count = linear->count; |
ba540394 NS |
434 | fde **chain_end = ▮ |
435 | size_t i, j, k; | |
436 | ||
437 | /* This should optimize out, but it is wise to make sure this assumption | |
438 | is correct. Should these have different sizes, we cannot cast between | |
439 | them and the overlaying onto ERRATIC will not work. */ | |
440 | if (sizeof (fde *) != sizeof (fde **)) | |
441 | abort (); | |
41077ce4 | 442 | |
72dd050a BH |
443 | for (i = 0; i < count; i++) |
444 | { | |
ba540394 | 445 | fde **probe; |
41077ce4 | 446 | |
ba540394 | 447 | for (probe = chain_end; |
41077ce4 KH |
448 | probe != &marker && fde_compare (ob, linear->array[i], *probe) < 0; |
449 | probe = chain_end) | |
450 | { | |
451 | chain_end = (fde **) erratic->array[probe - linear->array]; | |
452 | erratic->array[probe - linear->array] = NULL; | |
453 | } | |
e9d1b155 | 454 | erratic->array[i] = (fde *) chain_end; |
ba540394 | 455 | chain_end = &linear->array[i]; |
72dd050a BH |
456 | } |
457 | ||
ba540394 NS |
458 | /* Each entry in LINEAR which is part of the linear sequence we have |
459 | discovered will correspond to a non-NULL entry in the chain we built in | |
460 | the ERRATIC array. */ | |
461 | for (i = j = k = 0; i < count; i++) | |
462 | if (erratic->array[i]) | |
72dd050a | 463 | linear->array[j++] = linear->array[i]; |
ba540394 NS |
464 | else |
465 | erratic->array[k++] = linear->array[i]; | |
72dd050a | 466 | linear->count = j; |
ba540394 | 467 | erratic->count = k; |
72dd050a BH |
468 | } |
469 | ||
80d83b16 JM |
470 | #define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0) |
471 | ||
472 | /* Convert a semi-heap to a heap. A semi-heap is a heap except possibly | |
473 | for the first (root) node; push it down to its rightful place. */ | |
474 | ||
475 | static void | |
476 | frame_downheap (struct object *ob, fde_compare_t fde_compare, fde **a, | |
477 | int lo, int hi) | |
478 | { | |
479 | int i, j; | |
480 | ||
481 | for (i = lo, j = 2*i+1; | |
482 | j < hi; | |
483 | j = 2*i+1) | |
484 | { | |
485 | if (j+1 < hi && fde_compare (ob, a[j], a[j+1]) < 0) | |
486 | ++j; | |
487 | ||
488 | if (fde_compare (ob, a[i], a[j]) < 0) | |
489 | { | |
490 | SWAP (a[i], a[j]); | |
491 | i = j; | |
492 | } | |
493 | else | |
494 | break; | |
495 | } | |
496 | } | |
497 | ||
99b13ed3 JW |
498 | /* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must |
499 | use a name that does not conflict. */ | |
e1f9550a RH |
500 | |
501 | static void | |
502 | frame_heapsort (struct object *ob, fde_compare_t fde_compare, | |
503 | struct fde_vector *erratic) | |
72dd050a BH |
504 | { |
505 | /* For a description of this algorithm, see: | |
506 | Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed., | |
2d76cb1a | 507 | p. 60-61. */ |
72dd050a BH |
508 | fde ** a = erratic->array; |
509 | /* A portion of the array is called a "heap" if for all i>=0: | |
510 | If i and 2i+1 are valid indices, then a[i] >= a[2i+1]. | |
2d76cb1a | 511 | If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */ |
72dd050a | 512 | size_t n = erratic->count; |
80d83b16 JM |
513 | int m; |
514 | ||
515 | /* Expand our heap incrementally from the end of the array, heapifying | |
516 | each resulting semi-heap as we go. After each step, a[m] is the top | |
517 | of a heap. */ | |
518 | for (m = n/2-1; m >= 0; --m) | |
519 | frame_downheap (ob, fde_compare, a, m, n); | |
520 | ||
521 | /* Shrink our heap incrementally from the end of the array, first | |
522 | swapping out the largest element a[0] and then re-heapifying the | |
523 | resulting semi-heap. After each step, a[0..m) is a heap. */ | |
524 | for (m = n-1; m >= 1; --m) | |
72dd050a | 525 | { |
80d83b16 JM |
526 | SWAP (a[0], a[m]); |
527 | frame_downheap (ob, fde_compare, a, 0, m); | |
72dd050a BH |
528 | } |
529 | #undef SWAP | |
530 | } | |
531 | ||
2d76cb1a | 532 | /* Merge V1 and V2, both sorted, and put the result into V1. */ |
e1f9550a RH |
533 | static inline void |
534 | fde_merge (struct object *ob, fde_compare_t fde_compare, | |
535 | struct fde_vector *v1, struct fde_vector *v2) | |
0021b564 | 536 | { |
72dd050a BH |
537 | size_t i1, i2; |
538 | fde * fde2; | |
0021b564 | 539 | |
72dd050a BH |
540 | i2 = v2->count; |
541 | if (i2 > 0) | |
0021b564 | 542 | { |
72dd050a | 543 | i1 = v1->count; |
a01da83b KH |
544 | do |
545 | { | |
546 | i2--; | |
547 | fde2 = v2->array[i2]; | |
548 | while (i1 > 0 && fde_compare (ob, v1->array[i1-1], fde2) > 0) | |
549 | { | |
550 | v1->array[i1+i2] = v1->array[i1-1]; | |
551 | i1--; | |
552 | } | |
41077ce4 | 553 | v1->array[i1+i2] = fde2; |
a01da83b KH |
554 | } |
555 | while (i2 > 0); | |
72dd050a | 556 | v1->count += v2->count; |
0021b564 JM |
557 | } |
558 | } | |
559 | ||
e1f9550a RH |
560 | static inline void |
561 | end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count) | |
72dd050a | 562 | { |
e1f9550a RH |
563 | fde_compare_t fde_compare; |
564 | ||
565 | if (accu->linear && accu->linear->count != count) | |
72dd050a | 566 | abort (); |
e1f9550a RH |
567 | |
568 | if (ob->s.b.mixed_encoding) | |
569 | fde_compare = fde_mixed_encoding_compare; | |
570 | else if (ob->s.b.encoding == DW_EH_PE_absptr) | |
571 | fde_compare = fde_unencoded_compare; | |
572 | else | |
573 | fde_compare = fde_single_encoding_compare; | |
574 | ||
575 | if (accu->erratic) | |
d593d1d2 | 576 | { |
e1f9550a RH |
577 | fde_split (ob, fde_compare, accu->linear, accu->erratic); |
578 | if (accu->linear->count + accu->erratic->count != count) | |
d593d1d2 | 579 | abort (); |
e1f9550a RH |
580 | frame_heapsort (ob, fde_compare, accu->erratic); |
581 | fde_merge (ob, fde_compare, accu->linear, accu->erratic); | |
582 | free (accu->erratic); | |
d593d1d2 NS |
583 | } |
584 | else | |
585 | { | |
e1f9550a RH |
586 | /* We've not managed to malloc an erratic array, |
587 | so heap sort in the linear one. */ | |
588 | frame_heapsort (ob, fde_compare, accu->linear); | |
d593d1d2 | 589 | } |
72dd050a BH |
590 | } |
591 | ||
52a11cbf | 592 | \f |
41077ce4 | 593 | /* Update encoding, mixed_encoding, and pc_begin for OB for the |
e1f9550a RH |
594 | fde array beginning at THIS_FDE. Return the number of fdes |
595 | encountered along the way. */ | |
596 | ||
52a11cbf | 597 | static size_t |
e1f9550a | 598 | classify_object_over_fdes (struct object *ob, fde *this_fde) |
52a11cbf | 599 | { |
e1f9550a RH |
600 | struct dwarf_cie *last_cie = 0; |
601 | size_t count = 0; | |
602 | int encoding = DW_EH_PE_absptr; | |
603 | _Unwind_Ptr base = 0; | |
0021b564 | 604 | |
3cfe49da | 605 | for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde)) |
e1f9550a RH |
606 | { |
607 | struct dwarf_cie *this_cie; | |
608 | _Unwind_Ptr mask, pc_begin; | |
52a11cbf | 609 | |
e1f9550a RH |
610 | /* Skip CIEs. */ |
611 | if (this_fde->CIE_delta == 0) | |
612 | continue; | |
52a11cbf | 613 | |
e1f9550a RH |
614 | /* Determine the encoding for this FDE. Note mixed encoded |
615 | objects for later. */ | |
616 | this_cie = get_cie (this_fde); | |
617 | if (this_cie != last_cie) | |
618 | { | |
619 | last_cie = this_cie; | |
620 | encoding = get_cie_encoding (this_cie); | |
621 | base = base_from_object (encoding, ob); | |
622 | if (ob->s.b.encoding == DW_EH_PE_omit) | |
623 | ob->s.b.encoding = encoding; | |
624 | else if (ob->s.b.encoding != encoding) | |
625 | ob->s.b.mixed_encoding = 1; | |
626 | } | |
0021b564 | 627 | |
e1f9550a RH |
628 | read_encoded_value_with_base (encoding, base, this_fde->pc_begin, |
629 | &pc_begin); | |
0021b564 | 630 | |
e1f9550a RH |
631 | /* Take care to ignore link-once functions that were removed. |
632 | In these cases, the function address will be NULL, but if | |
633 | the encoding is smaller than a pointer a true NULL may not | |
634 | be representable. Assume 0 in the representable bits is NULL. */ | |
635 | mask = size_of_encoded_value (encoding); | |
636 | if (mask < sizeof (void *)) | |
637 | mask = (1L << (mask << 3)) - 1; | |
638 | else | |
639 | mask = -1; | |
640 | ||
641 | if ((pc_begin & mask) == 0) | |
642 | continue; | |
154bba13 | 643 | |
e1f9550a | 644 | count += 1; |
e9d1b155 KH |
645 | if ((void *) pc_begin < ob->pc_begin) |
646 | ob->pc_begin = (void *) pc_begin; | |
52a11cbf | 647 | } |
154bba13 | 648 | |
e1f9550a | 649 | return count; |
0021b564 JM |
650 | } |
651 | ||
e1f9550a RH |
652 | static void |
653 | add_fdes (struct object *ob, struct fde_accumulator *accu, fde *this_fde) | |
a3fd4e75 | 654 | { |
e1f9550a RH |
655 | struct dwarf_cie *last_cie = 0; |
656 | int encoding = ob->s.b.encoding; | |
657 | _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob); | |
658 | ||
3cfe49da | 659 | for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde)) |
52a11cbf | 660 | { |
e1f9550a | 661 | struct dwarf_cie *this_cie; |
52a11cbf | 662 | |
e1f9550a RH |
663 | /* Skip CIEs. */ |
664 | if (this_fde->CIE_delta == 0) | |
665 | continue; | |
666 | ||
667 | if (ob->s.b.mixed_encoding) | |
668 | { | |
669 | /* Determine the encoding for this FDE. Note mixed encoded | |
670 | objects for later. */ | |
671 | this_cie = get_cie (this_fde); | |
672 | if (this_cie != last_cie) | |
673 | { | |
674 | last_cie = this_cie; | |
675 | encoding = get_cie_encoding (this_cie); | |
676 | base = base_from_object (encoding, ob); | |
677 | } | |
678 | } | |
679 | ||
680 | if (encoding == DW_EH_PE_absptr) | |
681 | { | |
e9d1b155 | 682 | if (*(_Unwind_Ptr *) this_fde->pc_begin == 0) |
e1f9550a RH |
683 | continue; |
684 | } | |
685 | else | |
686 | { | |
687 | _Unwind_Ptr pc_begin, mask; | |
688 | ||
689 | read_encoded_value_with_base (encoding, base, this_fde->pc_begin, | |
690 | &pc_begin); | |
691 | ||
692 | /* Take care to ignore link-once functions that were removed. | |
693 | In these cases, the function address will be NULL, but if | |
694 | the encoding is smaller than a pointer a true NULL may not | |
695 | be representable. Assume 0 in the representable bits is NULL. */ | |
696 | mask = size_of_encoded_value (encoding); | |
697 | if (mask < sizeof (void *)) | |
698 | mask = (1L << (mask << 3)) - 1; | |
699 | else | |
700 | mask = -1; | |
701 | ||
702 | if ((pc_begin & mask) == 0) | |
703 | continue; | |
704 | } | |
705 | ||
706 | fde_insert (accu, this_fde); | |
52a11cbf | 707 | } |
a3fd4e75 JL |
708 | } |
709 | ||
52a11cbf RH |
710 | /* Set up a sorted array of pointers to FDEs for a loaded object. We |
711 | count up the entries before allocating the array because it's likely to | |
712 | be faster. We can be called multiple times, should we have failed to | |
713 | allocate a sorted fde array on a previous occasion. */ | |
0021b564 | 714 | |
e1f9550a RH |
715 | static inline void |
716 | init_object (struct object* ob) | |
0021b564 | 717 | { |
e1f9550a | 718 | struct fde_accumulator accu; |
52a11cbf | 719 | size_t count; |
0021b564 | 720 | |
e1f9550a RH |
721 | count = ob->s.b.count; |
722 | if (count == 0) | |
52a11cbf | 723 | { |
e1f9550a RH |
724 | if (ob->s.b.from_array) |
725 | { | |
726 | fde **p = ob->u.array; | |
727 | for (count = 0; *p; ++p) | |
728 | count += classify_object_over_fdes (ob, *p); | |
729 | } | |
730 | else | |
731 | count = classify_object_over_fdes (ob, ob->u.single); | |
732 | ||
733 | /* The count field we have in the main struct object is somewhat | |
734 | limited, but should suffice for virtually all cases. If the | |
735 | counted value doesn't fit, re-write a zero. The worst that | |
736 | happens is that we re-count next time -- admittedly non-trivial | |
737 | in that this implies some 2M fdes, but at least we function. */ | |
738 | ob->s.b.count = count; | |
739 | if (ob->s.b.count != count) | |
740 | ob->s.b.count = 0; | |
52a11cbf | 741 | } |
154bba13 | 742 | |
e1f9550a | 743 | if (!start_fde_sort (&accu, count)) |
52a11cbf | 744 | return; |
154bba13 | 745 | |
e1f9550a | 746 | if (ob->s.b.from_array) |
52a11cbf | 747 | { |
e1f9550a RH |
748 | fde **p; |
749 | for (p = ob->u.array; *p; ++p) | |
41077ce4 | 750 | add_fdes (ob, &accu, *p); |
52a11cbf RH |
751 | } |
752 | else | |
e1f9550a RH |
753 | add_fdes (ob, &accu, ob->u.single); |
754 | ||
755 | end_fde_sort (ob, &accu, count); | |
756 | ||
757 | /* Save the original fde pointer, since this is the key by which the | |
758 | DSO will deregister the object. */ | |
759 | accu.linear->orig_data = ob->u.single; | |
760 | ob->u.sort = accu.linear; | |
761 | ||
762 | ob->s.b.sorted = 1; | |
a3fd4e75 JL |
763 | } |
764 | ||
e1f9550a RH |
765 | /* A linear search through a set of FDEs for the given PC. This is |
766 | used when there was insufficient memory to allocate and sort an | |
767 | array. */ | |
768 | ||
769 | static fde * | |
770 | linear_search_fdes (struct object *ob, fde *this_fde, void *pc) | |
0021b564 | 771 | { |
e1f9550a RH |
772 | struct dwarf_cie *last_cie = 0; |
773 | int encoding = ob->s.b.encoding; | |
774 | _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob); | |
775 | ||
3cfe49da | 776 | for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde)) |
e1f9550a RH |
777 | { |
778 | struct dwarf_cie *this_cie; | |
779 | _Unwind_Ptr pc_begin, pc_range; | |
780 | ||
781 | /* Skip CIEs. */ | |
782 | if (this_fde->CIE_delta == 0) | |
783 | continue; | |
784 | ||
785 | if (ob->s.b.mixed_encoding) | |
786 | { | |
787 | /* Determine the encoding for this FDE. Note mixed encoded | |
788 | objects for later. */ | |
789 | this_cie = get_cie (this_fde); | |
790 | if (this_cie != last_cie) | |
791 | { | |
792 | last_cie = this_cie; | |
793 | encoding = get_cie_encoding (this_cie); | |
794 | base = base_from_object (encoding, ob); | |
795 | } | |
796 | } | |
797 | ||
798 | if (encoding == DW_EH_PE_absptr) | |
799 | { | |
e9d1b155 KH |
800 | pc_begin = ((_Unwind_Ptr *) this_fde->pc_begin)[0]; |
801 | pc_range = ((_Unwind_Ptr *) this_fde->pc_begin)[1]; | |
e1f9550a RH |
802 | if (pc_begin == 0) |
803 | continue; | |
804 | } | |
805 | else | |
806 | { | |
807 | _Unwind_Ptr mask; | |
808 | const char *p; | |
809 | ||
810 | p = read_encoded_value_with_base (encoding, base, | |
811 | this_fde->pc_begin, &pc_begin); | |
812 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
813 | ||
814 | /* Take care to ignore link-once functions that were removed. | |
815 | In these cases, the function address will be NULL, but if | |
816 | the encoding is smaller than a pointer a true NULL may not | |
817 | be representable. Assume 0 in the representable bits is NULL. */ | |
818 | mask = size_of_encoded_value (encoding); | |
819 | if (mask < sizeof (void *)) | |
820 | mask = (1L << (mask << 3)) - 1; | |
821 | else | |
822 | mask = -1; | |
823 | ||
824 | if ((pc_begin & mask) == 0) | |
825 | continue; | |
826 | } | |
827 | ||
e9d1b155 | 828 | if ((_Unwind_Ptr) pc - pc_begin < pc_range) |
41077ce4 | 829 | return this_fde; |
e1f9550a RH |
830 | } |
831 | ||
832 | return NULL; | |
833 | } | |
834 | ||
835 | /* Binary search for an FDE containing the given PC. Here are three | |
836 | implementations of increasing complexity. */ | |
837 | ||
838 | static inline fde * | |
839 | binary_search_unencoded_fdes (struct object *ob, void *pc) | |
840 | { | |
841 | struct fde_vector *vec = ob->u.sort; | |
52a11cbf | 842 | size_t lo, hi; |
41077ce4 | 843 | |
e1f9550a RH |
844 | for (lo = 0, hi = vec->count; lo < hi; ) |
845 | { | |
846 | size_t i = (lo + hi) / 2; | |
847 | fde *f = vec->array[i]; | |
848 | void *pc_begin; | |
849 | uaddr pc_range; | |
850 | ||
e9d1b155 KH |
851 | pc_begin = ((void **) f->pc_begin)[0]; |
852 | pc_range = ((uaddr *) f->pc_begin)[1]; | |
e1f9550a RH |
853 | |
854 | if (pc < pc_begin) | |
855 | hi = i; | |
856 | else if (pc >= pc_begin + pc_range) | |
857 | lo = i + 1; | |
858 | else | |
859 | return f; | |
860 | } | |
0021b564 | 861 | |
e1f9550a RH |
862 | return NULL; |
863 | } | |
154bba13 | 864 | |
e1f9550a RH |
865 | static inline fde * |
866 | binary_search_single_encoding_fdes (struct object *ob, void *pc) | |
867 | { | |
868 | struct fde_vector *vec = ob->u.sort; | |
869 | int encoding = ob->s.b.encoding; | |
870 | _Unwind_Ptr base = base_from_object (encoding, ob); | |
871 | size_t lo, hi; | |
41077ce4 | 872 | |
e1f9550a | 873 | for (lo = 0, hi = vec->count; lo < hi; ) |
0021b564 | 874 | { |
e1f9550a RH |
875 | size_t i = (lo + hi) / 2; |
876 | fde *f = vec->array[i]; | |
877 | _Unwind_Ptr pc_begin, pc_range; | |
878 | const char *p; | |
879 | ||
880 | p = read_encoded_value_with_base (encoding, base, f->pc_begin, | |
881 | &pc_begin); | |
882 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
883 | ||
e9d1b155 | 884 | if ((_Unwind_Ptr) pc < pc_begin) |
e1f9550a | 885 | hi = i; |
e9d1b155 | 886 | else if ((_Unwind_Ptr) pc >= pc_begin + pc_range) |
e1f9550a RH |
887 | lo = i + 1; |
888 | else | |
889 | return f; | |
52a11cbf | 890 | } |
0021b564 | 891 | |
e1f9550a RH |
892 | return NULL; |
893 | } | |
894 | ||
895 | static inline fde * | |
896 | binary_search_mixed_encoding_fdes (struct object *ob, void *pc) | |
897 | { | |
898 | struct fde_vector *vec = ob->u.sort; | |
899 | size_t lo, hi; | |
41077ce4 | 900 | |
e1f9550a | 901 | for (lo = 0, hi = vec->count; lo < hi; ) |
52a11cbf | 902 | { |
e1f9550a RH |
903 | size_t i = (lo + hi) / 2; |
904 | fde *f = vec->array[i]; | |
905 | _Unwind_Ptr pc_begin, pc_range; | |
906 | const char *p; | |
907 | int encoding; | |
908 | ||
909 | encoding = get_fde_encoding (f); | |
910 | p = read_encoded_value_with_base (encoding, | |
911 | base_from_object (encoding, ob), | |
912 | f->pc_begin, &pc_begin); | |
913 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
914 | ||
e9d1b155 | 915 | if ((_Unwind_Ptr) pc < pc_begin) |
e1f9550a | 916 | hi = i; |
e9d1b155 | 917 | else if ((_Unwind_Ptr) pc >= pc_begin + pc_range) |
e1f9550a RH |
918 | lo = i + 1; |
919 | else | |
920 | return f; | |
52a11cbf | 921 | } |
0021b564 | 922 | |
e1f9550a RH |
923 | return NULL; |
924 | } | |
52a11cbf | 925 | |
e1f9550a RH |
926 | static fde * |
927 | search_object (struct object* ob, void *pc) | |
928 | { | |
929 | /* If the data hasn't been sorted, try to do this now. We may have | |
930 | more memory available than last time we tried. */ | |
931 | if (! ob->s.b.sorted) | |
52a11cbf | 932 | { |
e1f9550a | 933 | init_object (ob); |
52a11cbf | 934 | |
e1f9550a RH |
935 | /* Despite the above comment, the normal reason to get here is |
936 | that we've not processed this object before. A quick range | |
937 | check is in order. */ | |
938 | if (pc < ob->pc_begin) | |
939 | return NULL; | |
940 | } | |
941 | ||
942 | if (ob->s.b.sorted) | |
943 | { | |
944 | if (ob->s.b.mixed_encoding) | |
945 | return binary_search_mixed_encoding_fdes (ob, pc); | |
946 | else if (ob->s.b.encoding == DW_EH_PE_absptr) | |
947 | return binary_search_unencoded_fdes (ob, pc); | |
948 | else | |
949 | return binary_search_single_encoding_fdes (ob, pc); | |
0021b564 | 950 | } |
52a11cbf RH |
951 | else |
952 | { | |
e1f9550a RH |
953 | /* Long slow labourious linear search, cos we've no memory. */ |
954 | if (ob->s.b.from_array) | |
41077ce4 KH |
955 | { |
956 | fde **p; | |
e1f9550a RH |
957 | for (p = ob->u.array; *p ; p++) |
958 | { | |
959 | fde *f = linear_search_fdes (ob, *p, pc); | |
41077ce4 | 960 | if (f) |
e1f9550a | 961 | return f; |
41077ce4 | 962 | } |
e1f9550a RH |
963 | return NULL; |
964 | } | |
52a11cbf | 965 | else |
e1f9550a RH |
966 | return linear_search_fdes (ob, ob->u.single, pc); |
967 | } | |
968 | } | |
969 | ||
970 | fde * | |
971 | _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases) | |
972 | { | |
973 | struct object *ob; | |
974 | fde *f = NULL; | |
975 | ||
976 | init_object_mutex_once (); | |
977 | __gthread_mutex_lock (&object_mutex); | |
978 | ||
979 | /* Linear search through the classified objects, to find the one | |
e3aafbad | 980 | containing the pc. Note that pc_begin is sorted descending, and |
e1f9550a RH |
981 | we expect objects to be non-overlapping. */ |
982 | for (ob = seen_objects; ob; ob = ob->next) | |
983 | if (pc >= ob->pc_begin) | |
984 | { | |
985 | f = search_object (ob, pc); | |
986 | if (f) | |
987 | goto fini; | |
988 | break; | |
989 | } | |
990 | ||
991 | /* Classify and search the objects we've not yet processed. */ | |
992 | while ((ob = unseen_objects)) | |
993 | { | |
994 | struct object **p; | |
995 | ||
996 | unseen_objects = ob->next; | |
997 | f = search_object (ob, pc); | |
998 | ||
999 | /* Insert the object into the classified list. */ | |
1000 | for (p = &seen_objects; *p ; p = &(*p)->next) | |
1001 | if ((*p)->pc_begin < ob->pc_begin) | |
1002 | break; | |
1003 | ob->next = *p; | |
1004 | *p = ob; | |
1005 | ||
1006 | if (f) | |
1007 | goto fini; | |
1008 | } | |
1009 | ||
1010 | fini: | |
1011 | __gthread_mutex_unlock (&object_mutex); | |
1012 | ||
1013 | if (f) | |
1014 | { | |
1015 | int encoding; | |
1016 | ||
1017 | bases->tbase = ob->tbase; | |
1018 | bases->dbase = ob->dbase; | |
154bba13 | 1019 | |
e1f9550a RH |
1020 | encoding = ob->s.b.encoding; |
1021 | if (ob->s.b.mixed_encoding) | |
1022 | encoding = get_fde_encoding (f); | |
1023 | read_encoded_value_with_base (encoding, base_from_object (encoding, ob), | |
1024 | f->pc_begin, (_Unwind_Ptr *)&bases->func); | |
52a11cbf | 1025 | } |
0021b564 | 1026 | |
e1f9550a | 1027 | return f; |
a3fd4e75 | 1028 | } |