8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / libelf / common / error.c
blob52b2cb2548ed9c710e7d91f19b285d12fe561b76
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
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"
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;
50 const char *
51 _libelf_msg(Msg mid)
53 return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
56 void
57 _elf_seterr(Msg lib_err, int sys_err)
59 /*LINTED*/
60 intptr_t encerr = ((int)lib_err << ELFERRSHIFT) |
61 (sys_err & SYSERRMASK);
63 if (thr_main()) {
64 _elf_err = (int)encerr;
65 return;
67 (void) thr_keycreate_once(&errkey, 0);
68 (void) thr_setspecific(errkey, (void *)encerr);
71 int
72 _elf_geterr() {
73 if (thr_main())
74 return (_elf_err);
75 return ((uintptr_t)pthread_getspecific(errkey));
78 const char *
79 elf_errmsg(int err)
81 char *errno_str;
82 char *elferr_str;
83 char *buffer = 0;
84 int syserr;
85 int elferr;
86 static char intbuf[MAXELFERR];
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;
97 if (thr_main())
98 buffer = intbuf;
99 else {
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);
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));
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';
132 return (buffer);
136 elf_errno()
138 int rc = _elf_geterr();
140 _elf_seterr(0, 0);
141 return (rc);