Blame elf/setup-vdso.h

Packit 6c4009
/* Set up the data structures for the system-supplied DSO.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the 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; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
static inline void __attribute__ ((always_inline))
Packit 6c4009
setup_vdso (struct link_map *main_map __attribute__ ((unused)),
Packit 6c4009
	    struct link_map ***first_preload __attribute__ ((unused)))
Packit 6c4009
{
Packit 6c4009
#ifdef NEED_DL_SYSINFO_DSO
Packit 6c4009
  if (GLRO(dl_sysinfo_dso) == NULL)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Do an abridged version of the work _dl_map_object_from_fd would do
Packit 6c4009
     to map in the object.  It's already mapped and prelinked (and
Packit 6c4009
     better be, since it's read-only and so we couldn't relocate it).
Packit 6c4009
     We just want our data structures to describe it as if we had just
Packit 6c4009
     mapped and relocated it normally.  */
Packit 6c4009
  struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
Packit 6c4009
				       0, LM_ID_BASE);
Packit 6c4009
  if (__glibc_likely (l != NULL))
Packit 6c4009
    {
Packit 6c4009
      static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro;
Packit 6c4009
Packit 6c4009
      l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
Packit 6c4009
		   + GLRO(dl_sysinfo_dso)->e_phoff);
Packit 6c4009
      l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum;
Packit 6c4009
      for (uint_fast16_t i = 0; i < l->l_phnum; ++i)
Packit 6c4009
	{
Packit 6c4009
	  const ElfW(Phdr) *const ph = &l->l_phdr[i];
Packit 6c4009
	  if (ph->p_type == PT_DYNAMIC)
Packit 6c4009
	    {
Packit 6c4009
	      l->l_ld = (void *) ph->p_vaddr;
Packit 6c4009
	      l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
Packit 6c4009
	    }
Packit 6c4009
	  else if (ph->p_type == PT_LOAD)
Packit 6c4009
	    {
Packit 6c4009
	      if (! l->l_addr)
Packit 6c4009
		l->l_addr = ph->p_vaddr;
Packit 6c4009
	      if (ph->p_vaddr + ph->p_memsz >= l->l_map_end)
Packit 6c4009
		l->l_map_end = ph->p_vaddr + ph->p_memsz;
Packit 6c4009
	      if ((ph->p_flags & PF_X)
Packit 6c4009
		  && ph->p_vaddr + ph->p_memsz >= l->l_text_end)
Packit 6c4009
		l->l_text_end = ph->p_vaddr + ph->p_memsz;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    /* There must be no TLS segment.  */
Packit 6c4009
	    assert (ph->p_type != PT_TLS);
Packit 6c4009
	}
Packit 6c4009
      l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso);
Packit 6c4009
      l->l_addr = l->l_map_start - l->l_addr;
Packit 6c4009
      l->l_map_end += l->l_addr;
Packit 6c4009
      l->l_text_end += l->l_addr;
Packit 6c4009
      l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
Packit 6c4009
      elf_get_dynamic_info (l, dyn_temp);
Packit 6c4009
      _dl_setup_hash (l);
Packit 6c4009
      l->l_relocated = 1;
Packit 6c4009
Packit 6c4009
      /* The vDSO is always used.  */
Packit 6c4009
      l->l_used = 1;
Packit 6c4009
Packit 6c4009
      /* Initialize l_local_scope to contain just this map.  This allows
Packit 6c4009
	 the use of dl_lookup_symbol_x to resolve symbols within the vdso.
Packit 6c4009
	 So we create a single entry list pointing to l_real as its only
Packit 6c4009
	 element */
Packit 6c4009
      l->l_local_scope[0]->r_nlist = 1;
Packit 6c4009
      l->l_local_scope[0]->r_list = &l->l_real;
Packit 6c4009
Packit 6c4009
      /* Now that we have the info handy, use the DSO image's soname
Packit 6c4009
	 so this object can be looked up by name.  Note that we do not
Packit 6c4009
	 set l_name here.  That field gives the file name of the DSO,
Packit 6c4009
	 and this DSO is not associated with any file.  */
Packit 6c4009
      if (l->l_info[DT_SONAME] != NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Work around a kernel problem.  The kernel cannot handle
Packit 6c4009
	     addresses in the vsyscall DSO pages in writev() calls.  */
Packit 6c4009
	  const char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB])
Packit 6c4009
				 + l->l_info[DT_SONAME]->d_un.d_val);
Packit 6c4009
	  size_t len = strlen (dsoname) + 1;
Packit 6c4009
	  char *copy = malloc (len);
Packit 6c4009
	  if (copy == NULL)
Packit 6c4009
	    _dl_fatal_printf ("out of memory\n");
Packit 6c4009
	  l->l_libname->name = l->l_name = memcpy (copy, dsoname, len);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Add the vDSO to the object list.  */
Packit 6c4009
      _dl_add_to_namespace_list (l, LM_ID_BASE);
Packit 6c4009
Packit 6c4009
# if IS_IN (rtld)
Packit 6c4009
      /* Rearrange the list so this DSO appears after rtld_map.  */
Packit 6c4009
      assert (l->l_next == NULL);
Packit 6c4009
      assert (l->l_prev == main_map);
Packit 6c4009
      GL(dl_rtld_map).l_next = l;
Packit 6c4009
      l->l_prev = &GL(dl_rtld_map);
Packit 6c4009
      *first_preload = &l->l_next;
Packit 6c4009
# else
Packit 6c4009
      GL(dl_nns) = 1;
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
      /* We have a prelinked DSO preloaded by the system.  */
Packit 6c4009
      GLRO(dl_sysinfo_map) = l;
Packit 6c4009
# ifdef NEED_DL_SYSINFO
Packit 6c4009
      if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT)
Packit 6c4009
	GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr;
Packit 6c4009
# endif
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
}