Radix cross Linux Toolchains

Toolchains for all supported by Radix cross Linux devices

80 Commits   2 Branches   13 Tags
     9         kx /* SEC_MERGE support.
     9         kx    Copyright (C) 2001-2023 Free Software Foundation, Inc.
     9         kx    Written by Jakub Jelinek <jakub@redhat.com>.
     9         kx 
     9         kx    This file is part of BFD, the Binary File Descriptor library.
     9         kx 
     9         kx    This program is free software; you can redistribute it and/or modify
     9         kx    it under the terms of the GNU General Public License as published by
     9         kx    the Free Software Foundation; either version 3 of the License, or
     9         kx    (at your option) any later version.
     9         kx 
     9         kx    This program is distributed in the hope that it will be useful,
     9         kx    but WITHOUT ANY WARRANTY; without even the implied warranty of
     9         kx    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     9         kx    GNU General Public License for more details.
     9         kx 
     9         kx    You should have received a copy of the GNU General Public License
     9         kx    along with this program; if not, write to the Free Software
     9         kx    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     9         kx    MA 02110-1301, USA.  */
     9         kx 
     9         kx 
     9         kx /* This file contains support for merging duplicate entities within sections,
     9         kx    as used in ELF SHF_MERGE.  */
     9         kx 
     9         kx #include "sysdep.h"
     9         kx #include <limits.h>
     9         kx #include "bfd.h"
     9         kx #include "elf-bfd.h"
     9         kx #include "libbfd.h"
     9         kx #include "hashtab.h"
     9         kx #include "libiberty.h"
     9         kx 
     9         kx struct sec_merge_sec_info;
     9         kx 
     9         kx /* An entry in the section merge hash table.  */
     9         kx 
     9         kx struct sec_merge_hash_entry
     9         kx {
     9         kx   struct bfd_hash_entry root;
     9         kx   /* Length of this entry.  This includes the zero terminator.  */
     9         kx   unsigned int len;
     9         kx   /* Start of this string needs to be aligned to
     9         kx      alignment octets (not 1 << align).  */
     9         kx   unsigned int alignment;
     9         kx   union
     9         kx   {
     9         kx     /* Index within the merged section.  */
     9         kx     bfd_size_type index;
     9         kx     /* Entry this is a suffix of (if alignment is 0).  */
     9         kx     struct sec_merge_hash_entry *suffix;
     9         kx   } u;
     9         kx   /* Which section is it in.  */
     9         kx   struct sec_merge_sec_info *secinfo;
     9         kx   /* Next entity in the hash table.  */
     9         kx   struct sec_merge_hash_entry *next;
     9         kx };
     9         kx 
     9         kx /* The section merge hash table.  */
     9         kx 
     9         kx struct sec_merge_hash
     9         kx {
     9         kx   struct bfd_hash_table table;
     9         kx   /* Next available index.  */
     9         kx   bfd_size_type size;
     9         kx   /* First entity in the SEC_MERGE sections of this type.  */
     9         kx   struct sec_merge_hash_entry *first;
     9         kx   /* Last entity in the SEC_MERGE sections of this type.  */
     9         kx   struct sec_merge_hash_entry *last;
     9         kx   /* Entity size.  */
     9         kx   unsigned int entsize;
     9         kx   /* Are entries fixed size or zero terminated strings?  */
     9         kx   bool strings;
     9         kx };
     9         kx 
     9         kx struct sec_merge_info
     9         kx {
     9         kx   /* Chain of sec_merge_infos.  */
     9         kx   struct sec_merge_info *next;
     9         kx   /* Chain of sec_merge_sec_infos.  */
     9         kx   struct sec_merge_sec_info *chain;
     9         kx   /* A hash table used to hold section content.  */
     9         kx   struct sec_merge_hash *htab;
     9         kx };
     9         kx 
     9         kx struct sec_merge_sec_info
     9         kx {
     9         kx   /* Chain of sec_merge_sec_infos.  */
     9         kx   struct sec_merge_sec_info *next;
     9         kx   /* The corresponding section.  */
     9         kx   asection *sec;
     9         kx   /* Pointer to merge_info pointing to us.  */
     9         kx   void **psecinfo;
     9         kx   /* A hash table used to hold section content.  */
     9         kx   struct sec_merge_hash *htab;
     9         kx   /* First string in this section.  */
     9         kx   struct sec_merge_hash_entry *first_str;
     9         kx   /* Original section content.  */
     9         kx   unsigned char contents[1];
     9         kx };
     9         kx 
     9         kx 
     9         kx /* Routine to create an entry in a section merge hashtab.  */
     9         kx 
     9         kx static struct bfd_hash_entry *
     9         kx sec_merge_hash_newfunc (struct bfd_hash_entry *entry,
     9         kx 			struct bfd_hash_table *table, const char *string)
     9         kx {
     9         kx   /* Allocate the structure if it has not already been allocated by a
     9         kx      subclass.  */
     9         kx   if (entry == NULL)
     9         kx     entry = (struct bfd_hash_entry *)
     9         kx 	bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry));
     9         kx   if (entry == NULL)
     9         kx     return NULL;
     9         kx 
     9         kx   /* Call the allocation method of the superclass.  */
     9         kx   entry = bfd_hash_newfunc (entry, table, string);
     9         kx 
     9         kx   if (entry != NULL)
     9         kx     {
     9         kx       /* Initialize the local fields.  */
     9         kx       struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
     9         kx 
     9         kx       ret->u.suffix = NULL;
     9         kx       ret->alignment = 0;
     9         kx       ret->secinfo = NULL;
     9         kx       ret->next = NULL;
     9         kx     }
     9         kx 
     9         kx   return entry;
     9         kx }
     9         kx 
     9         kx /* Look up an entry in a section merge hash table.  */
     9         kx 
     9         kx static struct sec_merge_hash_entry *
     9         kx sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
     9         kx 		       unsigned int alignment, bool create)
     9         kx {
     9         kx   const unsigned char *s;
     9         kx   unsigned long hash;
     9         kx   unsigned int c;
     9         kx   struct sec_merge_hash_entry *hashp;
     9         kx   unsigned int len, i;
     9         kx   unsigned int _index;
     9         kx 
     9         kx   hash = 0;
     9         kx   len = 0;
     9         kx   s = (const unsigned char *) string;
     9         kx   if (table->strings)
     9         kx     {
     9         kx       if (table->entsize == 1)
     9         kx 	{
     9         kx 	  while ((c = *s++) != '\0')
     9         kx 	    {
     9         kx 	      hash += c + (c << 17);
     9         kx 	      hash ^= hash >> 2;
     9         kx 	      ++len;
     9         kx 	    }
     9         kx 	  hash += len + (len << 17);
     9         kx 	}
     9         kx       else
     9         kx 	{
     9         kx 	  for (;;)
     9         kx 	    {
     9         kx 	      for (i = 0; i < table->entsize; ++i)
     9         kx 		if (s[i] != '\0')
     9         kx 		  break;
     9         kx 	      if (i == table->entsize)
     9         kx 		break;
     9         kx 	      for (i = 0; i < table->entsize; ++i)
     9         kx 		{
     9         kx 		  c = *s++;
     9         kx 		  hash += c + (c << 17);
     9         kx 		  hash ^= hash >> 2;
     9         kx 		}
     9         kx 	      ++len;
     9         kx 	    }
     9         kx 	  hash += len + (len << 17);
     9         kx 	  len *= table->entsize;
     9         kx 	}
     9         kx       hash ^= hash >> 2;
     9         kx       len += table->entsize;
     9         kx     }
     9         kx   else
     9         kx     {
     9         kx       for (i = 0; i < table->entsize; ++i)
     9         kx 	{
     9         kx 	  c = *s++;
     9         kx 	  hash += c + (c << 17);
     9         kx 	  hash ^= hash >> 2;
     9         kx 	}
     9         kx       len = table->entsize;
     9         kx     }
     9         kx 
     9         kx   _index = hash % table->table.size;
     9         kx   for (hashp = (struct sec_merge_hash_entry *) table->table.table[_index];
     9         kx        hashp != NULL;
     9         kx        hashp = (struct sec_merge_hash_entry *) hashp->root.next)
     9         kx     {
     9         kx       if (hashp->root.hash == hash
     9         kx 	  && len == hashp->len
     9         kx 	  && memcmp (hashp->root.string, string, len) == 0)
     9         kx 	{
     9         kx 	  /* If the string we found does not have at least the required
     9         kx 	     alignment, we need to insert another copy.  */
     9         kx 	  if (hashp->alignment < alignment)
     9         kx 	    {
     9         kx 	      if (create)
     9         kx 		{
     9         kx 		  /*  Mark the less aligned copy as deleted.  */
     9         kx 		  hashp->len = 0;
     9         kx 		  hashp->alignment = 0;
     9         kx 		}
     9         kx 	      break;
     9         kx 	    }
     9         kx 	  return hashp;
     9         kx 	}
     9         kx     }
     9         kx 
     9         kx   if (! create)
     9         kx     return NULL;
     9         kx 
     9         kx   hashp = ((struct sec_merge_hash_entry *)
     9         kx 	   bfd_hash_insert (&table->table, string, hash));
     9         kx   if (hashp == NULL)
     9         kx     return NULL;
     9         kx   hashp->len = len;
     9         kx   hashp->alignment = alignment;
     9         kx   return hashp;
     9         kx }
     9         kx 
     9         kx /* Create a new hash table.  */
     9         kx 
     9         kx static struct sec_merge_hash *
     9         kx sec_merge_init (unsigned int entsize, bool strings)
     9         kx {
     9         kx   struct sec_merge_hash *table;
     9         kx 
     9         kx   table = (struct sec_merge_hash *) bfd_malloc (sizeof (struct sec_merge_hash));
     9         kx   if (table == NULL)
     9         kx     return NULL;
     9         kx 
     9         kx   if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc,
     9         kx 			       sizeof (struct sec_merge_hash_entry), 16699))
     9         kx     {
     9         kx       free (table);
     9         kx       return NULL;
     9         kx     }
     9         kx 
     9         kx   table->size = 0;
     9         kx   table->first = NULL;
     9         kx   table->last = NULL;
     9         kx   table->entsize = entsize;
     9         kx   table->strings = strings;
     9         kx 
     9         kx   return table;
     9         kx }
     9         kx 
     9         kx /* Get the index of an entity in a hash table, adding it if it is not
     9         kx    already present.  */
     9         kx 
     9         kx static struct sec_merge_hash_entry *
     9         kx sec_merge_add (struct sec_merge_hash *tab, const char *str,
     9         kx 	       unsigned int alignment, struct sec_merge_sec_info *secinfo)
     9         kx {
     9         kx   struct sec_merge_hash_entry *entry;
     9         kx 
     9         kx   entry = sec_merge_hash_lookup (tab, str, alignment, true);
     9         kx   if (entry == NULL)
     9         kx     return NULL;
     9         kx 
     9         kx   if (entry->secinfo == NULL)
     9         kx     {
     9         kx       tab->size++;
     9         kx       entry->secinfo = secinfo;
     9         kx       if (tab->first == NULL)
     9         kx 	tab->first = entry;
     9         kx       else
     9         kx 	tab->last->next = entry;
     9         kx       tab->last = entry;
     9         kx     }
     9         kx 
     9         kx   return entry;
     9         kx }
     9         kx 
     9         kx static bool
     9         kx sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry,
     9         kx 		unsigned char *contents, file_ptr offset)
     9         kx {
     9         kx   struct sec_merge_sec_info *secinfo = entry->secinfo;
     9         kx   asection *sec = secinfo->sec;
     9         kx   char *pad = NULL;
     9         kx   bfd_size_type off = 0;
     9         kx   unsigned int opb = bfd_octets_per_byte (abfd, sec);
     9         kx   int alignment_power = sec->output_section->alignment_power * opb;
     9         kx   bfd_size_type pad_len;  /* Octets.  */
     9         kx 
     9         kx   /* FIXME: If alignment_power is 0 then really we should scan the
     9         kx      entry list for the largest required alignment and use that.  */
     9         kx   pad_len = alignment_power ? ((bfd_size_type) 1 << alignment_power) : 16;
     9         kx 
     9         kx   pad = (char *) bfd_zmalloc (pad_len);
     9         kx   if (pad == NULL)
     9         kx     return false;
     9         kx 
     9         kx   for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
     9         kx     {
     9         kx       const char *str;
     9         kx       bfd_size_type len;
     9         kx 
     9         kx       len = -off & (entry->alignment - 1);
     9         kx       if (len != 0)
     9         kx 	{
     9         kx 	  BFD_ASSERT (len <= pad_len);
     9         kx 	  if (contents)
     9         kx 	    {
     9         kx 	      memcpy (contents + offset, pad, len);
     9         kx 	      offset += len;
     9         kx 	    }
     9         kx 	  else if (bfd_bwrite (pad, len, abfd) != len)
     9         kx 	    goto err;
     9         kx 	  off += len;
     9         kx 	}
     9         kx 
     9         kx       str = entry->root.string;
     9         kx       len = entry->len;
     9         kx 
     9         kx       if (contents)
     9         kx 	{
     9         kx 	  memcpy (contents + offset, str, len);
     9         kx 	  offset += len;
     9         kx 	}
     9         kx       else if (bfd_bwrite (str, len, abfd) != len)
     9         kx 	goto err;
     9         kx 
     9         kx       off += len;
     9         kx     }
     9         kx 
     9         kx   /* Trailing alignment needed?  */
     9         kx   off = sec->size - off;
     9         kx   if (off != 0 && alignment_power)
     9         kx     {
     9         kx       BFD_ASSERT (off <= pad_len);
     9         kx       if (contents)
     9         kx 	memcpy (contents + offset, pad, off);
     9         kx       else if (bfd_bwrite (pad, off, abfd) != off)
     9         kx 	goto err;
     9         kx     }
     9         kx 
     9         kx   free (pad);
     9         kx   return true;
     9         kx 
     9         kx  err:
     9         kx   free (pad);
     9         kx   return false;
     9         kx }
     9         kx 
     9         kx /* Register a SEC_MERGE section as a candidate for merging.
     9         kx    This function is called for all non-dynamic SEC_MERGE input sections.  */
     9         kx 
     9         kx bool
     9         kx _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
     9         kx 			void **psecinfo)
     9         kx {
     9         kx   struct sec_merge_info *sinfo;
     9         kx   struct sec_merge_sec_info *secinfo;
     9         kx   unsigned int alignment_power;  /* Octets.  */
     9         kx   unsigned int align;            /* Octets.  */
     9         kx   bfd_size_type amt;
     9         kx   bfd_byte *contents;
     9         kx   unsigned int opb = bfd_octets_per_byte (abfd, sec);
     9         kx 
     9         kx   if ((abfd->flags & DYNAMIC) != 0
     9         kx       || (sec->flags & SEC_MERGE) == 0)
     9         kx     abort ();
     9         kx 
     9         kx   if (sec->size == 0
     9         kx       || (sec->flags & SEC_EXCLUDE) != 0
     9         kx       || sec->entsize == 0)
     9         kx     return true;
     9         kx 
     9         kx   if (sec->size % sec->entsize != 0)
     9         kx     return true;
     9         kx 
     9         kx   if ((sec->flags & SEC_RELOC) != 0)
     9         kx     {
     9         kx       /* We aren't prepared to handle relocations in merged sections.  */
     9         kx       return true;
     9         kx     }
     9         kx 
     9         kx #ifndef CHAR_BIT
     9         kx #define CHAR_BIT 8
     9         kx #endif
     9         kx   alignment_power = sec->alignment_power * opb;
     9         kx   if (alignment_power >= sizeof (align) * CHAR_BIT)
     9         kx     return true;
     9         kx 
     9         kx   align = 1u << alignment_power;
     9         kx   if ((sec->entsize < align
     9         kx        && ((sec->entsize & (sec->entsize - 1))
     9         kx 	   || !(sec->flags & SEC_STRINGS)))
     9         kx       || (sec->entsize > align
     9         kx 	  && (sec->entsize & (align - 1))))
     9         kx     {
     9         kx       /* Sanity check.  If string character size is smaller than
     9         kx 	 alignment, then we require character size to be a power
     9         kx 	 of 2, otherwise character size must be integer multiple
     9         kx 	 of alignment.  For non-string constants, alignment must
     9         kx 	 be smaller than or equal to entity size and entity size
     9         kx 	 must be integer multiple of alignment.  */
     9         kx       return true;
     9         kx     }
     9         kx 
     9         kx   for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
     9         kx     if ((secinfo = sinfo->chain)
     9         kx 	&& ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
     9         kx 	&& secinfo->sec->entsize == sec->entsize
     9         kx 	&& secinfo->sec->alignment_power == sec->alignment_power
     9         kx 	&& secinfo->sec->output_section == sec->output_section)
     9         kx       break;
     9         kx 
     9         kx   if (sinfo == NULL)
     9         kx     {
     9         kx       /* Initialize the information we need to keep track of.  */
     9         kx       sinfo = (struct sec_merge_info *)
     9         kx 	  bfd_alloc (abfd, sizeof (struct sec_merge_info));
     9         kx       if (sinfo == NULL)
     9         kx 	goto error_return;
     9         kx       sinfo->next = (struct sec_merge_info *) *psinfo;
     9         kx       sinfo->chain = NULL;
     9         kx       *psinfo = sinfo;
     9         kx       sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
     9         kx       if (sinfo->htab == NULL)
     9         kx 	goto error_return;
     9         kx     }
     9         kx 
     9         kx   /* Read the section from abfd.  */
     9         kx 
     9         kx   amt = sizeof (struct sec_merge_sec_info) - 1 + sec->size;
     9         kx   if (sec->flags & SEC_STRINGS)
     9         kx     /* Some versions of gcc may emit a string without a zero terminator.
     9         kx        See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
     9         kx        Allocate space for an extra zero.  */
     9         kx     amt += sec->entsize;
     9         kx   *psecinfo = bfd_alloc (abfd, amt);
     9         kx   if (*psecinfo == NULL)
     9         kx     goto error_return;
     9         kx 
     9         kx   secinfo = (struct sec_merge_sec_info *) *psecinfo;
     9         kx   if (sinfo->chain)
     9         kx     {
     9         kx       secinfo->next = sinfo->chain->next;
     9         kx       sinfo->chain->next = secinfo;
     9         kx     }
     9         kx   else
     9         kx     secinfo->next = secinfo;
     9         kx   sinfo->chain = secinfo;
     9         kx   secinfo->sec = sec;
     9         kx   secinfo->psecinfo = psecinfo;
     9         kx   secinfo->htab = sinfo->htab;
     9         kx   secinfo->first_str = NULL;
     9         kx 
     9         kx   sec->rawsize = sec->size;
     9         kx   if (sec->flags & SEC_STRINGS)
     9         kx     memset (secinfo->contents + sec->size, 0, sec->entsize);
     9         kx   contents = secinfo->contents;
     9         kx   if (! bfd_get_full_section_contents (sec->owner, sec, &contents))
     9         kx     goto error_return;
     9         kx 
     9         kx   return true;
     9         kx 
     9         kx  error_return:
     9         kx   *psecinfo = NULL;
     9         kx   return false;
     9         kx }
     9         kx 
     9         kx /* Record one section into the hash table.  */
     9         kx static bool
     9         kx record_section (struct sec_merge_info *sinfo,
     9         kx 		struct sec_merge_sec_info *secinfo)
     9         kx {
     9         kx   asection *sec = secinfo->sec;
     9         kx   struct sec_merge_hash_entry *entry;
     9         kx   bool nul;
     9         kx   unsigned char *p, *end;
     9         kx   bfd_vma mask, eltalign;
     9         kx   unsigned int align, i;
     9         kx 
     9         kx   align = sec->alignment_power;
     9         kx   end = secinfo->contents + sec->size;
     9         kx   nul = false;
     9         kx   mask = ((bfd_vma) 1 << align) - 1;
     9         kx   if (sec->flags & SEC_STRINGS)
     9         kx     {
     9         kx       for (p = secinfo->contents; p < end; )
     9         kx 	{
     9         kx 	  eltalign = p - secinfo->contents;
     9         kx 	  eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
     9         kx 	  if (!eltalign || eltalign > mask)
     9         kx 	    eltalign = mask + 1;
     9         kx 	  entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
     9         kx 				 secinfo);
     9         kx 	  if (! entry)
     9         kx 	    goto error_return;
     9         kx 	  p += entry->len;
     9         kx 	  if (sec->entsize == 1)
     9         kx 	    {
     9         kx 	      while (p < end && *p == 0)
     9         kx 		{
     9         kx 		  if (!nul && !((p - secinfo->contents) & mask))
     9         kx 		    {
     9         kx 		      nul = true;
     9         kx 		      entry = sec_merge_add (sinfo->htab, "",
     9         kx 					     (unsigned) mask + 1, secinfo);
     9         kx 		      if (! entry)
     9         kx 			goto error_return;
     9         kx 		    }
     9         kx 		  p++;
     9         kx 		}
     9         kx 	    }
     9         kx 	  else
     9         kx 	    {
     9         kx 	      while (p < end)
     9         kx 		{
     9         kx 		  for (i = 0; i < sec->entsize; i++)
     9         kx 		    if (p[i] != '\0')
     9         kx 		      break;
     9         kx 		  if (i != sec->entsize)
     9         kx 		    break;
     9         kx 		  if (!nul && !((p - secinfo->contents) & mask))
     9         kx 		    {
     9         kx 		      nul = true;
     9         kx 		      entry = sec_merge_add (sinfo->htab, (char *) p,
     9         kx 					     (unsigned) mask + 1, secinfo);
     9         kx 		      if (! entry)
     9         kx 			goto error_return;
     9         kx 		    }
     9         kx 		  p += sec->entsize;
     9         kx 		}
     9         kx 	    }
     9         kx 	}
     9         kx     }
     9         kx   else
     9         kx     {
     9         kx       for (p = secinfo->contents; p < end; p += sec->entsize)
     9         kx 	{
     9         kx 	  entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
     9         kx 	  if (! entry)
     9         kx 	    goto error_return;
     9         kx 	}
     9         kx     }
     9         kx 
     9         kx   return true;
     9         kx 
     9         kx  error_return:
     9         kx   for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
     9         kx     *secinfo->psecinfo = NULL;
     9         kx   return false;
     9         kx }
     9         kx 
     9         kx /* qsort comparison function.  Won't ever return zero as all entries
     9         kx    differ, so there is no issue with qsort stability here.  */
     9         kx 
     9         kx static int
     9         kx strrevcmp (const void *a, const void *b)
     9         kx {
     9         kx   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
     9         kx   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
     9         kx   unsigned int lenA = A->len;
     9         kx   unsigned int lenB = B->len;
     9         kx   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
     9         kx   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
     9         kx   int l = lenA < lenB ? lenA : lenB;
     9         kx 
     9         kx   while (l)
     9         kx     {
     9         kx       if (*s != *t)
     9         kx 	return (int) *s - (int) *t;
     9         kx       s--;
     9         kx       t--;
     9         kx       l--;
     9         kx     }
     9         kx   return lenA - lenB;
     9         kx }
     9         kx 
     9         kx /* Like strrevcmp, but for the case where all strings have the same
     9         kx    alignment > entsize.  */
     9         kx 
     9         kx static int
     9         kx strrevcmp_align (const void *a, const void *b)
     9         kx {
     9         kx   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
     9         kx   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
     9         kx   unsigned int lenA = A->len;
     9         kx   unsigned int lenB = B->len;
     9         kx   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
     9         kx   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
     9         kx   int l = lenA < lenB ? lenA : lenB;
     9         kx   int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
     9         kx 
     9         kx   if (tail_align != 0)
     9         kx     return tail_align;
     9         kx 
     9         kx   while (l)
     9         kx     {
     9         kx       if (*s != *t)
     9         kx 	return (int) *s - (int) *t;
     9         kx       s--;
     9         kx       t--;
     9         kx       l--;
     9         kx     }
     9         kx   return lenA - lenB;
     9         kx }
     9         kx 
     9         kx static inline int
     9         kx is_suffix (const struct sec_merge_hash_entry *A,
     9         kx 	   const struct sec_merge_hash_entry *B)
     9         kx {
     9         kx   if (A->len <= B->len)
     9         kx     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
     9         kx        not to be equal by the hash table.  */
     9         kx     return 0;
     9         kx 
     9         kx   return memcmp (A->root.string + (A->len - B->len),
     9         kx 		 B->root.string, B->len) == 0;
     9         kx }
     9         kx 
     9         kx /* This is a helper function for _bfd_merge_sections.  It attempts to
     9         kx    merge strings matching suffixes of longer strings.  */
     9         kx static struct sec_merge_sec_info *
     9         kx merge_strings (struct sec_merge_info *sinfo)
     9         kx {
     9         kx   struct sec_merge_hash_entry **array, **a, *e;
     9         kx   struct sec_merge_sec_info *secinfo;
     9         kx   bfd_size_type size, amt;
     9         kx   unsigned int alignment = 0;
     9         kx 
     9         kx   /* Now sort the strings */
     9         kx   amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
     9         kx   array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
     9         kx   if (array == NULL)
     9         kx     return NULL;
     9         kx 
     9         kx   for (e = sinfo->htab->first, a = array; e; e = e->next)
     9         kx     if (e->alignment)
     9         kx       {
     9         kx 	*a++ = e;
     9         kx 	/* Adjust the length to not include the zero terminator.  */
     9         kx 	e->len -= sinfo->htab->entsize;
     9         kx 	if (alignment != e->alignment)
     9         kx 	  {
     9         kx 	    if (alignment == 0)
     9         kx 	      alignment = e->alignment;
     9         kx 	    else
     9         kx 	      alignment = (unsigned) -1;
     9         kx 	  }
     9         kx       }
     9         kx 
     9         kx   sinfo->htab->size = a - array;
     9         kx   if (sinfo->htab->size != 0)
     9         kx     {
     9         kx       qsort (array, (size_t) sinfo->htab->size,
     9         kx 	     sizeof (struct sec_merge_hash_entry *),
     9         kx 	     (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
     9         kx 	      ? strrevcmp_align : strrevcmp));
     9         kx 
     9         kx       /* Loop over the sorted array and merge suffixes */
     9         kx       e = *--a;
     9         kx       e->len += sinfo->htab->entsize;
     9         kx       while (--a >= array)
     9         kx 	{
     9         kx 	  struct sec_merge_hash_entry *cmp = *a;
     9         kx 
     9         kx 	  cmp->len += sinfo->htab->entsize;
     9         kx 	  if (e->alignment >= cmp->alignment
     9         kx 	      && !((e->len - cmp->len) & (cmp->alignment - 1))
     9         kx 	      && is_suffix (e, cmp))
     9         kx 	    {
     9         kx 	      cmp->u.suffix = e;
     9         kx 	      cmp->alignment = 0;
     9         kx 	    }
     9         kx 	  else
     9         kx 	    e = cmp;
     9         kx 	}
     9         kx     }
     9         kx 
     9         kx   free (array);
     9         kx 
     9         kx   /* Now assign positions to the strings we want to keep.  */
     9         kx   size = 0;
     9         kx   secinfo = sinfo->htab->first->secinfo;
     9         kx   for (e = sinfo->htab->first; e; e = e->next)
     9         kx     {
     9         kx       if (e->secinfo != secinfo)
     9         kx 	{
     9         kx 	  secinfo->sec->size = size;
     9         kx 	  secinfo = e->secinfo;
     9         kx 	}
     9         kx       if (e->alignment)
     9         kx 	{
     9         kx 	  if (e->secinfo->first_str == NULL)
     9         kx 	    {
     9         kx 	      e->secinfo->first_str = e;
     9         kx 	      size = 0;
     9         kx 	    }
     9         kx 	  size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
     9         kx 	  e->u.index = size;
     9         kx 	  size += e->len;
     9         kx 	}
     9         kx     }
     9         kx   secinfo->sec->size = size;
     9         kx 
     9         kx   /* And now adjust the rest, removing them from the chain (but not hashtable)
     9         kx      at the same time.  */
     9         kx   for (a = &sinfo->htab->first, e = *a; e; e = e->next)
     9         kx     if (e->alignment)
     9         kx       a = &e->next;
     9         kx     else
     9         kx       {
     9         kx 	*a = e->next;
     9         kx 	if (e->len)
     9         kx 	  {
     9         kx 	    e->secinfo = e->u.suffix->secinfo;
     9         kx 	    e->alignment = e->u.suffix->alignment;
     9         kx 	    e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
     9         kx 	  }
     9         kx       }
     9         kx   return secinfo;
     9         kx }
     9         kx 
     9         kx /* This function is called once after all SEC_MERGE sections are registered
     9         kx    with _bfd_merge_section.  */
     9         kx 
     9         kx bool
     9         kx _bfd_merge_sections (bfd *abfd,
     9         kx 		     struct bfd_link_info *info ATTRIBUTE_UNUSED,
     9         kx 		     void *xsinfo,
     9         kx 		     void (*remove_hook) (bfd *, asection *))
     9         kx {
     9         kx   struct sec_merge_info *sinfo;
     9         kx 
     9         kx   for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
     9         kx     {
     9         kx       struct sec_merge_sec_info *secinfo;
     9         kx       bfd_size_type align;  /* Bytes.  */
     9         kx 
     9         kx       if (! sinfo->chain)
     9         kx 	continue;
     9         kx 
     9         kx       /* Move sinfo->chain to head of the chain, terminate it.  */
     9         kx       secinfo = sinfo->chain;
     9         kx       sinfo->chain = secinfo->next;
     9         kx       secinfo->next = NULL;
     9         kx 
     9         kx       /* Record the sections into the hash table.  */
     9         kx       align = 1;
     9         kx       for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
     9         kx 	if (secinfo->sec->flags & SEC_EXCLUDE)
     9         kx 	  {
     9         kx 	    *secinfo->psecinfo = NULL;
     9         kx 	    if (remove_hook)
     9         kx 	      (*remove_hook) (abfd, secinfo->sec);
     9         kx 	  }
     9         kx 	else
     9         kx 	  {
     9         kx 	    if (!record_section (sinfo, secinfo))
     9         kx 	      return false;
     9         kx 	    if (align)
     9         kx 	      {
     9         kx 		unsigned int opb = bfd_octets_per_byte (abfd, secinfo->sec);
     9         kx 
     9         kx 		align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
     9         kx 		if (((secinfo->sec->size / opb) & (align - 1)) != 0)
     9         kx 		  align = 0;
     9         kx 	      }
     9         kx 	  }
     9         kx 
     9         kx       if (sinfo->htab->first == NULL)
     9         kx 	continue;
     9         kx 
     9         kx       if (sinfo->htab->strings)
     9         kx 	{
     9         kx 	  secinfo = merge_strings (sinfo);
     9         kx 	  if (!secinfo)
     9         kx 	    return false;
     9         kx 	}
     9         kx       else
     9         kx 	{
     9         kx 	  struct sec_merge_hash_entry *e;
     9         kx 	  bfd_size_type size = 0;  /* Octets.  */
     9         kx 
     9         kx 	  /* Things are much simpler for non-strings.
     9         kx 	     Just assign them slots in the section.  */
     9         kx 	  secinfo = NULL;
     9         kx 	  for (e = sinfo->htab->first; e; e = e->next)
     9         kx 	    {
     9         kx 	      if (e->secinfo->first_str == NULL)
     9         kx 		{
     9         kx 		  if (secinfo)
     9         kx 		    secinfo->sec->size = size;
     9         kx 		  e->secinfo->first_str = e;
     9         kx 		  size = 0;
     9         kx 		}
     9         kx 	      size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
     9         kx 	      e->u.index = size;
     9         kx 	      size += e->len;
     9         kx 	      secinfo = e->secinfo;
     9         kx 	    }
     9         kx 	  secinfo->sec->size = size;
     9         kx 	}
     9         kx 
     9         kx       /* If the input sections were padded according to their alignments,
     9         kx 	 then pad the output too.  */
     9         kx       if (align)
     9         kx 	secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
     9         kx 
     9         kx       /* Finally remove all input sections which have not made it into
     9         kx 	 the hash table at all.  */
     9         kx       for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
     9         kx 	if (secinfo->first_str == NULL)
     9         kx 	  secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
     9         kx     }
     9         kx 
     9         kx   return true;
     9         kx }
     9         kx 
     9         kx /* Write out the merged section.  */
     9         kx 
     9         kx bool
     9         kx _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
     9         kx {
     9         kx   struct sec_merge_sec_info *secinfo;
     9         kx   file_ptr pos;
     9         kx   unsigned char *contents;
     9         kx   Elf_Internal_Shdr *hdr;
     9         kx 
     9         kx   secinfo = (struct sec_merge_sec_info *) psecinfo;
     9         kx 
     9         kx   if (!secinfo)
     9         kx     return false;
     9         kx 
     9         kx   if (secinfo->first_str == NULL)
     9         kx     return true;
     9         kx 
     9         kx   /* FIXME: octets_per_byte.  */
     9         kx   hdr = &elf_section_data (sec->output_section)->this_hdr;
     9         kx   if (hdr->sh_offset == (file_ptr) -1)
     9         kx     {
     9         kx       /* We must compress this section.  Write output to the
     9         kx 	 buffer.  */
     9         kx       contents = hdr->contents;
     9         kx       if (contents == NULL)
     9         kx 	abort ();
     9         kx     }
     9         kx   else
     9         kx     {
     9         kx       contents = NULL;
     9         kx       pos = sec->output_section->filepos + sec->output_offset;
     9         kx       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
     9         kx 	return false;
     9         kx     }
     9         kx 
     9         kx   if (! sec_merge_emit (output_bfd, secinfo->first_str, contents,
     9         kx 			sec->output_offset))
     9         kx     return false;
     9         kx 
     9         kx   return true;
     9         kx }
     9         kx 
     9         kx /* Adjust an address in the SEC_MERGE section.  Given OFFSET within
     9         kx    *PSEC, this returns the new offset in the adjusted SEC_MERGE
     9         kx    section and writes the new section back into *PSEC.  */
     9         kx 
     9         kx bfd_vma
     9         kx _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
     9         kx 			    void *psecinfo, bfd_vma offset)
     9         kx {
     9         kx   struct sec_merge_sec_info *secinfo;
     9         kx   struct sec_merge_hash_entry *entry;
     9         kx   unsigned char *p;
     9         kx   asection *sec = *psec;
     9         kx 
     9         kx   secinfo = (struct sec_merge_sec_info *) psecinfo;
     9         kx 
     9         kx   if (!secinfo)
     9         kx     return offset;
     9         kx 
     9         kx   if (offset >= sec->rawsize)
     9         kx     {
     9         kx       if (offset > sec->rawsize)
     9         kx 	_bfd_error_handler
     9         kx 	  /* xgettext:c-format */
     9         kx 	  (_("%pB: access beyond end of merged section (%" PRId64 ")"),
     9         kx 	   sec->owner, (int64_t) offset);
     9         kx       return secinfo->first_str ? sec->size : 0;
     9         kx     }
     9         kx 
     9         kx   if (secinfo->htab->strings)
     9         kx     {
     9         kx       if (sec->entsize == 1)
     9         kx 	{
     9         kx 	  p = secinfo->contents + offset - 1;
     9         kx 	  while (p >= secinfo->contents && *p)
     9         kx 	    --p;
     9         kx 	  ++p;
     9         kx 	}
     9         kx       else
     9         kx 	{
     9         kx 	  p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
     9         kx 	  p -= sec->entsize;
     9         kx 	  while (p >= secinfo->contents)
     9         kx 	    {
     9         kx 	      unsigned int i;
     9         kx 
     9         kx 	      for (i = 0; i < sec->entsize; ++i)
     9         kx 		if (p[i] != '\0')
     9         kx 		  break;
     9         kx 	      if (i == sec->entsize)
     9         kx 		break;
     9         kx 	      p -= sec->entsize;
     9         kx 	    }
     9         kx 	  p += sec->entsize;
     9         kx 	}
     9         kx     }
     9         kx   else
     9         kx     {
     9         kx       p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
     9         kx     }
     9         kx   entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, false);
     9         kx   if (!entry)
     9         kx     {
     9         kx       if (! secinfo->htab->strings)
     9         kx 	abort ();
     9         kx       /* This should only happen if somebody points into the padding
     9         kx 	 after a NUL character but before next entity.  */
     9         kx       if (*p)
     9         kx 	abort ();
     9         kx       if (! secinfo->htab->first)
     9         kx 	abort ();
     9         kx       entry = secinfo->htab->first;
     9         kx       p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
     9         kx 	   - entry->len);
     9         kx     }
     9         kx 
     9         kx   *psec = entry->secinfo->sec;
     9         kx   return entry->u.index + (secinfo->contents + offset - p);
     9         kx }
     9         kx 
     9         kx /* Tidy up when done.  */
     9         kx 
     9         kx void
     9         kx _bfd_merge_sections_free (void *xsinfo)
     9         kx {
     9         kx   struct sec_merge_info *sinfo;
     9         kx 
     9         kx   for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
     9         kx     {
     9         kx       bfd_hash_table_free (&sinfo->htab->table);
     9         kx       free (sinfo->htab);
     9         kx     }
     9         kx }