Remove some compile time warnings about duplicate definitions.
[official-gcc.git] / gcc / unwind-dw2-fde.c
blobd87cac5b625b6a31027bee42fee4be9f5cb6e8f9
1 /* Subroutines needed for unwinding stack frames for exception handling. */
2 /* Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Jason Merrill <jason@cygnus.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 02111-1307, USA. */
31 #include "tconfig.h"
32 #include "tsystem.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 #include "gthr.h"
40 /* The unseen_objects list contains objects that have been registered
41 but not yet categorized in any way. The seen_objects list has had
42 it's pc_begin and count fields initialized at minimum, and is sorted
43 by decreasing value of pc_begin. */
44 static struct object *unseen_objects;
45 static struct object *seen_objects;
47 #ifdef __GTHREAD_MUTEX_INIT
48 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
49 #else
50 static __gthread_mutex_t object_mutex;
51 #endif
53 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
54 static void
55 init_object_mutex (void)
57 __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
60 static void
61 init_object_mutex_once (void)
63 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
64 __gthread_once (&once, init_object_mutex);
66 #else
67 #define init_object_mutex_once()
68 #endif
70 /* Called from crtbegin.o to register the unwind info for an object. */
72 void
73 __register_frame_info_bases (void *begin, struct object *ob,
74 void *tbase, void *dbase)
76 /* If .eh_frame is empty, don't register at all. */
77 if (*(uword *)begin == 0)
78 return;
80 ob->pc_begin = (void *)-1;
81 ob->tbase = tbase;
82 ob->dbase = dbase;
83 ob->u.single = begin;
84 ob->s.i = 0;
85 ob->s.b.encoding = DW_EH_PE_omit;
87 init_object_mutex_once ();
88 __gthread_mutex_lock (&object_mutex);
90 ob->next = unseen_objects;
91 unseen_objects = ob;
93 __gthread_mutex_unlock (&object_mutex);
96 void
97 __register_frame_info (void *begin, struct object *ob)
99 __register_frame_info_bases (begin, ob, 0, 0);
102 void
103 __register_frame (void *begin)
105 struct object *ob;
107 /* If .eh_frame is empty, don't register at all. */
108 if (*(uword *)begin == 0)
109 return;
111 ob = (struct object *) malloc (sizeof (struct object));
112 __register_frame_info (begin, ob);
115 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
116 for different translation units. Called from the file generated by
117 collect2. */
119 void
120 __register_frame_info_table_bases (void *begin, struct object *ob,
121 void *tbase, void *dbase)
123 ob->pc_begin = (void *)-1;
124 ob->tbase = tbase;
125 ob->dbase = dbase;
126 ob->u.array = begin;
127 ob->s.i = 0;
128 ob->s.b.from_array = 1;
129 ob->s.b.encoding = DW_EH_PE_omit;
131 init_object_mutex_once ();
132 __gthread_mutex_lock (&object_mutex);
134 ob->next = unseen_objects;
135 unseen_objects = ob;
137 __gthread_mutex_unlock (&object_mutex);
140 void
141 __register_frame_info_table (void *begin, struct object *ob)
143 __register_frame_info_table_bases (begin, ob, 0, 0);
146 void
147 __register_frame_table (void *begin)
149 struct object *ob = (struct object *) malloc (sizeof (struct object));
150 __register_frame_info_table (begin, ob);
153 /* Called from crtbegin.o to deregister the unwind info for an object. */
154 /* ??? Glibc has for a while now exported __register_frame_info and
155 __deregister_frame_info. If we call __register_frame_info_bases
156 from crtbegin (wherein it is declared weak), and this object does
157 not get pulled from libgcc.a for other reasons, then the
158 invocation of __deregister_frame_info will be resolved from glibc.
159 Since the registration did not happen there, we'll abort.
161 Therefore, declare a new deregistration entry point that does the
162 exact same thing, but will resolve to the same library as
163 implements __register_frame_info_bases. */
165 void *
166 __deregister_frame_info_bases (void *begin)
168 struct object **p;
169 struct object *ob = 0;
171 /* If .eh_frame is empty, we haven't registered. */
172 if (*(uword *)begin == 0)
173 return ob;
175 init_object_mutex_once ();
176 __gthread_mutex_lock (&object_mutex);
178 for (p = &unseen_objects; *p ; p = &(*p)->next)
179 if ((*p)->u.single == begin)
181 ob = *p;
182 *p = ob->next;
183 goto out;
186 for (p = &seen_objects; *p ; p = &(*p)->next)
187 if ((*p)->s.b.sorted)
189 if ((*p)->u.sort->orig_data == begin)
191 ob = *p;
192 *p = ob->next;
193 free (ob->u.sort);
194 goto out;
197 else
199 if ((*p)->u.single == begin)
201 ob = *p;
202 *p = ob->next;
203 goto out;
207 __gthread_mutex_unlock (&object_mutex);
208 abort ();
210 out:
211 __gthread_mutex_unlock (&object_mutex);
212 return (void *) ob;
215 void *
216 __deregister_frame_info (void *begin)
218 return __deregister_frame_info_bases (begin);
221 void
222 __deregister_frame (void *begin)
224 /* If .eh_frame is empty, we haven't registered. */
225 if (*(uword *)begin != 0)
226 free (__deregister_frame_info (begin));
230 /* Like base_of_encoded_value, but take the base from a struct object
231 instead of an _Unwind_Context. */
233 static _Unwind_Ptr
234 base_from_object (unsigned char encoding, struct object *ob)
236 if (encoding == DW_EH_PE_omit)
237 return 0;
239 switch (encoding & 0x70)
241 case DW_EH_PE_absptr:
242 case DW_EH_PE_pcrel:
243 case DW_EH_PE_aligned:
244 return 0;
246 case DW_EH_PE_textrel:
247 return (_Unwind_Ptr) ob->tbase;
248 case DW_EH_PE_datarel:
249 return (_Unwind_Ptr) ob->dbase;
251 abort ();
254 /* Return the FDE pointer encoding from the CIE. */
255 /* ??? This is a subset of extract_cie_info from unwind-dw2.c. */
257 static int
258 get_cie_encoding (struct dwarf_cie *cie)
260 const unsigned char *aug, *p;
261 _Unwind_Ptr dummy;
262 _Unwind_Word utmp;
263 _Unwind_Sword stmp;
265 aug = cie->augmentation;
266 if (aug[0] != 'z')
267 return DW_EH_PE_absptr;
269 p = aug + strlen (aug) + 1; /* Skip the augmentation string. */
270 p = read_uleb128 (p, &utmp); /* Skip code alignment. */
271 p = read_sleb128 (p, &stmp); /* Skip data alignment. */
272 p++; /* Skip return address column. */
274 aug++; /* Skip 'z' */
275 p = read_uleb128 (p, &utmp); /* Skip augmentation length. */
276 while (1)
278 /* This is what we're looking for. */
279 if (*aug == 'R')
280 return *p;
281 /* Personality encoding and pointer. */
282 else if (*aug == 'P')
284 /* ??? Avoid dereferencing indirect pointers, since we're
285 faking the base address. Gotta keep DW_EH_PE_aligned
286 intact, however. */
287 p = read_encoded_value_with_base (*p & 0x7F, 0, p + 1, &dummy);
289 /* LSDA encoding. */
290 else if (*aug == 'L')
291 p++;
292 /* Otherwise end of string, or unknown augmentation. */
293 else
294 return DW_EH_PE_absptr;
295 aug++;
299 static inline int
300 get_fde_encoding (struct dwarf_fde *f)
302 return get_cie_encoding (get_cie (f));
306 /* Sorting an array of FDEs by address.
307 (Ideally we would have the linker sort the FDEs so we don't have to do
308 it at run time. But the linkers are not yet prepared for this.) */
310 /* Comparison routines. Three variants of increasing complexity. */
312 static int
313 fde_unencoded_compare (struct object *ob __attribute__((unused)),
314 fde *x, fde *y)
316 _Unwind_Ptr x_ptr = *(_Unwind_Ptr *) x->pc_begin;
317 _Unwind_Ptr y_ptr = *(_Unwind_Ptr *) y->pc_begin;
319 if (x_ptr > y_ptr)
320 return 1;
321 if (x_ptr < y_ptr)
322 return -1;
323 return 0;
326 static int
327 fde_single_encoding_compare (struct object *ob, fde *x, fde *y)
329 _Unwind_Ptr base, x_ptr, y_ptr;
331 base = base_from_object (ob->s.b.encoding, ob);
332 read_encoded_value_with_base (ob->s.b.encoding, base, x->pc_begin, &x_ptr);
333 read_encoded_value_with_base (ob->s.b.encoding, base, y->pc_begin, &y_ptr);
335 if (x_ptr > y_ptr)
336 return 1;
337 if (x_ptr < y_ptr)
338 return -1;
339 return 0;
342 static int
343 fde_mixed_encoding_compare (struct object *ob, fde *x, fde *y)
345 int x_encoding, y_encoding;
346 _Unwind_Ptr x_ptr, y_ptr;
348 x_encoding = get_fde_encoding (x);
349 read_encoded_value_with_base (x_encoding, base_from_object (x_encoding, ob),
350 x->pc_begin, &x_ptr);
352 y_encoding = get_fde_encoding (y);
353 read_encoded_value_with_base (y_encoding, base_from_object (y_encoding, ob),
354 y->pc_begin, &y_ptr);
356 if (x_ptr > y_ptr)
357 return 1;
358 if (x_ptr < y_ptr)
359 return -1;
360 return 0;
363 typedef int (*fde_compare_t) (struct object *, fde *, fde *);
366 /* This is a special mix of insertion sort and heap sort, optimized for
367 the data sets that actually occur. They look like
368 101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
369 I.e. a linearly increasing sequence (coming from functions in the text
370 section), with additionally a few unordered elements (coming from functions
371 in gnu_linkonce sections) whose values are higher than the values in the
372 surrounding linear sequence (but not necessarily higher than the values
373 at the end of the linear sequence!).
374 The worst-case total run time is O(N) + O(n log (n)), where N is the
375 total number of FDEs and n is the number of erratic ones. */
377 struct fde_accumulator
379 struct fde_vector *linear;
380 struct fde_vector *erratic;
383 static inline int
384 start_fde_sort (struct fde_accumulator *accu, size_t count)
386 size_t size;
387 if (! count)
388 return 0;
390 size = sizeof (struct fde_vector) + sizeof (fde *) * count;
391 if ((accu->linear = (struct fde_vector *) malloc (size)))
393 accu->linear->count = 0;
394 if ((accu->erratic = (struct fde_vector *) malloc (size)))
395 accu->erratic->count = 0;
396 return 1;
398 else
399 return 0;
402 static inline void
403 fde_insert (struct fde_accumulator *accu, fde *this_fde)
405 if (accu->linear)
406 accu->linear->array[accu->linear->count++] = this_fde;
409 /* Split LINEAR into a linear sequence with low values and an erratic
410 sequence with high values, put the linear one (of longest possible
411 length) into LINEAR and the erratic one into ERRATIC. This is O(N).
413 Because the longest linear sequence we are trying to locate within the
414 incoming LINEAR array can be interspersed with (high valued) erratic
415 entries. We construct a chain indicating the sequenced entries.
416 To avoid having to allocate this chain, we overlay it onto the space of
417 the ERRATIC array during construction. A final pass iterates over the
418 chain to determine what should be placed in the ERRATIC array, and
419 what is the linear sequence. This overlay is safe from aliasing. */
421 static inline void
422 fde_split (struct object *ob, fde_compare_t fde_compare,
423 struct fde_vector *linear, struct fde_vector *erratic)
425 static fde *marker;
426 size_t count = linear->count;
427 fde **chain_end = &marker;
428 size_t i, j, k;
430 /* This should optimize out, but it is wise to make sure this assumption
431 is correct. Should these have different sizes, we cannot cast between
432 them and the overlaying onto ERRATIC will not work. */
433 if (sizeof (fde *) != sizeof (fde **))
434 abort ();
436 for (i = 0; i < count; i++)
438 fde **probe;
440 for (probe = chain_end;
441 probe != &marker && fde_compare (ob, linear->array[i], *probe) < 0;
442 probe = chain_end)
444 chain_end = (fde **)erratic->array[probe - linear->array];
445 erratic->array[probe - linear->array] = NULL;
447 erratic->array[i] = (fde *)chain_end;
448 chain_end = &linear->array[i];
451 /* Each entry in LINEAR which is part of the linear sequence we have
452 discovered will correspond to a non-NULL entry in the chain we built in
453 the ERRATIC array. */
454 for (i = j = k = 0; i < count; i++)
455 if (erratic->array[i])
456 linear->array[j++] = linear->array[i];
457 else
458 erratic->array[k++] = linear->array[i];
459 linear->count = j;
460 erratic->count = k;
463 /* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must
464 use a name that does not conflict. */
466 static void
467 frame_heapsort (struct object *ob, fde_compare_t fde_compare,
468 struct fde_vector *erratic)
470 /* For a description of this algorithm, see:
471 Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
472 p. 60-61. */
473 fde ** a = erratic->array;
474 /* A portion of the array is called a "heap" if for all i>=0:
475 If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
476 If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
477 #define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0)
478 size_t n = erratic->count;
479 size_t m = n;
480 size_t i;
482 while (m > 0)
484 /* Invariant: a[m..n-1] is a heap. */
485 m--;
486 for (i = m; 2*i+1 < n; )
488 if (2*i+2 < n
489 && fde_compare (ob, a[2*i+2], a[2*i+1]) > 0
490 && fde_compare (ob, a[2*i+2], a[i]) > 0)
492 SWAP (a[i], a[2*i+2]);
493 i = 2*i+2;
495 else if (fde_compare (ob, a[2*i+1], a[i]) > 0)
497 SWAP (a[i], a[2*i+1]);
498 i = 2*i+1;
500 else
501 break;
504 while (n > 1)
506 /* Invariant: a[0..n-1] is a heap. */
507 n--;
508 SWAP (a[0], a[n]);
509 for (i = 0; 2*i+1 < n; )
511 if (2*i+2 < n
512 && fde_compare (ob, a[2*i+2], a[2*i+1]) > 0
513 && fde_compare (ob, a[2*i+2], a[i]) > 0)
515 SWAP (a[i], a[2*i+2]);
516 i = 2*i+2;
518 else if (fde_compare (ob, a[2*i+1], a[i]) > 0)
520 SWAP (a[i], a[2*i+1]);
521 i = 2*i+1;
523 else
524 break;
527 #undef SWAP
530 /* Merge V1 and V2, both sorted, and put the result into V1. */
531 static inline void
532 fde_merge (struct object *ob, fde_compare_t fde_compare,
533 struct fde_vector *v1, struct fde_vector *v2)
535 size_t i1, i2;
536 fde * fde2;
538 i2 = v2->count;
539 if (i2 > 0)
541 i1 = v1->count;
542 do {
543 i2--;
544 fde2 = v2->array[i2];
545 while (i1 > 0 && fde_compare (ob, v1->array[i1-1], fde2) > 0)
547 v1->array[i1+i2] = v1->array[i1-1];
548 i1--;
550 v1->array[i1+i2] = fde2;
551 } while (i2 > 0);
552 v1->count += v2->count;
556 static inline void
557 end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count)
559 fde_compare_t fde_compare;
561 if (accu->linear && accu->linear->count != count)
562 abort ();
564 if (ob->s.b.mixed_encoding)
565 fde_compare = fde_mixed_encoding_compare;
566 else if (ob->s.b.encoding == DW_EH_PE_absptr)
567 fde_compare = fde_unencoded_compare;
568 else
569 fde_compare = fde_single_encoding_compare;
571 if (accu->erratic)
573 fde_split (ob, fde_compare, accu->linear, accu->erratic);
574 if (accu->linear->count + accu->erratic->count != count)
575 abort ();
576 frame_heapsort (ob, fde_compare, accu->erratic);
577 fde_merge (ob, fde_compare, accu->linear, accu->erratic);
578 free (accu->erratic);
580 else
582 /* We've not managed to malloc an erratic array,
583 so heap sort in the linear one. */
584 frame_heapsort (ob, fde_compare, accu->linear);
589 /* Update encoding, mixed_encoding, and pc_begin for OB for the
590 fde array beginning at THIS_FDE. Return the number of fdes
591 encountered along the way. */
593 static size_t
594 classify_object_over_fdes (struct object *ob, fde *this_fde)
596 struct dwarf_cie *last_cie = 0;
597 size_t count = 0;
598 int encoding = DW_EH_PE_absptr;
599 _Unwind_Ptr base = 0;
601 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
603 struct dwarf_cie *this_cie;
604 _Unwind_Ptr mask, pc_begin;
606 /* Skip CIEs. */
607 if (this_fde->CIE_delta == 0)
608 continue;
610 /* Determine the encoding for this FDE. Note mixed encoded
611 objects for later. */
612 this_cie = get_cie (this_fde);
613 if (this_cie != last_cie)
615 last_cie = this_cie;
616 encoding = get_cie_encoding (this_cie);
617 base = base_from_object (encoding, ob);
618 if (ob->s.b.encoding == DW_EH_PE_omit)
619 ob->s.b.encoding = encoding;
620 else if (ob->s.b.encoding != encoding)
621 ob->s.b.mixed_encoding = 1;
624 read_encoded_value_with_base (encoding, base, this_fde->pc_begin,
625 &pc_begin);
627 /* Take care to ignore link-once functions that were removed.
628 In these cases, the function address will be NULL, but if
629 the encoding is smaller than a pointer a true NULL may not
630 be representable. Assume 0 in the representable bits is NULL. */
631 mask = size_of_encoded_value (encoding);
632 if (mask < sizeof (void *))
633 mask = (1L << (mask << 3)) - 1;
634 else
635 mask = -1;
637 if ((pc_begin & mask) == 0)
638 continue;
640 count += 1;
641 if ((void *)pc_begin < ob->pc_begin)
642 ob->pc_begin = (void *)pc_begin;
645 return count;
648 static void
649 add_fdes (struct object *ob, struct fde_accumulator *accu, fde *this_fde)
651 struct dwarf_cie *last_cie = 0;
652 int encoding = ob->s.b.encoding;
653 _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
655 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
657 struct dwarf_cie *this_cie;
659 /* Skip CIEs. */
660 if (this_fde->CIE_delta == 0)
661 continue;
663 if (ob->s.b.mixed_encoding)
665 /* Determine the encoding for this FDE. Note mixed encoded
666 objects for later. */
667 this_cie = get_cie (this_fde);
668 if (this_cie != last_cie)
670 last_cie = this_cie;
671 encoding = get_cie_encoding (this_cie);
672 base = base_from_object (encoding, ob);
676 if (encoding == DW_EH_PE_absptr)
678 if (*(_Unwind_Ptr *)this_fde->pc_begin == 0)
679 continue;
681 else
683 _Unwind_Ptr pc_begin, mask;
685 read_encoded_value_with_base (encoding, base, this_fde->pc_begin,
686 &pc_begin);
688 /* Take care to ignore link-once functions that were removed.
689 In these cases, the function address will be NULL, but if
690 the encoding is smaller than a pointer a true NULL may not
691 be representable. Assume 0 in the representable bits is NULL. */
692 mask = size_of_encoded_value (encoding);
693 if (mask < sizeof (void *))
694 mask = (1L << (mask << 3)) - 1;
695 else
696 mask = -1;
698 if ((pc_begin & mask) == 0)
699 continue;
702 fde_insert (accu, this_fde);
706 /* Set up a sorted array of pointers to FDEs for a loaded object. We
707 count up the entries before allocating the array because it's likely to
708 be faster. We can be called multiple times, should we have failed to
709 allocate a sorted fde array on a previous occasion. */
711 static inline void
712 init_object (struct object* ob)
714 struct fde_accumulator accu;
715 size_t count;
717 count = ob->s.b.count;
718 if (count == 0)
720 if (ob->s.b.from_array)
722 fde **p = ob->u.array;
723 for (count = 0; *p; ++p)
724 count += classify_object_over_fdes (ob, *p);
726 else
727 count = classify_object_over_fdes (ob, ob->u.single);
729 /* The count field we have in the main struct object is somewhat
730 limited, but should suffice for virtually all cases. If the
731 counted value doesn't fit, re-write a zero. The worst that
732 happens is that we re-count next time -- admittedly non-trivial
733 in that this implies some 2M fdes, but at least we function. */
734 ob->s.b.count = count;
735 if (ob->s.b.count != count)
736 ob->s.b.count = 0;
739 if (!start_fde_sort (&accu, count))
740 return;
742 if (ob->s.b.from_array)
744 fde **p;
745 for (p = ob->u.array; *p; ++p)
746 add_fdes (ob, &accu, *p);
748 else
749 add_fdes (ob, &accu, ob->u.single);
751 end_fde_sort (ob, &accu, count);
753 /* Save the original fde pointer, since this is the key by which the
754 DSO will deregister the object. */
755 accu.linear->orig_data = ob->u.single;
756 ob->u.sort = accu.linear;
758 ob->s.b.sorted = 1;
761 /* A linear search through a set of FDEs for the given PC. This is
762 used when there was insufficient memory to allocate and sort an
763 array. */
765 static fde *
766 linear_search_fdes (struct object *ob, fde *this_fde, void *pc)
768 struct dwarf_cie *last_cie = 0;
769 int encoding = ob->s.b.encoding;
770 _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
772 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
774 struct dwarf_cie *this_cie;
775 _Unwind_Ptr pc_begin, pc_range;
777 /* Skip CIEs. */
778 if (this_fde->CIE_delta == 0)
779 continue;
781 if (ob->s.b.mixed_encoding)
783 /* Determine the encoding for this FDE. Note mixed encoded
784 objects for later. */
785 this_cie = get_cie (this_fde);
786 if (this_cie != last_cie)
788 last_cie = this_cie;
789 encoding = get_cie_encoding (this_cie);
790 base = base_from_object (encoding, ob);
794 if (encoding == DW_EH_PE_absptr)
796 pc_begin = ((_Unwind_Ptr *)this_fde->pc_begin)[0];
797 pc_range = ((_Unwind_Ptr *)this_fde->pc_begin)[1];
798 if (pc_begin == 0)
799 continue;
801 else
803 _Unwind_Ptr mask;
804 const char *p;
806 p = read_encoded_value_with_base (encoding, base,
807 this_fde->pc_begin, &pc_begin);
808 read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
810 /* Take care to ignore link-once functions that were removed.
811 In these cases, the function address will be NULL, but if
812 the encoding is smaller than a pointer a true NULL may not
813 be representable. Assume 0 in the representable bits is NULL. */
814 mask = size_of_encoded_value (encoding);
815 if (mask < sizeof (void *))
816 mask = (1L << (mask << 3)) - 1;
817 else
818 mask = -1;
820 if ((pc_begin & mask) == 0)
821 continue;
824 if ((_Unwind_Ptr)pc - pc_begin < pc_range)
825 return this_fde;
828 return NULL;
831 /* Binary search for an FDE containing the given PC. Here are three
832 implementations of increasing complexity. */
834 static inline fde *
835 binary_search_unencoded_fdes (struct object *ob, void *pc)
837 struct fde_vector *vec = ob->u.sort;
838 size_t lo, hi;
840 for (lo = 0, hi = vec->count; lo < hi; )
842 size_t i = (lo + hi) / 2;
843 fde *f = vec->array[i];
844 void *pc_begin;
845 uaddr pc_range;
847 pc_begin = ((void **)f->pc_begin)[0];
848 pc_range = ((uaddr *)f->pc_begin)[1];
850 if (pc < pc_begin)
851 hi = i;
852 else if (pc >= pc_begin + pc_range)
853 lo = i + 1;
854 else
855 return f;
858 return NULL;
861 static inline fde *
862 binary_search_single_encoding_fdes (struct object *ob, void *pc)
864 struct fde_vector *vec = ob->u.sort;
865 int encoding = ob->s.b.encoding;
866 _Unwind_Ptr base = base_from_object (encoding, ob);
867 size_t lo, hi;
869 for (lo = 0, hi = vec->count; lo < hi; )
871 size_t i = (lo + hi) / 2;
872 fde *f = vec->array[i];
873 _Unwind_Ptr pc_begin, pc_range;
874 const char *p;
876 p = read_encoded_value_with_base (encoding, base, f->pc_begin,
877 &pc_begin);
878 read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
880 if ((_Unwind_Ptr)pc < pc_begin)
881 hi = i;
882 else if ((_Unwind_Ptr)pc >= pc_begin + pc_range)
883 lo = i + 1;
884 else
885 return f;
888 return NULL;
891 static inline fde *
892 binary_search_mixed_encoding_fdes (struct object *ob, void *pc)
894 struct fde_vector *vec = ob->u.sort;
895 size_t lo, hi;
897 for (lo = 0, hi = vec->count; lo < hi; )
899 size_t i = (lo + hi) / 2;
900 fde *f = vec->array[i];
901 _Unwind_Ptr pc_begin, pc_range;
902 const char *p;
903 int encoding;
905 encoding = get_fde_encoding (f);
906 p = read_encoded_value_with_base (encoding,
907 base_from_object (encoding, ob),
908 f->pc_begin, &pc_begin);
909 read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
911 if ((_Unwind_Ptr)pc < pc_begin)
912 hi = i;
913 else if ((_Unwind_Ptr)pc >= pc_begin + pc_range)
914 lo = i + 1;
915 else
916 return f;
919 return NULL;
922 static fde *
923 search_object (struct object* ob, void *pc)
925 /* If the data hasn't been sorted, try to do this now. We may have
926 more memory available than last time we tried. */
927 if (! ob->s.b.sorted)
929 init_object (ob);
931 /* Despite the above comment, the normal reason to get here is
932 that we've not processed this object before. A quick range
933 check is in order. */
934 if (pc < ob->pc_begin)
935 return NULL;
938 if (ob->s.b.sorted)
940 if (ob->s.b.mixed_encoding)
941 return binary_search_mixed_encoding_fdes (ob, pc);
942 else if (ob->s.b.encoding == DW_EH_PE_absptr)
943 return binary_search_unencoded_fdes (ob, pc);
944 else
945 return binary_search_single_encoding_fdes (ob, pc);
947 else
949 /* Long slow labourious linear search, cos we've no memory. */
950 if (ob->s.b.from_array)
952 fde **p;
953 for (p = ob->u.array; *p ; p++)
955 fde *f = linear_search_fdes (ob, *p, pc);
956 if (f)
957 return f;
959 return NULL;
961 else
962 return linear_search_fdes (ob, ob->u.single, pc);
966 fde *
967 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
969 struct object *ob;
970 fde *f = NULL;
972 init_object_mutex_once ();
973 __gthread_mutex_lock (&object_mutex);
975 /* Linear search through the classified objects, to find the one
976 containing the pc. Note that pc_begin is sorted descending, and
977 we expect objects to be non-overlapping. */
978 for (ob = seen_objects; ob; ob = ob->next)
979 if (pc >= ob->pc_begin)
981 f = search_object (ob, pc);
982 if (f)
983 goto fini;
984 break;
987 /* Classify and search the objects we've not yet processed. */
988 while ((ob = unseen_objects))
990 struct object **p;
992 unseen_objects = ob->next;
993 f = search_object (ob, pc);
995 /* Insert the object into the classified list. */
996 for (p = &seen_objects; *p ; p = &(*p)->next)
997 if ((*p)->pc_begin < ob->pc_begin)
998 break;
999 ob->next = *p;
1000 *p = ob;
1002 if (f)
1003 goto fini;
1006 fini:
1007 __gthread_mutex_unlock (&object_mutex);
1009 if (f)
1011 int encoding;
1013 bases->tbase = ob->tbase;
1014 bases->dbase = ob->dbase;
1016 encoding = ob->s.b.encoding;
1017 if (ob->s.b.mixed_encoding)
1018 encoding = get_fde_encoding (f);
1019 read_encoded_value_with_base (encoding, base_from_object (encoding, ob),
1020 f->pc_begin, (_Unwind_Ptr *)&bases->func);
1023 return f;