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 277c478bd9Sstevel@tonic-gate #include <thread.h> 28cb620785Sraf #include <pthread.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <stdio.h> 327c478bd9Sstevel@tonic-gate #include <libelf.h> 33*7257d1b4Sraf #include <libintl.h> 347c478bd9Sstevel@tonic-gate #include "msg.h" 357c478bd9Sstevel@tonic-gate #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 * 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 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 #ifndef __lock_lint 647c478bd9Sstevel@tonic-gate if (thr_main()) { 657c478bd9Sstevel@tonic-gate _elf_err = (int)encerr; 667c478bd9Sstevel@tonic-gate return; 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate #endif 69cb620785Sraf (void) thr_keycreate_once(&errkey, 0); 707c478bd9Sstevel@tonic-gate (void) thr_setspecific(errkey, (void *)encerr); 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate int 747c478bd9Sstevel@tonic-gate _elf_geterr() { 757c478bd9Sstevel@tonic-gate #ifndef __lock_lint 767c478bd9Sstevel@tonic-gate if (thr_main()) 777c478bd9Sstevel@tonic-gate return (_elf_err); 787c478bd9Sstevel@tonic-gate #endif 79cb620785Sraf return ((uintptr_t)pthread_getspecific(errkey)); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate const char * 837c478bd9Sstevel@tonic-gate elf_errmsg(int err) 847c478bd9Sstevel@tonic-gate { 857c478bd9Sstevel@tonic-gate char *errno_str; 867c478bd9Sstevel@tonic-gate char *elferr_str; 877c478bd9Sstevel@tonic-gate char *buffer = 0; 887c478bd9Sstevel@tonic-gate int syserr; 897c478bd9Sstevel@tonic-gate int elferr; 907c478bd9Sstevel@tonic-gate static char intbuf[MAXELFERR]; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate if (err == 0) { 937c478bd9Sstevel@tonic-gate if ((err = _elf_geterr()) == 0) 947c478bd9Sstevel@tonic-gate return (0); 957c478bd9Sstevel@tonic-gate } else if (err == -1) { 967c478bd9Sstevel@tonic-gate if ((err = _elf_geterr()) == 0) 977c478bd9Sstevel@tonic-gate /*LINTED*/ /* MSG_INTL(EINF_NULLERROR) */ 987c478bd9Sstevel@tonic-gate err = (int)EINF_NULLERROR << ELFERRSHIFT; 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (thr_main()) 1027c478bd9Sstevel@tonic-gate buffer = intbuf; 1037c478bd9Sstevel@tonic-gate else { 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * If this is a threaded APP then we store the 1067c478bd9Sstevel@tonic-gate * errmsg buffer in Thread Specific Storage. 1077c478bd9Sstevel@tonic-gate * 1087c478bd9Sstevel@tonic-gate * Each thread has its own private buffer. 1097c478bd9Sstevel@tonic-gate */ 110cb620785Sraf if (thr_keycreate_once(&bufkey, free) != 0) 1117c478bd9Sstevel@tonic-gate return (MSG_INTL(EBUG_THRDKEY)); 112cb620785Sraf buffer = pthread_getspecific(bufkey); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if (!buffer) { 1157c478bd9Sstevel@tonic-gate if ((buffer = malloc(MAXELFERR)) == 0) 1167c478bd9Sstevel@tonic-gate return (MSG_INTL(EMEM_ERRMSG)); 1177c478bd9Sstevel@tonic-gate if (thr_setspecific(bufkey, buffer) != 0) { 1187c478bd9Sstevel@tonic-gate free(buffer); 1197c478bd9Sstevel@tonic-gate return (MSG_INTL(EBUG_THRDSET)); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate elferr = (int)((uint_t)err >> ELFERRSHIFT); 1257c478bd9Sstevel@tonic-gate syserr = err & SYSERRMASK; 1267c478bd9Sstevel@tonic-gate /*LINTED*/ 1277c478bd9Sstevel@tonic-gate elferr_str = (char *)MSG_INTL(elferr); 1287c478bd9Sstevel@tonic-gate if (syserr && (errno_str = strerror(syserr))) 1297c478bd9Sstevel@tonic-gate (void) snprintf(buffer, MAXELFERR, 1307c478bd9Sstevel@tonic-gate MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str); 1317c478bd9Sstevel@tonic-gate else { 1327c478bd9Sstevel@tonic-gate (void) strncpy(buffer, elferr_str, MAXELFERR - 1); 1337c478bd9Sstevel@tonic-gate buffer[MAXELFERR - 1] = '\0'; 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate return (buffer); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate int 1407c478bd9Sstevel@tonic-gate elf_errno() 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate int rc = _elf_geterr(); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate _elf_seterr(0, 0); 1457c478bd9Sstevel@tonic-gate return (rc); 1467c478bd9Sstevel@tonic-gate } 147