Back to index

glibc  2.9
dl-open.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <malloc.h>
00020 #include <stdarg.h>
00021 #include <stdlib.h>
00022 #include <sys/types.h>
00023 #include <errno.h>
00024 #include <dlfcn.h>
00025 #include <dlldr.h>
00026 
00027 #define NUM_SHARED_OBJECTS 32
00028 
00029 int _dl_numso = NUM_SHARED_OBJECTS;
00030 DL_SODATA *_dl_sotable = NULL;
00031 
00032 void *
00033 _dl_open (const char *file, int mode, const void *caller)
00034 {
00035   DL_SODATA *new_so;
00036   void *handle;
00037   int entry;
00038   int bsize = _dl_numso * sizeof (DL_INFO);
00039   DL_INFO *dl_info = malloc (bsize);
00040 
00041   if (dl_info == NULL)
00042     return NULL;
00043 
00044   /* 1st time thru initial shared object data table.  */
00045   if (_dl_sotable == NULL)
00046     {
00047       _dl_sotable = (DL_SODATA *) calloc (_dl_numso, sizeof (DL_SODATA));
00048       if (_dl_sotable == NULL)
00049        return NULL;
00050 
00051       __loadx (DL_POSTLOADQ, dl_info, bsize, NULL);
00052       while (!(dl_info[0].dlinfo_xflags & DL_INFO_OK)
00053             || dl_info[0].dlinfo_arraylen == 0)
00054        {
00055          bsize *= 2;
00056          dl_info = realloc (dl_info, bsize);
00057          if (dl_info == NULL)
00058            return NULL;
00059 
00060          __loadx (DL_POSTLOADQ, dl_info, bsize, NULL);
00061        }
00062     }
00063 
00064   /* Validate mode bits.  */
00065   if (!(mode & RTLD_NOW) && !(mode & RTLD_LAZY))
00066     {
00067       free (dl_info);
00068       errno = EINVAL;
00069       return NULL;
00070     }
00071 
00072   /* Load the module.  */
00073   handle = (void *) __loadx (DL_LOAD | DL_LOAD_RTL | DL_LOAD_LDX1,
00074                              dl_info, bsize, file, NULL);
00075   if (handle == NULL)
00076     {
00077       free (dl_info);
00078       errno = EINVAL;
00079       return NULL;
00080     }
00081 
00082   /* Was dl_info buffer to small to get info.  */
00083   while (!(dl_info[0].dlinfo_xflags & DL_INFO_OK)
00084         || dl_info[0].dlinfo_arraylen == 0)
00085     {
00086       bsize *= 2;
00087       dl_info = realloc (dl_info, bsize);
00088       if (dl_info == NULL)
00089         {
00090          (void) __unload ((void *) handle);
00091           errno = ENOMEM;
00092           return NULL;
00093         }
00094       __loadx (DL_POSTLOADQ | DL_LOAD_RTL, dl_info, bsize, handle);
00095     }
00096 
00097   /* Get an empty entry in the shared object table.  */
00098   for (entry = 0; entry < _dl_numso; ++entry)
00099     if (_dl_sotable[entry].type == 0)
00100       break;
00101 
00102   /* See if the table needs to be increased.  */
00103   if (entry == _dl_numso)
00104     {
00105       new_so = (DL_SODATA *) realloc (_dl_sotable,
00106                                   _dl_numso * 2 * sizeof (DL_SODATA));
00107       if (new_so == NULL)
00108        return NULL;
00109 
00110       memset (new_so + _dl_numso, '\0', _dl_numso * sizeof (DL_SODATA));
00111       _dl_numso  *= 2;
00112       _dl_sotable = new_so;
00113     }
00114 
00115   /* See if this is syscall (look for /unix in file).  */
00116   if (strcmp ("/unix", file) == 0)
00117     {
00118       _dl_sotable[entry].index = dl_info[1].dlinfo_index;
00119       _dl_sotable[entry].dataorg = dl_info[1].dlinfo_dataorg;
00120       _dl_sotable[entry].handle = handle;
00121       _dl_sotable[entry].type = DL_UNIX_SYSCALL;
00122     }
00123   else
00124     {
00125       _dl_sotable[entry].index = dl_info[1].dlinfo_index;
00126       _dl_sotable[entry].dataorg = dl_info[1].dlinfo_dataorg;
00127       _dl_sotable[entry].handle = handle;
00128       _dl_sotable[entry].type = DL_GETSYM;
00129     }
00130 
00131   free (dl_info);
00132   return (void *) entry;
00133 }