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