• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--------------------- UnwindLevel1-gcc-ext.c -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //
9 //  Implements gcc extensions to the C++ ABI Exception Handling Level 1.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 
18 #include "libunwind.h"
19 #include "unwind.h"
20 #include "libunwind_ext.h"
21 #include "config.h"
22 
23 #if _LIBUNWIND_BUILD_ZERO_COST_APIS
24 
25 ///  Called by __cxa_rethrow().
26 _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_Resume_or_Rethrow(_Unwind_Exception * exception_object)27 _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) {
28   _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), "
29                        "private_1=%ld\n",
30                        exception_object,
31 #if LIBCXXABI_ARM_EHABI
32                        (long)exception_object->unwinder_cache.reserved1);
33 #else
34                        (long)exception_object->private_1);
35 #endif
36 
37 #if LIBCXXABI_ARM_EHABI
38   // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
39   // which is in the same position as private_1 below.
40   return _Unwind_RaiseException(exception_object);
41 #else
42   // If this is non-forced and a stopping place was found, then this is a
43   // re-throw.
44   // Call _Unwind_RaiseException() as if this was a new exception
45   if (exception_object->private_1 == 0) {
46     return _Unwind_RaiseException(exception_object);
47     // Will return if there is no catch clause, so that __cxa_rethrow can call
48     // std::terminate().
49   }
50 
51   // Call through to _Unwind_Resume() which distiguishes between forced and
52   // regular exceptions.
53   _Unwind_Resume(exception_object);
54   _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()"
55                    " which unexpectedly returned");
56 #endif
57 }
58 
59 
60 /// Called by personality handler during phase 2 to get base address for data
61 /// relative encodings.
62 _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetDataRelBase(struct _Unwind_Context * context)63 _Unwind_GetDataRelBase(struct _Unwind_Context *context) {
64   (void)context;
65   _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)\n", context);
66   _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
67 }
68 
69 
70 /// Called by personality handler during phase 2 to get base address for text
71 /// relative encodings.
72 _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetTextRelBase(struct _Unwind_Context * context)73 _Unwind_GetTextRelBase(struct _Unwind_Context *context) {
74   (void)context;
75   _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)\n", context);
76   _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
77 }
78 
79 
80 /// Scans unwind information to find the function that contains the
81 /// specified code address "pc".
_Unwind_FindEnclosingFunction(void * pc)82 _LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) {
83   _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)\n", pc);
84   // This is slow, but works.
85   // We create an unwind cursor then alter the IP to be pc
86   unw_cursor_t cursor;
87   unw_context_t uc;
88   unw_proc_info_t info;
89   unw_getcontext(&uc);
90   unw_init_local(&cursor, &uc);
91   unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc);
92   if (unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS)
93     return (void *)(long) info.start_ip;
94   else
95     return NULL;
96 }
97 
98 
99 /// Walk every frame and call trace function at each one.  If trace function
100 /// returns anything other than _URC_NO_REASON, then walk is terminated.
101 _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_Backtrace(_Unwind_Trace_Fn callback,void * ref)102 _Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {
103   unw_cursor_t cursor;
104   unw_context_t uc;
105   unw_getcontext(&uc);
106   unw_init_local(&cursor, &uc);
107 
108   _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)\n", callback);
109 
110   // walk each frame
111   while (true) {
112     _Unwind_Reason_Code result;
113 
114     // ask libuwind to get next frame (skip over first frame which is
115     // _Unwind_Backtrace())
116     if (unw_step(&cursor) <= 0) {
117       _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached "
118                                  "bottom of stack, returning %d\n",
119                                  _URC_END_OF_STACK);
120       return _URC_END_OF_STACK;
121     }
122 
123 #if LIBCXXABI_ARM_EHABI
124     // Get the information for this frame.
125     unw_proc_info_t frameInfo;
126     if (unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) {
127       return _URC_END_OF_STACK;
128     }
129 
130     struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor;
131     size_t off;
132     size_t len;
133     uint32_t* unwindInfo = (uint32_t *) frameInfo.unwind_info;
134     unwindInfo = decode_eht_entry(unwindInfo, &off, &len);
135     if (unwindInfo == NULL) {
136       return _URC_FAILURE;
137     }
138 
139     result = _Unwind_VRS_Interpret(context, unwindInfo, off, len);
140     if (result != _URC_CONTINUE_UNWIND) {
141       return _URC_END_OF_STACK;
142     }
143 #endif // LIBCXXABI_ARM_EHABI
144 
145     // debugging
146     if (_LIBUNWIND_TRACING_UNWINDING) {
147       char functionName[512];
148       unw_proc_info_t frameInfo;
149       unw_word_t offset;
150       unw_get_proc_name(&cursor, functionName, 512, &offset);
151       unw_get_proc_info(&cursor, &frameInfo);
152       _LIBUNWIND_TRACE_UNWINDING(
153           " _backtrace: start_ip=0x%llX, func=%s, lsda=0x%llX, context=%p\n",
154           (long long)frameInfo.start_ip, functionName, (long long)frameInfo.lsda,
155           &cursor);
156     }
157 
158     // call trace function with this frame
159     result = (*callback)((struct _Unwind_Context *)(&cursor), ref);
160     if (result != _URC_NO_REASON) {
161       _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because callback "
162                                  "returned %d\n",
163                                  result);
164       return result;
165     }
166   }
167 }
168 
169 
170 /// Find dwarf unwind info for an address 'pc' in some function.
_Unwind_Find_FDE(const void * pc,struct dwarf_eh_bases * bases)171 _LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc,
172                                                struct dwarf_eh_bases *bases) {
173   // This is slow, but works.
174   // We create an unwind cursor then alter the IP to be pc
175   unw_cursor_t cursor;
176   unw_context_t uc;
177   unw_proc_info_t info;
178   unw_getcontext(&uc);
179   unw_init_local(&cursor, &uc);
180   unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc);
181   unw_get_proc_info(&cursor, &info);
182   bases->tbase = (uintptr_t)info.extra;
183   bases->dbase = 0; // dbase not used on Mac OS X
184   bases->func = (uintptr_t)info.start_ip;
185   _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p\n", pc,
186                   (void *)(long) info.unwind_info);
187   return (void *)(long) info.unwind_info;
188 }
189 
190 /// Returns the CFA (call frame area, or stack pointer at start of function)
191 /// for the current context.
_Unwind_GetCFA(struct _Unwind_Context * context)192 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
193   unw_cursor_t *cursor = (unw_cursor_t *)context;
194   unw_word_t result;
195   unw_get_reg(cursor, UNW_REG_SP, &result);
196   _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%llX\n", context,
197                   (uint64_t) result);
198   return (uintptr_t)result;
199 }
200 
201 
202 /// Called by personality handler during phase 2 to get instruction pointer.
203 /// ipBefore is a boolean that says if IP is already adjusted to be the call
204 /// site address.  Normally IP is the return address.
_Unwind_GetIPInfo(struct _Unwind_Context * context,int * ipBefore)205 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
206                                               int *ipBefore) {
207   _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)\n", context);
208   *ipBefore = 0;
209   return _Unwind_GetIP(context);
210 }
211 
212 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
213 
214 /// Called by programs with dynamic code generators that want
215 /// to register a dynamically generated FDE.
216 /// This function has existed on Mac OS X since 10.4, but
217 /// was broken until 10.6.
__register_frame(const void * fde)218 _LIBUNWIND_EXPORT void __register_frame(const void *fde) {
219   _LIBUNWIND_TRACE_API("__register_frame(%p)\n", fde);
220   _unw_add_dynamic_fde((unw_word_t)(uintptr_t) fde);
221 }
222 
223 
224 /// Called by programs with dynamic code generators that want
225 /// to unregister a dynamically generated FDE.
226 /// This function has existed on Mac OS X since 10.4, but
227 /// was broken until 10.6.
__deregister_frame(const void * fde)228 _LIBUNWIND_EXPORT void __deregister_frame(const void *fde) {
229   _LIBUNWIND_TRACE_API("__deregister_frame(%p)\n", fde);
230   _unw_remove_dynamic_fde((unw_word_t)(uintptr_t) fde);
231 }
232 
233 
234 // The following register/deregister functions are gcc extensions.
235 // They have existed on Mac OS X, but have never worked because Mac OS X
236 // before 10.6 used keymgr to track known FDEs, but these functions
237 // never got updated to use keymgr.
238 // For now, we implement these as do-nothing functions to keep any existing
239 // applications working.  We also add the not in 10.6 symbol so that nwe
240 // application won't be able to use them.
241 
242 #if _LIBUNWIND_SUPPORT_FRAME_APIS
__register_frame_info_bases(const void * fde,void * ob,void * tb,void * db)243 _LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob,
244                                                    void *tb, void *db) {
245   (void)fde;
246   (void)ob;
247   (void)tb;
248   (void)db;
249  _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)\n",
250                             fde, ob, tb, db);
251   // do nothing, this function never worked in Mac OS X
252 }
253 
__register_frame_info(const void * fde,void * ob)254 _LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) {
255   (void)fde;
256   (void)ob;
257   _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)\n", fde, ob);
258   // do nothing, this function never worked in Mac OS X
259 }
260 
__register_frame_info_table_bases(const void * fde,void * ob,void * tb,void * db)261 _LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde,
262                                                          void *ob, void *tb,
263                                                          void *db) {
264   (void)fde;
265   (void)ob;
266   (void)tb;
267   (void)db;
268   _LIBUNWIND_TRACE_API("__register_frame_info_table_bases"
269                              "(%p,%p, %p, %p)\n", fde, ob, tb, db);
270   // do nothing, this function never worked in Mac OS X
271 }
272 
__register_frame_info_table(const void * fde,void * ob)273 _LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) {
274   (void)fde;
275   (void)ob;
276   _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)\n", fde, ob);
277   // do nothing, this function never worked in Mac OS X
278 }
279 
__register_frame_table(const void * fde)280 _LIBUNWIND_EXPORT void __register_frame_table(const void *fde) {
281   (void)fde;
282   _LIBUNWIND_TRACE_API("__register_frame_table(%p)\n", fde);
283   // do nothing, this function never worked in Mac OS X
284 }
285 
__deregister_frame_info(const void * fde)286 _LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) {
287   (void)fde;
288   _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)\n", fde);
289   // do nothing, this function never worked in Mac OS X
290   return NULL;
291 }
292 
__deregister_frame_info_bases(const void * fde)293 _LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) {
294   (void)fde;
295   _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)\n", fde);
296   // do nothing, this function never worked in Mac OS X
297   return NULL;
298 }
299 #endif // _LIBUNWIND_SUPPORT_FRAME_APIS
300 
301 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
302 
303 #endif // _LIBUNWIND_BUILD_ZERO_COST_APIS
304