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*3864Sraf  * Common Development and Distribution License (the "License").
6*3864Sraf  * 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  */
21*3864Sraf 
220Sstevel@tonic-gate /*
23*3864Sraf  * Copyright 2007 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" 	/* SVr4.0 1.16	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma weak	elf_errmsg = _elf_errmsg
310Sstevel@tonic-gate #pragma weak	elf_errno = _elf_errno
320Sstevel@tonic-gate 
33*3864Sraf #include "syn.h"
34*3864Sraf #include <thread.h>
35*3864Sraf #include <pthread.h>
36*3864Sraf #include <stdlib.h>
37*3864Sraf #include <string.h>
38*3864Sraf #include <stdio.h>
39*3864Sraf #include <libelf.h>
40*3864Sraf #include "msg.h"
41*3864Sraf #include "decl.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	ELFERRSHIFT	16
440Sstevel@tonic-gate #define	SYSERRMASK	0xffff
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * _elf_err has two values encoded in it, both the _elf_err # and
490Sstevel@tonic-gate  * the system errno value (if relevant).  These values are encoded
500Sstevel@tonic-gate  * in the upper & lower 16 bits of the 4 byte integer.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static int		_elf_err = 0;
530Sstevel@tonic-gate 
54*3864Sraf #if !defined(NATIVE_BUILD)
55*3864Sraf 
56*3864Sraf static thread_key_t	errkey = THR_ONCE_KEY;
57*3864Sraf static thread_key_t	bufkey = THR_ONCE_KEY;
58*3864Sraf 
59*3864Sraf #else	/* NATIVE_BUILD */
60*3864Sraf 
61*3864Sraf /*
62*3864Sraf  * This code is here to enable the building of a native version
63*3864Sraf  * of libelf.so when the build machine has not yet been upgraded
64*3864Sraf  * to a version of libc that provides thr_keycreate_once().
65*3864Sraf  * It should be deleted when solaris_nevada ships.
66*3864Sraf  * The code is not MT-safe in a relaxed memory model.
67*3864Sraf  */
68*3864Sraf 
69*3864Sraf static thread_key_t	errkey = 0;
70*3864Sraf static thread_key_t	bufkey = 0;
710Sstevel@tonic-gate 
72*3864Sraf int
73*3864Sraf thr_keycreate_once(thread_key_t *keyp, void (*destructor)(void *))
74*3864Sraf {
75*3864Sraf 	static mutex_t key_lock = DEFAULTMUTEX;
76*3864Sraf 	thread_key_t key;
77*3864Sraf 	int error;
78*3864Sraf 
79*3864Sraf 	if (*keyp == 0) {
80*3864Sraf 		mutex_lock(&key_lock);
81*3864Sraf 		if (*keyp == 0) {
82*3864Sraf 			error = thr_keycreate(&key, destructor);
83*3864Sraf 			if (error) {
84*3864Sraf 				mutex_unlock(&key_lock);
85*3864Sraf 				return (error);
86*3864Sraf 			}
87*3864Sraf 			*keyp = key;
88*3864Sraf 		}
89*3864Sraf 		mutex_unlock(&key_lock);
90*3864Sraf 	}
91*3864Sraf 
92*3864Sraf 	return (0);
93*3864Sraf }
94*3864Sraf 
95*3864Sraf #endif	/* NATIVE_BUILD */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate extern char *_dgettext(const char *, const char *);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate const char *
1010Sstevel@tonic-gate _libelf_msg(Msg mid)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	return (_dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate void
1080Sstevel@tonic-gate _elf_seterr(Msg lib_err, int sys_err)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	/*LINTED*/
1110Sstevel@tonic-gate 	intptr_t encerr = ((int)lib_err << ELFERRSHIFT) |
1120Sstevel@tonic-gate 	    (sys_err & SYSERRMASK);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #ifndef	__lock_lint
1150Sstevel@tonic-gate 	if (thr_main()) {
1160Sstevel@tonic-gate 		_elf_err = (int)encerr;
1170Sstevel@tonic-gate 		return;
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate #endif
120*3864Sraf 	(void) thr_keycreate_once(&errkey, 0);
1210Sstevel@tonic-gate 	(void) thr_setspecific(errkey, (void *)encerr);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
1250Sstevel@tonic-gate _elf_geterr() {
1260Sstevel@tonic-gate #ifndef	__lock_lint
1270Sstevel@tonic-gate 	if (thr_main())
1280Sstevel@tonic-gate 		return (_elf_err);
1290Sstevel@tonic-gate #endif
130*3864Sraf 	return ((uintptr_t)pthread_getspecific(errkey));
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate const char *
1340Sstevel@tonic-gate elf_errmsg(int err)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	char			*errno_str;
1370Sstevel@tonic-gate 	char			*elferr_str;
1380Sstevel@tonic-gate 	char			*buffer = 0;
1390Sstevel@tonic-gate 	int			syserr;
1400Sstevel@tonic-gate 	int			elferr;
1410Sstevel@tonic-gate 	static char		intbuf[MAXELFERR];
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (err == 0) {
1440Sstevel@tonic-gate 		if ((err = _elf_geterr()) == 0)
1450Sstevel@tonic-gate 			return (0);
1460Sstevel@tonic-gate 	} else if (err == -1) {
1470Sstevel@tonic-gate 		if ((err = _elf_geterr()) == 0)
1480Sstevel@tonic-gate 			/*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */
1490Sstevel@tonic-gate 			err = (int)EINF_NULLERROR << ELFERRSHIFT;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (thr_main())
1530Sstevel@tonic-gate 		buffer = intbuf;
1540Sstevel@tonic-gate 	else {
1550Sstevel@tonic-gate 		/*
1560Sstevel@tonic-gate 		 * If this is a threaded APP then we store the
1570Sstevel@tonic-gate 		 * errmsg buffer in Thread Specific Storage.
1580Sstevel@tonic-gate 		 *
1590Sstevel@tonic-gate 		 * Each thread has its own private buffer.
1600Sstevel@tonic-gate 		 */
161*3864Sraf 		if (thr_keycreate_once(&bufkey, free) != 0)
162*3864Sraf 			return (MSG_INTL(EBUG_THRDKEY));
163*3864Sraf 		buffer = pthread_getspecific(bufkey);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		if (!buffer) {
1660Sstevel@tonic-gate 			if ((buffer = malloc(MAXELFERR)) == 0)
1670Sstevel@tonic-gate 				return (MSG_INTL(EMEM_ERRMSG));
1680Sstevel@tonic-gate 			if (thr_setspecific(bufkey, buffer) != 0) {
1690Sstevel@tonic-gate 				free(buffer);
1700Sstevel@tonic-gate 				return (MSG_INTL(EBUG_THRDSET));
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	elferr = (int)((uint_t)err >> ELFERRSHIFT);
1760Sstevel@tonic-gate 	syserr = err & SYSERRMASK;
1770Sstevel@tonic-gate 	/*LINTED*/
1780Sstevel@tonic-gate 	elferr_str = (char *)MSG_INTL(elferr);
1790Sstevel@tonic-gate 	if (syserr && (errno_str = strerror(syserr)))
1800Sstevel@tonic-gate 		(void) snprintf(buffer, MAXELFERR,
1810Sstevel@tonic-gate 		    MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str);
1820Sstevel@tonic-gate 	else {
1830Sstevel@tonic-gate 		(void) strncpy(buffer, elferr_str, MAXELFERR - 1);
1840Sstevel@tonic-gate 		buffer[MAXELFERR - 1] = '\0';
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	return (buffer);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
1910Sstevel@tonic-gate elf_errno()
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	int	rc = _elf_geterr();
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	_elf_seterr(0, 0);
1960Sstevel@tonic-gate 	return (rc);
1970Sstevel@tonic-gate }
198