1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.16 */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #pragma weak elf_errmsg = _elf_errmsg 31*0Sstevel@tonic-gate #pragma weak elf_errno = _elf_errno 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include "syn.h" 34*0Sstevel@tonic-gate #include <thread.h> 35*0Sstevel@tonic-gate #include <stdlib.h> 36*0Sstevel@tonic-gate #include <string.h> 37*0Sstevel@tonic-gate #include <stdio.h> 38*0Sstevel@tonic-gate #include <libelf.h> 39*0Sstevel@tonic-gate #include "msg.h" 40*0Sstevel@tonic-gate #include "decl.h" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #define ELFERRSHIFT 16 43*0Sstevel@tonic-gate #define SYSERRMASK 0xffff 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate /* 47*0Sstevel@tonic-gate * _elf_err has two values encoded in it, both the _elf_err # and 48*0Sstevel@tonic-gate * the system errno value (if relevant). These values are encoded 49*0Sstevel@tonic-gate * in the upper & lower 16 bits of the 4 byte integer. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate static int _elf_err = 0; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate static mutex_t keylock; 54*0Sstevel@tonic-gate static mutex_t buflock; 55*0Sstevel@tonic-gate static thread_key_t errkey; 56*0Sstevel@tonic-gate static thread_key_t bufkey; 57*0Sstevel@tonic-gate static int keyonce = 0; 58*0Sstevel@tonic-gate static int bufonce = 0; 59*0Sstevel@tonic-gate NOTE(DATA_READABLE_WITHOUT_LOCK(keyonce)) 60*0Sstevel@tonic-gate NOTE(DATA_READABLE_WITHOUT_LOCK(bufonce)) 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate extern char *_dgettext(const char *, const char *); 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate const char * 67*0Sstevel@tonic-gate _libelf_msg(Msg mid) 68*0Sstevel@tonic-gate { 69*0Sstevel@tonic-gate return (_dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid))); 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate void 74*0Sstevel@tonic-gate _elf_seterr(Msg lib_err, int sys_err) 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate /*LINTED*/ 77*0Sstevel@tonic-gate intptr_t encerr = ((int)lib_err << ELFERRSHIFT) | 78*0Sstevel@tonic-gate (sys_err & SYSERRMASK); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate #ifndef __lock_lint 81*0Sstevel@tonic-gate if (thr_main()) { 82*0Sstevel@tonic-gate _elf_err = (int)encerr; 83*0Sstevel@tonic-gate return; 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate #endif 86*0Sstevel@tonic-gate if (keyonce == 0) { 87*0Sstevel@tonic-gate (void) mutex_lock(&keylock); 88*0Sstevel@tonic-gate if (keyonce == 0) { 89*0Sstevel@tonic-gate (void) thr_keycreate(&errkey, 0); 90*0Sstevel@tonic-gate keyonce++; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate (void) mutex_unlock(&keylock); 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate (void) thr_setspecific(errkey, (void *)encerr); 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate int 99*0Sstevel@tonic-gate _elf_geterr() { 100*0Sstevel@tonic-gate intptr_t rc; 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate #ifndef __lock_lint 103*0Sstevel@tonic-gate if (thr_main()) 104*0Sstevel@tonic-gate return (_elf_err); 105*0Sstevel@tonic-gate #endif 106*0Sstevel@tonic-gate if (keyonce == 0) 107*0Sstevel@tonic-gate return (0); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate (void) thr_getspecific(errkey, (void **)(&rc)); 110*0Sstevel@tonic-gate return ((int)rc); 111*0Sstevel@tonic-gate } 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate const char * 114*0Sstevel@tonic-gate elf_errmsg(int err) 115*0Sstevel@tonic-gate { 116*0Sstevel@tonic-gate char *errno_str; 117*0Sstevel@tonic-gate char *elferr_str; 118*0Sstevel@tonic-gate char *buffer = 0; 119*0Sstevel@tonic-gate int syserr; 120*0Sstevel@tonic-gate int elferr; 121*0Sstevel@tonic-gate static char intbuf[MAXELFERR]; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate if (err == 0) { 124*0Sstevel@tonic-gate if ((err = _elf_geterr()) == 0) 125*0Sstevel@tonic-gate return (0); 126*0Sstevel@tonic-gate } else if (err == -1) { 127*0Sstevel@tonic-gate if ((err = _elf_geterr()) == 0) 128*0Sstevel@tonic-gate /*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */ 129*0Sstevel@tonic-gate err = (int)EINF_NULLERROR << ELFERRSHIFT; 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate if (thr_main()) 133*0Sstevel@tonic-gate buffer = intbuf; 134*0Sstevel@tonic-gate else { 135*0Sstevel@tonic-gate /* 136*0Sstevel@tonic-gate * If this is a threaded APP then we store the 137*0Sstevel@tonic-gate * errmsg buffer in Thread Specific Storage. 138*0Sstevel@tonic-gate * 139*0Sstevel@tonic-gate * Each thread has its own private buffer. 140*0Sstevel@tonic-gate */ 141*0Sstevel@tonic-gate if (bufonce == 0) { 142*0Sstevel@tonic-gate (void) mutex_lock(&buflock); 143*0Sstevel@tonic-gate if (bufonce == 0) { 144*0Sstevel@tonic-gate if (thr_keycreate(&bufkey, free) != 0) { 145*0Sstevel@tonic-gate (void) mutex_unlock(&buflock); 146*0Sstevel@tonic-gate return (MSG_INTL(EBUG_THRDKEY)); 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate bufonce++; 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate (void) mutex_unlock(&buflock); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate (void) thr_getspecific(bufkey, (void **)&buffer); 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate if (!buffer) { 156*0Sstevel@tonic-gate if ((buffer = malloc(MAXELFERR)) == 0) 157*0Sstevel@tonic-gate return (MSG_INTL(EMEM_ERRMSG)); 158*0Sstevel@tonic-gate if (thr_setspecific(bufkey, buffer) != 0) { 159*0Sstevel@tonic-gate free(buffer); 160*0Sstevel@tonic-gate return (MSG_INTL(EBUG_THRDSET)); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate elferr = (int)((uint_t)err >> ELFERRSHIFT); 166*0Sstevel@tonic-gate syserr = err & SYSERRMASK; 167*0Sstevel@tonic-gate /*LINTED*/ 168*0Sstevel@tonic-gate elferr_str = (char *)MSG_INTL(elferr); 169*0Sstevel@tonic-gate if (syserr && (errno_str = strerror(syserr))) 170*0Sstevel@tonic-gate (void) snprintf(buffer, MAXELFERR, 171*0Sstevel@tonic-gate MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str); 172*0Sstevel@tonic-gate else { 173*0Sstevel@tonic-gate (void) strncpy(buffer, elferr_str, MAXELFERR - 1); 174*0Sstevel@tonic-gate buffer[MAXELFERR - 1] = '\0'; 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate return (buffer); 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate int 181*0Sstevel@tonic-gate elf_errno() 182*0Sstevel@tonic-gate { 183*0Sstevel@tonic-gate int rc = _elf_geterr(); 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate _elf_seterr(0, 0); 186*0Sstevel@tonic-gate return (rc); 187*0Sstevel@tonic-gate } 188