xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/error.c (revision 2bbdd445a21f9d61f4a0ca0faf05d5ceb2bd91f3)
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 #include <thread.h>
28 #include <pthread.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <libelf.h>
33 #include <libintl.h>
34 #include "msg.h"
35 #include "decl.h"
36 
37 #define	ELFERRSHIFT	16
38 #define	SYSERRMASK	0xffff
39 
40 /*
41  * _elf_err has two values encoded in it, both the _elf_err # and
42  * the system errno value (if relevant).  These values are encoded
43  * in the upper & lower 16 bits of the 4 byte integer.
44  */
45 static int		_elf_err = 0;
46 
47 static thread_key_t	errkey = THR_ONCE_KEY;
48 static thread_key_t	bufkey = THR_ONCE_KEY;
49 
50 const char *
51 _libelf_msg(Msg mid)
52 {
53 	return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
54 }
55 
56 void
57 _elf_seterr(Msg lib_err, int sys_err)
58 {
59 	/*LINTED*/
60 	intptr_t encerr = ((int)lib_err << ELFERRSHIFT) |
61 	    (sys_err & SYSERRMASK);
62 
63 #ifndef	__lock_lint
64 	if (thr_main()) {
65 		_elf_err = (int)encerr;
66 		return;
67 	}
68 #endif
69 	(void) thr_keycreate_once(&errkey, 0);
70 	(void) thr_setspecific(errkey, (void *)encerr);
71 }
72 
73 int
74 _elf_geterr() {
75 #ifndef	__lock_lint
76 	if (thr_main())
77 		return (_elf_err);
78 #endif
79 	return ((uintptr_t)pthread_getspecific(errkey));
80 }
81 
82 const char *
83 elf_errmsg(int err)
84 {
85 	char			*errno_str;
86 	char			*elferr_str;
87 	char			*buffer = 0;
88 	int			syserr;
89 	int			elferr;
90 	static char		intbuf[MAXELFERR];
91 
92 	if (err == 0) {
93 		if ((err = _elf_geterr()) == 0)
94 			return (0);
95 	} else if (err == -1) {
96 		if ((err = _elf_geterr()) == 0)
97 			/*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */
98 			err = (int)EINF_NULLERROR << ELFERRSHIFT;
99 	}
100 
101 	if (thr_main())
102 		buffer = intbuf;
103 	else {
104 		/*
105 		 * If this is a threaded APP then we store the
106 		 * errmsg buffer in Thread Specific Storage.
107 		 *
108 		 * Each thread has its own private buffer.
109 		 */
110 		if (thr_keycreate_once(&bufkey, free) != 0)
111 			return (MSG_INTL(EBUG_THRDKEY));
112 		buffer = pthread_getspecific(bufkey);
113 
114 		if (!buffer) {
115 			if ((buffer = malloc(MAXELFERR)) == 0)
116 				return (MSG_INTL(EMEM_ERRMSG));
117 			if (thr_setspecific(bufkey, buffer) != 0) {
118 				free(buffer);
119 				return (MSG_INTL(EBUG_THRDSET));
120 			}
121 		}
122 	}
123 
124 	elferr = (int)((uint_t)err >> ELFERRSHIFT);
125 	syserr = err & SYSERRMASK;
126 	/*LINTED*/
127 	elferr_str = (char *)MSG_INTL(elferr);
128 	if (syserr && (errno_str = strerror(syserr)))
129 		(void) snprintf(buffer, MAXELFERR,
130 		    MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str);
131 	else {
132 		(void) strncpy(buffer, elferr_str, MAXELFERR - 1);
133 		buffer[MAXELFERR - 1] = '\0';
134 	}
135 
136 	return (buffer);
137 }
138 
139 int
140 elf_errno()
141 {
142 	int	rc = _elf_geterr();
143 
144 	_elf_seterr(0, 0);
145 	return (rc);
146 }
147