xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Subroutines needed for unwinding stack frames for exception handling. */ |
| 2 | /* Copyright (C) 1997-2016 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 | |
| 22 | struct fde_vector |
| 23 | { |
| 24 | void *orig_data; |
| 25 | size_t count; |
| 26 | struct dwarf_fde *array __flexarr; |
| 27 | }; |
| 28 | |
| 29 | #ifdef _LIBC |
| 30 | #include <gccframe.h> |
| 31 | #else |
| 32 | struct object |
| 33 | { |
| 34 | void *pc_begin; |
| 35 | void *tbase; |
| 36 | void *dbase; |
| 37 | union { |
| 38 | struct dwarf_fde *single; |
| 39 | struct dwarf_fde **array; |
| 40 | struct fde_vector *sort; |
| 41 | } u; |
| 42 | |
| 43 | union { |
| 44 | struct { |
| 45 | unsigned long sorted : 1; |
| 46 | unsigned long from_array : 1; |
| 47 | unsigned long mixed_encoding : 1; |
| 48 | unsigned long encoding : 8; |
| 49 | /* ??? Wish there was an easy way to detect a 64-bit host here; |
| 50 | we've got 32 bits left to play with... */ |
| 51 | unsigned long count : 21; |
| 52 | } b; |
| 53 | size_t i; |
| 54 | } s; |
| 55 | |
| 56 | #ifdef DWARF2_OBJECT_END_PTR_EXTENSION |
| 57 | char *fde_end; |
| 58 | #endif |
| 59 | |
| 60 | struct object *next; |
| 61 | }; |
| 62 | #endif |
| 63 | |
| 64 | /* This is the original definition of struct object. While the struct |
| 65 | itself was opaque to users, they did know how large it was, and |
| 66 | allocate one statically in crtbegin for each DSO. Keep this around |
| 67 | so that we're aware of the static size limitations for the new struct. */ |
| 68 | struct old_object |
| 69 | { |
| 70 | void *pc_begin; |
| 71 | void *pc_end; |
| 72 | struct dwarf_fde *fde_begin; |
| 73 | struct dwarf_fde **fde_array; |
| 74 | size_t count; |
| 75 | struct old_object *next; |
| 76 | }; |
| 77 | |
| 78 | struct dwarf_eh_bases |
| 79 | { |
| 80 | void *tbase; |
| 81 | void *dbase; |
| 82 | void *func; |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | extern void __register_frame_info_bases (void *, struct object *, |
| 87 | void *, void *); |
| 88 | extern void __register_frame_info (void *, struct object *); |
| 89 | extern void __register_frame (void *); |
| 90 | extern void __register_frame_info_table_bases (void *, struct object *, |
| 91 | void *, void *); |
| 92 | extern void __register_frame_info_table (void *, struct object *); |
| 93 | extern void __register_frame_table (void *); |
| 94 | extern void *__deregister_frame_info (void *); |
| 95 | extern void *__deregister_frame_info_bases (void *); |
| 96 | extern void __deregister_frame (void *); |
| 97 | |
| 98 | |
| 99 | typedef int sword __attribute__ ((mode (SI))); |
| 100 | typedef unsigned int uword __attribute__ ((mode (SI))); |
| 101 | typedef unsigned int uaddr __attribute__ ((mode (pointer))); |
| 102 | typedef int saddr __attribute__ ((mode (pointer))); |
| 103 | typedef unsigned char ubyte; |
| 104 | |
| 105 | /* Terminology: |
| 106 | CIE - Common Information Element |
| 107 | FDE - Frame Descriptor Element |
| 108 | |
| 109 | There is one per function, and it describes where the function code |
| 110 | is located, and what the register lifetimes and stack layout are |
| 111 | within the function. |
| 112 | |
| 113 | The data structures are defined in the DWARF specification, although |
| 114 | not in a very readable way (see LITERATURE). |
| 115 | |
| 116 | Every time an exception is thrown, the code needs to locate the FDE |
| 117 | for the current function, and starts to look for exception regions |
| 118 | from that FDE. This works in a two-level search: |
| 119 | a) in a linear search, find the shared image (i.e. DLL) containing |
| 120 | the PC |
| 121 | b) using the FDE table for that shared object, locate the FDE using |
| 122 | binary search (which requires the sorting). */ |
| 123 | |
| 124 | /* The first few fields of a CIE. The CIE_id field is 0 for a CIE, |
| 125 | to distinguish it from a valid FDE. FDEs are aligned to an addressing |
| 126 | unit boundary, but the fields within are unaligned. */ |
| 127 | struct dwarf_cie |
| 128 | { |
| 129 | uword length; |
| 130 | sword CIE_id; |
| 131 | ubyte version; |
| 132 | unsigned char augmentation __flexarr; |
| 133 | } __attribute__ ((packed, aligned (__alignof__ (void *)))); |
| 134 | |
| 135 | /* The first few fields of an FDE. */ |
| 136 | struct dwarf_fde |
| 137 | { |
| 138 | uword length; |
| 139 | sword CIE_delta; |
| 140 | unsigned char pc_begin __flexarr; |
| 141 | } __attribute__ ((packed, aligned (__alignof__ (void *)))); |
| 142 | |
| 143 | typedef struct dwarf_fde fde; |
| 144 | |
| 145 | /* Locate the CIE for a given FDE. */ |
| 146 | |
| 147 | static inline struct dwarf_cie * |
| 148 | get_cie (struct dwarf_fde *f) |
| 149 | { |
| 150 | return (void *)&f->CIE_delta - f->CIE_delta; |
| 151 | } |
| 152 | |
| 153 | static inline fde * |
| 154 | next_fde (fde *f) |
| 155 | { |
| 156 | return (fde *) ((char *) f + f->length + sizeof (f->length)); |
| 157 | } |
| 158 | |
| 159 | extern fde * _Unwind_Find_FDE (void *, struct dwarf_eh_bases *); |
| 160 | |
| 161 | static inline int |
| 162 | last_fde (struct object *obj __attribute__ ((__unused__)), fde *f) |
| 163 | { |
| 164 | #ifdef DWARF2_OBJECT_END_PTR_EXTENSION |
| 165 | return (char *)f == obj->fde_end || f->length == 0; |
| 166 | #else |
| 167 | return f->length == 0; |
| 168 | #endif |
| 169 | } |