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 27*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 293864Sraf #include <thread.h> 303864Sraf #include <pthread.h> 313864Sraf #include <stdlib.h> 323864Sraf #include <string.h> 333864Sraf #include <stdio.h> 343864Sraf #include <libelf.h> 35*6812Sraf #include <libintl.h> 363864Sraf #include "msg.h" 373864Sraf #include "decl.h" 380Sstevel@tonic-gate 390Sstevel@tonic-gate #define ELFERRSHIFT 16 400Sstevel@tonic-gate #define SYSERRMASK 0xffff 410Sstevel@tonic-gate 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * _elf_err has two values encoded in it, both the _elf_err # and 450Sstevel@tonic-gate * the system errno value (if relevant). These values are encoded 460Sstevel@tonic-gate * in the upper & lower 16 bits of the 4 byte integer. 470Sstevel@tonic-gate */ 480Sstevel@tonic-gate static int _elf_err = 0; 490Sstevel@tonic-gate 503864Sraf #if !defined(NATIVE_BUILD) 513864Sraf 523864Sraf static thread_key_t errkey = THR_ONCE_KEY; 533864Sraf static thread_key_t bufkey = THR_ONCE_KEY; 543864Sraf 553864Sraf #else /* NATIVE_BUILD */ 563864Sraf 573864Sraf /* 583864Sraf * This code is here to enable the building of a native version 593864Sraf * of libelf.so when the build machine has not yet been upgraded 603864Sraf * to a version of libc that provides thr_keycreate_once(). 613864Sraf * It should be deleted when solaris_nevada ships. 623864Sraf * The code is not MT-safe in a relaxed memory model. 633864Sraf */ 643864Sraf 653864Sraf static thread_key_t errkey = 0; 663864Sraf static thread_key_t bufkey = 0; 670Sstevel@tonic-gate 683864Sraf int 693864Sraf thr_keycreate_once(thread_key_t *keyp, void (*destructor)(void *)) 703864Sraf { 713864Sraf static mutex_t key_lock = DEFAULTMUTEX; 723864Sraf thread_key_t key; 733864Sraf int error; 743864Sraf 753864Sraf if (*keyp == 0) { 763864Sraf mutex_lock(&key_lock); 773864Sraf if (*keyp == 0) { 783864Sraf error = thr_keycreate(&key, destructor); 793864Sraf if (error) { 803864Sraf mutex_unlock(&key_lock); 813864Sraf return (error); 823864Sraf } 833864Sraf *keyp = key; 843864Sraf } 853864Sraf mutex_unlock(&key_lock); 863864Sraf } 873864Sraf 883864Sraf return (0); 893864Sraf } 903864Sraf 913864Sraf #endif /* NATIVE_BUILD */ 920Sstevel@tonic-gate 930Sstevel@tonic-gate 940Sstevel@tonic-gate const char * 950Sstevel@tonic-gate _libelf_msg(Msg mid) 960Sstevel@tonic-gate { 97*6812Sraf return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid))); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate void 1020Sstevel@tonic-gate _elf_seterr(Msg lib_err, int sys_err) 1030Sstevel@tonic-gate { 1040Sstevel@tonic-gate /*LINTED*/ 1050Sstevel@tonic-gate intptr_t encerr = ((int)lib_err << ELFERRSHIFT) | 1060Sstevel@tonic-gate (sys_err & SYSERRMASK); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate #ifndef __lock_lint 1090Sstevel@tonic-gate if (thr_main()) { 1100Sstevel@tonic-gate _elf_err = (int)encerr; 1110Sstevel@tonic-gate return; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate #endif 1143864Sraf (void) thr_keycreate_once(&errkey, 0); 1150Sstevel@tonic-gate (void) thr_setspecific(errkey, (void *)encerr); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate int 1190Sstevel@tonic-gate _elf_geterr() { 1200Sstevel@tonic-gate #ifndef __lock_lint 1210Sstevel@tonic-gate if (thr_main()) 1220Sstevel@tonic-gate return (_elf_err); 1230Sstevel@tonic-gate #endif 1243864Sraf return ((uintptr_t)pthread_getspecific(errkey)); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate const char * 1280Sstevel@tonic-gate elf_errmsg(int err) 1290Sstevel@tonic-gate { 1300Sstevel@tonic-gate char *errno_str; 1310Sstevel@tonic-gate char *elferr_str; 1320Sstevel@tonic-gate char *buffer = 0; 1330Sstevel@tonic-gate int syserr; 1340Sstevel@tonic-gate int elferr; 1350Sstevel@tonic-gate static char intbuf[MAXELFERR]; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate if (err == 0) { 1380Sstevel@tonic-gate if ((err = _elf_geterr()) == 0) 1390Sstevel@tonic-gate return (0); 1400Sstevel@tonic-gate } else if (err == -1) { 1410Sstevel@tonic-gate if ((err = _elf_geterr()) == 0) 1420Sstevel@tonic-gate /*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */ 1430Sstevel@tonic-gate err = (int)EINF_NULLERROR << ELFERRSHIFT; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate if (thr_main()) 1470Sstevel@tonic-gate buffer = intbuf; 1480Sstevel@tonic-gate else { 1490Sstevel@tonic-gate /* 1500Sstevel@tonic-gate * If this is a threaded APP then we store the 1510Sstevel@tonic-gate * errmsg buffer in Thread Specific Storage. 1520Sstevel@tonic-gate * 1530Sstevel@tonic-gate * Each thread has its own private buffer. 1540Sstevel@tonic-gate */ 1553864Sraf if (thr_keycreate_once(&bufkey, free) != 0) 1563864Sraf return (MSG_INTL(EBUG_THRDKEY)); 1573864Sraf buffer = pthread_getspecific(bufkey); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate if (!buffer) { 1600Sstevel@tonic-gate if ((buffer = malloc(MAXELFERR)) == 0) 1610Sstevel@tonic-gate return (MSG_INTL(EMEM_ERRMSG)); 1620Sstevel@tonic-gate if (thr_setspecific(bufkey, buffer) != 0) { 1630Sstevel@tonic-gate free(buffer); 1640Sstevel@tonic-gate return (MSG_INTL(EBUG_THRDSET)); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate elferr = (int)((uint_t)err >> ELFERRSHIFT); 1700Sstevel@tonic-gate syserr = err & SYSERRMASK; 1710Sstevel@tonic-gate /*LINTED*/ 1720Sstevel@tonic-gate elferr_str = (char *)MSG_INTL(elferr); 1730Sstevel@tonic-gate if (syserr && (errno_str = strerror(syserr))) 1740Sstevel@tonic-gate (void) snprintf(buffer, MAXELFERR, 1750Sstevel@tonic-gate MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str); 1760Sstevel@tonic-gate else { 1770Sstevel@tonic-gate (void) strncpy(buffer, elferr_str, MAXELFERR - 1); 1780Sstevel@tonic-gate buffer[MAXELFERR - 1] = '\0'; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate return (buffer); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate int 1850Sstevel@tonic-gate elf_errno() 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate int rc = _elf_geterr(); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate _elf_seterr(0, 0); 1900Sstevel@tonic-gate return (rc); 1910Sstevel@tonic-gate } 192