Back to index

glibc  2.9
dl-error.c
Go to the documentation of this file.
00001 /* Error handling for runtime dynamic linker.
00002    Copyright (C) 1995-2002,2004,2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <libintl.h>
00021 #include <setjmp.h>
00022 #include <stdbool.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <ldsodefs.h>
00027 
00028 /* This structure communicates state between _dl_catch_error and
00029    _dl_signal_error.  */
00030 struct catch
00031   {
00032     const char *objname;    /* Object/File name.  */
00033     const char *errstring;  /* Error detail filled in here.  */
00034     bool malloced;          /* Nonzero if the string is malloced
00035                                by the libc malloc.  */
00036     jmp_buf env;            /* longjmp here on error.  */
00037   };
00038 
00039 /* Multiple threads at once can use the `_dl_catch_error' function.  The
00040    calls can come from `_dl_map_object_deps', `_dlerror_run', or from
00041    any of the libc functionality which loads dynamic objects (NSS, iconv).
00042    Therefore we have to be prepared to save the state in thread-local
00043    memory.  The _dl_error_catch_tsd function pointer is reset by the thread
00044    library so that it returns the address of a thread-local variable.  */
00045 
00046 
00047 /* This message we return as a last resort.  We define the string in a
00048    variable since we have to avoid freeing it and so have to enable
00049    a pointer comparison.  See below and in dlfcn/dlerror.c.  */
00050 static const char _dl_out_of_memory[] = "out of memory";
00051 
00052 
00053 /* This points to a function which is called when an continuable error is
00054    received.  Unlike the handling of `catch' this function may return.
00055    The arguments will be the `errstring' and `objname'.
00056 
00057    Since this functionality is not used in normal programs (only in ld.so)
00058    we do not care about multi-threaded programs here.  We keep this as a
00059    global variable.  */
00060 static receiver_fct receiver;
00061 
00062 #ifdef _LIBC_REENTRANT
00063 # define CATCH_HOOK  (*(struct catch **) (*GL(dl_error_catch_tsd)) ())
00064 #else
00065 static struct catch *catch_hook;
00066 # define CATCH_HOOK  catch_hook
00067 #endif
00068 
00069 void
00070 internal_function
00071 _dl_signal_error (int errcode, const char *objname, const char *occation,
00072                 const char *errstring)
00073 {
00074   struct catch *lcatch;
00075 
00076   if (! errstring)
00077     errstring = N_("DYNAMIC LINKER BUG!!!");
00078 
00079   lcatch = CATCH_HOOK;
00080   if (objname == NULL)
00081     objname = "";
00082   if (lcatch != NULL)
00083     {
00084       /* We are inside _dl_catch_error.  Return to it.  We have to
00085         duplicate the error string since it might be allocated on the
00086         stack.  The object name is always a string constant.  */
00087       size_t len_objname = strlen (objname) + 1;
00088       size_t len_errstring = strlen (errstring) + 1;
00089 
00090       lcatch->errstring = (char *) malloc (len_objname + len_errstring);
00091       if (lcatch->errstring != NULL)
00092        {
00093          /* Make a copy of the object file name and the error string.  */
00094          lcatch->objname = memcpy (__mempcpy ((char *) lcatch->errstring,
00095                                           errstring, len_errstring),
00096                                 objname, len_objname);
00097 
00098          /* If the main executable is relocated it means the libc's malloc
00099             is used.  */
00100 #ifdef SHARED
00101          lcatch->malloced = (GL(dl_ns)[LM_ID_BASE]._ns_loaded != NULL
00102                            && (GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_relocated
00103                               != 0));
00104 #else
00105          lcatch->malloced = true;
00106 #endif
00107        }
00108       else
00109        {
00110          /* This is better than nothing.  */
00111          lcatch->objname = "";
00112          lcatch->errstring = _dl_out_of_memory;
00113          lcatch->malloced = false;
00114        }
00115       /* We do not restore the signal mask because none was saved.  */
00116       __longjmp (lcatch->env[0].__jmpbuf, errcode ?: -1);
00117     }
00118   else
00119     {
00120       /* Lossage while resolving the program's own symbols is always fatal.  */
00121       char buffer[1024];
00122       _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
00123                      rtld_progname ?: "<program name unknown>",
00124                      occation ?: N_("error while loading shared libraries"),
00125                      objname, *objname ? ": " : "",
00126                      errstring, errcode ? ": " : "",
00127                      (errcode
00128                       ? __strerror_r (errcode, buffer, sizeof buffer)
00129                       : ""));
00130     }
00131 }
00132 
00133 
00134 void
00135 internal_function
00136 _dl_signal_cerror (int errcode, const char *objname, const char *occation,
00137                  const char *errstring)
00138 {
00139   if (__builtin_expect (GLRO(dl_debug_mask)
00140                      & ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0))
00141     _dl_debug_printf ("%s: error: %s: %s (%s)\n", objname, occation,
00142                     errstring, receiver ? "continued" : "fatal");
00143 
00144   if (receiver)
00145     {
00146       /* We are inside _dl_receive_error.  Call the user supplied
00147         handler and resume the work.  The receiver will still be
00148         installed.  */
00149       (*receiver) (errcode, objname, errstring);
00150     }
00151   else
00152     _dl_signal_error (errcode, objname, occation, errstring);
00153 }
00154 
00155 
00156 int
00157 internal_function
00158 _dl_catch_error (const char **objname, const char **errstring,
00159                bool *mallocedp, void (*operate) (void *), void *args)
00160 {
00161   int errcode;
00162   struct catch *volatile old;
00163   struct catch c;
00164   /* We need not handle `receiver' since setting a `catch' is handled
00165      before it.  */
00166 
00167   /* Some systems (e.g., SPARC) handle constructors to local variables
00168      inefficient.  So we initialize `c' by hand.  */
00169   c.errstring = NULL;
00170 
00171   struct catch **const catchp = &CATCH_HOOK;
00172   old = *catchp;
00173   /* Do not save the signal mask.  */
00174   errcode = __sigsetjmp (c.env, 0);
00175   if (__builtin_expect (errcode, 0) == 0)
00176     {
00177       *catchp = &c;
00178       (*operate) (args);
00179       *catchp = old;
00180       *objname = NULL;
00181       *errstring = NULL;
00182       *mallocedp = false;
00183       return 0;
00184     }
00185 
00186   /* We get here only if we longjmp'd out of OPERATE.  */
00187   *catchp = old;
00188   *objname = c.objname;
00189   *errstring = c.errstring;
00190   *mallocedp = c.malloced;
00191   return errcode == -1 ? 0 : errcode;
00192 }
00193 
00194 
00195 void
00196 internal_function
00197 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
00198 {
00199   struct catch **const catchp = &CATCH_HOOK;
00200   struct catch *old_catch;
00201   receiver_fct old_receiver;
00202 
00203   old_catch = *catchp;
00204   old_receiver = receiver;
00205 
00206   /* Set the new values.  */
00207   *catchp = NULL;
00208   receiver = fct;
00209 
00210   (*operate) (args);
00211 
00212   *catchp = old_catch;
00213   receiver = old_receiver;
00214 }