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
53864Sraf * Common Development and Distribution License (the "License").
63864Sraf * 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 */
213864Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
273864Sraf #include <thread.h>
283864Sraf #include <pthread.h>
293864Sraf #include <stdlib.h>
303864Sraf #include <string.h>
313864Sraf #include <stdio.h>
323864Sraf #include <libelf.h>
33*6812Sraf #include <libintl.h>
343864Sraf #include "msg.h"
353864Sraf #include "decl.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate #define ELFERRSHIFT 16
380Sstevel@tonic-gate #define SYSERRMASK 0xffff
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * _elf_err has two values encoded in it, both the _elf_err # and
420Sstevel@tonic-gate * the system errno value (if relevant). These values are encoded
430Sstevel@tonic-gate * in the upper & lower 16 bits of the 4 byte integer.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate static int _elf_err = 0;
460Sstevel@tonic-gate
473864Sraf static thread_key_t errkey = THR_ONCE_KEY;
483864Sraf static thread_key_t bufkey = THR_ONCE_KEY;
493864Sraf
500Sstevel@tonic-gate const char *
_libelf_msg(Msg mid)510Sstevel@tonic-gate _libelf_msg(Msg mid)
520Sstevel@tonic-gate {
53*6812Sraf return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
540Sstevel@tonic-gate }
550Sstevel@tonic-gate
560Sstevel@tonic-gate void
_elf_seterr(Msg lib_err,int sys_err)570Sstevel@tonic-gate _elf_seterr(Msg lib_err, int sys_err)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate /*LINTED*/
600Sstevel@tonic-gate intptr_t encerr = ((int)lib_err << ELFERRSHIFT) |
610Sstevel@tonic-gate (sys_err & SYSERRMASK);
620Sstevel@tonic-gate
630Sstevel@tonic-gate #ifndef __lock_lint
640Sstevel@tonic-gate if (thr_main()) {
650Sstevel@tonic-gate _elf_err = (int)encerr;
660Sstevel@tonic-gate return;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate #endif
693864Sraf (void) thr_keycreate_once(&errkey, 0);
700Sstevel@tonic-gate (void) thr_setspecific(errkey, (void *)encerr);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate int
_elf_geterr()740Sstevel@tonic-gate _elf_geterr() {
750Sstevel@tonic-gate #ifndef __lock_lint
760Sstevel@tonic-gate if (thr_main())
770Sstevel@tonic-gate return (_elf_err);
780Sstevel@tonic-gate #endif
793864Sraf return ((uintptr_t)pthread_getspecific(errkey));
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate const char *
elf_errmsg(int err)830Sstevel@tonic-gate elf_errmsg(int err)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate char *errno_str;
860Sstevel@tonic-gate char *elferr_str;
870Sstevel@tonic-gate char *buffer = 0;
880Sstevel@tonic-gate int syserr;
890Sstevel@tonic-gate int elferr;
900Sstevel@tonic-gate static char intbuf[MAXELFERR];
910Sstevel@tonic-gate
920Sstevel@tonic-gate if (err == 0) {
930Sstevel@tonic-gate if ((err = _elf_geterr()) == 0)
940Sstevel@tonic-gate return (0);
950Sstevel@tonic-gate } else if (err == -1) {
960Sstevel@tonic-gate if ((err = _elf_geterr()) == 0)
970Sstevel@tonic-gate /*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */
980Sstevel@tonic-gate err = (int)EINF_NULLERROR << ELFERRSHIFT;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (thr_main())
1020Sstevel@tonic-gate buffer = intbuf;
1030Sstevel@tonic-gate else {
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * If this is a threaded APP then we store the
1060Sstevel@tonic-gate * errmsg buffer in Thread Specific Storage.
1070Sstevel@tonic-gate *
1080Sstevel@tonic-gate * Each thread has its own private buffer.
1090Sstevel@tonic-gate */
1103864Sraf if (thr_keycreate_once(&bufkey, free) != 0)
1113864Sraf return (MSG_INTL(EBUG_THRDKEY));
1123864Sraf buffer = pthread_getspecific(bufkey);
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate if (!buffer) {
1150Sstevel@tonic-gate if ((buffer = malloc(MAXELFERR)) == 0)
1160Sstevel@tonic-gate return (MSG_INTL(EMEM_ERRMSG));
1170Sstevel@tonic-gate if (thr_setspecific(bufkey, buffer) != 0) {
1180Sstevel@tonic-gate free(buffer);
1190Sstevel@tonic-gate return (MSG_INTL(EBUG_THRDSET));
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate elferr = (int)((uint_t)err >> ELFERRSHIFT);
1250Sstevel@tonic-gate syserr = err & SYSERRMASK;
1260Sstevel@tonic-gate /*LINTED*/
1270Sstevel@tonic-gate elferr_str = (char *)MSG_INTL(elferr);
1280Sstevel@tonic-gate if (syserr && (errno_str = strerror(syserr)))
1290Sstevel@tonic-gate (void) snprintf(buffer, MAXELFERR,
1300Sstevel@tonic-gate MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str);
1310Sstevel@tonic-gate else {
1320Sstevel@tonic-gate (void) strncpy(buffer, elferr_str, MAXELFERR - 1);
1330Sstevel@tonic-gate buffer[MAXELFERR - 1] = '\0';
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate return (buffer);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate int
elf_errno()1400Sstevel@tonic-gate elf_errno()
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate int rc = _elf_geterr();
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate _elf_seterr(0, 0);
1450Sstevel@tonic-gate return (rc);
1460Sstevel@tonic-gate }
147