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