Blame elf/dl-error-skeleton.c

Packit 6c4009
/* Template for error handling for runtime dynamic linker.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* The following macro needs to be defined before including this
Packit 6c4009
   skeleton file:
Packit 6c4009
Packit 6c4009
   DL_ERROR_BOOTSTRAP
Packit 6c4009
Packit 6c4009
     If 1, do not use TLS and implement _dl_signal_cerror and
Packit 6c4009
     _dl_receive_error.  If 0, TLS is used, and the variants with
Packit 6c4009
     error callbacks are not provided.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <setjmp.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
Packit 6c4009
/* This structure communicates state between _dl_catch_error and
Packit 6c4009
   _dl_signal_error.  */
Packit 6c4009
struct catch
Packit 6c4009
  {
Packit 6c4009
    struct dl_exception *exception; /* The exception data is stored there.  */
Packit 6c4009
    volatile int *errcode;	/* Return value of _dl_signal_error.  */
Packit 6c4009
    jmp_buf env;		/* longjmp here on error.  */
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
/* Multiple threads at once can use the `_dl_catch_error' function.  The
Packit 6c4009
   calls can come from `_dl_map_object_deps', `_dlerror_run', or from
Packit 6c4009
   any of the libc functionality which loads dynamic objects (NSS, iconv).
Packit 6c4009
   Therefore we have to be prepared to save the state in thread-local
Packit 6c4009
   memory.  */
Packit 6c4009
#if !DL_ERROR_BOOTSTRAP
Packit 6c4009
static __thread struct catch *catch_hook attribute_tls_model_ie;
Packit 6c4009
#else
Packit 6c4009
/* The version of this code in ld.so cannot use thread-local variables
Packit 6c4009
   and is used during bootstrap only.  */
Packit 6c4009
static struct catch *catch_hook;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if DL_ERROR_BOOTSTRAP
Packit 6c4009
/* This points to a function which is called when an continuable error is
Packit 6c4009
   received.  Unlike the handling of `catch' this function may return.
Packit 6c4009
   The arguments will be the `errstring' and `objname'.
Packit 6c4009
Packit 6c4009
   Since this functionality is not used in normal programs (only in ld.so)
Packit 6c4009
   we do not care about multi-threaded programs here.  We keep this as a
Packit 6c4009
   global variable.  */
Packit 6c4009
static receiver_fct receiver;
Packit 6c4009
#endif /* DL_ERROR_BOOTSTRAP */
Packit 6c4009
Packit 6c4009
/* Lossage while resolving the program's own symbols is always fatal.  */
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((noreturn))
Packit 6c4009
fatal_error (int errcode, const char *objname, const char *occasion,
Packit 6c4009
	     const char *errstring)
Packit 6c4009
{
Packit 6c4009
  char buffer[1024];
Packit 6c4009
  _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
Packit 6c4009
		    RTLD_PROGNAME,
Packit 6c4009
		    occasion ?: N_("error while loading shared libraries"),
Packit 6c4009
		    objname, *objname ? ": " : "",
Packit 6c4009
		    errstring, errcode ? ": " : "",
Packit 6c4009
		    (errcode
Packit 6c4009
		     ? __strerror_r (errcode, buffer, sizeof buffer)
Packit 6c4009
		     : ""));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_dl_signal_exception (int errcode, struct dl_exception *exception,
Packit 6c4009
		      const char *occasion)
Packit 6c4009
{
Packit 6c4009
  struct catch *lcatch = catch_hook;
Packit 6c4009
  if (lcatch != NULL)
Packit 6c4009
    {
Packit 6c4009
      *lcatch->exception = *exception;
Packit 6c4009
      *lcatch->errcode = errcode;
Packit 6c4009
Packit 6c4009
      /* We do not restore the signal mask because none was saved.  */
Packit 6c4009
      __longjmp (lcatch->env[0].__jmpbuf, 1);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    fatal_error (errcode, exception->objname, occasion, exception->errstring);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (_dl_signal_exception)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_dl_signal_error (int errcode, const char *objname, const char *occation,
Packit 6c4009
		  const char *errstring)
Packit 6c4009
{
Packit 6c4009
  struct catch *lcatch = catch_hook;
Packit 6c4009
Packit 6c4009
  if (! errstring)
Packit 6c4009
    errstring = N_("DYNAMIC LINKER BUG!!!");
Packit 6c4009
Packit 6c4009
  if (lcatch != NULL)
Packit 6c4009
    {
Packit 6c4009
      _dl_exception_create (lcatch->exception, objname, errstring);
Packit 6c4009
      *lcatch->errcode = errcode;
Packit 6c4009
Packit 6c4009
      /* We do not restore the signal mask because none was saved.  */
Packit 6c4009
      __longjmp (lcatch->env[0].__jmpbuf, 1);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    fatal_error (errcode, objname, occation, errstring);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (_dl_signal_error)
Packit 6c4009
Packit 6c4009
Packit 6c4009
#if DL_ERROR_BOOTSTRAP
Packit 6c4009
void
Packit 6c4009
_dl_signal_cexception (int errcode, struct dl_exception *exception,
Packit 6c4009
		       const char *occasion)
Packit 6c4009
{
Packit 6c4009
  if (__builtin_expect (GLRO(dl_debug_mask)
Packit 6c4009
			& ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0))
Packit 6c4009
    _dl_debug_printf ("%s: error: %s: %s (%s)\n",
Packit 6c4009
		      exception->objname, occasion,
Packit 6c4009
		      exception->errstring, receiver ? "continued" : "fatal");
Packit 6c4009
Packit 6c4009
  if (receiver)
Packit 6c4009
    {
Packit 6c4009
      /* We are inside _dl_receive_error.  Call the user supplied
Packit 6c4009
	 handler and resume the work.  The receiver will still be
Packit 6c4009
	 installed.  */
Packit 6c4009
      (*receiver) (errcode, exception->objname, exception->errstring);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    _dl_signal_exception (errcode, exception, occasion);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_dl_signal_cerror (int errcode, const char *objname, const char *occation,
Packit 6c4009
		   const char *errstring)
Packit 6c4009
{
Packit 6c4009
  if (__builtin_expect (GLRO(dl_debug_mask)
Packit 6c4009
			& ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0))
Packit 6c4009
    _dl_debug_printf ("%s: error: %s: %s (%s)\n", objname, occation,
Packit 6c4009
		      errstring, receiver ? "continued" : "fatal");
Packit 6c4009
Packit 6c4009
  if (receiver)
Packit 6c4009
    {
Packit 6c4009
      /* We are inside _dl_receive_error.  Call the user supplied
Packit 6c4009
	 handler and resume the work.  The receiver will still be
Packit 6c4009
	 installed.  */
Packit 6c4009
      (*receiver) (errcode, objname, errstring);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    _dl_signal_error (errcode, objname, occation, errstring);
Packit 6c4009
}
Packit 6c4009
#endif /* DL_ERROR_BOOTSTRAP */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_dl_catch_exception (struct dl_exception *exception,
Packit 6c4009
		     void (*operate) (void *), void *args)
Packit 6c4009
{
Packit 6c4009
  /* We need not handle `receiver' since setting a `catch' is handled
Packit 6c4009
     before it.  */
Packit 6c4009
Packit 6c4009
  /* Only this needs to be marked volatile, because it is the only local
Packit 6c4009
     variable that gets changed between the setjmp invocation and the
Packit 6c4009
     longjmp call.  All others are just set here (before setjmp) and read
Packit 6c4009
     in _dl_signal_error (before longjmp).  */
Packit 6c4009
  volatile int errcode;
Packit 6c4009
Packit 6c4009
  struct catch c;
Packit 6c4009
  /* Don't use an initializer since we don't need to clear C.env.  */
Packit 6c4009
  c.exception = exception;
Packit 6c4009
  c.errcode = &errcode;
Packit 6c4009
Packit 6c4009
  struct catch *const old = catch_hook;
Packit 6c4009
  catch_hook = &c;
Packit 6c4009
Packit 6c4009
  /* Do not save the signal mask.  */
Packit 6c4009
  if (__builtin_expect (__sigsetjmp (c.env, 0), 0) == 0)
Packit 6c4009
    {
Packit 6c4009
      (*operate) (args);
Packit 6c4009
      catch_hook = old;
Packit 6c4009
      *exception = (struct dl_exception) { NULL };
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We get here only if we longjmp'd out of OPERATE.
Packit 6c4009
     _dl_signal_exception has already stored values into
Packit 6c4009
     *EXCEPTION.  */
Packit 6c4009
  catch_hook = old;
Packit 6c4009
  return errcode;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (_dl_catch_exception)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_dl_catch_error (const char **objname, const char **errstring,
Packit 6c4009
		 bool *mallocedp, void (*operate) (void *), void *args)
Packit 6c4009
{
Packit 6c4009
  struct dl_exception exception;
Packit 6c4009
  int errorcode = _dl_catch_exception (&exception, operate, args);
Packit 6c4009
  *objname = exception.objname;
Packit 6c4009
  *errstring = exception.errstring;
Packit 6c4009
  *mallocedp = exception.message_buffer == exception.errstring;
Packit 6c4009
  return errorcode;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (_dl_catch_error)
Packit 6c4009
Packit 6c4009
#if DL_ERROR_BOOTSTRAP
Packit 6c4009
void
Packit 6c4009
_dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
Packit 6c4009
{
Packit 6c4009
  struct catch *old_catch = catch_hook;
Packit 6c4009
  receiver_fct old_receiver = receiver;
Packit 6c4009
Packit 6c4009
  /* Set the new values.  */
Packit 6c4009
  catch_hook = NULL;
Packit 6c4009
  receiver = fct;
Packit 6c4009
Packit 6c4009
  (*operate) (args);
Packit 6c4009
Packit 6c4009
  catch_hook = old_catch;
Packit 6c4009
  receiver = old_receiver;
Packit 6c4009
}
Packit 6c4009
#endif /* DL_ERROR_BOOTSTRAP */