]>
Commit | Line | Data |
---|---|---|
0021b564 | 1 | /* Subroutines needed for unwinding stack frames for exception handling. */ |
83ffe9cd | 2 | /* Copyright (C) 1997-2023 Free Software Foundation, Inc. |
0021b564 JM |
3 | Contributed by Jason Merrill <jason@cygnus.com>. |
4 | ||
1322177d | 5 | This file is part of GCC. |
0021b564 | 6 | |
1322177d LB |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
748086b7 | 9 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 10 | version. |
0021b564 | 11 | |
1322177d LB |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
0021b564 | 16 | |
748086b7 JJ |
17 | Under Section 7 of GPL version 3, you are granted additional |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | <http://www.gnu.org/licenses/>. */ | |
0021b564 | 25 | |
275b60d6 | 26 | #ifndef _Unwind_Find_FDE |
52a11cbf RH |
27 | #include "tconfig.h" |
28 | #include "tsystem.h" | |
4977bab6 ZW |
29 | #include "coretypes.h" |
30 | #include "tm.h" | |
852b75ed | 31 | #include "libgcc_tm.h" |
a80b0574 | 32 | #include "dwarf2.h" |
e1f9550a | 33 | #include "unwind.h" |
bda33a6e | 34 | #define NO_BASE_OF_ENCODED_VALUE |
e1f9550a | 35 | #include "unwind-pe.h" |
52a11cbf RH |
36 | #include "unwind-dw2-fde.h" |
37 | #include "gthr.h" | |
29991048 JJ |
38 | #else |
39 | #if (defined(__GTHREAD_MUTEX_INIT) || defined(__GTHREAD_MUTEX_INIT_FUNCTION)) \ | |
40 | && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) | |
41 | #define ATOMIC_FDE_FAST_PATH 1 | |
42 | #endif | |
275b60d6 | 43 | #endif |
52a11cbf | 44 | |
d458f806 TN |
45 | typedef __UINTPTR_TYPE__ uintptr_type; |
46 | ||
6e80a1d1 TN |
47 | #ifdef ATOMIC_FDE_FAST_PATH |
48 | #include "unwind-dw2-btree.h" | |
49 | ||
50 | static struct btree registered_frames; | |
94ccaf62 | 51 | static bool in_shutdown; |
6e80a1d1 TN |
52 | |
53 | static void | |
386ebf75 | 54 | release_registered_frames (void) __attribute__ ((destructor)); |
6e80a1d1 TN |
55 | static void |
56 | release_registered_frames (void) | |
57 | { | |
58 | /* Release the b-tree and all frames. Frame releases that happen later are | |
59 | * silently ignored */ | |
60 | btree_destroy (®istered_frames); | |
94ccaf62 | 61 | in_shutdown = true; |
6e80a1d1 TN |
62 | } |
63 | ||
64 | static void | |
d458f806 | 65 | get_pc_range (const struct object *ob, uintptr_type *range); |
6e80a1d1 TN |
66 | |
67 | #else | |
386ebf75 TN |
68 | /* Without fast path frame deregistration must always succeed. */ |
69 | static const int in_shutdown = 0; | |
6e80a1d1 | 70 | |
e1f9550a RH |
71 | /* The unseen_objects list contains objects that have been registered |
72 | but not yet categorized in any way. The seen_objects list has had | |
0ab2a34f | 73 | its pc_begin and count fields initialized at minimum, and is sorted |
e1f9550a RH |
74 | by decreasing value of pc_begin. */ |
75 | static struct object *unseen_objects; | |
76 | static struct object *seen_objects; | |
6e56633d | 77 | #endif |
52a11cbf RH |
78 | |
79 | #ifdef __GTHREAD_MUTEX_INIT | |
80 | static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT; | |
33e3e24d | 81 | #define init_object_mutex_once() |
52a11cbf | 82 | #else |
9c4271f3 | 83 | #ifdef __GTHREAD_MUTEX_INIT_FUNCTION |
52a11cbf | 84 | static __gthread_mutex_t object_mutex; |
52a11cbf | 85 | |
41077ce4 | 86 | static void |
52a11cbf RH |
87 | init_object_mutex (void) |
88 | { | |
89 | __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex); | |
90 | } | |
91 | ||
92 | static void | |
93 | init_object_mutex_once (void) | |
94 | { | |
95 | static __gthread_once_t once = __GTHREAD_ONCE_INIT; | |
96 | __gthread_once (&once, init_object_mutex); | |
97 | } | |
9c4271f3 RG |
98 | #else |
99 | /* ??? Several targets include this file with stubbing parts of gthr.h | |
100 | and expect no locking to be done. */ | |
101 | #define init_object_mutex_once() | |
102 | static __gthread_mutex_t object_mutex; | |
103 | #endif | |
52a11cbf RH |
104 | #endif |
105 | ||
106 | /* Called from crtbegin.o to register the unwind info for an object. */ | |
107 | ||
108 | void | |
a30794da | 109 | __register_frame_info_bases (const void *begin, struct object *ob, |
e1f9550a | 110 | void *tbase, void *dbase) |
52a11cbf | 111 | { |
55fae1a9 | 112 | /* If .eh_frame is empty, don't register at all. */ |
5f754896 | 113 | if ((const uword *) begin == 0 || *(const uword *) begin == 0) |
55fae1a9 JJ |
114 | return; |
115 | ||
e1f9550a RH |
116 | ob->pc_begin = (void *)-1; |
117 | ob->tbase = tbase; | |
118 | ob->dbase = dbase; | |
119 | ob->u.single = begin; | |
120 | ob->s.i = 0; | |
121 | ob->s.b.encoding = DW_EH_PE_omit; | |
3cfe49da GK |
122 | #ifdef DWARF2_OBJECT_END_PTR_EXTENSION |
123 | ob->fde_end = NULL; | |
124 | #endif | |
52a11cbf | 125 | |
6e80a1d1 | 126 | #ifdef ATOMIC_FDE_FAST_PATH |
6e56633d | 127 | // Register the frame in the b-tree |
d458f806 | 128 | uintptr_type range[2]; |
6e80a1d1 TN |
129 | get_pc_range (ob, range); |
130 | btree_insert (®istered_frames, range[0], range[1] - range[0], ob); | |
131 | #else | |
52a11cbf RH |
132 | init_object_mutex_once (); |
133 | __gthread_mutex_lock (&object_mutex); | |
134 | ||
e1f9550a RH |
135 | ob->next = unseen_objects; |
136 | unseen_objects = ob; | |
52a11cbf RH |
137 | |
138 | __gthread_mutex_unlock (&object_mutex); | |
6e80a1d1 | 139 | #endif |
52a11cbf RH |
140 | } |
141 | ||
e1f9550a | 142 | void |
a30794da | 143 | __register_frame_info (const void *begin, struct object *ob) |
e1f9550a RH |
144 | { |
145 | __register_frame_info_bases (begin, ob, 0, 0); | |
146 | } | |
147 | ||
52a11cbf RH |
148 | void |
149 | __register_frame (void *begin) | |
150 | { | |
55fae1a9 JJ |
151 | struct object *ob; |
152 | ||
153 | /* If .eh_frame is empty, don't register at all. */ | |
e9d1b155 | 154 | if (*(uword *) begin == 0) |
55fae1a9 JJ |
155 | return; |
156 | ||
703ad42b | 157 | ob = malloc (sizeof (struct object)); |
41077ce4 | 158 | __register_frame_info (begin, ob); |
52a11cbf RH |
159 | } |
160 | ||
161 | /* Similar, but BEGIN is actually a pointer to a table of unwind entries | |
162 | for different translation units. Called from the file generated by | |
163 | collect2. */ | |
164 | ||
165 | void | |
e1f9550a RH |
166 | __register_frame_info_table_bases (void *begin, struct object *ob, |
167 | void *tbase, void *dbase) | |
52a11cbf | 168 | { |
e1f9550a RH |
169 | ob->pc_begin = (void *)-1; |
170 | ob->tbase = tbase; | |
171 | ob->dbase = dbase; | |
172 | ob->u.array = begin; | |
173 | ob->s.i = 0; | |
174 | ob->s.b.from_array = 1; | |
175 | ob->s.b.encoding = DW_EH_PE_omit; | |
52a11cbf | 176 | |
6e80a1d1 | 177 | #ifdef ATOMIC_FDE_FAST_PATH |
6e56633d | 178 | // Register the frame in the b-tree |
d458f806 | 179 | uintptr_type range[2]; |
6e80a1d1 TN |
180 | get_pc_range (ob, range); |
181 | btree_insert (®istered_frames, range[0], range[1] - range[0], ob); | |
182 | #else | |
52a11cbf RH |
183 | init_object_mutex_once (); |
184 | __gthread_mutex_lock (&object_mutex); | |
185 | ||
e1f9550a RH |
186 | ob->next = unseen_objects; |
187 | unseen_objects = ob; | |
52a11cbf RH |
188 | |
189 | __gthread_mutex_unlock (&object_mutex); | |
6e80a1d1 | 190 | #endif |
52a11cbf RH |
191 | } |
192 | ||
e1f9550a RH |
193 | void |
194 | __register_frame_info_table (void *begin, struct object *ob) | |
195 | { | |
196 | __register_frame_info_table_bases (begin, ob, 0, 0); | |
197 | } | |
198 | ||
52a11cbf RH |
199 | void |
200 | __register_frame_table (void *begin) | |
201 | { | |
703ad42b | 202 | struct object *ob = malloc (sizeof (struct object)); |
52a11cbf RH |
203 | __register_frame_info_table (begin, ob); |
204 | } | |
205 | ||
206 | /* Called from crtbegin.o to deregister the unwind info for an object. */ | |
101fa48c RH |
207 | /* ??? Glibc has for a while now exported __register_frame_info and |
208 | __deregister_frame_info. If we call __register_frame_info_bases | |
209 | from crtbegin (wherein it is declared weak), and this object does | |
210 | not get pulled from libgcc.a for other reasons, then the | |
211 | invocation of __deregister_frame_info will be resolved from glibc. | |
79d0dfa3 | 212 | Since the registration did not happen there, we'll die. |
101fa48c RH |
213 | |
214 | Therefore, declare a new deregistration entry point that does the | |
41077ce4 | 215 | exact same thing, but will resolve to the same library as |
101fa48c | 216 | implements __register_frame_info_bases. */ |
52a11cbf RH |
217 | |
218 | void * | |
a30794da | 219 | __deregister_frame_info_bases (const void *begin) |
52a11cbf | 220 | { |
e1f9550a | 221 | struct object *ob = 0; |
52a11cbf | 222 | |
55fae1a9 | 223 | /* If .eh_frame is empty, we haven't registered. */ |
5f754896 | 224 | if ((const uword *) begin == 0 || *(const uword *) begin == 0) |
5d393c8e | 225 | return ob; |
55fae1a9 | 226 | |
6e80a1d1 TN |
227 | #ifdef ATOMIC_FDE_FAST_PATH |
228 | // Find the corresponding PC range | |
229 | struct object lookupob; | |
230 | lookupob.tbase = 0; | |
231 | lookupob.dbase = 0; | |
232 | lookupob.u.single = begin; | |
233 | lookupob.s.i = 0; | |
234 | lookupob.s.b.encoding = DW_EH_PE_omit; | |
235 | #ifdef DWARF2_OBJECT_END_PTR_EXTENSION | |
236 | lookupob.fde_end = NULL; | |
237 | #endif | |
d458f806 | 238 | uintptr_type range[2]; |
6e80a1d1 TN |
239 | get_pc_range (&lookupob, range); |
240 | ||
241 | // And remove | |
242 | ob = btree_remove (®istered_frames, range[0]); | |
30adfb85 | 243 | bool empty_table = (range[1] - range[0]) == 0; |
5cf60b6b TN |
244 | |
245 | // Deallocate the sort array if any. | |
246 | if (ob && ob->s.b.sorted) | |
247 | { | |
248 | free (ob->u.sort); | |
249 | } | |
6e80a1d1 | 250 | #else |
52a11cbf RH |
251 | init_object_mutex_once (); |
252 | __gthread_mutex_lock (&object_mutex); | |
253 | ||
6e80a1d1 | 254 | struct object **p; |
e1f9550a RH |
255 | for (p = &unseen_objects; *p ; p = &(*p)->next) |
256 | if ((*p)->u.single == begin) | |
257 | { | |
258 | ob = *p; | |
259 | *p = ob->next; | |
260 | goto out; | |
261 | } | |
262 | ||
263 | for (p = &seen_objects; *p ; p = &(*p)->next) | |
264 | if ((*p)->s.b.sorted) | |
265 | { | |
266 | if ((*p)->u.sort->orig_data == begin) | |
267 | { | |
268 | ob = *p; | |
269 | *p = ob->next; | |
270 | free (ob->u.sort); | |
271 | goto out; | |
272 | } | |
273 | } | |
274 | else | |
275 | { | |
276 | if ((*p)->u.single == begin) | |
277 | { | |
278 | ob = *p; | |
279 | *p = ob->next; | |
280 | goto out; | |
281 | } | |
282 | } | |
52a11cbf | 283 | |
e1f9550a RH |
284 | out: |
285 | __gthread_mutex_unlock (&object_mutex); | |
30adfb85 | 286 | const int empty_table = 0; // The non-atomic path stores all tables. |
6e80a1d1 TN |
287 | #endif |
288 | ||
9be9be82 ST |
289 | // If we didn't find anything in the lookup data structures then they |
290 | // were either already destroyed or we tried to remove an empty range. | |
30adfb85 | 291 | gcc_assert (in_shutdown || (empty_table || ob)); |
e1f9550a | 292 | return (void *) ob; |
52a11cbf RH |
293 | } |
294 | ||
101fa48c | 295 | void * |
a30794da | 296 | __deregister_frame_info (const void *begin) |
101fa48c RH |
297 | { |
298 | return __deregister_frame_info_bases (begin); | |
299 | } | |
101fa48c | 300 | |
52a11cbf RH |
301 | void |
302 | __deregister_frame (void *begin) | |
303 | { | |
55fae1a9 | 304 | /* If .eh_frame is empty, we haven't registered. */ |
e9d1b155 | 305 | if (*(uword *) begin != 0) |
55fae1a9 | 306 | free (__deregister_frame_info (begin)); |
52a11cbf RH |
307 | } |
308 | ||
e1f9550a RH |
309 | \f |
310 | /* Like base_of_encoded_value, but take the base from a struct object | |
311 | instead of an _Unwind_Context. */ | |
312 | ||
313 | static _Unwind_Ptr | |
6e80a1d1 | 314 | base_from_object (unsigned char encoding, const struct object *ob) |
e1f9550a RH |
315 | { |
316 | if (encoding == DW_EH_PE_omit) | |
317 | return 0; | |
318 | ||
319 | switch (encoding & 0x70) | |
320 | { | |
321 | case DW_EH_PE_absptr: | |
322 | case DW_EH_PE_pcrel: | |
099c8b17 | 323 | case DW_EH_PE_aligned: |
e1f9550a RH |
324 | return 0; |
325 | ||
326 | case DW_EH_PE_textrel: | |
327 | return (_Unwind_Ptr) ob->tbase; | |
328 | case DW_EH_PE_datarel: | |
329 | return (_Unwind_Ptr) ob->dbase; | |
79d0dfa3 NS |
330 | default: |
331 | gcc_unreachable (); | |
e1f9550a | 332 | } |
e1f9550a RH |
333 | } |
334 | ||
335 | /* Return the FDE pointer encoding from the CIE. */ | |
336 | /* ??? This is a subset of extract_cie_info from unwind-dw2.c. */ | |
337 | ||
338 | static int | |
a30794da | 339 | get_cie_encoding (const struct dwarf_cie *cie) |
e1f9550a RH |
340 | { |
341 | const unsigned char *aug, *p; | |
342 | _Unwind_Ptr dummy; | |
f767122b AK |
343 | _uleb128_t utmp; |
344 | _sleb128_t stmp; | |
e1f9550a RH |
345 | |
346 | aug = cie->augmentation; | |
8f65940d JJ |
347 | p = aug + strlen ((const char *)aug) + 1; /* Skip the augmentation string. */ |
348 | if (__builtin_expect (cie->version >= 4, 0)) | |
349 | { | |
350 | if (p[0] != sizeof (void *) || p[1] != 0) | |
351 | return DW_EH_PE_omit; /* We are not prepared to handle unexpected | |
352 | address sizes or segment selectors. */ | |
353 | p += 2; /* Skip address size and segment size. */ | |
354 | } | |
355 | ||
e1f9550a RH |
356 | if (aug[0] != 'z') |
357 | return DW_EH_PE_absptr; | |
358 | ||
a9985a92 JM |
359 | p = read_uleb128 (p, &utmp); /* Skip code alignment. */ |
360 | p = read_sleb128 (p, &stmp); /* Skip data alignment. */ | |
0ef54a47 PB |
361 | if (cie->version == 1) /* Skip return address column. */ |
362 | p++; | |
363 | else | |
364 | p = read_uleb128 (p, &utmp); | |
e1f9550a RH |
365 | |
366 | aug++; /* Skip 'z' */ | |
a9985a92 | 367 | p = read_uleb128 (p, &utmp); /* Skip augmentation length. */ |
e1f9550a RH |
368 | while (1) |
369 | { | |
370 | /* This is what we're looking for. */ | |
371 | if (*aug == 'R') | |
372 | return *p; | |
373 | /* Personality encoding and pointer. */ | |
374 | else if (*aug == 'P') | |
099c8b17 RH |
375 | { |
376 | /* ??? Avoid dereferencing indirect pointers, since we're | |
377 | faking the base address. Gotta keep DW_EH_PE_aligned | |
378 | intact, however. */ | |
379 | p = read_encoded_value_with_base (*p & 0x7F, 0, p + 1, &dummy); | |
380 | } | |
e1f9550a RH |
381 | /* LSDA encoding. */ |
382 | else if (*aug == 'L') | |
383 | p++; | |
8fc16d72 ST |
384 | /* aarch64 b-key pointer authentication. */ |
385 | else if (*aug == 'B') | |
386 | p++; | |
e1f9550a RH |
387 | /* Otherwise end of string, or unknown augmentation. */ |
388 | else | |
389 | return DW_EH_PE_absptr; | |
390 | aug++; | |
391 | } | |
392 | } | |
393 | ||
394 | static inline int | |
a30794da | 395 | get_fde_encoding (const struct dwarf_fde *f) |
e1f9550a RH |
396 | { |
397 | return get_cie_encoding (get_cie (f)); | |
398 | } | |
399 | ||
52a11cbf | 400 | \f |
72dd050a BH |
401 | /* Sorting an array of FDEs by address. |
402 | (Ideally we would have the linker sort the FDEs so we don't have to do | |
403 | it at run time. But the linkers are not yet prepared for this.) */ | |
404 | ||
e1f9550a RH |
405 | /* Comparison routines. Three variants of increasing complexity. */ |
406 | ||
bde257ff | 407 | static int |
e1f9550a | 408 | fde_unencoded_compare (struct object *ob __attribute__((unused)), |
a30794da | 409 | const fde *x, const fde *y) |
e1f9550a | 410 | { |
ce883f54 BE |
411 | _Unwind_Ptr x_ptr, y_ptr; |
412 | memcpy (&x_ptr, x->pc_begin, sizeof (_Unwind_Ptr)); | |
413 | memcpy (&y_ptr, y->pc_begin, sizeof (_Unwind_Ptr)); | |
2f9ec5e5 HPN |
414 | |
415 | if (x_ptr > y_ptr) | |
bde257ff | 416 | return 1; |
2f9ec5e5 | 417 | if (x_ptr < y_ptr) |
bde257ff RH |
418 | return -1; |
419 | return 0; | |
e1f9550a RH |
420 | } |
421 | ||
bde257ff | 422 | static int |
a30794da | 423 | fde_single_encoding_compare (struct object *ob, const fde *x, const fde *y) |
e1f9550a RH |
424 | { |
425 | _Unwind_Ptr base, x_ptr, y_ptr; | |
426 | ||
427 | base = base_from_object (ob->s.b.encoding, ob); | |
428 | read_encoded_value_with_base (ob->s.b.encoding, base, x->pc_begin, &x_ptr); | |
429 | read_encoded_value_with_base (ob->s.b.encoding, base, y->pc_begin, &y_ptr); | |
430 | ||
bde257ff RH |
431 | if (x_ptr > y_ptr) |
432 | return 1; | |
433 | if (x_ptr < y_ptr) | |
434 | return -1; | |
435 | return 0; | |
e1f9550a RH |
436 | } |
437 | ||
bde257ff | 438 | static int |
a30794da | 439 | fde_mixed_encoding_compare (struct object *ob, const fde *x, const fde *y) |
e1f9550a RH |
440 | { |
441 | int x_encoding, y_encoding; | |
442 | _Unwind_Ptr x_ptr, y_ptr; | |
443 | ||
444 | x_encoding = get_fde_encoding (x); | |
445 | read_encoded_value_with_base (x_encoding, base_from_object (x_encoding, ob), | |
446 | x->pc_begin, &x_ptr); | |
447 | ||
448 | y_encoding = get_fde_encoding (y); | |
449 | read_encoded_value_with_base (y_encoding, base_from_object (y_encoding, ob), | |
450 | y->pc_begin, &y_ptr); | |
451 | ||
bde257ff RH |
452 | if (x_ptr > y_ptr) |
453 | return 1; | |
454 | if (x_ptr < y_ptr) | |
455 | return -1; | |
456 | return 0; | |
e1f9550a RH |
457 | } |
458 | ||
a30794da | 459 | typedef int (*fde_compare_t) (struct object *, const fde *, const fde *); |
e1f9550a | 460 | |
1c118c99 TN |
461 | // The extractor functions compute the pointer values for a block of |
462 | // fdes. The block processing hides the call overhead. | |
e1f9550a | 463 | |
1c118c99 TN |
464 | static void |
465 | fde_unencoded_extract (struct object *ob __attribute__ ((unused)), | |
466 | _Unwind_Ptr *target, const fde **x, int count) | |
467 | { | |
468 | for (int index = 0; index < count; ++index) | |
469 | memcpy (target + index, x[index]->pc_begin, sizeof (_Unwind_Ptr)); | |
470 | } | |
471 | ||
472 | static void | |
473 | fde_single_encoding_extract (struct object *ob, _Unwind_Ptr *target, | |
474 | const fde **x, int count) | |
475 | { | |
476 | _Unwind_Ptr base; | |
477 | ||
478 | base = base_from_object (ob->s.b.encoding, ob); | |
479 | for (int index = 0; index < count; ++index) | |
480 | read_encoded_value_with_base (ob->s.b.encoding, base, x[index]->pc_begin, | |
481 | target + index); | |
482 | } | |
483 | ||
484 | static void | |
485 | fde_mixed_encoding_extract (struct object *ob, _Unwind_Ptr *target, | |
486 | const fde **x, int count) | |
487 | { | |
488 | for (int index = 0; index < count; ++index) | |
489 | { | |
490 | int encoding = get_fde_encoding (x[index]); | |
491 | read_encoded_value_with_base (encoding, base_from_object (encoding, ob), | |
492 | x[index]->pc_begin, target + index); | |
493 | } | |
494 | } | |
495 | ||
496 | typedef void (*fde_extractor_t) (struct object *, _Unwind_Ptr *, const fde **, | |
497 | int); | |
498 | ||
499 | // Data is is sorted using radix sort if possible, using an temporary | |
500 | // auxiliary data structure of the same size as the input. When running | |
501 | // out of memory do in-place heap sort. | |
72dd050a | 502 | |
e1f9550a | 503 | struct fde_accumulator |
72dd050a | 504 | { |
e1f9550a | 505 | struct fde_vector *linear; |
1c118c99 | 506 | struct fde_vector *aux; |
e1f9550a | 507 | }; |
52a11cbf | 508 | |
d593d1d2 | 509 | static inline int |
e1f9550a | 510 | start_fde_sort (struct fde_accumulator *accu, size_t count) |
72dd050a | 511 | { |
e1f9550a RH |
512 | size_t size; |
513 | if (! count) | |
514 | return 0; | |
515 | ||
a30794da | 516 | size = sizeof (struct fde_vector) + sizeof (const fde *) * count; |
703ad42b | 517 | if ((accu->linear = malloc (size))) |
e1f9550a RH |
518 | { |
519 | accu->linear->count = 0; | |
1c118c99 TN |
520 | if ((accu->aux = malloc (size))) |
521 | accu->aux->count = 0; | |
e1f9550a RH |
522 | return 1; |
523 | } | |
524 | else | |
41077ce4 | 525 | return 0; |
72dd050a | 526 | } |
0021b564 | 527 | |
72dd050a | 528 | static inline void |
a30794da | 529 | fde_insert (struct fde_accumulator *accu, const fde *this_fde) |
72dd050a | 530 | { |
e1f9550a RH |
531 | if (accu->linear) |
532 | accu->linear->array[accu->linear->count++] = this_fde; | |
72dd050a BH |
533 | } |
534 | ||
a30794da | 535 | #define SWAP(x,y) do { const fde * tmp = x; x = y; y = tmp; } while (0) |
80d83b16 JM |
536 | |
537 | /* Convert a semi-heap to a heap. A semi-heap is a heap except possibly | |
538 | for the first (root) node; push it down to its rightful place. */ | |
539 | ||
540 | static void | |
a30794da | 541 | frame_downheap (struct object *ob, fde_compare_t fde_compare, const fde **a, |
80d83b16 JM |
542 | int lo, int hi) |
543 | { | |
544 | int i, j; | |
545 | ||
546 | for (i = lo, j = 2*i+1; | |
547 | j < hi; | |
548 | j = 2*i+1) | |
549 | { | |
550 | if (j+1 < hi && fde_compare (ob, a[j], a[j+1]) < 0) | |
551 | ++j; | |
552 | ||
553 | if (fde_compare (ob, a[i], a[j]) < 0) | |
554 | { | |
555 | SWAP (a[i], a[j]); | |
556 | i = j; | |
557 | } | |
558 | else | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
99b13ed3 JW |
563 | /* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must |
564 | use a name that does not conflict. */ | |
e1f9550a RH |
565 | |
566 | static void | |
567 | frame_heapsort (struct object *ob, fde_compare_t fde_compare, | |
568 | struct fde_vector *erratic) | |
72dd050a BH |
569 | { |
570 | /* For a description of this algorithm, see: | |
571 | Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed., | |
2d76cb1a | 572 | p. 60-61. */ |
a30794da | 573 | const fde ** a = erratic->array; |
72dd050a BH |
574 | /* A portion of the array is called a "heap" if for all i>=0: |
575 | If i and 2i+1 are valid indices, then a[i] >= a[2i+1]. | |
2d76cb1a | 576 | If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */ |
72dd050a | 577 | size_t n = erratic->count; |
80d83b16 JM |
578 | int m; |
579 | ||
580 | /* Expand our heap incrementally from the end of the array, heapifying | |
581 | each resulting semi-heap as we go. After each step, a[m] is the top | |
582 | of a heap. */ | |
583 | for (m = n/2-1; m >= 0; --m) | |
584 | frame_downheap (ob, fde_compare, a, m, n); | |
585 | ||
586 | /* Shrink our heap incrementally from the end of the array, first | |
587 | swapping out the largest element a[0] and then re-heapifying the | |
588 | resulting semi-heap. After each step, a[0..m) is a heap. */ | |
589 | for (m = n-1; m >= 1; --m) | |
72dd050a | 590 | { |
80d83b16 JM |
591 | SWAP (a[0], a[m]); |
592 | frame_downheap (ob, fde_compare, a, 0, m); | |
72dd050a BH |
593 | } |
594 | #undef SWAP | |
595 | } | |
596 | ||
1c118c99 | 597 | // Radix sort data in V1 using V2 as aux memory. Runtime O(n). |
e1f9550a | 598 | static inline void |
1c118c99 TN |
599 | fde_radixsort (struct object *ob, fde_extractor_t fde_extractor, |
600 | struct fde_vector *v1, struct fde_vector *v2) | |
0021b564 | 601 | { |
1c118c99 TN |
602 | #define FANOUTBITS 8 |
603 | #define FANOUT (1 << FANOUTBITS) | |
604 | #define BLOCKSIZE 128 | |
605 | const unsigned rounds | |
606 | = (__CHAR_BIT__ * sizeof (_Unwind_Ptr) + FANOUTBITS - 1) / FANOUTBITS; | |
607 | const fde **a1 = v1->array, **a2 = v2->array; | |
608 | _Unwind_Ptr ptrs[BLOCKSIZE + 1]; | |
609 | unsigned n = v1->count; | |
610 | for (unsigned round = 0; round != rounds; ++round) | |
0021b564 | 611 | { |
1c118c99 TN |
612 | unsigned counts[FANOUT] = {0}; |
613 | unsigned violations = 0; | |
614 | ||
615 | // Count the number of elements per bucket and check if we are already | |
616 | // sorted. | |
617 | _Unwind_Ptr last = 0; | |
618 | for (unsigned i = 0; i < n;) | |
a01da83b | 619 | { |
1c118c99 TN |
620 | unsigned chunk = ((n - i) <= BLOCKSIZE) ? (n - i) : BLOCKSIZE; |
621 | fde_extractor (ob, ptrs + 1, a1 + i, chunk); | |
622 | ptrs[0] = last; | |
623 | for (unsigned j = 0; j < chunk; ++j) | |
a01da83b | 624 | { |
1c118c99 TN |
625 | unsigned b = (ptrs[j + 1] >> (round * FANOUTBITS)) & (FANOUT - 1); |
626 | counts[b]++; | |
627 | // Use summation instead of an if to eliminate branches. | |
628 | violations += ptrs[j + 1] < ptrs[j]; | |
a01da83b | 629 | } |
1c118c99 TN |
630 | i += chunk; |
631 | last = ptrs[chunk]; | |
a01da83b | 632 | } |
1c118c99 TN |
633 | |
634 | // Stop if we are already sorted. | |
635 | if (!violations) | |
636 | { | |
637 | // The sorted data is in a1 now. | |
638 | a2 = a1; | |
639 | break; | |
640 | } | |
641 | ||
642 | // Compute the prefix sum. | |
643 | unsigned sum = 0; | |
644 | for (unsigned i = 0; i != FANOUT; ++i) | |
645 | { | |
646 | unsigned s = sum; | |
647 | sum += counts[i]; | |
648 | counts[i] = s; | |
649 | } | |
650 | ||
651 | // Place all elements. | |
652 | for (unsigned i = 0; i < n;) | |
653 | { | |
654 | unsigned chunk = ((n - i) <= BLOCKSIZE) ? (n - i) : BLOCKSIZE; | |
655 | fde_extractor (ob, ptrs, a1 + i, chunk); | |
656 | for (unsigned j = 0; j < chunk; ++j) | |
657 | { | |
658 | unsigned b = (ptrs[j] >> (round * FANOUTBITS)) & (FANOUT - 1); | |
659 | a2[counts[b]++] = a1[i + j]; | |
660 | } | |
661 | i += chunk; | |
662 | } | |
663 | ||
664 | // Swap a1 and a2. | |
665 | const fde **tmp = a1; | |
666 | a1 = a2; | |
667 | a2 = tmp; | |
0021b564 | 668 | } |
1c118c99 TN |
669 | #undef BLOCKSIZE |
670 | #undef FANOUT | |
671 | #undef FANOUTBITS | |
672 | ||
673 | // The data is in a2 now, move in place if needed. | |
674 | if (a2 != v1->array) | |
675 | memcpy (v1->array, a2, sizeof (const fde *) * n); | |
0021b564 JM |
676 | } |
677 | ||
e1f9550a RH |
678 | static inline void |
679 | end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count) | |
72dd050a | 680 | { |
79d0dfa3 | 681 | gcc_assert (!accu->linear || accu->linear->count == count); |
e1f9550a | 682 | |
1c118c99 | 683 | if (accu->aux) |
d593d1d2 | 684 | { |
1c118c99 TN |
685 | fde_extractor_t fde_extractor; |
686 | if (ob->s.b.mixed_encoding) | |
687 | fde_extractor = fde_mixed_encoding_extract; | |
688 | else if (ob->s.b.encoding == DW_EH_PE_absptr) | |
689 | fde_extractor = fde_unencoded_extract; | |
690 | else | |
691 | fde_extractor = fde_single_encoding_extract; | |
692 | ||
693 | fde_radixsort (ob, fde_extractor, accu->linear, accu->aux); | |
694 | free (accu->aux); | |
d593d1d2 NS |
695 | } |
696 | else | |
697 | { | |
1c118c99 TN |
698 | fde_compare_t fde_compare; |
699 | if (ob->s.b.mixed_encoding) | |
700 | fde_compare = fde_mixed_encoding_compare; | |
701 | else if (ob->s.b.encoding == DW_EH_PE_absptr) | |
702 | fde_compare = fde_unencoded_compare; | |
703 | else | |
704 | fde_compare = fde_single_encoding_compare; | |
705 | ||
706 | /* We've not managed to malloc an aux array, | |
e1f9550a RH |
707 | so heap sort in the linear one. */ |
708 | frame_heapsort (ob, fde_compare, accu->linear); | |
d593d1d2 | 709 | } |
72dd050a BH |
710 | } |
711 | ||
6e80a1d1 TN |
712 | /* Inspect the fde array beginning at this_fde. This |
713 | function can be used either in query mode (RANGE is | |
714 | not null, OB is const), or in update mode (RANGE is | |
715 | null, OB is modified). In query mode the function computes | |
716 | the range of PC values and stores it in RANGE. In | |
717 | update mode it updates encoding, mixed_encoding, and pc_begin | |
718 | for OB. Return the number of fdes encountered along the way. */ | |
e1f9550a | 719 | |
52a11cbf | 720 | static size_t |
6e80a1d1 | 721 | classify_object_over_fdes (struct object *ob, const fde *this_fde, |
d458f806 | 722 | uintptr_type *range) |
52a11cbf | 723 | { |
a30794da | 724 | const struct dwarf_cie *last_cie = 0; |
e1f9550a RH |
725 | size_t count = 0; |
726 | int encoding = DW_EH_PE_absptr; | |
727 | _Unwind_Ptr base = 0; | |
0021b564 | 728 | |
3cfe49da | 729 | for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde)) |
e1f9550a | 730 | { |
a30794da | 731 | const struct dwarf_cie *this_cie; |
e1f9550a | 732 | _Unwind_Ptr mask, pc_begin; |
52a11cbf | 733 | |
e1f9550a RH |
734 | /* Skip CIEs. */ |
735 | if (this_fde->CIE_delta == 0) | |
736 | continue; | |
52a11cbf | 737 | |
e1f9550a RH |
738 | /* Determine the encoding for this FDE. Note mixed encoded |
739 | objects for later. */ | |
740 | this_cie = get_cie (this_fde); | |
741 | if (this_cie != last_cie) | |
742 | { | |
743 | last_cie = this_cie; | |
744 | encoding = get_cie_encoding (this_cie); | |
8f65940d JJ |
745 | if (encoding == DW_EH_PE_omit) |
746 | return -1; | |
e1f9550a | 747 | base = base_from_object (encoding, ob); |
6e80a1d1 TN |
748 | if (!range) |
749 | { | |
750 | if (ob->s.b.encoding == DW_EH_PE_omit) | |
751 | ob->s.b.encoding = encoding; | |
752 | else if (ob->s.b.encoding != encoding) | |
753 | ob->s.b.mixed_encoding = 1; | |
754 | } | |
e1f9550a | 755 | } |
0021b564 | 756 | |
6e80a1d1 TN |
757 | const unsigned char *p; |
758 | p = read_encoded_value_with_base (encoding, base, this_fde->pc_begin, | |
759 | &pc_begin); | |
0021b564 | 760 | |
e1f9550a RH |
761 | /* Take care to ignore link-once functions that were removed. |
762 | In these cases, the function address will be NULL, but if | |
763 | the encoding is smaller than a pointer a true NULL may not | |
764 | be representable. Assume 0 in the representable bits is NULL. */ | |
765 | mask = size_of_encoded_value (encoding); | |
766 | if (mask < sizeof (void *)) | |
0b1d7060 | 767 | mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1; |
e1f9550a RH |
768 | else |
769 | mask = -1; | |
770 | ||
771 | if ((pc_begin & mask) == 0) | |
772 | continue; | |
154bba13 | 773 | |
e1f9550a | 774 | count += 1; |
6e80a1d1 TN |
775 | if (range) |
776 | { | |
777 | _Unwind_Ptr pc_range, pc_end; | |
778 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
779 | pc_end = pc_begin + pc_range; | |
780 | if ((!range[0]) && (!range[1])) | |
781 | { | |
782 | range[0] = pc_begin; | |
783 | range[1] = pc_end; | |
784 | } | |
785 | else | |
786 | { | |
787 | if (pc_begin < range[0]) | |
788 | range[0] = pc_begin; | |
789 | if (pc_end > range[1]) | |
790 | range[1] = pc_end; | |
791 | } | |
792 | } | |
793 | else | |
794 | { | |
795 | if ((void *) pc_begin < ob->pc_begin) | |
796 | ob->pc_begin = (void *) pc_begin; | |
797 | } | |
52a11cbf | 798 | } |
154bba13 | 799 | |
e1f9550a | 800 | return count; |
0021b564 JM |
801 | } |
802 | ||
e1f9550a | 803 | static void |
a30794da | 804 | add_fdes (struct object *ob, struct fde_accumulator *accu, const fde *this_fde) |
a3fd4e75 | 805 | { |
a30794da | 806 | const struct dwarf_cie *last_cie = 0; |
e1f9550a RH |
807 | int encoding = ob->s.b.encoding; |
808 | _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob); | |
809 | ||
3cfe49da | 810 | for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde)) |
52a11cbf | 811 | { |
a30794da | 812 | const struct dwarf_cie *this_cie; |
52a11cbf | 813 | |
e1f9550a RH |
814 | /* Skip CIEs. */ |
815 | if (this_fde->CIE_delta == 0) | |
816 | continue; | |
817 | ||
818 | if (ob->s.b.mixed_encoding) | |
819 | { | |
820 | /* Determine the encoding for this FDE. Note mixed encoded | |
821 | objects for later. */ | |
822 | this_cie = get_cie (this_fde); | |
823 | if (this_cie != last_cie) | |
824 | { | |
825 | last_cie = this_cie; | |
826 | encoding = get_cie_encoding (this_cie); | |
827 | base = base_from_object (encoding, ob); | |
828 | } | |
829 | } | |
830 | ||
831 | if (encoding == DW_EH_PE_absptr) | |
832 | { | |
ce883f54 BE |
833 | _Unwind_Ptr ptr; |
834 | memcpy (&ptr, this_fde->pc_begin, sizeof (_Unwind_Ptr)); | |
835 | if (ptr == 0) | |
e1f9550a RH |
836 | continue; |
837 | } | |
838 | else | |
839 | { | |
840 | _Unwind_Ptr pc_begin, mask; | |
841 | ||
842 | read_encoded_value_with_base (encoding, base, this_fde->pc_begin, | |
843 | &pc_begin); | |
844 | ||
845 | /* Take care to ignore link-once functions that were removed. | |
846 | In these cases, the function address will be NULL, but if | |
847 | the encoding is smaller than a pointer a true NULL may not | |
848 | be representable. Assume 0 in the representable bits is NULL. */ | |
849 | mask = size_of_encoded_value (encoding); | |
850 | if (mask < sizeof (void *)) | |
0b1d7060 | 851 | mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1; |
e1f9550a RH |
852 | else |
853 | mask = -1; | |
854 | ||
855 | if ((pc_begin & mask) == 0) | |
856 | continue; | |
857 | } | |
858 | ||
859 | fde_insert (accu, this_fde); | |
52a11cbf | 860 | } |
a3fd4e75 JL |
861 | } |
862 | ||
52a11cbf RH |
863 | /* Set up a sorted array of pointers to FDEs for a loaded object. We |
864 | count up the entries before allocating the array because it's likely to | |
865 | be faster. We can be called multiple times, should we have failed to | |
866 | allocate a sorted fde array on a previous occasion. */ | |
0021b564 | 867 | |
e1f9550a RH |
868 | static inline void |
869 | init_object (struct object* ob) | |
0021b564 | 870 | { |
e1f9550a | 871 | struct fde_accumulator accu; |
52a11cbf | 872 | size_t count; |
0021b564 | 873 | |
e1f9550a RH |
874 | count = ob->s.b.count; |
875 | if (count == 0) | |
52a11cbf | 876 | { |
e1f9550a RH |
877 | if (ob->s.b.from_array) |
878 | { | |
879 | fde **p = ob->u.array; | |
880 | for (count = 0; *p; ++p) | |
8f65940d | 881 | { |
6e80a1d1 | 882 | size_t cur_count = classify_object_over_fdes (ob, *p, NULL); |
8f65940d JJ |
883 | if (cur_count == (size_t) -1) |
884 | goto unhandled_fdes; | |
885 | count += cur_count; | |
886 | } | |
e1f9550a RH |
887 | } |
888 | else | |
8f65940d | 889 | { |
6e80a1d1 | 890 | count = classify_object_over_fdes (ob, ob->u.single, NULL); |
8f65940d JJ |
891 | if (count == (size_t) -1) |
892 | { | |
893 | static const fde terminator; | |
894 | unhandled_fdes: | |
895 | ob->s.i = 0; | |
896 | ob->s.b.encoding = DW_EH_PE_omit; | |
897 | ob->u.single = &terminator; | |
898 | return; | |
899 | } | |
900 | } | |
e1f9550a RH |
901 | |
902 | /* The count field we have in the main struct object is somewhat | |
903 | limited, but should suffice for virtually all cases. If the | |
904 | counted value doesn't fit, re-write a zero. The worst that | |
905 | happens is that we re-count next time -- admittedly non-trivial | |
906 | in that this implies some 2M fdes, but at least we function. */ | |
907 | ob->s.b.count = count; | |
908 | if (ob->s.b.count != count) | |
909 | ob->s.b.count = 0; | |
52a11cbf | 910 | } |
154bba13 | 911 | |
e1f9550a | 912 | if (!start_fde_sort (&accu, count)) |
52a11cbf | 913 | return; |
154bba13 | 914 | |
e1f9550a | 915 | if (ob->s.b.from_array) |
52a11cbf | 916 | { |
e1f9550a RH |
917 | fde **p; |
918 | for (p = ob->u.array; *p; ++p) | |
41077ce4 | 919 | add_fdes (ob, &accu, *p); |
52a11cbf RH |
920 | } |
921 | else | |
e1f9550a RH |
922 | add_fdes (ob, &accu, ob->u.single); |
923 | ||
924 | end_fde_sort (ob, &accu, count); | |
925 | ||
926 | /* Save the original fde pointer, since this is the key by which the | |
927 | DSO will deregister the object. */ | |
928 | accu.linear->orig_data = ob->u.single; | |
929 | ob->u.sort = accu.linear; | |
930 | ||
6e56633d TN |
931 | #ifdef ATOMIC_FDE_FAST_PATH |
932 | // We must update the sorted bit with an atomic operation | |
933 | struct object tmp; | |
934 | tmp.s.b = ob->s.b; | |
935 | tmp.s.b.sorted = 1; | |
936 | __atomic_store (&(ob->s.b), &(tmp.s.b), __ATOMIC_RELEASE); | |
937 | #else | |
e1f9550a | 938 | ob->s.b.sorted = 1; |
6e56633d | 939 | #endif |
a3fd4e75 JL |
940 | } |
941 | ||
6e80a1d1 TN |
942 | #ifdef ATOMIC_FDE_FAST_PATH |
943 | /* Get the PC range for lookup */ | |
944 | static void | |
d458f806 | 945 | get_pc_range (const struct object *ob, uintptr_type *range) |
6e80a1d1 TN |
946 | { |
947 | // It is safe to cast to non-const object* here as | |
948 | // classify_object_over_fdes does not modify ob in query mode. | |
d458f806 | 949 | struct object *ncob = (struct object *) (uintptr_type) ob; |
6e80a1d1 TN |
950 | range[0] = range[1] = 0; |
951 | if (ob->s.b.sorted) | |
952 | { | |
953 | classify_object_over_fdes (ncob, ob->u.sort->orig_data, range); | |
954 | } | |
955 | else if (ob->s.b.from_array) | |
956 | { | |
957 | fde **p = ob->u.array; | |
958 | for (; *p; ++p) | |
959 | classify_object_over_fdes (ncob, *p, range); | |
960 | } | |
961 | else | |
962 | { | |
963 | classify_object_over_fdes (ncob, ob->u.single, range); | |
964 | } | |
965 | } | |
966 | #endif | |
967 | ||
e1f9550a RH |
968 | /* A linear search through a set of FDEs for the given PC. This is |
969 | used when there was insufficient memory to allocate and sort an | |
970 | array. */ | |
971 | ||
a30794da AJ |
972 | static const fde * |
973 | linear_search_fdes (struct object *ob, const fde *this_fde, void *pc) | |
0021b564 | 974 | { |
a30794da | 975 | const struct dwarf_cie *last_cie = 0; |
e1f9550a RH |
976 | int encoding = ob->s.b.encoding; |
977 | _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob); | |
978 | ||
3cfe49da | 979 | for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde)) |
e1f9550a | 980 | { |
a30794da | 981 | const struct dwarf_cie *this_cie; |
e1f9550a RH |
982 | _Unwind_Ptr pc_begin, pc_range; |
983 | ||
984 | /* Skip CIEs. */ | |
985 | if (this_fde->CIE_delta == 0) | |
986 | continue; | |
987 | ||
988 | if (ob->s.b.mixed_encoding) | |
989 | { | |
990 | /* Determine the encoding for this FDE. Note mixed encoded | |
991 | objects for later. */ | |
992 | this_cie = get_cie (this_fde); | |
993 | if (this_cie != last_cie) | |
994 | { | |
995 | last_cie = this_cie; | |
996 | encoding = get_cie_encoding (this_cie); | |
997 | base = base_from_object (encoding, ob); | |
998 | } | |
999 | } | |
1000 | ||
1001 | if (encoding == DW_EH_PE_absptr) | |
1002 | { | |
ce883f54 BE |
1003 | const _Unwind_Ptr *pc_array = (const _Unwind_Ptr *) this_fde->pc_begin; |
1004 | pc_begin = pc_array[0]; | |
1005 | pc_range = pc_array[1]; | |
e1f9550a RH |
1006 | if (pc_begin == 0) |
1007 | continue; | |
1008 | } | |
1009 | else | |
1010 | { | |
1011 | _Unwind_Ptr mask; | |
ca29916b | 1012 | const unsigned char *p; |
e1f9550a RH |
1013 | |
1014 | p = read_encoded_value_with_base (encoding, base, | |
1015 | this_fde->pc_begin, &pc_begin); | |
1016 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
1017 | ||
1018 | /* Take care to ignore link-once functions that were removed. | |
1019 | In these cases, the function address will be NULL, but if | |
1020 | the encoding is smaller than a pointer a true NULL may not | |
1021 | be representable. Assume 0 in the representable bits is NULL. */ | |
1022 | mask = size_of_encoded_value (encoding); | |
1023 | if (mask < sizeof (void *)) | |
0b1d7060 | 1024 | mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1; |
e1f9550a RH |
1025 | else |
1026 | mask = -1; | |
1027 | ||
1028 | if ((pc_begin & mask) == 0) | |
1029 | continue; | |
1030 | } | |
1031 | ||
e9d1b155 | 1032 | if ((_Unwind_Ptr) pc - pc_begin < pc_range) |
41077ce4 | 1033 | return this_fde; |
e1f9550a RH |
1034 | } |
1035 | ||
1036 | return NULL; | |
1037 | } | |
1038 | ||
1039 | /* Binary search for an FDE containing the given PC. Here are three | |
1040 | implementations of increasing complexity. */ | |
1041 | ||
a30794da | 1042 | static inline const fde * |
e1f9550a RH |
1043 | binary_search_unencoded_fdes (struct object *ob, void *pc) |
1044 | { | |
1045 | struct fde_vector *vec = ob->u.sort; | |
52a11cbf | 1046 | size_t lo, hi; |
41077ce4 | 1047 | |
e1f9550a RH |
1048 | for (lo = 0, hi = vec->count; lo < hi; ) |
1049 | { | |
1050 | size_t i = (lo + hi) / 2; | |
5f754896 | 1051 | const fde *const f = vec->array[i]; |
ce883f54 BE |
1052 | void *pc_begin; |
1053 | uaddr pc_range; | |
1054 | memcpy (&pc_begin, (const void * const *) f->pc_begin, sizeof (void *)); | |
1055 | memcpy (&pc_range, (const uaddr *) f->pc_begin + 1, sizeof (uaddr)); | |
e1f9550a RH |
1056 | |
1057 | if (pc < pc_begin) | |
1058 | hi = i; | |
1059 | else if (pc >= pc_begin + pc_range) | |
1060 | lo = i + 1; | |
1061 | else | |
1062 | return f; | |
1063 | } | |
0021b564 | 1064 | |
e1f9550a RH |
1065 | return NULL; |
1066 | } | |
154bba13 | 1067 | |
a30794da | 1068 | static inline const fde * |
e1f9550a RH |
1069 | binary_search_single_encoding_fdes (struct object *ob, void *pc) |
1070 | { | |
1071 | struct fde_vector *vec = ob->u.sort; | |
1072 | int encoding = ob->s.b.encoding; | |
1073 | _Unwind_Ptr base = base_from_object (encoding, ob); | |
1074 | size_t lo, hi; | |
41077ce4 | 1075 | |
e1f9550a | 1076 | for (lo = 0, hi = vec->count; lo < hi; ) |
0021b564 | 1077 | { |
e1f9550a | 1078 | size_t i = (lo + hi) / 2; |
a30794da | 1079 | const fde *f = vec->array[i]; |
e1f9550a | 1080 | _Unwind_Ptr pc_begin, pc_range; |
ca29916b | 1081 | const unsigned char *p; |
e1f9550a RH |
1082 | |
1083 | p = read_encoded_value_with_base (encoding, base, f->pc_begin, | |
1084 | &pc_begin); | |
1085 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
1086 | ||
e9d1b155 | 1087 | if ((_Unwind_Ptr) pc < pc_begin) |
e1f9550a | 1088 | hi = i; |
e9d1b155 | 1089 | else if ((_Unwind_Ptr) pc >= pc_begin + pc_range) |
e1f9550a RH |
1090 | lo = i + 1; |
1091 | else | |
1092 | return f; | |
52a11cbf | 1093 | } |
0021b564 | 1094 | |
e1f9550a RH |
1095 | return NULL; |
1096 | } | |
1097 | ||
a30794da | 1098 | static inline const fde * |
e1f9550a RH |
1099 | binary_search_mixed_encoding_fdes (struct object *ob, void *pc) |
1100 | { | |
1101 | struct fde_vector *vec = ob->u.sort; | |
1102 | size_t lo, hi; | |
41077ce4 | 1103 | |
e1f9550a | 1104 | for (lo = 0, hi = vec->count; lo < hi; ) |
52a11cbf | 1105 | { |
e1f9550a | 1106 | size_t i = (lo + hi) / 2; |
a30794da | 1107 | const fde *f = vec->array[i]; |
e1f9550a | 1108 | _Unwind_Ptr pc_begin, pc_range; |
ca29916b | 1109 | const unsigned char *p; |
e1f9550a RH |
1110 | int encoding; |
1111 | ||
1112 | encoding = get_fde_encoding (f); | |
1113 | p = read_encoded_value_with_base (encoding, | |
1114 | base_from_object (encoding, ob), | |
1115 | f->pc_begin, &pc_begin); | |
1116 | read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range); | |
1117 | ||
e9d1b155 | 1118 | if ((_Unwind_Ptr) pc < pc_begin) |
e1f9550a | 1119 | hi = i; |
e9d1b155 | 1120 | else if ((_Unwind_Ptr) pc >= pc_begin + pc_range) |
e1f9550a RH |
1121 | lo = i + 1; |
1122 | else | |
1123 | return f; | |
52a11cbf | 1124 | } |
0021b564 | 1125 | |
e1f9550a RH |
1126 | return NULL; |
1127 | } | |
52a11cbf | 1128 | |
a30794da | 1129 | static const fde * |
e1f9550a RH |
1130 | search_object (struct object* ob, void *pc) |
1131 | { | |
6e80a1d1 TN |
1132 | /* The fast path initializes objects eagerly to avoid locking. |
1133 | * On the slow path we initialize them now */ | |
1134 | #ifndef ATOMIC_FDE_FAST_PATH | |
e1f9550a RH |
1135 | /* If the data hasn't been sorted, try to do this now. We may have |
1136 | more memory available than last time we tried. */ | |
1137 | if (! ob->s.b.sorted) | |
52a11cbf | 1138 | { |
e1f9550a | 1139 | init_object (ob); |
52a11cbf | 1140 | |
e1f9550a RH |
1141 | /* Despite the above comment, the normal reason to get here is |
1142 | that we've not processed this object before. A quick range | |
1143 | check is in order. */ | |
1144 | if (pc < ob->pc_begin) | |
1145 | return NULL; | |
1146 | } | |
6e80a1d1 | 1147 | #endif |
e1f9550a RH |
1148 | |
1149 | if (ob->s.b.sorted) | |
1150 | { | |
1151 | if (ob->s.b.mixed_encoding) | |
1152 | return binary_search_mixed_encoding_fdes (ob, pc); | |
1153 | else if (ob->s.b.encoding == DW_EH_PE_absptr) | |
1154 | return binary_search_unencoded_fdes (ob, pc); | |
1155 | else | |
1156 | return binary_search_single_encoding_fdes (ob, pc); | |
0021b564 | 1157 | } |
52a11cbf RH |
1158 | else |
1159 | { | |
fa10beec | 1160 | /* Long slow laborious linear search, cos we've no memory. */ |
e1f9550a | 1161 | if (ob->s.b.from_array) |
41077ce4 KH |
1162 | { |
1163 | fde **p; | |
e1f9550a RH |
1164 | for (p = ob->u.array; *p ; p++) |
1165 | { | |
a30794da | 1166 | const fde *f = linear_search_fdes (ob, *p, pc); |
41077ce4 | 1167 | if (f) |
e1f9550a | 1168 | return f; |
41077ce4 | 1169 | } |
e1f9550a RH |
1170 | return NULL; |
1171 | } | |
52a11cbf | 1172 | else |
e1f9550a RH |
1173 | return linear_search_fdes (ob, ob->u.single, pc); |
1174 | } | |
1175 | } | |
1176 | ||
6e56633d TN |
1177 | #ifdef ATOMIC_FDE_FAST_PATH |
1178 | ||
1179 | // Check if the object was already initialized | |
1180 | static inline bool | |
1181 | is_object_initialized (struct object *ob) | |
1182 | { | |
1183 | // We have to use acquire atomics for the read, which | |
1184 | // is a bit involved as we read from a bitfield | |
1185 | struct object tmp; | |
1186 | __atomic_load (&(ob->s.b), &(tmp.s.b), __ATOMIC_ACQUIRE); | |
1187 | return tmp.s.b.sorted; | |
1188 | } | |
1189 | ||
1190 | #endif | |
1191 | ||
a30794da | 1192 | const fde * |
e1f9550a RH |
1193 | _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases) |
1194 | { | |
1195 | struct object *ob; | |
a30794da | 1196 | const fde *f = NULL; |
e1f9550a | 1197 | |
29991048 | 1198 | #ifdef ATOMIC_FDE_FAST_PATH |
d458f806 | 1199 | ob = btree_lookup (®istered_frames, (uintptr_type) pc); |
6e80a1d1 | 1200 | if (!ob) |
29991048 | 1201 | return NULL; |
6e80a1d1 | 1202 | |
6e56633d TN |
1203 | // Initialize the object lazily |
1204 | if (!is_object_initialized (ob)) | |
1205 | { | |
1206 | // Check again under mutex | |
1207 | init_object_mutex_once (); | |
1208 | __gthread_mutex_lock (&object_mutex); | |
1209 | ||
1210 | if (!ob->s.b.sorted) | |
1211 | { | |
1212 | init_object (ob); | |
1213 | } | |
1214 | ||
1215 | __gthread_mutex_unlock (&object_mutex); | |
1216 | } | |
1217 | ||
6e80a1d1 TN |
1218 | f = search_object (ob, pc); |
1219 | #else | |
29991048 | 1220 | |
e1f9550a RH |
1221 | init_object_mutex_once (); |
1222 | __gthread_mutex_lock (&object_mutex); | |
1223 | ||
1224 | /* Linear search through the classified objects, to find the one | |
e3aafbad | 1225 | containing the pc. Note that pc_begin is sorted descending, and |
e1f9550a RH |
1226 | we expect objects to be non-overlapping. */ |
1227 | for (ob = seen_objects; ob; ob = ob->next) | |
1228 | if (pc >= ob->pc_begin) | |
1229 | { | |
1230 | f = search_object (ob, pc); | |
1231 | if (f) | |
1232 | goto fini; | |
1233 | break; | |
1234 | } | |
1235 | ||
1236 | /* Classify and search the objects we've not yet processed. */ | |
1237 | while ((ob = unseen_objects)) | |
1238 | { | |
1239 | struct object **p; | |
1240 | ||
1241 | unseen_objects = ob->next; | |
1242 | f = search_object (ob, pc); | |
1243 | ||
1244 | /* Insert the object into the classified list. */ | |
1245 | for (p = &seen_objects; *p ; p = &(*p)->next) | |
1246 | if ((*p)->pc_begin < ob->pc_begin) | |
1247 | break; | |
1248 | ob->next = *p; | |
1249 | *p = ob; | |
1250 | ||
1251 | if (f) | |
1252 | goto fini; | |
1253 | } | |
1254 | ||
1255 | fini: | |
1256 | __gthread_mutex_unlock (&object_mutex); | |
6e80a1d1 | 1257 | #endif |
e1f9550a RH |
1258 | |
1259 | if (f) | |
1260 | { | |
1261 | int encoding; | |
950ccbc4 | 1262 | _Unwind_Ptr func; |
e1f9550a RH |
1263 | |
1264 | bases->tbase = ob->tbase; | |
1265 | bases->dbase = ob->dbase; | |
154bba13 | 1266 | |
e1f9550a RH |
1267 | encoding = ob->s.b.encoding; |
1268 | if (ob->s.b.mixed_encoding) | |
1269 | encoding = get_fde_encoding (f); | |
1270 | read_encoded_value_with_base (encoding, base_from_object (encoding, ob), | |
950ccbc4 NS |
1271 | f->pc_begin, &func); |
1272 | bases->func = (void *) func; | |
52a11cbf | 1273 | } |
0021b564 | 1274 | |
e1f9550a | 1275 | return f; |
a3fd4e75 | 1276 | } |