1 //===--------------------------- libuwind.cpp -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Implements C++ ABI Exception Handling Level 1 as documented at:
10 // http://mentorembedded.github.io/cxx-abi/abi-eh.html
12 //===----------------------------------------------------------------------===//
14 #define _UNWIND_GCC_EXTENSIONS
18 #include "UnwindCursor.hpp"
20 using namespace _Unwind
;
22 typedef CFI_Parser
<LocalAddressSpace
, NativeUnwindRegisters
> MyCFIParser
;
24 // Internal object representing the address space of this process.
25 static LocalAddressSpace
sThisAddressSpace(MyCFIParser::findPCRange
);
27 typedef UnwindCursor
<LocalAddressSpace
, NativeUnwindRegisters
> ThisUnwindCursor
;
29 static _Unwind_Reason_Code
unwind_phase1(ThisUnwindCursor
&cursor
,
30 struct _Unwind_Exception
*exc
) {
31 cursor
.setInfoBasedOnIPRegister();
33 // Walk frames looking for a place to stop.
36 // First frame is _Unwind_RaiseException and skipped.
37 switch (cursor
.step()) {
39 return _URC_END_OF_STACK
;
41 return _URC_FATAL_PHASE1_ERROR
;
42 case UNW_STEP_SUCCESS
:
46 // Check if there is a personality routine for this frame.
47 unw_proc_info_t frameInfo
;
48 cursor
.getInfo(&frameInfo
);
49 if (frameInfo
.end_ip
== 0)
50 return _URC_FATAL_PHASE1_ERROR
;
52 if (frameInfo
.handler
== 0)
53 continue; // No personality routine, so try next frame.
55 __personality_routine p
= (__personality_routine
)(frameInfo
.handler
);
56 _Unwind_Reason_Code result
= (*p
)(1, _UA_SEARCH_PHASE
, exc
->exception_class
,
57 exc
, (struct _Unwind_Context
*)(&cursor
));
60 case _URC_HANDLER_FOUND
:
61 // This is either a catch clause or a local variable
63 // Stop search and remember the frame for phase 2.
64 exc
->private_2
= cursor
.getSP();
65 return _URC_NO_REASON
;
67 case _URC_CONTINUE_UNWIND
:
72 // Bad personality routine.
73 return _URC_FATAL_PHASE1_ERROR
;
78 static _Unwind_Reason_Code
unwind_phase2(ThisUnwindCursor
&cursor
,
79 struct _Unwind_Exception
*exc
) {
80 cursor
.setInfoBasedOnIPRegister();
82 // Walk frames until the frame selected in phase 1 is reached.
85 // First frame is _Unwind_RaiseException and skipped.
86 switch (cursor
.step()) {
88 return _URC_END_OF_STACK
;
90 return _URC_FATAL_PHASE2_ERROR
;
91 case UNW_STEP_SUCCESS
:
95 unw_proc_info_t frameInfo
;
96 cursor
.getInfo(&frameInfo
);
97 if (frameInfo
.end_ip
== 0)
98 return _URC_FATAL_PHASE2_ERROR
;
100 if (frameInfo
.handler
== 0)
101 continue; // No personality routine, continue.
103 uintptr_t sp
= cursor
.getSP();
105 _Unwind_Action action
= _UA_CLEANUP_PHASE
;
106 // If this frame was selected in phase 1,
107 // inform the personality routine.
108 if (sp
== exc
->private_2
)
109 action
= (_Unwind_Action
)(action
| _UA_HANDLER_FRAME
);
110 __personality_routine p
= (__personality_routine
)(frameInfo
.handler
);
111 _Unwind_Reason_Code result
= (*p
)(1, action
, exc
->exception_class
, exc
,
112 (struct _Unwind_Context
*)(&cursor
));
114 case _URC_CONTINUE_UNWIND
:
115 // Continue unwinding unless the selected frame passed.
116 if (sp
== exc
->private_2
)
117 return _URC_FATAL_PHASE2_ERROR
;
119 case _URC_INSTALL_CONTEXT
:
120 // Transfer control to landing pad.
123 // Bad personality routine.
124 return _URC_FATAL_PHASE2_ERROR
;
129 static _Unwind_Reason_Code
unwind_phase2_forced(ThisUnwindCursor
&cursor
,
130 struct _Unwind_Exception
*exc
,
131 _Unwind_Stop_Fn stop
,
133 _Unwind_Action action
;
134 cursor
.setInfoBasedOnIPRegister();
136 // Walk frames until the frame selected in phase 1 is reached.
139 // First frame is _Unwind_RaiseException and skipped.
140 switch (cursor
.step()) {
142 case UNW_STEP_FAILED
:
143 // End of stack or error condition.
144 // Call the stop function one last time.
145 action
= (_Unwind_Action
)(_UA_FORCE_UNWIND
| _UA_CLEANUP_PHASE
|
147 (*stop
)(1, action
, exc
->exception_class
, exc
,
148 (struct _Unwind_Context
*)(&cursor
), stop_arg
);
150 // Didn't stop at the expected frame, so return error.
151 return _URC_FATAL_PHASE2_ERROR
;
153 case UNW_STEP_SUCCESS
:
157 unw_proc_info_t frameInfo
;
158 cursor
.getInfo(&frameInfo
);
159 if (frameInfo
.end_ip
== 0)
160 return _URC_FATAL_PHASE2_ERROR
;
162 // Call stop function for each frame
163 action
= (_Unwind_Action
)(_UA_FORCE_UNWIND
| _UA_CLEANUP_PHASE
);
164 _Unwind_Reason_Code result
=
165 (*stop
)(1, action
, exc
->exception_class
, exc
,
166 (struct _Unwind_Context
*)(&cursor
), stop_arg
);
167 if (result
!= _URC_NO_REASON
)
168 return _URC_FATAL_PHASE2_ERROR
;
170 if (frameInfo
.handler
== 0)
171 continue; // No personality routine, continue.
173 __personality_routine p
= (__personality_routine
)(frameInfo
.handler
);
174 result
= (*p
)(1, action
, exc
->exception_class
, exc
,
175 (struct _Unwind_Context
*)(&cursor
));
178 case _URC_CONTINUE_UNWIND
:
179 // Destructors called, continue.
181 case _URC_INSTALL_CONTEXT
:
182 // Transfer control to landing pad.
185 // Bad personality routine.
186 return _URC_FATAL_PHASE2_ERROR
;
191 _Unwind_Reason_Code
_Unwind_RaiseException(struct _Unwind_Exception
*exc
) {
192 NativeUnwindRegisters registers
;
193 ThisUnwindCursor
cursor1(registers
, sThisAddressSpace
);
194 ThisUnwindCursor
cursor2(registers
, sThisAddressSpace
);
196 // Mark this as a non-forced unwind for _Unwind_Resume().
200 // Phase 1: searching.
201 _Unwind_Reason_Code phase1
= unwind_phase1(cursor1
, exc
);
202 if (phase1
!= _URC_NO_REASON
)
205 // Phase 2: cleaning up.
206 return unwind_phase2(cursor2
, exc
);
209 _Unwind_Reason_Code
_Unwind_ForcedUnwind(struct _Unwind_Exception
*exc
,
210 _Unwind_Stop_Fn stop
, void *stop_arg
) {
211 NativeUnwindRegisters registers
;
212 ThisUnwindCursor
cursor(registers
, sThisAddressSpace
);
214 // Mark this as forced unwind for _Unwind_Resume().
215 exc
->private_1
= (uintptr_t)stop
;
216 exc
->private_2
= (uintptr_t)stop_arg
;
218 return unwind_phase2_forced(cursor
, exc
, stop
, stop_arg
);
221 void _Unwind_Resume(struct _Unwind_Exception
*exc
) {
222 NativeUnwindRegisters registers
;
223 ThisUnwindCursor
cursor(registers
, sThisAddressSpace
);
225 if (exc
->private_1
!= 0)
226 unwind_phase2_forced(cursor
, exc
, (_Unwind_Stop_Fn
)exc
->private_1
,
227 (void *)exc
->private_2
);
229 unwind_phase2(cursor
, exc
);
233 _Unwind_Reason_Code
_Unwind_Resume_or_Rethrow(struct _Unwind_Exception
*exc
) {
234 // This is a re-throw, if this is a non-forced unwind
235 // and the stopping place was found.
236 // In that case, call _Unwind_RaiseException() as if
237 // it was a new exception.
239 if (exc
->private_1
!= 0)
242 // This can return if there is no catch clause.
243 // In that case, __cxa_rethrow is expected to call std::terminate().
244 return _Unwind_RaiseException(exc
);
247 void _Unwind_DeleteException(struct _Unwind_Exception
*exc
) {
248 if (exc
->exception_cleanup
!= NULL
)
249 (*exc
->exception_cleanup
)(_URC_FOREIGN_EXCEPTION_CAUGHT
, exc
);
252 uintptr_t _Unwind_GetGR(struct _Unwind_Context
*context
, int index
) {
253 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
254 return cursor
->getReg(index
);
257 void _Unwind_SetGR(struct _Unwind_Context
*context
, int index
,
258 uintptr_t new_value
) {
259 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
260 cursor
->setReg(index
, new_value
);
263 uintptr_t _Unwind_GetIP(struct _Unwind_Context
*context
) {
264 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
265 return cursor
->getIP();
268 uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context
*context
, int *isSignalFrame
) {
269 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
270 *isSignalFrame
= cursor
->isSignalFrame() ? 1 : 0;
271 return cursor
->getIP();
274 void _Unwind_SetIP(struct _Unwind_Context
*context
, uintptr_t new_value
) {
275 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
276 cursor
->setIP(new_value
);
277 unw_proc_info_t info
;
278 cursor
->getInfo(&info
);
279 cursor
->setInfoBasedOnIPRegister(false);
282 uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context
*context
) {
283 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
284 unw_proc_info_t frameInfo
;
285 cursor
->getInfo(&frameInfo
);
286 return frameInfo
.end_ip
? frameInfo
.start_ip
: 0;
289 uintptr_t _Unwind_GetLanguageSpecificData(struct _Unwind_Context
*context
) {
290 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
291 unw_proc_info_t frameInfo
;
292 cursor
->getInfo(&frameInfo
);
293 return frameInfo
.end_ip
? frameInfo
.lsda
: 0;
296 _Unwind_Reason_Code
_Unwind_Backtrace(_Unwind_Trace_Fn callback
, void *ref
) {
297 NativeUnwindRegisters registers
;
298 ThisUnwindCursor
cursor(registers
, sThisAddressSpace
);
299 cursor
.setInfoBasedOnIPRegister();
304 // Ask libuwind to get next frame (skip over first frame which is
305 // _Unwind_Backtrace()).
306 if (cursor
.step() != UNW_STEP_SUCCESS
)
307 return _URC_END_OF_STACK
;
309 // Call trace function with this frame.
310 _Unwind_Reason_Code result
=
311 (*callback
)((struct _Unwind_Context
*)(&cursor
), ref
);
312 if (result
!= _URC_NO_REASON
)
317 uintptr_t _Unwind_GetCFA(struct _Unwind_Context
*context
) {
318 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
319 return cursor
->getSP();
322 void *_Unwind_FindEnclosingFunction(void *pc
) {
323 NativeUnwindRegisters registers
;
324 ThisUnwindCursor
cursor(registers
, sThisAddressSpace
);
326 unw_proc_info_t info
;
327 cursor
.setIP((uintptr_t)pc
);
328 cursor
.setInfoBasedOnIPRegister();
330 cursor
.getInfo(&info
);
331 return info
.end_ip
? (void *)info
.start_ip
: NULL
;
334 void *_Unwind_Find_FDE(void *pc
, struct dwarf_eh_bases
*bases
) {
335 NativeUnwindRegisters registers
;
336 ThisUnwindCursor
cursor(registers
, sThisAddressSpace
);
338 unw_proc_info_t info
;
339 cursor
.setIP((uintptr_t)pc
);
340 cursor
.setInfoBasedOnIPRegister();
342 cursor
.getInfo(&info
);
343 if (info
.end_ip
== 0)
345 bases
->tbase
= 0; /* Not supported */
346 bases
->dbase
= (void *)info
.data_base
;
347 bases
->func
= (void *)info
.start_ip
;
348 return (void *)info
.unwind_info
;
351 uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context
*context
) {
352 ThisUnwindCursor
*cursor
= (ThisUnwindCursor
*)context
;
353 unw_proc_info_t frameInfo
;
354 cursor
->getInfo(&frameInfo
);
355 return frameInfo
.data_base
;
358 uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context
*context
) { return 0; }
360 void __register_frame(const void *fde
) {
361 MyCFIParser::pint_t pcStart
, pcEnd
;
363 MyCFIParser::findPCRange(sThisAddressSpace
, (uintptr_t)fde
, pcStart
, pcEnd
);
367 sThisAddressSpace
.addFDE(pcStart
, pcEnd
, (uintptr_t)fde
);
370 void __register_frame_info(const void *ehframe
, void *storage
) {
371 sThisAddressSpace
.setLazyReload();
374 void __deregister_frame(const void *fde
) {
375 MyCFIParser::pint_t pcStart
, pcEnd
;
377 MyCFIParser::findPCRange(sThisAddressSpace
, (uintptr_t)fde
, pcStart
, pcEnd
);
381 sThisAddressSpace
.removeFDE(pcStart
, pcEnd
, (uintptr_t)fde
);
384 void *__deregister_frame_info(const void *ehFrameStart
) {
385 sThisAddressSpace
.removeDSO((LocalAddressSpace::pint_t
)ehFrameStart
);