Back to index

cell-binutils  2.17cvs20070401
merge.c
Go to the documentation of this file.
00001 /* SEC_MERGE support.
00002    Copyright 2001, 2002, 2003, 2004, 2005, 2006
00003    Free Software Foundation, Inc.
00004    Written by Jakub Jelinek <jakub@redhat.com>.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* This file contains support for merging duplicate entities within sections,
00023    as used in ELF SHF_MERGE.  */
00024 
00025 #include "bfd.h"
00026 #include "sysdep.h"
00027 #include "libbfd.h"
00028 #include "hashtab.h"
00029 #include "libiberty.h"
00030 
00031 struct sec_merge_sec_info;
00032 
00033 /* An entry in the section merge hash table.  */
00034 
00035 struct sec_merge_hash_entry
00036 {
00037   struct bfd_hash_entry root;
00038   /* Length of this entry.  This includes the zero terminator.  */
00039   unsigned int len;
00040   /* Start of this string needs to be aligned to
00041      alignment octets (not 1 << align).  */
00042   unsigned int alignment;
00043   union
00044   {
00045     /* Index within the merged section.  */
00046     bfd_size_type index;
00047     /* Entry this is a suffix of (if alignment is 0).  */
00048     struct sec_merge_hash_entry *suffix;
00049   } u;
00050   /* Which section is it in.  */
00051   struct sec_merge_sec_info *secinfo;
00052   /* Next entity in the hash table.  */
00053   struct sec_merge_hash_entry *next;
00054 };
00055 
00056 /* The section merge hash table.  */
00057 
00058 struct sec_merge_hash
00059 {
00060   struct bfd_hash_table table;
00061   /* Next available index.  */
00062   bfd_size_type size;
00063   /* First entity in the SEC_MERGE sections of this type.  */
00064   struct sec_merge_hash_entry *first;
00065   /* Last entity in the SEC_MERGE sections of this type.  */
00066   struct sec_merge_hash_entry *last;
00067   /* Entity size.  */
00068   unsigned int entsize;
00069   /* Are entries fixed size or zero terminated strings?  */
00070   bfd_boolean strings;
00071 };
00072 
00073 struct sec_merge_info
00074 {
00075   /* Chain of sec_merge_infos.  */
00076   struct sec_merge_info *next;
00077   /* Chain of sec_merge_sec_infos.  */
00078   struct sec_merge_sec_info *chain;
00079   /* A hash table used to hold section content.  */
00080   struct sec_merge_hash *htab;
00081 };
00082 
00083 struct sec_merge_sec_info
00084 {
00085   /* Chain of sec_merge_sec_infos.  */
00086   struct sec_merge_sec_info *next;
00087   /* The corresponding section.  */
00088   asection *sec;
00089   /* Pointer to merge_info pointing to us.  */
00090   void **psecinfo;
00091   /* A hash table used to hold section content.  */
00092   struct sec_merge_hash *htab;
00093   /* First string in this section.  */
00094   struct sec_merge_hash_entry *first_str;
00095   /* Original section content.  */
00096   unsigned char contents[1];
00097 };
00098 
00099 
00100 /* Routine to create an entry in a section merge hashtab.  */
00101 
00102 static struct bfd_hash_entry *
00103 sec_merge_hash_newfunc (struct bfd_hash_entry *entry,
00104                      struct bfd_hash_table *table, const char *string)
00105 {
00106   /* Allocate the structure if it has not already been allocated by a
00107      subclass.  */
00108   if (entry == NULL)
00109     entry = bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry));
00110   if (entry == NULL)
00111     return NULL;
00112 
00113   /* Call the allocation method of the superclass.  */
00114   entry = bfd_hash_newfunc (entry, table, string);
00115 
00116   if (entry != NULL)
00117     {
00118       /* Initialize the local fields.  */
00119       struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
00120 
00121       ret->u.suffix = NULL;
00122       ret->alignment = 0;
00123       ret->secinfo = NULL;
00124       ret->next = NULL;
00125     }
00126 
00127   return entry;
00128 }
00129 
00130 /* Look up an entry in a section merge hash table.  */
00131 
00132 static struct sec_merge_hash_entry *
00133 sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
00134                      unsigned int alignment, bfd_boolean create)
00135 {
00136   register const unsigned char *s;
00137   register unsigned long hash;
00138   register unsigned int c;
00139   struct sec_merge_hash_entry *hashp;
00140   unsigned int len, i;
00141   unsigned int index;
00142 
00143   hash = 0;
00144   len = 0;
00145   s = (const unsigned char *) string;
00146   if (table->strings)
00147     {
00148       if (table->entsize == 1)
00149        {
00150          while ((c = *s++) != '\0')
00151            {
00152              hash += c + (c << 17);
00153              hash ^= hash >> 2;
00154              ++len;
00155            }
00156          hash += len + (len << 17);
00157        }
00158       else
00159        {
00160          for (;;)
00161            {
00162              for (i = 0; i < table->entsize; ++i)
00163               if (s[i] != '\0')
00164                 break;
00165              if (i == table->entsize)
00166               break;
00167              for (i = 0; i < table->entsize; ++i)
00168               {
00169                 c = *s++;
00170                 hash += c + (c << 17);
00171                 hash ^= hash >> 2;
00172               }
00173              ++len;
00174            }
00175          hash += len + (len << 17);
00176          len *= table->entsize;
00177        }
00178       hash ^= hash >> 2;
00179       len += table->entsize;
00180     }
00181   else
00182     {
00183       for (i = 0; i < table->entsize; ++i)
00184        {
00185          c = *s++;
00186          hash += c + (c << 17);
00187          hash ^= hash >> 2;
00188        }
00189       len = table->entsize;
00190     }
00191 
00192   index = hash % table->table.size;
00193   for (hashp = (struct sec_merge_hash_entry *) table->table.table[index];
00194        hashp != NULL;
00195        hashp = (struct sec_merge_hash_entry *) hashp->root.next)
00196     {
00197       if (hashp->root.hash == hash
00198          && len == hashp->len
00199          && memcmp (hashp->root.string, string, len) == 0)
00200        {
00201          /* If the string we found does not have at least the required
00202             alignment, we need to insert another copy.  */
00203          if (hashp->alignment < alignment)
00204            {
00205              if (create)
00206               {
00207                 /*  Mark the less aligned copy as deleted.  */
00208                 hashp->len = 0;
00209                 hashp->alignment = 0;
00210               }
00211              break;
00212            }
00213          return hashp;
00214        }
00215     }
00216 
00217   if (! create)
00218     return NULL;
00219 
00220   hashp = ((struct sec_merge_hash_entry *)
00221           sec_merge_hash_newfunc (NULL, &table->table, string));
00222   if (hashp == NULL)
00223     return NULL;
00224   hashp->root.string = string;
00225   hashp->root.hash = hash;
00226   hashp->len = len;
00227   hashp->alignment = alignment;
00228   hashp->root.next = table->table.table[index];
00229   table->table.table[index] = (struct bfd_hash_entry *) hashp;
00230 
00231   return hashp;
00232 }
00233 
00234 /* Create a new hash table.  */
00235 
00236 static struct sec_merge_hash *
00237 sec_merge_init (unsigned int entsize, bfd_boolean strings)
00238 {
00239   struct sec_merge_hash *table;
00240 
00241   table = bfd_malloc (sizeof (struct sec_merge_hash));
00242   if (table == NULL)
00243     return NULL;
00244 
00245   if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc,
00246                             sizeof (struct sec_merge_hash_entry), 16699))
00247     {
00248       free (table);
00249       return NULL;
00250     }
00251 
00252   table->size = 0;
00253   table->first = NULL;
00254   table->last = NULL;
00255   table->entsize = entsize;
00256   table->strings = strings;
00257 
00258   return table;
00259 }
00260 
00261 /* Get the index of an entity in a hash table, adding it if it is not
00262    already present.  */
00263 
00264 static struct sec_merge_hash_entry *
00265 sec_merge_add (struct sec_merge_hash *tab, const char *str,
00266               unsigned int alignment, struct sec_merge_sec_info *secinfo)
00267 {
00268   register struct sec_merge_hash_entry *entry;
00269 
00270   entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
00271   if (entry == NULL)
00272     return NULL;
00273 
00274   if (entry->secinfo == NULL)
00275     {
00276       tab->size++;
00277       entry->secinfo = secinfo;
00278       if (tab->first == NULL)
00279        tab->first = entry;
00280       else
00281        tab->last->next = entry;
00282       tab->last = entry;
00283     }
00284 
00285   return entry;
00286 }
00287 
00288 static bfd_boolean
00289 sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
00290 {
00291   struct sec_merge_sec_info *secinfo = entry->secinfo;
00292   asection *sec = secinfo->sec;
00293   char *pad = NULL;
00294   bfd_size_type off = 0;
00295   int alignment_power = sec->output_section->alignment_power;
00296 
00297   if (alignment_power)
00298     {
00299       pad = bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
00300       if (pad == NULL)
00301        return FALSE;
00302     }
00303 
00304   for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
00305     {
00306       const char *str;
00307       bfd_size_type len;
00308 
00309       len = -off & (entry->alignment - 1);
00310       if (len != 0)
00311        {
00312          if (bfd_bwrite (pad, len, abfd) != len)
00313            goto err;
00314          off += len;
00315        }
00316 
00317       str = entry->root.string;
00318       len = entry->len;
00319 
00320       if (bfd_bwrite (str, len, abfd) != len)
00321        goto err;
00322 
00323       off += len;
00324     }
00325 
00326   /* Trailing alignment needed?  */
00327   off = sec->size - off;
00328   if (off != 0
00329       && bfd_bwrite (pad, off, abfd) != off)
00330     goto err;
00331 
00332   if (pad != NULL)
00333     free (pad);
00334   return TRUE;
00335 
00336  err:
00337   if (pad != NULL)
00338     free (pad);
00339   return FALSE;
00340 }
00341 
00342 /* Register a SEC_MERGE section as a candidate for merging.
00343    This function is called for all non-dynamic SEC_MERGE input sections.  */
00344 
00345 bfd_boolean
00346 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
00347                      void **psecinfo)
00348 {
00349   struct sec_merge_info *sinfo;
00350   struct sec_merge_sec_info *secinfo;
00351   unsigned int align;
00352   bfd_size_type amt;
00353 
00354   if ((abfd->flags & DYNAMIC) != 0
00355       || (sec->flags & SEC_MERGE) == 0)
00356     abort ();
00357 
00358   if (sec->size == 0
00359       || (sec->flags & SEC_EXCLUDE) != 0
00360       || sec->entsize == 0)
00361     return TRUE;
00362 
00363   if ((sec->flags & SEC_RELOC) != 0)
00364     {
00365       /* We aren't prepared to handle relocations in merged sections.  */
00366       return TRUE;
00367     }
00368 
00369   align = sec->alignment_power;
00370   if ((sec->entsize < (unsigned) 1 << align
00371        && ((sec->entsize & (sec->entsize - 1))
00372           || !(sec->flags & SEC_STRINGS)))
00373       || (sec->entsize > (unsigned) 1 << align
00374          && (sec->entsize & (((unsigned) 1 << align) - 1))))
00375     {
00376       /* Sanity check.  If string character size is smaller than
00377         alignment, then we require character size to be a power
00378         of 2, otherwise character size must be integer multiple
00379         of alignment.  For non-string constants, alignment must
00380         be smaller than or equal to entity size and entity size
00381         must be integer multiple of alignment.  */
00382       return TRUE;
00383     }
00384 
00385   for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
00386     if ((secinfo = sinfo->chain)
00387        && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
00388        && secinfo->sec->entsize == sec->entsize
00389        && secinfo->sec->alignment_power == sec->alignment_power
00390        && secinfo->sec->output_section == sec->output_section)
00391       break;
00392 
00393   if (sinfo == NULL)
00394     {
00395       /* Initialize the information we need to keep track of.  */
00396       sinfo = bfd_alloc (abfd, sizeof (struct sec_merge_info));
00397       if (sinfo == NULL)
00398        goto error_return;
00399       sinfo->next = (struct sec_merge_info *) *psinfo;
00400       sinfo->chain = NULL;
00401       *psinfo = sinfo;
00402       sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
00403       if (sinfo->htab == NULL)
00404        goto error_return;
00405     }
00406 
00407   /* Read the section from abfd.  */
00408 
00409   amt = sizeof (struct sec_merge_sec_info) + sec->size - 1;
00410   *psecinfo = bfd_alloc (abfd, amt);
00411   if (*psecinfo == NULL)
00412     goto error_return;
00413 
00414   secinfo = (struct sec_merge_sec_info *) *psecinfo;
00415   if (sinfo->chain)
00416     {
00417       secinfo->next = sinfo->chain->next;
00418       sinfo->chain->next = secinfo;
00419     }
00420   else
00421     secinfo->next = secinfo;
00422   sinfo->chain = secinfo;
00423   secinfo->sec = sec;
00424   secinfo->psecinfo = psecinfo;
00425   secinfo->htab = sinfo->htab;
00426   secinfo->first_str = NULL;
00427 
00428   sec->rawsize = sec->size;
00429   if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents,
00430                               0, sec->size))
00431     goto error_return;
00432 
00433   return TRUE;
00434 
00435  error_return:
00436   *psecinfo = NULL;
00437   return FALSE;
00438 }
00439 
00440 /* Record one section into the hash table.  */
00441 static bfd_boolean
00442 record_section (struct sec_merge_info *sinfo,
00443               struct sec_merge_sec_info *secinfo)
00444 {
00445   asection *sec = secinfo->sec;
00446   struct sec_merge_hash_entry *entry;
00447   bfd_boolean nul;
00448   unsigned char *p, *end;
00449   bfd_vma mask, eltalign;
00450   unsigned int align, i;
00451 
00452   align = sec->alignment_power;
00453   end = secinfo->contents + sec->size;
00454   nul = FALSE;
00455   mask = ((bfd_vma) 1 << align) - 1;
00456   if (sec->flags & SEC_STRINGS)
00457     {
00458       for (p = secinfo->contents; p < end; )
00459        {
00460          eltalign = p - secinfo->contents;
00461          eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
00462          if (!eltalign || eltalign > mask)
00463            eltalign = mask + 1;
00464          entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
00465                              secinfo);
00466          if (! entry)
00467            goto error_return;
00468          p += entry->len;
00469          if (sec->entsize == 1)
00470            {
00471              while (p < end && *p == 0)
00472               {
00473                 if (!nul && !((p - secinfo->contents) & mask))
00474                   {
00475                     nul = TRUE;
00476                     entry = sec_merge_add (sinfo->htab, "",
00477                                         (unsigned) mask + 1, secinfo);
00478                     if (! entry)
00479                      goto error_return;
00480                   }
00481                 p++;
00482               }
00483            }
00484          else
00485            {
00486              while (p < end)
00487               {
00488                 for (i = 0; i < sec->entsize; i++)
00489                   if (p[i] != '\0')
00490                     break;
00491                 if (i != sec->entsize)
00492                   break;
00493                 if (!nul && !((p - secinfo->contents) & mask))
00494                   {
00495                     nul = TRUE;
00496                     entry = sec_merge_add (sinfo->htab, (char *) p,
00497                                         (unsigned) mask + 1, secinfo);
00498                     if (! entry)
00499                      goto error_return;
00500                   }
00501                 p += sec->entsize;
00502               }
00503            }
00504        }
00505     }
00506   else
00507     {
00508       for (p = secinfo->contents; p < end; p += sec->entsize)
00509        {
00510          entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
00511          if (! entry)
00512            goto error_return;
00513        }
00514     }
00515 
00516   return TRUE;
00517 
00518 error_return:
00519   for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
00520     *secinfo->psecinfo = NULL;
00521   return FALSE;
00522 }
00523 
00524 static int
00525 strrevcmp (const void *a, const void *b)
00526 {
00527   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
00528   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
00529   unsigned int lenA = A->len;
00530   unsigned int lenB = B->len;
00531   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
00532   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
00533   int l = lenA < lenB ? lenA : lenB;
00534 
00535   while (l)
00536     {
00537       if (*s != *t)
00538        return (int) *s - (int) *t;
00539       s--;
00540       t--;
00541       l--;
00542     }
00543   return lenA - lenB;
00544 }
00545 
00546 /* Like strrevcmp, but for the case where all strings have the same
00547    alignment > entsize.  */
00548 
00549 static int
00550 strrevcmp_align (const void *a, const void *b)
00551 {
00552   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
00553   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
00554   unsigned int lenA = A->len;
00555   unsigned int lenB = B->len;
00556   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
00557   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
00558   int l = lenA < lenB ? lenA : lenB;
00559   int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
00560 
00561   if (tail_align != 0)
00562     return tail_align;
00563 
00564   while (l)
00565     {
00566       if (*s != *t)
00567        return (int) *s - (int) *t;
00568       s--;
00569       t--;
00570       l--;
00571     }
00572   return lenA - lenB;
00573 }
00574 
00575 static inline int
00576 is_suffix (const struct sec_merge_hash_entry *A,
00577           const struct sec_merge_hash_entry *B)
00578 {
00579   if (A->len <= B->len)
00580     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
00581        not to be equal by the hash table.  */
00582     return 0;
00583 
00584   return memcmp (A->root.string + (A->len - B->len),
00585                B->root.string, B->len) == 0;
00586 }
00587 
00588 /* This is a helper function for _bfd_merge_sections.  It attempts to
00589    merge strings matching suffixes of longer strings.  */
00590 static void
00591 merge_strings (struct sec_merge_info *sinfo)
00592 {
00593   struct sec_merge_hash_entry **array, **a, *e;
00594   struct sec_merge_sec_info *secinfo;
00595   bfd_size_type size, amt;
00596   unsigned int alignment = 0;
00597 
00598   /* Now sort the strings */
00599   amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
00600   array = bfd_malloc (amt);
00601   if (array == NULL)
00602     goto alloc_failure;
00603 
00604   for (e = sinfo->htab->first, a = array; e; e = e->next)
00605     if (e->alignment)
00606       {
00607        *a++ = e;
00608        /* Adjust the length to not include the zero terminator.  */
00609        e->len -= sinfo->htab->entsize;
00610        if (alignment != e->alignment)
00611          {
00612            if (alignment == 0)
00613              alignment = e->alignment;
00614            else
00615              alignment = (unsigned) -1;
00616          }
00617       }
00618 
00619   sinfo->htab->size = a - array;
00620   if (sinfo->htab->size != 0)
00621     {
00622       qsort (array, (size_t) sinfo->htab->size,
00623             sizeof (struct sec_merge_hash_entry *),
00624             (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
00625              ? strrevcmp_align : strrevcmp));
00626 
00627       /* Loop over the sorted array and merge suffixes */
00628       e = *--a;
00629       e->len += sinfo->htab->entsize;
00630       while (--a >= array)
00631        {
00632          struct sec_merge_hash_entry *cmp = *a;
00633 
00634          cmp->len += sinfo->htab->entsize;
00635          if (e->alignment >= cmp->alignment
00636              && !((e->len - cmp->len) & (cmp->alignment - 1))
00637              && is_suffix (e, cmp))
00638            {
00639              cmp->u.suffix = e;
00640              cmp->alignment = 0;
00641            }
00642          else
00643            e = cmp;
00644        }
00645     }
00646 
00647 alloc_failure:
00648   if (array)
00649     free (array);
00650 
00651   /* Now assign positions to the strings we want to keep.  */
00652   size = 0;
00653   secinfo = sinfo->htab->first->secinfo;
00654   for (e = sinfo->htab->first; e; e = e->next)
00655     {
00656       if (e->secinfo != secinfo)
00657        {
00658          secinfo->sec->size = size;
00659          secinfo = e->secinfo;
00660        }
00661       if (e->alignment)
00662        {
00663          if (e->secinfo->first_str == NULL)
00664            {
00665              e->secinfo->first_str = e;
00666              size = 0;
00667            }
00668          size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
00669          e->u.index = size;
00670          size += e->len;
00671        }
00672     }
00673   secinfo->sec->size = size;
00674   if (secinfo->sec->alignment_power != 0)
00675     {
00676       bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
00677       secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
00678     }
00679 
00680   /* And now adjust the rest, removing them from the chain (but not hashtable)
00681      at the same time.  */
00682   for (a = &sinfo->htab->first, e = *a; e; e = e->next)
00683     if (e->alignment)
00684       a = &e->next;
00685     else
00686       {
00687        *a = e->next;
00688        if (e->len)
00689          {
00690            e->secinfo = e->u.suffix->secinfo;
00691            e->alignment = e->u.suffix->alignment;
00692            e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
00693          }
00694       }
00695 }
00696 
00697 /* This function is called once after all SEC_MERGE sections are registered
00698    with _bfd_merge_section.  */
00699 
00700 bfd_boolean
00701 _bfd_merge_sections (bfd *abfd ATTRIBUTE_UNUSED,
00702                    struct bfd_link_info *info ATTRIBUTE_UNUSED,
00703                    void *xsinfo,
00704                    void (*remove_hook) (bfd *, asection *))
00705 {
00706   struct sec_merge_info *sinfo;
00707 
00708   for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
00709     {
00710       struct sec_merge_sec_info * secinfo;
00711 
00712       if (! sinfo->chain)
00713        continue;
00714 
00715       /* Move sinfo->chain to head of the chain, terminate it.  */
00716       secinfo = sinfo->chain;
00717       sinfo->chain = secinfo->next;
00718       secinfo->next = NULL;
00719 
00720       /* Record the sections into the hash table.  */
00721       for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
00722        if (secinfo->sec->flags & SEC_EXCLUDE)
00723          {
00724            *secinfo->psecinfo = NULL;
00725            if (remove_hook)
00726              (*remove_hook) (abfd, secinfo->sec);
00727          }
00728        else if (! record_section (sinfo, secinfo))
00729          break;
00730 
00731       if (secinfo)
00732        continue;
00733 
00734       if (sinfo->htab->first == NULL)
00735        continue;
00736 
00737       if (sinfo->htab->strings)
00738        merge_strings (sinfo);
00739       else
00740        {
00741          struct sec_merge_hash_entry *e;
00742          bfd_size_type size = 0;
00743 
00744          /* Things are much simpler for non-strings.
00745             Just assign them slots in the section.  */
00746          secinfo = NULL;
00747          for (e = sinfo->htab->first; e; e = e->next)
00748            {
00749              if (e->secinfo->first_str == NULL)
00750               {
00751                 if (secinfo)
00752                   secinfo->sec->size = size;
00753                 e->secinfo->first_str = e;
00754                 size = 0;
00755               }
00756              size = (size + e->alignment - 1)
00757                    & ~((bfd_vma) e->alignment - 1);
00758              e->u.index = size;
00759              size += e->len;
00760              secinfo = e->secinfo;
00761            }
00762          secinfo->sec->size = size;
00763        }
00764 
00765        /* Finally remove all input sections which have not made it into
00766           the hash table at all.  */
00767        for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
00768          if (secinfo->first_str == NULL)
00769            secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
00770     }
00771 
00772   return TRUE;
00773 }
00774 
00775 /* Write out the merged section.  */
00776 
00777 bfd_boolean
00778 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
00779 {
00780   struct sec_merge_sec_info *secinfo;
00781   file_ptr pos;
00782 
00783   secinfo = (struct sec_merge_sec_info *) psecinfo;
00784 
00785   if (secinfo->first_str == NULL)
00786     return TRUE;
00787 
00788   pos = sec->output_section->filepos + sec->output_offset;
00789   if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
00790     return FALSE;
00791 
00792   if (! sec_merge_emit (output_bfd, secinfo->first_str))
00793     return FALSE;
00794 
00795   return TRUE;
00796 }
00797 
00798 /* Adjust an address in the SEC_MERGE section.  Given OFFSET within
00799    *PSEC, this returns the new offset in the adjusted SEC_MERGE
00800    section and writes the new section back into *PSEC.  */
00801 
00802 bfd_vma
00803 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
00804                          void *psecinfo, bfd_vma offset)
00805 {
00806   struct sec_merge_sec_info *secinfo;
00807   struct sec_merge_hash_entry *entry;
00808   unsigned char *p;
00809   asection *sec = *psec;
00810 
00811   secinfo = (struct sec_merge_sec_info *) psecinfo;
00812 
00813   if (offset >= sec->rawsize)
00814     {
00815       if (offset > sec->rawsize)
00816        {
00817          (*_bfd_error_handler)
00818            (_("%s: access beyond end of merged section (%ld)"),
00819             bfd_get_filename (sec->owner), (long) offset);
00820        }
00821       return secinfo->first_str ? sec->size : 0;
00822     }
00823 
00824   if (secinfo->htab->strings)
00825     {
00826       if (sec->entsize == 1)
00827        {
00828          p = secinfo->contents + offset - 1;
00829          while (p >= secinfo->contents && *p)
00830            --p;
00831          ++p;
00832        }
00833       else
00834        {
00835          p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
00836          p -= sec->entsize;
00837          while (p >= secinfo->contents)
00838            {
00839              unsigned int i;
00840 
00841              for (i = 0; i < sec->entsize; ++i)
00842               if (p[i] != '\0')
00843                 break;
00844              if (i == sec->entsize)
00845               break;
00846              p -= sec->entsize;
00847            }
00848          p += sec->entsize;
00849        }
00850     }
00851   else
00852     {
00853       p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
00854     }
00855   entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
00856   if (!entry)
00857     {
00858       if (! secinfo->htab->strings)
00859        abort ();
00860       /* This should only happen if somebody points into the padding
00861         after a NUL character but before next entity.  */
00862       if (*p)
00863        abort ();
00864       if (! secinfo->htab->first)
00865        abort ();
00866       entry = secinfo->htab->first;
00867       p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
00868           - entry->len);
00869     }
00870 
00871   *psec = entry->secinfo->sec;
00872   return entry->u.index + (secinfo->contents + offset - p);
00873 }