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