Back to index

glibc  2.9
tst-dladdr.c
Go to the documentation of this file.
00001 /* Test for dladdr.
00002    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Volkmar Sieh <vs@caldera.de> and Andreas Jaeger <aj@suse.de>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <dlfcn.h>
00022 #include <errno.h>
00023 #include <error.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 #include <ldsodefs.h>
00029 
00030 
00031 #define TEST_FUNCTION do_test ()
00032 extern int do_test (void);
00033 
00034 int
00035 do_test (void)
00036 {
00037   void *handle;
00038   int (*sym) (void); /* We load ref1 from glreflib1.c.  */
00039   Dl_info info;
00040   int ret;
00041 
00042 
00043   handle = dlopen ("glreflib1.so", RTLD_NOW);
00044   if (handle == NULL)
00045     error (EXIT_FAILURE, 0, "cannot load: glreflib1.so");
00046 
00047   sym = dlsym (handle, "ref1");
00048   if (sym == NULL)
00049     error (EXIT_FAILURE, 0, "dlsym failed");
00050 
00051   memset (&info, 0, sizeof (info));
00052   ret = dladdr (sym, &info);
00053 
00054   if (ret == 0)
00055     error (EXIT_FAILURE, 0, "dladdr failed");
00056 
00057   printf ("address of ref1 = %lx\n",
00058          (unsigned long int)  DL_LOOKUP_ADDRESS (sym));
00059   printf ("ret = %d\n", ret);
00060   printf ("info.dli_fname = %p (\"%s\")\n", info.dli_fname, info.dli_fname);
00061   printf ("info.dli_fbase = %p\n", info.dli_fbase);
00062   printf ("info.dli_sname = %p (\"%s\")\n", info.dli_sname, info.dli_sname);
00063   printf ("info.dli_saddr = %p\n", info.dli_saddr);
00064 
00065   if (info.dli_fname == NULL)
00066     error (EXIT_FAILURE, 0, "dli_fname is NULL");
00067   if (info.dli_fbase == NULL)
00068     error (EXIT_FAILURE, 0, "dli_fbase is NULL");
00069   if (info.dli_sname == NULL)
00070     error (EXIT_FAILURE, 0, "dli_sname is NULL");
00071   if (info.dli_saddr == NULL)
00072     error (EXIT_FAILURE, 0, "dli_saddr is NULL");
00073 
00074   dlclose (handle);
00075 
00076   return 0;
00077 }
00078 
00079 
00080 #include "../test-skeleton.c"