Back to index

glibc  2.9
dlsym.c
Go to the documentation of this file.
00001 /* Look up a symbol in a shared object loaded by `dlopen'.
00002    Copyright (C) 1995-2000, 2004 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 <dlfcn.h>
00021 #include <stddef.h>
00022 
00023 #include <ldsodefs.h>
00024 
00025 #if !defined SHARED && defined IS_IN_libdl
00026 
00027 void *
00028 dlsym (void *handle, const char *name)
00029 {
00030   return __dlsym (handle, name, RETURN_ADDRESS (0));
00031 }
00032 
00033 #else
00034 
00035 struct dlsym_args
00036 {
00037   /* The arguments to dlsym_doit.  */
00038   void *handle;
00039   const char *name;
00040   void *who;
00041 
00042   /* The return value of dlsym_doit.  */
00043   void *sym;
00044 };
00045 
00046 static void
00047 dlsym_doit (void *a)
00048 {
00049   struct dlsym_args *args = (struct dlsym_args *) a;
00050 
00051   args->sym = _dl_sym (args->handle, args->name, args->who);
00052 }
00053 
00054 
00055 void *
00056 __dlsym (void *handle, const char *name DL_CALLER_DECL)
00057 {
00058 # ifdef SHARED
00059   if (__builtin_expect (_dlfcn_hook != NULL, 0))
00060     return _dlfcn_hook->dlsym (handle, name, DL_CALLER);
00061 # endif
00062 
00063   struct dlsym_args args;
00064   args.who = DL_CALLER;
00065   args.handle = handle;
00066   args.name = name;
00067 
00068   /* Protect against concurrent loads and unloads.  */
00069   __rtld_lock_lock_recursive (GL(dl_load_lock));
00070 
00071   void *result = (_dlerror_run (dlsym_doit, &args) ? NULL : args.sym);
00072 
00073   __rtld_lock_unlock_recursive (GL(dl_load_lock));
00074 
00075   return result;
00076 }
00077 # ifdef SHARED
00078 strong_alias (__dlsym, dlsym)
00079 # endif
00080 #endif