Back to index

glibc  2.9
dl-iteratephdr.c
Go to the documentation of this file.
00001 /* Get loaded objects program headers.
00002    Copyright (C) 2001-2004,2006,2007,2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2001.
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 License as
00008    published by the Free Software Foundation; either version 2.1 of the
00009    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; see the file COPYING.LIB.  If not,
00018    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.  */
00020 
00021 #include <errno.h>
00022 #include <ldsodefs.h>
00023 #include <stddef.h>
00024 #include <bits/libc-lock.h>
00025 
00026 static void
00027 cancel_handler (void *arg __attribute__((unused)))
00028 {
00029   __rtld_lock_unlock_recursive (GL(dl_load_lock));
00030 }
00031 
00032 hidden_proto (__dl_iterate_phdr)
00033 int
00034 __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
00035                                 size_t size, void *data), void *data)
00036 {
00037   struct link_map *l;
00038   struct dl_phdr_info info;
00039   int ret = 0;
00040 
00041   /* Make sure we are alone.  */
00042   __rtld_lock_lock_recursive (GL(dl_load_lock));
00043   __libc_cleanup_push (cancel_handler, 0);
00044 
00045   /* We have to determine the namespace of the caller since this determines
00046      which namespace is reported.  */
00047   const void *caller = RETURN_ADDRESS (0);
00048   size_t nloaded = GL(dl_ns)[0]._ns_nloaded;
00049   Lmid_t ns = 0;
00050   for (Lmid_t cnt = DL_NNS - 1; cnt > 0; --cnt)
00051     for (struct link_map *l = GL(dl_ns)[cnt]._ns_loaded; l; l = l->l_next)
00052       {
00053        /* We have to count the total number of loaded objects.  */
00054        nloaded += GL(dl_ns)[cnt]._ns_nloaded;
00055 
00056        if (caller >= (const void *) l->l_map_start
00057            && caller < (const void *) l->l_map_end
00058            && (l->l_contiguous
00059               || _dl_addr_inside_object (l, (ElfW(Addr)) caller)))
00060          ns = cnt;
00061       }
00062 
00063   for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
00064     {
00065       info.dlpi_addr = l->l_addr;
00066       info.dlpi_name = l->l_name;
00067       info.dlpi_phdr = l->l_phdr;
00068       info.dlpi_phnum = l->l_phnum;
00069       info.dlpi_adds = GL(dl_load_adds);
00070       info.dlpi_subs = GL(dl_load_adds) - nloaded;
00071       info.dlpi_tls_modid = 0;
00072       info.dlpi_tls_data = NULL;
00073       info.dlpi_tls_modid = l->l_tls_modid;
00074       if (info.dlpi_tls_modid != 0)
00075        info.dlpi_tls_data = GLRO(dl_tls_get_addr_soft) (l);
00076       ret = callback (&info, sizeof (struct dl_phdr_info), data);
00077       if (ret)
00078        break;
00079     }
00080 
00081   /* Release the lock.  */
00082   __libc_cleanup_pop (0);
00083   __rtld_lock_unlock_recursive (GL(dl_load_lock));
00084 
00085   return ret;
00086 }
00087 hidden_def (__dl_iterate_phdr)
00088 
00089 #ifdef SHARED
00090 
00091 weak_alias (__dl_iterate_phdr, dl_iterate_phdr);
00092 
00093 #else
00094 
00095 /* dl-support.c defines these and initializes them early on.  */
00096 extern ElfW(Phdr) *_dl_phdr;
00097 extern size_t _dl_phnum;
00098 
00099 int
00100 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
00101                               size_t size, void *data), void *data)
00102 {
00103   if (_dl_phnum != 0)
00104     {
00105       /* This entry describes this statically-linked program itself.  */
00106       struct dl_phdr_info info;
00107       int ret;
00108       info.dlpi_addr = 0;
00109       info.dlpi_name = "";
00110       info.dlpi_phdr = _dl_phdr;
00111       info.dlpi_phnum = _dl_phnum;
00112       info.dlpi_adds = GL(dl_load_adds);
00113       info.dlpi_subs = GL(dl_load_adds) - GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
00114       ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
00115       if (ret)
00116        return ret;
00117     }
00118 
00119   return __dl_iterate_phdr (callback, data);
00120 }
00121 
00122 
00123 #endif