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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
37 #define ELFERRSHIFT 16
38 #define SYSERRMASK 0xffff
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.
45 static int _elf_err
= 0;
47 static thread_key_t errkey
= THR_ONCE_KEY
;
48 static thread_key_t bufkey
= THR_ONCE_KEY
;
53 return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS
), MSG_ORIG(mid
)));
57 _elf_seterr(Msg lib_err
, int sys_err
)
60 intptr_t encerr
= ((int)lib_err
<< ELFERRSHIFT
) |
61 (sys_err
& SYSERRMASK
);
64 _elf_err
= (int)encerr
;
67 (void) thr_keycreate_once(&errkey
, 0);
68 (void) thr_setspecific(errkey
, (void *)encerr
);
75 return ((uintptr_t)pthread_getspecific(errkey
));
86 static char intbuf
[MAXELFERR
];
89 if ((err
= _elf_geterr()) == 0)
91 } else if (err
== -1) {
92 if ((err
= _elf_geterr()) == 0)
93 /*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */
94 err
= (int)EINF_NULLERROR
<< ELFERRSHIFT
;
101 * If this is a threaded APP then we store the
102 * errmsg buffer in Thread Specific Storage.
104 * Each thread has its own private buffer.
106 if (thr_keycreate_once(&bufkey
, free
) != 0)
107 return (MSG_INTL(EBUG_THRDKEY
));
108 buffer
= pthread_getspecific(bufkey
);
111 if ((buffer
= malloc(MAXELFERR
)) == 0)
112 return (MSG_INTL(EMEM_ERRMSG
));
113 if (thr_setspecific(bufkey
, buffer
) != 0) {
115 return (MSG_INTL(EBUG_THRDSET
));
120 elferr
= (int)((uint_t
)err
>> ELFERRSHIFT
);
121 syserr
= err
& SYSERRMASK
;
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
);
128 (void) strncpy(buffer
, elferr_str
, MAXELFERR
- 1);
129 buffer
[MAXELFERR
- 1] = '\0';
138 int rc
= _elf_geterr();