xref: /titanic_44/usr/src/lib/libc/port/unwind/unwind.c (revision 8883f1c270cc8e33c18dd088e744840092b47bbb)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "thr_uberdata.h"
31 #include <dlfcn.h>
32 
33 /*
34  * This is common code for sparc, sparcv9, and i386.
35  * The amd64 unwind code is vastly different from this.
36  * Look under the amd64-specific directory structure for details.
37  */
38 
39 /*
40  * _ex_unwind() is provided by libC, but if libC is not loaded we
41  * need to call a local version of _ex_unwind() which does exactly
42  * the same thing except for calling C++ destructors.
43  */
44 extern	void	_ex_clnup_handler(void *, void (*)(void *));
45 extern	void	_ex_unwind_local(void);
46 #pragma unknown_control_flow(_ex_clnup_handler)
47 #pragma unknown_control_flow(_ex_unwind_local)
48 
49 /*
50  * _t_cancel(fp):calls cleanup handlers if there are any in
51  *		 frame (fp), and calls _ex_unwind() to call
52  *		 destructors if libC has been linked.
53  *
54  * Control comes here from _thrp_unwind.  Logically:
55  *
56  *	_thrp_unwind: first arg = current fp;
57  *	    jump _t_cancel;
58  *
59  * We could have called _t_cancel(_getfp) from thr_exit()
60  * but _ex_unwind() also calls _t_cancel() and it does after
61  * poping out the two frames.  If _ex_unwind() passes the current
62  * fp, then it will be invalid.  For a caller of _thrp_unwind()
63  * it looks as if it is calling _t_cancel(fp).
64  *
65  * _t_cancel will eventually call _thrp_exit().
66  * It never returns from _t_cancel().
67  *
68  */
69 void
70 _t_cancel(void *fp)
71 {
72 	ulwp_t *self = curthread;
73 	__cleanup_t *head;
74 	void (*fptr)(void (*func)(void *), void *arg);
75 
76 	/* Do this once per thread exit, not once per unwind frame */
77 	if (self->ul_ex_unwind == NULL &&
78 	    (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
79 		self->ul_ex_unwind = (void *)-1;
80 
81 	if (self->ul_ex_unwind == (void *)-1)
82 		fptr = NULL;
83 	else
84 		fptr = (void (*)())self->ul_ex_unwind;
85 
86 	if (fp == NULL) {
87 		_thrp_exit();
88 		thr_panic("_t_cancel(): _thrp_exit() returned");
89 	}
90 
91 	if ((head = self->ul_clnup_hdr) != NULL && fp == head->fp) {
92 		self->ul_clnup_hdr = head->next;
93 		/* execute the cleanup handler */
94 		_ex_clnup_handler(head->arg, head->func);
95 		thr_panic("_t_cancel(): _ex_clnup_handler() returned");
96 	}
97 
98 	if (fptr != NULL && self->ul_unwind) {
99 		/* libC is loaded and thread is canceled, call libC version */
100 		(*fptr)(_thrp_unwind, NULL);
101 		thr_panic("_t_cancel(): _ex_unwind() returned");
102 	} else if (head != NULL) {
103 		/* libC not present, call local version */
104 		_ex_unwind_local();
105 		thr_panic("_t_cancel(): _ex_unwind_local() returned");
106 	} else {
107 		/* libC not present and no cleanup handlers, exit here */
108 		_thrp_exit();
109 		thr_panic("_t_cancel(): _thrp_exit() returned");
110 	}
111 	/* never returns here */
112 }
113