xref: /onnv-gate/usr/src/lib/libc/port/unwind/unwind.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate #include <dlfcn.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * This is common code for sparc, sparcv9, and i386.
350Sstevel@tonic-gate  * The amd64 unwind code is vastly different from this.
360Sstevel@tonic-gate  * Look under the amd64-specific directory structure for details.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * _ex_unwind() is provided by libC, but if libC is not loaded we
410Sstevel@tonic-gate  * need to call a local version of _ex_unwind() which does exactly
420Sstevel@tonic-gate  * the same thing except for calling C++ destructors.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate extern	void	_ex_clnup_handler(void *, void (*)(void *));
450Sstevel@tonic-gate extern	void	_ex_unwind_local(void);
460Sstevel@tonic-gate #pragma unknown_control_flow(_ex_clnup_handler)
470Sstevel@tonic-gate #pragma unknown_control_flow(_ex_unwind_local)
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * _t_cancel(fp):calls cleanup handlers if there are any in
510Sstevel@tonic-gate  *		 frame (fp), and calls _ex_unwind() to call
520Sstevel@tonic-gate  *		 destructors if libC has been linked.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * Control comes here from _thrp_unwind.  Logically:
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *	_thrp_unwind: first arg = current fp;
570Sstevel@tonic-gate  *	    jump _t_cancel;
580Sstevel@tonic-gate  *
59*6812Sraf  * We could have called _t_cancel(_getfp) from thr_exit()
600Sstevel@tonic-gate  * but _ex_unwind() also calls _t_cancel() and it does after
610Sstevel@tonic-gate  * poping out the two frames.  If _ex_unwind() passes the current
620Sstevel@tonic-gate  * fp, then it will be invalid.  For a caller of _thrp_unwind()
630Sstevel@tonic-gate  * it looks as if it is calling _t_cancel(fp).
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * _t_cancel will eventually call _thrp_exit().
660Sstevel@tonic-gate  * It never returns from _t_cancel().
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate void
_t_cancel(void * fp)700Sstevel@tonic-gate _t_cancel(void *fp)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	ulwp_t *self = curthread;
730Sstevel@tonic-gate 	__cleanup_t *head;
740Sstevel@tonic-gate 	void (*fptr)(void (*func)(void *), void *arg);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/* Do this once per thread exit, not once per unwind frame */
770Sstevel@tonic-gate 	if (self->ul_ex_unwind == NULL &&
780Sstevel@tonic-gate 	    (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
790Sstevel@tonic-gate 		self->ul_ex_unwind = (void *)-1;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (self->ul_ex_unwind == (void *)-1)
820Sstevel@tonic-gate 		fptr = NULL;
830Sstevel@tonic-gate 	else
840Sstevel@tonic-gate 		fptr = (void (*)())self->ul_ex_unwind;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (fp == NULL) {
870Sstevel@tonic-gate 		_thrp_exit();
880Sstevel@tonic-gate 		thr_panic("_t_cancel(): _thrp_exit() returned");
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if ((head = self->ul_clnup_hdr) != NULL && fp == head->fp) {
920Sstevel@tonic-gate 		self->ul_clnup_hdr = head->next;
930Sstevel@tonic-gate 		/* execute the cleanup handler */
940Sstevel@tonic-gate 		_ex_clnup_handler(head->arg, head->func);
950Sstevel@tonic-gate 		thr_panic("_t_cancel(): _ex_clnup_handler() returned");
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (fptr != NULL && self->ul_unwind) {
990Sstevel@tonic-gate 		/* libC is loaded and thread is canceled, call libC version */
1000Sstevel@tonic-gate 		(*fptr)(_thrp_unwind, NULL);
1010Sstevel@tonic-gate 		thr_panic("_t_cancel(): _ex_unwind() returned");
1020Sstevel@tonic-gate 	} else if (head != NULL) {
1030Sstevel@tonic-gate 		/* libC not present, call local version */
1040Sstevel@tonic-gate 		_ex_unwind_local();
1050Sstevel@tonic-gate 		thr_panic("_t_cancel(): _ex_unwind_local() returned");
1060Sstevel@tonic-gate 	} else {
1070Sstevel@tonic-gate 		/* libC not present and no cleanup handlers, exit here */
1080Sstevel@tonic-gate 		_thrp_exit();
1090Sstevel@tonic-gate 		thr_panic("_t_cancel(): _thrp_exit() returned");
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	/* never returns here */
1120Sstevel@tonic-gate }
113