xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/error.c (revision e2f4f3da)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21cb620785Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27cb620785Sraf #include <thread.h>
28cb620785Sraf #include <pthread.h>
29cb620785Sraf #include <stdlib.h>
30cb620785Sraf #include <string.h>
31cb620785Sraf #include <stdio.h>
32cb620785Sraf #include <libelf.h>
33*7257d1b4Sraf #include <libintl.h>
34cb620785Sraf #include "msg.h"
35cb620785Sraf #include "decl.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	ELFERRSHIFT	16
387c478bd9Sstevel@tonic-gate #define	SYSERRMASK	0xffff
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * _elf_err has two values encoded in it, both the _elf_err # and
427c478bd9Sstevel@tonic-gate  * the system errno value (if relevant).  These values are encoded
437c478bd9Sstevel@tonic-gate  * in the upper & lower 16 bits of the 4 byte integer.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate static int		_elf_err = 0;
467c478bd9Sstevel@tonic-gate 
47cb620785Sraf static thread_key_t	errkey = THR_ONCE_KEY;
48cb620785Sraf static thread_key_t	bufkey = THR_ONCE_KEY;
49cb620785Sraf 
507c478bd9Sstevel@tonic-gate const char *
_libelf_msg(Msg mid)517c478bd9Sstevel@tonic-gate _libelf_msg(Msg mid)
527c478bd9Sstevel@tonic-gate {
53*7257d1b4Sraf 	return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate void
_elf_seterr(Msg lib_err,int sys_err)577c478bd9Sstevel@tonic-gate _elf_seterr(Msg lib_err, int sys_err)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	/*LINTED*/
607c478bd9Sstevel@tonic-gate 	intptr_t encerr = ((int)lib_err << ELFERRSHIFT) |
617c478bd9Sstevel@tonic-gate 	    (sys_err & SYSERRMASK);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (thr_main()) {
647c478bd9Sstevel@tonic-gate 		_elf_err = (int)encerr;
657c478bd9Sstevel@tonic-gate 		return;
667c478bd9Sstevel@tonic-gate 	}
67cb620785Sraf 	(void) thr_keycreate_once(&errkey, 0);
687c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(errkey, (void *)encerr);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate int
_elf_geterr()727c478bd9Sstevel@tonic-gate _elf_geterr() {
737c478bd9Sstevel@tonic-gate 	if (thr_main())
747c478bd9Sstevel@tonic-gate 		return (_elf_err);
75cb620785Sraf 	return ((uintptr_t)pthread_getspecific(errkey));
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate const char *
elf_errmsg(int err)797c478bd9Sstevel@tonic-gate elf_errmsg(int err)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	char			*errno_str;
827c478bd9Sstevel@tonic-gate 	char			*elferr_str;
837c478bd9Sstevel@tonic-gate 	char			*buffer = 0;
847c478bd9Sstevel@tonic-gate 	int			syserr;
857c478bd9Sstevel@tonic-gate 	int			elferr;
867c478bd9Sstevel@tonic-gate 	static char		intbuf[MAXELFERR];
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (err == 0) {
897c478bd9Sstevel@tonic-gate 		if ((err = _elf_geterr()) == 0)
907c478bd9Sstevel@tonic-gate 			return (0);
917c478bd9Sstevel@tonic-gate 	} else if (err == -1) {
927c478bd9Sstevel@tonic-gate 		if ((err = _elf_geterr()) == 0)
937c478bd9Sstevel@tonic-gate 			/*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */
947c478bd9Sstevel@tonic-gate 			err = (int)EINF_NULLERROR << ELFERRSHIFT;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (thr_main())
987c478bd9Sstevel@tonic-gate 		buffer = intbuf;
997c478bd9Sstevel@tonic-gate 	else {
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * If this is a threaded APP then we store the
1027c478bd9Sstevel@tonic-gate 		 * errmsg buffer in Thread Specific Storage.
1037c478bd9Sstevel@tonic-gate 		 *
1047c478bd9Sstevel@tonic-gate 		 * Each thread has its own private buffer.
1057c478bd9Sstevel@tonic-gate 		 */
106cb620785Sraf 		if (thr_keycreate_once(&bufkey, free) != 0)
107cb620785Sraf 			return (MSG_INTL(EBUG_THRDKEY));
108cb620785Sraf 		buffer = pthread_getspecific(bufkey);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		if (!buffer) {
1117c478bd9Sstevel@tonic-gate 			if ((buffer = malloc(MAXELFERR)) == 0)
1127c478bd9Sstevel@tonic-gate 				return (MSG_INTL(EMEM_ERRMSG));
1137c478bd9Sstevel@tonic-gate 			if (thr_setspecific(bufkey, buffer) != 0) {
1147c478bd9Sstevel@tonic-gate 				free(buffer);
1157c478bd9Sstevel@tonic-gate 				return (MSG_INTL(EBUG_THRDSET));
1167c478bd9Sstevel@tonic-gate 			}
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	elferr = (int)((uint_t)err >> ELFERRSHIFT);
1217c478bd9Sstevel@tonic-gate 	syserr = err & SYSERRMASK;
1227c478bd9Sstevel@tonic-gate 	/*LINTED*/
1237c478bd9Sstevel@tonic-gate 	elferr_str = (char *)MSG_INTL(elferr);
1247c478bd9Sstevel@tonic-gate 	if (syserr && (errno_str = strerror(syserr)))
1257c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer, MAXELFERR,
1267c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str);
1277c478bd9Sstevel@tonic-gate 	else {
1287c478bd9Sstevel@tonic-gate 		(void) strncpy(buffer, elferr_str, MAXELFERR - 1);
1297c478bd9Sstevel@tonic-gate 		buffer[MAXELFERR - 1] = '\0';
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (buffer);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
elf_errno()1367c478bd9Sstevel@tonic-gate elf_errno()
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	int	rc = _elf_geterr();
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	_elf_seterr(0, 0);
1417c478bd9Sstevel@tonic-gate 	return (rc);
1427c478bd9Sstevel@tonic-gate }
143