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