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