#pragma ident "@(#)error.c 1.22 08/06/03 SMI"
#if !defined(__APPLE__)
#include <thread.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <libelf.h>
#include <libintl.h>
#include "msg.h"
#else
#include <pthread.h>
#define thr_keycreate pthread_key_create
#define thr_getspecific(key, pval) (*pval = pthread_getspecific( key ))
#define thr_setspecific pthread_setspecific
#define thr_main() 0
int __libc_threaded = 1;
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <libelf.h>
#include "msg.h"
#undef _elf_seterr
#define EINF_NULLERROR 0
#define EBUG_THRDKEY 0
#define MSG_SUNW_OST_SGS -2
#define MSG_FMT_ERR -1
#define MSG_INTL(x) "libelf internal error"
#define MSG_ORIG(x) (x == MSG_FMT_ERR ? "%s %s" : NULL)
char *_dgettext(const char *x, const char *y) { return "libelf internal error"; }
#define dgettext(x,y) _dgettext(x,y)
#define NATIVE_BUILD 1
#endif
#include "decl.h"
#define ELFERRSHIFT 16
#define SYSERRMASK 0xffff
static int _elf_err = 0;
#if !defined(NATIVE_BUILD)
static thread_key_t errkey = THR_ONCE_KEY;
static thread_key_t bufkey = THR_ONCE_KEY;
#else
static thread_key_t errkey = 0;
static thread_key_t bufkey = 0;
int
thr_keycreate_once(thread_key_t *keyp, void (*destructor)(void *))
{
static mutex_t key_lock = DEFAULTMUTEX;
thread_key_t key;
int error;
if (*keyp == 0) {
mutex_lock(&key_lock);
if (*keyp == 0) {
error = thr_keycreate(&key, destructor);
if (error) {
mutex_unlock(&key_lock);
return (error);
}
*keyp = key;
}
mutex_unlock(&key_lock);
}
return (0);
}
#endif
const char *
_libelf_msg(Msg mid)
{
return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
}
void
_elf_seterr(Msg lib_err, int sys_err)
{
intptr_t encerr = ((int)lib_err << ELFERRSHIFT) |
(sys_err & SYSERRMASK);
#ifndef __lock_lint
if (thr_main()) {
_elf_err = (int)encerr;
return;
}
#endif
(void) thr_keycreate_once(&errkey, 0);
(void) thr_setspecific(errkey, (void *)encerr);
}
int
_elf_geterr() {
#ifndef __lock_lint
if (thr_main())
return (_elf_err);
#endif
return ((uintptr_t)pthread_getspecific(errkey));
}
const char *
elf_errmsg(int err)
{
char *errno_str;
char *elferr_str;
char *buffer = 0;
int syserr;
int elferr;
static char intbuf[MAXELFERR];
if (err == 0) {
if ((err = _elf_geterr()) == 0)
return (0);
} else if (err == -1) {
if ((err = _elf_geterr()) == 0)
err = (int)EINF_NULLERROR << ELFERRSHIFT;
}
if (thr_main())
buffer = intbuf;
else {
if (thr_keycreate_once(&bufkey, free) != 0)
return (MSG_INTL(EBUG_THRDKEY));
buffer = pthread_getspecific(bufkey);
if (!buffer) {
if ((buffer = malloc(MAXELFERR)) == 0)
return (MSG_INTL(EMEM_ERRMSG));
if (thr_setspecific(bufkey, buffer) != 0) {
free(buffer);
return (MSG_INTL(EBUG_THRDSET));
}
}
}
elferr = (int)((uint_t)err >> ELFERRSHIFT);
syserr = err & SYSERRMASK;
elferr_str = (char *)MSG_INTL(elferr);
if (syserr && (errno_str = strerror(syserr)))
(void) snprintf(buffer, MAXELFERR,
MSG_ORIG(MSG_FMT_ERR), elferr_str, errno_str);
else {
(void) strncpy(buffer, elferr_str, MAXELFERR - 1);
buffer[MAXELFERR - 1] = '\0';
}
return (buffer);
}
int
elf_errno()
{
int rc = _elf_geterr();
_elf_seterr(0, 0);
return (rc);
}