Blame elf/dl-sort-maps.c

Packit 6c4009
/* Sort array of link maps according to dependencies.
Packit 6c4009
   Copyright (C) 2017-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
#include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Sort array MAPS according to dependencies of the contained objects.
Packit 6c4009
   Array USED, if non-NULL, is permutated along MAPS.  If FOR_FINI this is
Packit 6c4009
   called for finishing an object.  */
Packit 6c4009
void
Packit 6c4009
_dl_sort_maps (struct link_map **maps, unsigned int nmaps, char *used,
Packit 6c4009
	       bool for_fini)
Packit 6c4009
{
Packit 6c4009
  /* A list of one element need not be sorted.  */
Packit 6c4009
  if (nmaps <= 1)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  unsigned int i = 0;
Packit 6c4009
  uint16_t seen[nmaps];
Packit 6c4009
  memset (seen, 0, nmaps * sizeof (seen[0]));
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      /* Keep track of which object we looked at this round.  */
Packit 6c4009
      ++seen[i];
Packit 6c4009
      struct link_map *thisp = maps[i];
Packit 6c4009
Packit 6c4009
      if (__glibc_unlikely (for_fini))
Packit 6c4009
	{
Packit 6c4009
	  /* Do not handle ld.so in secondary namespaces and objects which
Packit 6c4009
	     are not removed.  */
Packit 6c4009
	  if (thisp != thisp->l_real || thisp->l_idx == -1)
Packit 6c4009
	    goto skip;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Find the last object in the list for which the current one is
Packit 6c4009
	 a dependency and move the current object behind the object
Packit 6c4009
	 with the dependency.  */
Packit 6c4009
      unsigned int k = nmaps - 1;
Packit 6c4009
      while (k > i)
Packit 6c4009
	{
Packit 6c4009
	  struct link_map **runp = maps[k]->l_initfini;
Packit 6c4009
	  if (runp != NULL)
Packit 6c4009
	    /* Look through the dependencies of the object.  */
Packit 6c4009
	    while (*runp != NULL)
Packit 6c4009
	      if (__glibc_unlikely (*runp++ == thisp))
Packit 6c4009
		{
Packit 6c4009
		move:
Packit 6c4009
		  /* Move the current object to the back past the last
Packit 6c4009
		     object with it as the dependency.  */
Packit 6c4009
		  memmove (&maps[i], &maps[i + 1],
Packit 6c4009
			   (k - i) * sizeof (maps[0]));
Packit 6c4009
		  maps[k] = thisp;
Packit 6c4009
Packit 6c4009
		  if (used != NULL)
Packit 6c4009
		    {
Packit 6c4009
		      char here_used = used[i];
Packit 6c4009
		      memmove (&used[i], &used[i + 1],
Packit 6c4009
			       (k - i) * sizeof (used[0]));
Packit 6c4009
		      used[k] = here_used;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  if (seen[i + 1] > nmaps - i)
Packit 6c4009
		    {
Packit 6c4009
		      ++i;
Packit 6c4009
		      goto next_clear;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  uint16_t this_seen = seen[i];
Packit 6c4009
		  memmove (&seen[i], &seen[i + 1], (k - i) * sizeof (seen[0]));
Packit 6c4009
		  seen[k] = this_seen;
Packit 6c4009
Packit 6c4009
		  goto next;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	  if (__glibc_unlikely (for_fini && maps[k]->l_reldeps != NULL))
Packit 6c4009
	    {
Packit 6c4009
	      unsigned int m = maps[k]->l_reldeps->act;
Packit 6c4009
	      struct link_map **relmaps = &maps[k]->l_reldeps->list[0];
Packit 6c4009
Packit 6c4009
	      /* Look through the relocation dependencies of the object.  */
Packit 6c4009
	      while (m-- > 0)
Packit 6c4009
		if (__glibc_unlikely (relmaps[m] == thisp))
Packit 6c4009
		  {
Packit 6c4009
		    /* If a cycle exists with a link time dependency,
Packit 6c4009
		       preserve the latter.  */
Packit 6c4009
		    struct link_map **runp = thisp->l_initfini;
Packit 6c4009
		    if (runp != NULL)
Packit 6c4009
		      while (*runp != NULL)
Packit 6c4009
			if (__glibc_unlikely (*runp++ == maps[k]))
Packit 6c4009
			  goto ignore;
Packit 6c4009
		    goto move;
Packit 6c4009
		  }
Packit 6c4009
	    ignore:;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  --k;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    skip:
Packit 6c4009
      if (++i == nmaps)
Packit 6c4009
	break;
Packit 6c4009
    next_clear:
Packit 6c4009
      memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
Packit 6c4009
Packit 6c4009
    next:;
Packit 6c4009
    }
Packit 6c4009
}