xref: /titanic_44/usr/src/lib/libc/port/unwind/unwind.c (revision 56b2bdd1f04d465cfe4a95b88ae5cba5884154e4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "lint.h"
28 #include "thr_uberdata.h"
29 #include <dlfcn.h>
30 
31 /*
32  * This is common code for sparc, sparcv9, and i386.
33  * The amd64 unwind code is vastly different from this.
34  * Look under the amd64-specific directory structure for details.
35  */
36 
37 /*
38  * _ex_unwind() is provided by libC, but if libC is not loaded we
39  * need to call a local version of _ex_unwind() which does exactly
40  * the same thing except for calling C++ destructors.
41  *
42  * Note that neither of these literally "returns twice" as, for eg, setjmp
43  * does, but they induce unusual control flow which the compiler should treat
44  * in the same manner (make all registers dead, etc.).
45  */
46 extern	void	_ex_clnup_handler(void *, void (*)(void *)) __RETURNS_TWICE;
47 extern	void	_ex_unwind_local(void) __RETURNS_TWICE;
48 #pragma unknown_control_flow(_ex_clnup_handler)
49 #pragma unknown_control_flow(_ex_unwind_local)
50 
51 /*
52  * _t_cancel(fp):calls cleanup handlers if there are any in
53  *		 frame (fp), and calls _ex_unwind() to call
54  *		 destructors if libC has been linked.
55  *
56  * Control comes here from _thrp_unwind.  Logically:
57  *
58  *	_thrp_unwind: first arg = current fp;
59  *	    jump _t_cancel;
60  *
61  * We could have called _t_cancel(_getfp) from thr_exit()
62  * but _ex_unwind() also calls _t_cancel() and it does after
63  * poping out the two frames.  If _ex_unwind() passes the current
64  * fp, then it will be invalid.  For a caller of _thrp_unwind()
65  * it looks as if it is calling _t_cancel(fp).
66  *
67  * _t_cancel will eventually call _thrp_exit().
68  * It never returns from _t_cancel().
69  *
70  */
71 void
72 _t_cancel(void *fp)
73 {
74 	ulwp_t *self = curthread;
75 	__cleanup_t *head;
76 	void (*fptr)(void (*func)(void *), void *arg);
77 
78 	/* Do this once per thread exit, not once per unwind frame */
79 	if (self->ul_ex_unwind == NULL &&
80 	    (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
81 		self->ul_ex_unwind = (void *)-1;
82 
83 	if (self->ul_ex_unwind == (void *)-1)
84 		fptr = NULL;
85 	else
86 		fptr = (void (*)())self->ul_ex_unwind;
87 
88 	if (fp == NULL) {
89 		_thrp_exit();
90 		thr_panic("_t_cancel(): _thrp_exit() returned");
91 	}
92 
93 	if ((head = self->ul_clnup_hdr) != NULL && fp == head->fp) {
94 		self->ul_clnup_hdr = head->next;
95 		/* execute the cleanup handler */
96 		_ex_clnup_handler(head->arg, head->func);
97 		thr_panic("_t_cancel(): _ex_clnup_handler() returned");
98 	}
99 
100 	if (fptr != NULL && self->ul_unwind) {
101 		/* libC is loaded and thread is canceled, call libC version */
102 		(*fptr)(_thrp_unwind, NULL);
103 		thr_panic("_t_cancel(): _ex_unwind() returned");
104 	} else if (head != NULL) {
105 		/* libC not present, call local version */
106 		_ex_unwind_local();
107 		thr_panic("_t_cancel(): _ex_unwind_local() returned");
108 	} else {
109 		/* libC not present and no cleanup handlers, exit here */
110 		_thrp_exit();
111 		thr_panic("_t_cancel(): _thrp_exit() returned");
112 	}
113 	/* never returns here */
114 }
115