xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/error.c (revision 3385b4726f1b09aa9511340017fb23c7cde6aadd)
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 	if (thr_main()) {
64 		_elf_err = (int)encerr;
65 		return;
66 	}
67 	(void) thr_keycreate_once(&errkey, 0);
68 	(void) thr_setspecific(errkey, (void *)encerr);
69 }
70 
71 int
72 _elf_geterr() {
73 	if (thr_main())
74 		return (_elf_err);
75 	return ((uintptr_t)pthread_getspecific(errkey));
76 }
77 
78 const char *
79 elf_errmsg(int err)
80 {
81 	char			*errno_str;
82 	char			*elferr_str;
83 	char			*buffer = 0;
84 	int			syserr;
85 	int			elferr;
86 	static char		intbuf[MAXELFERR];
87 
88 	if (err == 0) {
89 		if ((err = _elf_geterr()) == 0)
90 			return (0);
91 	} else if (err == -1) {
92 		if ((err = _elf_geterr()) == 0)
93 			/*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */
94 			err = (int)EINF_NULLERROR << ELFERRSHIFT;
95 	}
96 
97 	if (thr_main())
98 		buffer = intbuf;
99 	else {
100 		/*
101 		 * If this is a threaded APP then we store the
102 		 * errmsg buffer in Thread Specific Storage.
103 		 *
104 		 * Each thread has its own private buffer.
105 		 */
106 		if (thr_keycreate_once(&bufkey, free) != 0)
107 			return (MSG_INTL(EBUG_THRDKEY));
108 		buffer = pthread_getspecific(bufkey);
109 
110 		if (!buffer) {
111 			if ((buffer = malloc(MAXELFERR)) == 0)
112 				return (MSG_INTL(EMEM_ERRMSG));
113 			if (thr_setspecific(bufkey, buffer) != 0) {
114 				free(buffer);
115 				return (MSG_INTL(EBUG_THRDSET));
116 			}
117 		}
118 	}
119 
120 	elferr = (int)((uint_t)err >> ELFERRSHIFT);
121 	syserr = err & SYSERRMASK;
122 	/*LINTED*/
123 	elferr_str = (char *)MSG_INTL(elferr);
124 	if (syserr && (errno_str = strerror(syserr)))
125 		(void) snprintf(buffer, MAXELFERR,
126 		    MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str);
127 	else {
128 		(void) strncpy(buffer, elferr_str, MAXELFERR - 1);
129 		buffer[MAXELFERR - 1] = '\0';
130 	}
131 
132 	return (buffer);
133 }
134 
135 int
136 elf_errno()
137 {
138 	int	rc = _elf_geterr();
139 
140 	_elf_seterr(0, 0);
141 	return (rc);
142 }
143