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