Blame elf/dl-iteratephdr.c

Packit 6c4009
/* Get loaded objects program headers.
Packit 6c4009
   Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2001.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
cancel_handler (void *arg __attribute__((unused)))
Packit 6c4009
{
Packit 6c4009
  __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
Packit 6c4009
				    size_t size, void *data), void *data)
Packit 6c4009
{
Packit 6c4009
  struct link_map *l;
Packit 6c4009
  struct dl_phdr_info info;
Packit 6c4009
  int ret = 0;
Packit 6c4009
Packit 6c4009
  /* Make sure nobody modifies the list of loaded objects.  */
Packit 6c4009
  __rtld_lock_lock_recursive (GL(dl_load_write_lock));
Packit 6c4009
  __libc_cleanup_push (cancel_handler, NULL);
Packit 6c4009
Packit 6c4009
  /* We have to determine the namespace of the caller since this determines
Packit 6c4009
     which namespace is reported.  */
Packit 6c4009
  size_t nloaded = GL(dl_ns)[0]._ns_nloaded;
Packit 6c4009
  Lmid_t ns = 0;
Packit 6c4009
#ifdef SHARED
Packit 6c4009
  const void *caller = RETURN_ADDRESS (0);
Packit 6c4009
  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt)
Packit 6c4009
    for (struct link_map *l = GL(dl_ns)[cnt]._ns_loaded; l; l = l->l_next)
Packit 6c4009
      {
Packit 6c4009
	/* We have to count the total number of loaded objects.  */
Packit 6c4009
	nloaded += GL(dl_ns)[cnt]._ns_nloaded;
Packit 6c4009
Packit 6c4009
	if (caller >= (const void *) l->l_map_start
Packit 6c4009
	    && caller < (const void *) l->l_map_end
Packit 6c4009
	    && (l->l_contiguous
Packit 6c4009
		|| _dl_addr_inside_object (l, (ElfW(Addr)) caller)))
Packit 6c4009
	  ns = cnt;
Packit 6c4009
      }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
Packit 6c4009
    {
Packit 6c4009
      info.dlpi_addr = l->l_real->l_addr;
Packit 6c4009
      info.dlpi_name = l->l_real->l_name;
Packit 6c4009
      info.dlpi_phdr = l->l_real->l_phdr;
Packit 6c4009
      info.dlpi_phnum = l->l_real->l_phnum;
Packit 6c4009
      info.dlpi_adds = GL(dl_load_adds);
Packit 6c4009
      info.dlpi_subs = GL(dl_load_adds) - nloaded;
Packit 6c4009
      info.dlpi_tls_data = NULL;
Packit 6c4009
      info.dlpi_tls_modid = l->l_real->l_tls_modid;
Packit 6c4009
      if (info.dlpi_tls_modid != 0)
Packit 6c4009
	info.dlpi_tls_data = GLRO(dl_tls_get_addr_soft) (l->l_real);
Packit 6c4009
      ret = callback (&info, sizeof (struct dl_phdr_info), data);
Packit 6c4009
      if (ret)
Packit 6c4009
	break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Release the lock.  */
Packit 6c4009
  __libc_cleanup_pop (0);
Packit 6c4009
  __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
hidden_def (__dl_iterate_phdr)
Packit 6c4009
Packit 6c4009
weak_alias (__dl_iterate_phdr, dl_iterate_phdr);