Home | History | Annotate | Download | only in libasm
      1 /* Error handling in libasm.
      2    Copyright (C) 2002, 2004, 2005 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    Red Hat elfutils is an included package of the Open Invention Network.
     20    An included package of the Open Invention Network is a package for which
     21    Open Invention Network licensees cross-license their patents.  No patent
     22    license is granted, either expressly or impliedly, by designation as an
     23    included package.  Should you wish to participate in the Open Invention
     24    Network licensing program, please visit www.openinventionnetwork.com
     25    <http://www.openinventionnetwork.com>.  */
     26 
     27 #ifdef HAVE_CONFIG_H
     28 # include <config.h>
     29 #endif
     30 
     31 #include <libintl.h>
     32 #include <stdbool.h>
     33 #include <stdlib.h>
     34 
     35 #include "libasmP.h"
     36 
     37 
     38 /* This is the key for the thread specific memory.  */
     39 static tls_key_t key;
     40 
     41 /* The error number.  Used in non-threaded programs.  */
     42 static int global_error;
     43 static bool threaded;
     44 /* We need to initialize the thread-specific data.  */
     45 once_define (static, once);
     46 
     47 /* The initialization and destruction functions.  */
     48 static void init (void);
     49 static void free_key_mem (void *mem);
     50 
     51 
     52 int
     53 asm_errno (void)
     54 {
     55   int result;
     56 
     57   /* If we have not yet initialized the buffer do it now.  */
     58   once_execute (once, init);
     59 
     60   if (threaded)
     61     {
     62       /* We have a key.  Use it to get the thread-specific buffer.  */
     63       int *buffer = getspecific (key);
     64       if (buffer == NULL)
     65 	{
     66 	  /* No buffer allocated so far.  */
     67 	  buffer = (int *) malloc (sizeof (int));
     68 	  if (buffer == NULL)
     69 	    /* No more memory available.  We use the static buffer.  */
     70 	    buffer = &global_error;
     71 
     72 	  setspecific (key, buffer);
     73 
     74 	  *buffer = 0;
     75 	}
     76 
     77       result = *buffer;
     78       *buffer = ASM_E_NOERROR;
     79       return result;
     80     }
     81 
     82   result = global_error;
     83   global_error = ASM_E_NOERROR;
     84   return result;
     85 }
     86 
     87 
     88 void
     89 __libasm_seterrno (value)
     90      int value;
     91 {
     92   /* If we have not yet initialized the buffer do it now.  */
     93   once_execute (once, init);
     94 
     95   if (threaded)
     96     {
     97       /* We have a key.  Use it to get the thread-specific buffer.  */
     98       int *buffer = getspecific (key);
     99       if (buffer == NULL)
    100         {
    101           /* No buffer allocated so far.  */
    102           buffer = malloc (sizeof (int));
    103           if (buffer == NULL)
    104             /* No more memory available.  We use the static buffer.  */
    105             buffer = &global_error;
    106 
    107           setspecific (key, buffer);
    108         }
    109 
    110       *buffer = value;
    111     }
    112 
    113   global_error = value;
    114 }
    115 
    116 
    117 /* Return the appropriate message for the error.  */
    118 static const char *msgs[ASM_E_NUM] =
    119 {
    120   [ASM_E_NOERROR] = N_("no error"),
    121   [ASM_E_NOMEM] = N_("out of memory"),
    122   [ASM_E_CANNOT_CREATE] = N_("cannot create output file"),
    123   [ASM_E_INVALID] = N_("invalid parameter"),
    124   [ASM_E_CANNOT_CHMOD] = N_("cannot change mode of output file"),
    125   [ASM_E_CANNOT_RENAME] = N_("cannot rename output file"),
    126   [ASM_E_DUPLSYM] = N_("duplicate symbol"),
    127   [ASM_E_TYPE] = N_("invalid section type for operation"),
    128   [ASM_E_IOERROR] = N_("error during output of data"),
    129   [ASM_E_ENOSUP] = N_("no backend support available"),
    130 };
    131 
    132 const char *
    133 asm_errmsg (error)
    134      int error;
    135 {
    136   int last_error;
    137 
    138   /* If we have not yet initialized the buffer do it now.  */
    139   once_execute (once, init);
    140 
    141   if ((error == 0 || error == -1) && threaded)
    142     {
    143       /* We have a key.  Use it to get the thread-specific buffer.  */
    144       int *buffer = (int *) getspecific (key);
    145       if (buffer == NULL)
    146 	{
    147 	  /* No buffer allocated so far.  */
    148 	  buffer = (int *) malloc (sizeof (int));
    149 	  if (buffer == NULL)
    150 	    /* No more memory available.  We use the static buffer.  */
    151 	    buffer = &global_error;
    152 
    153 	  setspecific (key, buffer);
    154 	  *buffer = 0;
    155 	}
    156 
    157       last_error = *buffer;
    158     }
    159   else
    160     last_error = global_error;
    161 
    162   if (error < -1)
    163     return _("unknown error");
    164   if (error == 0 && last_error == 0)
    165     /* No error.  */
    166     return NULL;
    167 
    168   if (error != -1)
    169     last_error = error;
    170 
    171   if (last_error == ASM_E_LIBELF)
    172     return elf_errmsg (-1);
    173 
    174   return _(msgs[last_error]);
    175 }
    176 
    177 
    178 /* Free the thread specific data, this is done if a thread terminates.  */
    179 static void
    180 free_key_mem (void *mem)
    181 {
    182   free (mem);
    183   setspecific (key, NULL);
    184 }
    185 
    186 
    187 /* Initialize the key for the global variable.  */
    188 static void
    189 init (void)
    190 {
    191   // XXX Screw you, gcc4, the unused function attribute does not work.
    192   __asm ("" :: "r" (free_key_mem));
    193 
    194   if (key_create (&key, free_key_mem) == 0)
    195     /* Creating the key succeeded.  */
    196     threaded = true;
    197 }
    198