This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch] ld speedup 1/3 (suffix merge)


Committed.

2003-10-30  Lars Knoll  <lars@trolltech.com>
	    Michael Matz  <matz@suse.de>
	    Jakub Jelinek  <jakub@redhat.com>
	    Alan Modra  <amodra@bigpond.net.au>

	* merge.c (struct sec_merge_sec_info): Update comment.
	(struct sec_merge_hash_entry): Remove entsize.
	(sec_merge_hash_lookup): Only adjust alignment when creating.
	(sec_merge_emit): Remove register keyword.
	(cmplengthentry, last4_eq, last_eq): Delete.
	(strrevcmp, strrevcmp_align, is_suffix): New.
	(merge_strings): Use them to implement fast suffix merging.
	* elf-strtab.c (struct elf_strtab_hash_entry): Update comments.
	Make "len" signed.
	(_bfd_elf_strtab_add): Lose on >2G strings.
	(_bfd_elf_strtab_emit): Don't emit strings with len < 0.
	(cmplengthentry, last4_eq): Delete.
	(strrevcmp, is_suffix): New.
	(_bfd_elf_strtab_finalize): Rework to implement fast suffix merging.

Index: bfd/merge.c
===================================================================
RCS file: /cvs/src/src/bfd/merge.c,v
retrieving revision 1.17
diff -c -p -r1.17 merge.c
*** bfd/merge.c	4 Oct 2003 10:19:26 -0000	1.17
--- bfd/merge.c	29 Oct 2003 22:22:15 -0000
*************** struct sec_merge_sec_info;
*** 34,40 ****
  struct sec_merge_hash_entry
  {
    struct bfd_hash_entry root;
!   /* Length of this entry.  */
    unsigned int len;
    /* Start of this string needs to be aligned to
       alignment octets (not 1 << align).  */
--- 34,40 ----
  struct sec_merge_hash_entry
  {
    struct bfd_hash_entry root;
!   /* Length of this entry.  This includes the zero terminator.  */
    unsigned int len;
    /* Start of this string needs to be aligned to
       alignment octets (not 1 << align).  */
*************** struct sec_merge_hash_entry
*** 43,50 ****
    {
      /* Index within the merged section.  */
      bfd_size_type index;
-     /* Entity size (if present in suffix hash tables).  */
-     unsigned int entsize;
      /* Entry this is a suffix of (if alignment is 0).  */
      struct sec_merge_hash_entry *suffix;
    } u;
--- 43,48 ----
*************** sec_merge_hash_lookup (struct sec_merge_
*** 205,213 ****
  	     alignment, we need to insert another copy.  */
  	  if (hashp->alignment < alignment)
  	    {
! 	      /*  Mark the less aligned copy as deleted.  */
! 	      hashp->len = 0;
! 	      hashp->alignment = 0;
  	      break;
  	    }
  	  return hashp;
--- 203,214 ----
  	     alignment, we need to insert another copy.  */
  	  if (hashp->alignment < alignment)
  	    {
! 	      if (create)
! 		{
! 		  /*  Mark the less aligned copy as deleted.  */
! 		  hashp->len = 0;
! 		  hashp->alignment = 0;
! 		}
  	      break;
  	    }
  	  return hashp;
*************** sec_merge_add (struct sec_merge_hash *ta
*** 287,293 ****
  }
  
  static bfd_boolean
! sec_merge_emit (register bfd *abfd, struct sec_merge_hash_entry *entry)
  {
    struct sec_merge_sec_info *secinfo = entry->secinfo;
    asection *sec = secinfo->sec;
--- 288,294 ----
  }
  
  static bfd_boolean
! sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
  {
    struct sec_merge_sec_info *secinfo = entry->secinfo;
    asection *sec = secinfo->sec;
*************** _bfd_merge_section (bfd *abfd, void **ps
*** 420,498 ****
    return FALSE;
  }
  
- /* Compare two sec_merge_hash_entry structures.  This is called via qsort.  */
- 
- static int
- cmplengthentry (const void *a, const void *b)
- {
-   struct sec_merge_hash_entry * A = *(struct sec_merge_hash_entry **) a;
-   struct sec_merge_hash_entry * B = *(struct sec_merge_hash_entry **) b;
- 
-   if (A->len < B->len)
-     return 1;
-   else if (A->len > B->len)
-     return -1;
- 
-   return memcmp (A->root.string, B->root.string, A->len);
- }
- 
- static int
- last4_eq (const void *a, const void *b)
- {
-   struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
-   struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
- 
-   if (memcmp (A->root.string + A->len - 5 * A->u.entsize,
- 	      B->root.string + B->len - 5 * A->u.entsize,
- 	      4 * A->u.entsize) != 0)
-     /* This was a hashtable collision.  */
-     return 0;
- 
-   if (A->len <= B->len)
-     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
-        not to be equal by the hash table.  */
-     return 0;
- 
-   if (A->alignment < B->alignment
-       || ((A->len - B->len) & (B->alignment - 1)))
-     /* The suffix is not sufficiently aligned.  */
-     return 0;
- 
-   return memcmp (A->root.string + (A->len - B->len),
- 		 B->root.string, B->len - 5 * A->u.entsize) == 0;
- }
- 
- static int
- last_eq (const void *a, const void *b)
- {
-   struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
-   struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
- 
-   if (B->len >= 5 * A->u.entsize)
-     /* Longer strings are just pushed into the hash table,
-        they'll be used when looking up for very short strings.  */
-     return 0;
- 
-   if (memcmp (A->root.string + A->len - 2 * A->u.entsize,
- 	      B->root.string + B->len - 2 * A->u.entsize,
- 	      A->u.entsize) != 0)
-     /* This was a hashtable collision.  */
-     return 0;
- 
-   if (A->len <= B->len)
-     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
-        not to be equal by the hash table.  */
-     return 0;
- 
-   if (A->alignment < B->alignment
-       || ((A->len - B->len) & (B->alignment - 1)))
-     /* The suffix is not sufficiently aligned.  */
-     return 0;
- 
-   return memcmp (A->root.string + (A->len - B->len),
- 		 B->root.string, B->len - 2 * A->u.entsize) == 0;
- }
- 
  /* Record one section into the hash table.  */
  static bfd_boolean
  record_section (struct sec_merge_info *sinfo,
--- 421,426 ----
*************** error_return:
*** 576,593 ****
    return FALSE;
  }
  
  /* This is a helper function for _bfd_merge_sections.  It attempts to
     merge strings matching suffixes of longer strings.  */
  static void
  merge_strings (struct sec_merge_info *sinfo)
  {
!   struct sec_merge_hash_entry **array, **a, **end, *e;
    struct sec_merge_sec_info *secinfo;
-   htab_t lasttab = NULL, last4tab = NULL;
    bfd_size_type size, amt;
  
!   /* Now sort the strings by length, longest first.  */
!   array = NULL;
    amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
    array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
    if (array == NULL)
--- 504,584 ----
    return FALSE;
  }
  
+ static int
+ strrevcmp (const void *a, const void *b)
+ {
+   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
+   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
+   unsigned int lenA = A->len;
+   unsigned int lenB = B->len;
+   const unsigned char *s = A->root.string + lenA - 1;
+   const unsigned char *t = B->root.string + lenB - 1;
+   int l = lenA < lenB ? lenA : lenB;
+ 
+   while (l)
+     {
+       if (*s != *t)
+ 	return (int) *s - (int) *t;
+       s--;
+       t--;
+       l--;
+     }
+   return lenA - lenB;
+ }
+ 
+ /* Like strrevcmp, but for the case where all strings have the same
+    alignment > entsize.  */
+ 
+ static int
+ strrevcmp_align (const void *a, const void *b)
+ {
+   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
+   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
+   unsigned int lenA = A->len;
+   unsigned int lenB = B->len;
+   const unsigned char *s = A->root.string + lenA - 1;
+   const unsigned char *t = B->root.string + lenB - 1;
+   int l = lenA < lenB ? lenA : lenB;
+   int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
+ 
+   if (tail_align != 0)
+     return tail_align;
+ 
+   while (l)
+     {
+       if (*s != *t)
+ 	return (int) *s - (int) *t;
+       s--;
+       t--;
+       l--;
+     }
+   return lenA - lenB;
+ }
+ 
+ static inline int
+ is_suffix (const struct sec_merge_hash_entry *A,
+ 	   const struct sec_merge_hash_entry *B)
+ {
+   if (A->len <= B->len)
+     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
+        not to be equal by the hash table.  */
+     return 0;
+ 
+   return memcmp (A->root.string + (A->len - B->len),
+ 		 B->root.string, B->len) == 0;
+ }
+ 
  /* This is a helper function for _bfd_merge_sections.  It attempts to
     merge strings matching suffixes of longer strings.  */
  static void
  merge_strings (struct sec_merge_info *sinfo)
  {
!   struct sec_merge_hash_entry **array, **a, *e;
    struct sec_merge_sec_info *secinfo;
    bfd_size_type size, amt;
+   unsigned int alignment = 0;
  
!   /* Now sort the strings */
    amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
    array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
    if (array == NULL)
*************** merge_strings (struct sec_merge_info *si
*** 595,684 ****
  
    for (e = sinfo->htab->first, a = array; e; e = e->next)
      if (e->alignment)
!       *a++ = e;
  
    sinfo->htab->size = a - array;
! 
!   qsort (array, (size_t) sinfo->htab->size,
! 	 sizeof (struct sec_merge_hash_entry *), cmplengthentry);
! 
!   last4tab = htab_create_alloc ((size_t) sinfo->htab->size * 4,
! 				NULL, last4_eq, NULL, calloc, free);
!   lasttab = htab_create_alloc ((size_t) sinfo->htab->size * 4,
! 			       NULL, last_eq, NULL, calloc, free);
!   if (lasttab == NULL || last4tab == NULL)
!     goto alloc_failure;
! 
!   /* Now insert the strings into hash tables (strings with last 4 characters
!      and strings with last character equal), look for longer strings which
!      we're suffix of.  */
!   for (a = array, end = array + sinfo->htab->size; a < end; a++)
!     {
!       register hashval_t hash;
!       unsigned int c;
!       unsigned int i;
!       const unsigned char *s;
!       void **p;
! 
!       e = *a;
!       e->u.entsize = sinfo->htab->entsize;
!       if (e->len <= e->u.entsize)
! 	break;
!       if (e->len > 4 * e->u.entsize)
  	{
! 	  s = (const unsigned char *) (e->root.string + e->len - e->u.entsize);
! 	  hash = 0;
! 	  for (i = 0; i < 4 * e->u.entsize; i++)
! 	    {
! 	      c = *--s;
! 	      hash += c + (c << 17);
! 	      hash ^= hash >> 2;
! 	    }
! 	  p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
! 	  if (p == NULL)
! 	    goto alloc_failure;
! 	  if (*p)
! 	    {
! 	      struct sec_merge_hash_entry *ent;
  
! 	      ent = (struct sec_merge_hash_entry *) *p;
! 	      e->u.suffix = ent;
! 	      e->alignment = 0;
! 	      continue;
  	    }
  	  else
! 	    *p = e;
! 	}
!       s = (const unsigned char *) (e->root.string + e->len - e->u.entsize);
!       hash = 0;
!       for (i = 0; i < e->u.entsize; i++)
! 	{
! 	  c = *--s;
! 	  hash += c + (c << 17);
! 	  hash ^= hash >> 2;
  	}
-       p = htab_find_slot_with_hash (lasttab, e, hash, INSERT);
-       if (p == NULL)
- 	goto alloc_failure;
-       if (*p)
- 	{
- 	  struct sec_merge_hash_entry *ent;
- 
- 	  ent = (struct sec_merge_hash_entry *) *p;
- 	  e->u.suffix = ent;
- 	  e->alignment = 0;
- 	}
-       else
- 	*p = e;
      }
  
  alloc_failure:
    if (array)
      free (array);
-   if (lasttab)
-     htab_delete (lasttab);
-   if (last4tab)
-     htab_delete (last4tab);
  
    /* Now assign positions to the strings we want to keep.  */
    size = 0;
--- 586,635 ----
  
    for (e = sinfo->htab->first, a = array; e; e = e->next)
      if (e->alignment)
!       {
! 	*a++ = e;
! 	/* Adjust the length to not include the zero terminator.  */
! 	e->len -= sinfo->htab->entsize;
! 	if (alignment != e->alignment)
! 	  {
! 	    if (alignment == 0)
! 	      alignment = e->alignment;
! 	    else
! 	      alignment = (unsigned) -1;
! 	  }
!       }
  
    sinfo->htab->size = a - array;
!   if (sinfo->htab->size != 0)
!     {
!       qsort (array, (size_t) sinfo->htab->size,
! 	     sizeof (struct sec_merge_hash_entry *),
! 	     (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
! 	      ? strrevcmp_align : strrevcmp));
! 
!       /* Loop over the sorted array and merge suffixes */
!       e = *--a;
!       e->len += sinfo->htab->entsize;
!       while (--a >= array)
  	{
! 	  struct sec_merge_hash_entry *cmp = *a;
  
! 	  cmp->len += sinfo->htab->entsize;
! 	  if (e->alignment >= cmp->alignment
! 	      && !((e->len - cmp->len) & (cmp->alignment - 1))
! 	      && is_suffix (e, cmp))
! 	    {
! 	      cmp->u.suffix = e;
! 	      cmp->alignment = 0;
  	    }
  	  else
! 	    e = cmp;
  	}
      }
  
  alloc_failure:
    if (array)
      free (array);
  
    /* Now assign positions to the strings we want to keep.  */
    size = 0;
Index: bfd/elf-strtab.c
===================================================================
RCS file: /cvs/src/src/bfd/elf-strtab.c,v
retrieving revision 1.7
diff -c -p -r1.7 elf-strtab.c
*** bfd/elf-strtab.c	7 Aug 2003 07:25:34 -0000	1.7
--- bfd/elf-strtab.c	29 Oct 2003 22:22:15 -0000
***************
*** 1,5 ****
  /* ELF strtab with GC and suffix merging support.
!    Copyright 2001, 2002 Free Software Foundation, Inc.
     Written by Jakub Jelinek <jakub@redhat.com>.
  
     This file is part of BFD, the Binary File Descriptor library.
--- 1,5 ----
  /* ELF strtab with GC and suffix merging support.
!    Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
     Written by Jakub Jelinek <jakub@redhat.com>.
  
     This file is part of BFD, the Binary File Descriptor library.
***************
*** 30,44 ****
  struct elf_strtab_hash_entry
  {
    struct bfd_hash_entry root;
!   /* Length of this entry.  */
!   unsigned int len;
    unsigned int refcount;
    union {
      /* Index within the merged section.  */
      bfd_size_type index;
!     /* Entry this is a suffix of (if len is 0).  */
      struct elf_strtab_hash_entry *suffix;
-     struct elf_strtab_hash_entry *next;
    } u;
  };
  
--- 30,43 ----
  struct elf_strtab_hash_entry
  {
    struct bfd_hash_entry root;
!   /* Length of this entry.  This includes the zero terminator.  */
!   int len;
    unsigned int refcount;
    union {
      /* Index within the merged section.  */
      bfd_size_type index;
!     /* Entry this is a suffix of (if len < 0).  */
      struct elf_strtab_hash_entry *suffix;
    } u;
  };
  
*************** _bfd_elf_strtab_add (struct elf_strtab_h
*** 158,163 ****
--- 157,164 ----
    if (entry->len == 0)
      {
        entry->len = strlen (str) + 1;
+       /* 2G strings lose.  */
+       BFD_ASSERT (entry->len > 0);
        if (tab->size == tab->alloced)
  	{
  	  bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
*************** _bfd_elf_strtab_emit (register bfd *abfd
*** 235,248 ****
    for (i = 1; i < tab->size; ++i)
      {
        register const char *str;
!       register size_t len;
  
-       str = tab->array[i]->root.string;
-       len = tab->array[i]->len;
        BFD_ASSERT (tab->array[i]->refcount == 0);
!       if (len == 0)
  	continue;
  
        if (bfd_bwrite (str, len, abfd) != len)
  	return FALSE;
  
--- 236,249 ----
    for (i = 1; i < tab->size; ++i)
      {
        register const char *str;
!       register unsigned int len;
  
        BFD_ASSERT (tab->array[i]->refcount == 0);
!       len = tab->array[i]->len;
!       if ((int) len < 0)
  	continue;
  
+       str = tab->array[i]->root.string;
        if (bfd_bwrite (str, len, abfd) != len)
  	return FALSE;
  
*************** _bfd_elf_strtab_emit (register bfd *abfd
*** 253,292 ****
    return TRUE;
  }
  
! /* Compare two elf_strtab_hash_entry structures.  This is called via qsort.  */
  
  static int
! cmplengthentry (const void *a, const void *b)
  {
    struct elf_strtab_hash_entry *A = *(struct elf_strtab_hash_entry **) a;
    struct elf_strtab_hash_entry *B = *(struct elf_strtab_hash_entry **) b;
  
!   if (A->len < B->len)
!     return 1;
!   else if (A->len > B->len)
!     return -1;
! 
!   return memcmp (A->root.string, B->root.string, A->len);
  }
  
! static int
! last4_eq (const void *a, const void *b)
  {
-   const struct elf_strtab_hash_entry *A = a;
-   const struct elf_strtab_hash_entry *B = b;
- 
-   if (memcmp (A->root.string + A->len - 5, B->root.string + B->len - 5, 4)
-       != 0)
-     /* This was a hashtable collision.  */
-     return 0;
- 
    if (A->len <= B->len)
      /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
         not to be equal by the hash table.  */
      return 0;
  
    return memcmp (A->root.string + (A->len - B->len),
! 		 B->root.string, B->len - 5) == 0;
  }
  
  /* This function assigns final string table offsets for used strings,
--- 254,294 ----
    return TRUE;
  }
  
! /* Compare two elf_strtab_hash_entry structures.  Called via qsort.  */
  
  static int
! strrevcmp (const void *a, const void *b)
  {
    struct elf_strtab_hash_entry *A = *(struct elf_strtab_hash_entry **) a;
    struct elf_strtab_hash_entry *B = *(struct elf_strtab_hash_entry **) b;
+   unsigned int lenA = A->len;
+   unsigned int lenB = B->len;
+   const unsigned char *s = A->root.string + lenA - 1;
+   const unsigned char *t = B->root.string + lenB - 1;
+   int l = lenA < lenB ? lenA : lenB;
  
!   while (l)
!     {
!       if (*s != *t)
! 	return (int) *s - (int) *t;
!       s--;
!       t--;
!       l--;
!     }
!   return lenA - lenB;
  }
  
! static inline int
! is_suffix (const struct elf_strtab_hash_entry *A,
! 	   const struct elf_strtab_hash_entry *B)
  {
    if (A->len <= B->len)
      /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
         not to be equal by the hash table.  */
      return 0;
  
    return memcmp (A->root.string + (A->len - B->len),
! 		 B->root.string, B->len - 1) == 0;
  }
  
  /* This function assigns final string table offsets for used strings,
*************** last4_eq (const void *a, const void *b)
*** 295,304 ****
  void
  _bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
  {
!   struct elf_strtab_hash_entry **array, **a, **end, *e;
!   htab_t last4tab = NULL;
    bfd_size_type size, amt;
-   struct elf_strtab_hash_entry *last[256], **last_ptr[256];
  
    /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
       a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
--- 297,304 ----
  void
  _bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
  {
!   struct elf_strtab_hash_entry **array, **a, *e;
    bfd_size_type size, amt;
  
    /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
       a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
*************** _bfd_elf_strtab_finalize (struct elf_str
*** 306,410 ****
       cycles.  */
    size_t i;
  
!   /* Now sort the strings by length, longest first.  */
!   array = NULL;
    amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
    array = bfd_malloc (amt);
    if (array == NULL)
      goto alloc_failure;
  
-   memset (last, 0, sizeof (last));
-   for (i = 0; i < 256; ++i)
-     last_ptr[i] = &last[i];
    for (i = 1, a = array; i < tab->size; ++i)
!     if (tab->array[i]->refcount)
!       *a++ = tab->array[i];
!     else
!       tab->array[i]->len = 0;
  
    size = a - array;
  
!   qsort (array, size, sizeof (struct elf_strtab_hash_entry *), cmplengthentry);
  
!   last4tab = htab_create_alloc (size * 4, NULL, last4_eq, NULL, calloc, free);
!   if (last4tab == NULL)
!     goto alloc_failure;
  
!   /* Now insert the strings into hash tables (strings with last 4 characters
!      and strings with last character equal), look for longer strings which
!      we're suffix of.  */
!   for (a = array, end = array + size; a < end; a++)
!     {
!       register hashval_t hash;
!       unsigned int c;
!       unsigned int j;
!       const unsigned char *s;
!       void **p;
  
!       e = *a;
!       if (e->len > 4)
! 	{
! 	  s = e->root.string + e->len - 1;
! 	  hash = 0;
! 	  for (j = 0; j < 4; j++)
! 	    {
! 	      c = *--s;
! 	      hash += c + (c << 17);
! 	      hash ^= hash >> 2;
! 	    }
! 	  p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
! 	  if (p == NULL)
! 	    goto alloc_failure;
! 	  if (*p)
! 	    {
! 	      struct elf_strtab_hash_entry *ent;
  
! 	      ent = *p;
! 	      e->u.suffix = ent;
! 	      e->len = 0;
! 	      continue;
! 	    }
! 	  else
! 	    *p = e;
! 	}
!       else
  	{
! 	  struct elf_strtab_hash_entry *tem;
  
! 	  c = e->root.string[e->len - 2] & 0xff;
! 
! 	  for (tem = last[c]; tem; tem = tem->u.next)
! 	    if (tem->len > e->len
! 		&& memcmp (tem->root.string + (tem->len - e->len),
! 			   e->root.string, e->len - 1) == 0)
! 	      break;
! 	  if (tem)
  	    {
! 	      e->u.suffix = tem;
! 	      e->len = 0;
! 	      continue;
  	    }
  	}
- 
-       c = e->root.string[e->len - 2] & 0xff;
-       /* Put longest strings first.  */
-       *last_ptr[c] = e;
-       last_ptr[c] = &e->u.next;
-       e->u.next = NULL;
      }
  
  alloc_failure:
    if (array)
      free (array);
-   if (last4tab)
-     htab_delete (last4tab);
  
!   /* Now assign positions to the strings we want to keep.  */
    size = 1;
    for (i = 1; i < tab->size; ++i)
      {
        e = tab->array[i];
!       if (e->refcount && e->len)
  	{
  	  e->u.index = size;
  	  size += e->len;
--- 306,376 ----
       cycles.  */
    size_t i;
  
!   /* Sort the strings by suffix and length.  */
    amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
    array = bfd_malloc (amt);
    if (array == NULL)
      goto alloc_failure;
  
    for (i = 1, a = array; i < tab->size; ++i)
!     {
!       e = tab->array[i];
!       if (e->refcount)
! 	{
! 	  *a++ = e;
! 	  /* Adjust the length to not include the zero terminator.  */
! 	  e->len -= 1;
! 	}
!       else
! 	e->len = 0;
!     }
  
    size = a - array;
+   if (size != 0)
+     {
+       qsort (array, size, sizeof (struct elf_strtab_hash_entry *), strrevcmp);
  
!       /* Loop over the sorted array and merge suffixes.  Start from the
! 	 end because we want eg.
  
! 	 s1 -> "d"
! 	 s2 -> "bcd"
! 	 s3 -> "abcd"
  
! 	 to end up as
  
! 	 s3 -> "abcd"
! 	 s2 _____^
! 	 s1 _______^
  
! 	 ie. we don't want s1 pointing into the old s2.  */
!       e = *--a;
!       e->len += 1;
!       while (--a >= array)
  	{
! 	  struct elf_strtab_hash_entry *cmp = *a;
  
! 	  cmp->len += 1;
! 	  if (is_suffix (e, cmp))
  	    {
! 	      cmp->u.suffix = e;
! 	      cmp->len = -cmp->len;
  	    }
+ 	  else
+ 	    e = cmp;
  	}
      }
  
  alloc_failure:
    if (array)
      free (array);
  
!   /* Assign positions to the strings we want to keep.  */
    size = 1;
    for (i = 1; i < tab->size; ++i)
      {
        e = tab->array[i];
!       if (e->refcount && e->len > 0)
  	{
  	  e->u.index = size;
  	  size += e->len;
*************** alloc_failure:
*** 413,424 ****
  
    tab->sec_size = size;
  
!   /* And now adjust the rest.  */
    for (i = 1; i < tab->size; ++i)
      {
        e = tab->array[i];
!       if (e->refcount && ! e->len)
! 	e->u.index = e->u.suffix->u.index
! 		     + (e->u.suffix->len - strlen (e->root.string) - 1);
      }
  }
--- 379,389 ----
  
    tab->sec_size = size;
  
!   /* Adjust the rest.  */
    for (i = 1; i < tab->size; ++i)
      {
        e = tab->array[i];
!       if (e->refcount && e->len < 0)
! 	e->u.index = e->u.suffix->u.index + (e->u.suffix->len + e->len);
      }
  }

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]