OSDN Git Service

* merge.c (sec_merge_emit): Tidy. Check for bfd_zmalloc errors.
[pf3gnuchains/pf3gnuchains4x.git] / bfd / merge.c
1 /* SEC_MERGE support.
2    Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3    Written by Jakub Jelinek <jakub@redhat.com>.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* This file contains support for merging duplicate entities within sections,
22    as used in ELF SHF_MERGE.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "hashtab.h"
28 #include "libiberty.h"
29
30 struct sec_merge_sec_info;
31
32 /* An entry in the section merge hash table.  */
33
34 struct sec_merge_hash_entry
35 {
36   struct bfd_hash_entry root;
37   /* Length of this entry.  This includes the zero terminator.  */
38   unsigned int len;
39   /* Start of this string needs to be aligned to
40      alignment octets (not 1 << align).  */
41   unsigned int alignment;
42   union
43   {
44     /* Index within the merged section.  */
45     bfd_size_type index;
46     /* Entry this is a suffix of (if alignment is 0).  */
47     struct sec_merge_hash_entry *suffix;
48   } u;
49   /* Which section is it in.  */
50   struct sec_merge_sec_info *secinfo;
51   /* Next entity in the hash table.  */
52   struct sec_merge_hash_entry *next;
53 };
54
55 /* The section merge hash table.  */
56
57 struct sec_merge_hash
58 {
59   struct bfd_hash_table table;
60   /* Next available index.  */
61   bfd_size_type size;
62   /* First entity in the SEC_MERGE sections of this type.  */
63   struct sec_merge_hash_entry *first;
64   /* Last entity in the SEC_MERGE sections of this type.  */
65   struct sec_merge_hash_entry *last;
66   /* Entity size.  */
67   unsigned int entsize;
68   /* Are entries fixed size or zero terminated strings?  */
69   bfd_boolean strings;
70 };
71
72 struct sec_merge_info
73 {
74   /* Chain of sec_merge_infos.  */
75   struct sec_merge_info *next;
76   /* Chain of sec_merge_sec_infos.  */
77   struct sec_merge_sec_info *chain;
78   /* A hash table used to hold section content.  */
79   struct sec_merge_hash *htab;
80 };
81
82 struct sec_merge_sec_info
83 {
84   /* Chain of sec_merge_sec_infos.  */
85   struct sec_merge_sec_info *next;
86   /* The corresponding section.  */
87   asection *sec;
88   /* Pointer to merge_info pointing to us.  */
89   void **psecinfo;
90   /* A hash table used to hold section content.  */
91   struct sec_merge_hash *htab;
92   /* First string in this section.  */
93   struct sec_merge_hash_entry *first_str;
94   /* Original section content.  */
95   unsigned char contents[1];
96 };
97
98
99 /* Routine to create an entry in a section merge hashtab.  */
100
101 static struct bfd_hash_entry *
102 sec_merge_hash_newfunc (struct bfd_hash_entry *entry,
103                         struct bfd_hash_table *table, const char *string)
104 {
105   /* Allocate the structure if it has not already been allocated by a
106      subclass.  */
107   if (entry == NULL)
108     entry = bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry));
109   if (entry == NULL)
110     return NULL;
111
112   /* Call the allocation method of the superclass.  */
113   entry = bfd_hash_newfunc (entry, table, string);
114
115   if (entry != NULL)
116     {
117       /* Initialize the local fields.  */
118       struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
119
120       ret->u.suffix = NULL;
121       ret->alignment = 0;
122       ret->secinfo = NULL;
123       ret->next = NULL;
124     }
125
126   return entry;
127 }
128
129 /* Look up an entry in a section merge hash table.  */
130
131 static struct sec_merge_hash_entry *
132 sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
133                        unsigned int alignment, bfd_boolean create)
134 {
135   register const unsigned char *s;
136   register unsigned long hash;
137   register unsigned int c;
138   struct sec_merge_hash_entry *hashp;
139   unsigned int len, i;
140   unsigned int index;
141
142   hash = 0;
143   len = 0;
144   s = (const unsigned char *) string;
145   if (table->strings)
146     {
147       if (table->entsize == 1)
148         {
149           while ((c = *s++) != '\0')
150             {
151               hash += c + (c << 17);
152               hash ^= hash >> 2;
153               ++len;
154             }
155           hash += len + (len << 17);
156         }
157       else
158         {
159           for (;;)
160             {
161               for (i = 0; i < table->entsize; ++i)
162                 if (s[i] != '\0')
163                   break;
164               if (i == table->entsize)
165                 break;
166               for (i = 0; i < table->entsize; ++i)
167                 {
168                   c = *s++;
169                   hash += c + (c << 17);
170                   hash ^= hash >> 2;
171                 }
172               ++len;
173             }
174           hash += len + (len << 17);
175           len *= table->entsize;
176         }
177       hash ^= hash >> 2;
178       len += table->entsize;
179     }
180   else
181     {
182       for (i = 0; i < table->entsize; ++i)
183         {
184           c = *s++;
185           hash += c + (c << 17);
186           hash ^= hash >> 2;
187         }
188       len = table->entsize;
189     }
190
191   index = hash % table->table.size;
192   for (hashp = (struct sec_merge_hash_entry *) table->table.table[index];
193        hashp != NULL;
194        hashp = (struct sec_merge_hash_entry *) hashp->root.next)
195     {
196       if (hashp->root.hash == hash
197           && len == hashp->len
198           && memcmp (hashp->root.string, string, len) == 0)
199         {
200           /* If the string we found does not have at least the required
201              alignment, we need to insert another copy.  */
202           if (hashp->alignment < alignment)
203             {
204               if (create)
205                 {
206                   /*  Mark the less aligned copy as deleted.  */
207                   hashp->len = 0;
208                   hashp->alignment = 0;
209                 }
210               break;
211             }
212           return hashp;
213         }
214     }
215
216   if (! create)
217     return NULL;
218
219   hashp = ((struct sec_merge_hash_entry *)
220            sec_merge_hash_newfunc (NULL, &table->table, string));
221   if (hashp == NULL)
222     return NULL;
223   hashp->root.string = string;
224   hashp->root.hash = hash;
225   hashp->len = len;
226   hashp->alignment = alignment;
227   hashp->root.next = table->table.table[index];
228   table->table.table[index] = (struct bfd_hash_entry *) hashp;
229
230   return hashp;
231 }
232
233 /* Create a new hash table.  */
234
235 static struct sec_merge_hash *
236 sec_merge_init (unsigned int entsize, bfd_boolean strings)
237 {
238   struct sec_merge_hash *table;
239
240   table = bfd_malloc (sizeof (struct sec_merge_hash));
241   if (table == NULL)
242     return NULL;
243
244   if (! bfd_hash_table_init (&table->table, sec_merge_hash_newfunc))
245     {
246       free (table);
247       return NULL;
248     }
249
250   table->size = 0;
251   table->first = NULL;
252   table->last = NULL;
253   table->entsize = entsize;
254   table->strings = strings;
255
256   return table;
257 }
258
259 /* Get the index of an entity in a hash table, adding it if it is not
260    already present.  */
261
262 static struct sec_merge_hash_entry *
263 sec_merge_add (struct sec_merge_hash *tab, const char *str,
264                unsigned int alignment, struct sec_merge_sec_info *secinfo)
265 {
266   register struct sec_merge_hash_entry *entry;
267
268   entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
269   if (entry == NULL)
270     return NULL;
271
272   if (entry->secinfo == NULL)
273     {
274       tab->size++;
275       entry->secinfo = secinfo;
276       if (tab->first == NULL)
277         tab->first = entry;
278       else
279         tab->last->next = entry;
280       tab->last = entry;
281     }
282
283   return entry;
284 }
285
286 static bfd_boolean
287 sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
288 {
289   struct sec_merge_sec_info *secinfo = entry->secinfo;
290   asection *sec = secinfo->sec;
291   char *pad = NULL;
292   bfd_size_type off = 0;
293   int alignment_power = sec->output_section->alignment_power;
294
295   if (alignment_power)
296     {
297       pad = bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
298       if (pad == NULL)
299         return FALSE;
300     }
301
302   for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
303     {
304       const char *str;
305       bfd_size_type len;
306
307       len = -off & (entry->alignment - 1);
308       if (len != 0)
309         {
310           if (bfd_bwrite (pad, len, abfd) != len)
311             goto err;
312           off += len;
313         }
314
315       str = entry->root.string;
316       len = entry->len;
317
318       if (bfd_bwrite (str, len, abfd) != len)
319         goto err;
320
321       off += len;
322     }
323
324   /* Trailing alignment needed?  */
325   off = sec->size - off;
326   if (off != 0
327       && bfd_bwrite (pad, off, abfd) != off)
328     goto err;
329
330   if (pad != NULL)
331     free (pad);
332   return TRUE;
333
334  err:
335   if (pad != NULL)
336     free (pad);
337   return FALSE;
338 }
339
340 /* Register a SEC_MERGE section as a candidate for merging.
341    This function is called for all non-dynamic SEC_MERGE input sections.  */
342
343 bfd_boolean
344 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
345                         void **psecinfo)
346 {
347   struct sec_merge_info *sinfo;
348   struct sec_merge_sec_info *secinfo;
349   unsigned int align;
350   bfd_size_type amt;
351
352   if ((abfd->flags & DYNAMIC) != 0
353       || (sec->flags & SEC_MERGE) == 0)
354     abort ();
355
356   if (sec->size == 0
357       || (sec->flags & SEC_EXCLUDE) != 0
358       || sec->entsize == 0)
359     return TRUE;
360
361   if ((sec->flags & SEC_RELOC) != 0)
362     {
363       /* We aren't prepared to handle relocations in merged sections.  */
364       return TRUE;
365     }
366
367   align = sec->alignment_power;
368   if ((sec->entsize < (unsigned) 1 << align
369        && ((sec->entsize & (sec->entsize - 1))
370            || !(sec->flags & SEC_STRINGS)))
371       || (sec->entsize > (unsigned) 1 << align
372           && (sec->entsize & (((unsigned) 1 << align) - 1))))
373     {
374       /* Sanity check.  If string character size is smaller than
375          alignment, then we require character size to be a power
376          of 2, otherwise character size must be integer multiple
377          of alignment.  For non-string constants, alignment must
378          be smaller than or equal to entity size and entity size
379          must be integer multiple of alignment.  */
380       return TRUE;
381     }
382
383   for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
384     if ((secinfo = sinfo->chain)
385         && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
386         && secinfo->sec->entsize == sec->entsize
387         && secinfo->sec->alignment_power == sec->alignment_power
388         && secinfo->sec->output_section == sec->output_section)
389       break;
390
391   if (sinfo == NULL)
392     {
393       /* Initialize the information we need to keep track of.  */
394       sinfo = bfd_alloc (abfd, sizeof (struct sec_merge_info));
395       if (sinfo == NULL)
396         goto error_return;
397       sinfo->next = (struct sec_merge_info *) *psinfo;
398       sinfo->chain = NULL;
399       *psinfo = sinfo;
400       sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
401       if (sinfo->htab == NULL)
402         goto error_return;
403     }
404
405   /* Read the section from abfd.  */
406
407   amt = sizeof (struct sec_merge_sec_info) + sec->size - 1;
408   *psecinfo = bfd_alloc (abfd, amt);
409   if (*psecinfo == NULL)
410     goto error_return;
411
412   secinfo = (struct sec_merge_sec_info *) *psecinfo;
413   if (sinfo->chain)
414     {
415       secinfo->next = sinfo->chain->next;
416       sinfo->chain->next = secinfo;
417     }
418   else
419     secinfo->next = secinfo;
420   sinfo->chain = secinfo;
421   secinfo->sec = sec;
422   secinfo->psecinfo = psecinfo;
423   secinfo->htab = sinfo->htab;
424   secinfo->first_str = NULL;
425
426   sec->rawsize = sec->size;
427   if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents,
428                                   0, sec->size))
429     goto error_return;
430
431   return TRUE;
432
433  error_return:
434   *psecinfo = NULL;
435   return FALSE;
436 }
437
438 /* Record one section into the hash table.  */
439 static bfd_boolean
440 record_section (struct sec_merge_info *sinfo,
441                 struct sec_merge_sec_info *secinfo)
442 {
443   asection *sec = secinfo->sec;
444   struct sec_merge_hash_entry *entry;
445   bfd_boolean nul;
446   unsigned char *p, *end;
447   bfd_vma mask, eltalign;
448   unsigned int align, i;
449
450   align = sec->alignment_power;
451   end = secinfo->contents + sec->size;
452   nul = FALSE;
453   mask = ((bfd_vma) 1 << align) - 1;
454   if (sec->flags & SEC_STRINGS)
455     {
456       for (p = secinfo->contents; p < end; )
457         {
458           eltalign = p - secinfo->contents;
459           eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
460           if (!eltalign || eltalign > mask)
461             eltalign = mask + 1;
462           entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
463                                  secinfo);
464           if (! entry)
465             goto error_return;
466           p += entry->len;
467           if (sec->entsize == 1)
468             {
469               while (p < end && *p == 0)
470                 {
471                   if (!nul && !((p - secinfo->contents) & mask))
472                     {
473                       nul = TRUE;
474                       entry = sec_merge_add (sinfo->htab, "",
475                                              (unsigned) mask + 1, secinfo);
476                       if (! entry)
477                         goto error_return;
478                     }
479                   p++;
480                 }
481             }
482           else
483             {
484               while (p < end)
485                 {
486                   for (i = 0; i < sec->entsize; i++)
487                     if (p[i] != '\0')
488                       break;
489                   if (i != sec->entsize)
490                     break;
491                   if (!nul && !((p - secinfo->contents) & mask))
492                     {
493                       nul = TRUE;
494                       entry = sec_merge_add (sinfo->htab, (char *) p,
495                                              (unsigned) mask + 1, secinfo);
496                       if (! entry)
497                         goto error_return;
498                     }
499                   p += sec->entsize;
500                 }
501             }
502         }
503     }
504   else
505     {
506       for (p = secinfo->contents; p < end; p += sec->entsize)
507         {
508           entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
509           if (! entry)
510             goto error_return;
511         }
512     }
513
514   return TRUE;
515
516 error_return:
517   for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
518     *secinfo->psecinfo = NULL;
519   return FALSE;
520 }
521
522 static int
523 strrevcmp (const void *a, const void *b)
524 {
525   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
526   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
527   unsigned int lenA = A->len;
528   unsigned int lenB = B->len;
529   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
530   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
531   int l = lenA < lenB ? lenA : lenB;
532
533   while (l)
534     {
535       if (*s != *t)
536         return (int) *s - (int) *t;
537       s--;
538       t--;
539       l--;
540     }
541   return lenA - lenB;
542 }
543
544 /* Like strrevcmp, but for the case where all strings have the same
545    alignment > entsize.  */
546
547 static int
548 strrevcmp_align (const void *a, const void *b)
549 {
550   struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
551   struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
552   unsigned int lenA = A->len;
553   unsigned int lenB = B->len;
554   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
555   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
556   int l = lenA < lenB ? lenA : lenB;
557   int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
558
559   if (tail_align != 0)
560     return tail_align;
561
562   while (l)
563     {
564       if (*s != *t)
565         return (int) *s - (int) *t;
566       s--;
567       t--;
568       l--;
569     }
570   return lenA - lenB;
571 }
572
573 static inline int
574 is_suffix (const struct sec_merge_hash_entry *A,
575            const struct sec_merge_hash_entry *B)
576 {
577   if (A->len <= B->len)
578     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
579        not to be equal by the hash table.  */
580     return 0;
581
582   return memcmp (A->root.string + (A->len - B->len),
583                  B->root.string, B->len) == 0;
584 }
585
586 /* This is a helper function for _bfd_merge_sections.  It attempts to
587    merge strings matching suffixes of longer strings.  */
588 static void
589 merge_strings (struct sec_merge_info *sinfo)
590 {
591   struct sec_merge_hash_entry **array, **a, *e;
592   struct sec_merge_sec_info *secinfo;
593   bfd_size_type size, amt;
594   unsigned int alignment = 0;
595
596   /* Now sort the strings */
597   amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
598   array = bfd_malloc (amt);
599   if (array == NULL)
600     goto alloc_failure;
601
602   for (e = sinfo->htab->first, a = array; e; e = e->next)
603     if (e->alignment)
604       {
605         *a++ = e;
606         /* Adjust the length to not include the zero terminator.  */
607         e->len -= sinfo->htab->entsize;
608         if (alignment != e->alignment)
609           {
610             if (alignment == 0)
611               alignment = e->alignment;
612             else
613               alignment = (unsigned) -1;
614           }
615       }
616
617   sinfo->htab->size = a - array;
618   if (sinfo->htab->size != 0)
619     {
620       qsort (array, (size_t) sinfo->htab->size,
621              sizeof (struct sec_merge_hash_entry *),
622              (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
623               ? strrevcmp_align : strrevcmp));
624
625       /* Loop over the sorted array and merge suffixes */
626       e = *--a;
627       e->len += sinfo->htab->entsize;
628       while (--a >= array)
629         {
630           struct sec_merge_hash_entry *cmp = *a;
631
632           cmp->len += sinfo->htab->entsize;
633           if (e->alignment >= cmp->alignment
634               && !((e->len - cmp->len) & (cmp->alignment - 1))
635               && is_suffix (e, cmp))
636             {
637               cmp->u.suffix = e;
638               cmp->alignment = 0;
639             }
640           else
641             e = cmp;
642         }
643     }
644
645 alloc_failure:
646   if (array)
647     free (array);
648
649   /* Now assign positions to the strings we want to keep.  */
650   size = 0;
651   secinfo = sinfo->htab->first->secinfo;
652   for (e = sinfo->htab->first; e; e = e->next)
653     {
654       if (e->secinfo != secinfo)
655         {
656           secinfo->sec->size = size;
657           secinfo = e->secinfo;
658         }
659       if (e->alignment)
660         {
661           if (e->secinfo->first_str == NULL)
662             {
663               e->secinfo->first_str = e;
664               size = 0;
665             }
666           size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
667           e->u.index = size;
668           size += e->len;
669         }
670     }
671   secinfo->sec->size = size;
672   if (secinfo->sec->alignment_power != 0)
673     {
674       bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
675       secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
676     }
677
678   /* And now adjust the rest, removing them from the chain (but not hashtable)
679      at the same time.  */
680   for (a = &sinfo->htab->first, e = *a; e; e = e->next)
681     if (e->alignment)
682       a = &e->next;
683     else
684       {
685         *a = e->next;
686         if (e->len)
687           {
688             e->secinfo = e->u.suffix->secinfo;
689             e->alignment = e->u.suffix->alignment;
690             e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
691           }
692       }
693 }
694
695 /* This function is called once after all SEC_MERGE sections are registered
696    with _bfd_merge_section.  */
697
698 bfd_boolean
699 _bfd_merge_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info,
700                      void *xsinfo, void (*remove_hook) (bfd *, asection *))
701 {
702   struct sec_merge_info *sinfo;
703
704   for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
705     {
706       struct sec_merge_sec_info * secinfo;
707
708       if (! sinfo->chain)
709         continue;
710
711       /* Move sinfo->chain to head of the chain, terminate it.  */
712       secinfo = sinfo->chain;
713       sinfo->chain = secinfo->next;
714       secinfo->next = NULL;
715
716       /* Record the sections into the hash table.  */
717       for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
718         if (secinfo->sec->flags & SEC_EXCLUDE)
719           {
720             *secinfo->psecinfo = NULL;
721             if (remove_hook)
722               (*remove_hook) (abfd, secinfo->sec);
723           }
724         else if (! record_section (sinfo, secinfo))
725           break;
726
727       if (secinfo)
728         continue;
729
730       if (sinfo->htab->first == NULL)
731         continue;
732
733       if (sinfo->htab->strings)
734         merge_strings (sinfo);
735       else
736         {
737           struct sec_merge_hash_entry *e;
738           bfd_size_type size = 0;
739
740           /* Things are much simpler for non-strings.
741              Just assign them slots in the section.  */
742           secinfo = NULL;
743           for (e = sinfo->htab->first; e; e = e->next)
744             {
745               if (e->secinfo->first_str == NULL)
746                 {
747                   if (secinfo)
748                     secinfo->sec->size = size;
749                   e->secinfo->first_str = e;
750                   size = 0;
751                 }
752               size = (size + e->alignment - 1)
753                      & ~((bfd_vma) e->alignment - 1);
754               e->u.index = size;
755               size += e->len;
756               secinfo = e->secinfo;
757             }
758           secinfo->sec->size = size;
759         }
760
761         /* Finally remove all input sections which have not made it into
762            the hash table at all.  */
763         for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
764           if (secinfo->first_str == NULL)
765             _bfd_strip_section_from_output (info, secinfo->sec);
766     }
767
768   return TRUE;
769 }
770
771 /* Write out the merged section.  */
772
773 bfd_boolean
774 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
775 {
776   struct sec_merge_sec_info *secinfo;
777   file_ptr pos;
778
779   secinfo = (struct sec_merge_sec_info *) psecinfo;
780
781   if (secinfo->first_str == NULL)
782     return TRUE;
783
784   pos = sec->output_section->filepos + sec->output_offset;
785   if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
786     return FALSE;
787
788   if (! sec_merge_emit (output_bfd, secinfo->first_str))
789     return FALSE;
790
791   return TRUE;
792 }
793
794 /* Adjust an address in the SEC_MERGE section.  Given OFFSET within
795    *PSEC, this returns the new offset in the adjusted SEC_MERGE
796    section and writes the new section back into *PSEC.  */
797
798 bfd_vma
799 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
800                             void *psecinfo, bfd_vma offset)
801 {
802   struct sec_merge_sec_info *secinfo;
803   struct sec_merge_hash_entry *entry;
804   unsigned char *p;
805   asection *sec = *psec;
806
807   secinfo = (struct sec_merge_sec_info *) psecinfo;
808
809   if (offset >= sec->rawsize)
810     {
811       if (offset > sec->rawsize)
812         {
813           (*_bfd_error_handler)
814             (_("%s: access beyond end of merged section (%ld)"),
815              bfd_get_filename (sec->owner), (long) offset);
816         }
817       return secinfo->first_str ? sec->size : 0;
818     }
819
820   if (secinfo->htab->strings)
821     {
822       if (sec->entsize == 1)
823         {
824           p = secinfo->contents + offset - 1;
825           while (p >= secinfo->contents && *p)
826             --p;
827           ++p;
828         }
829       else
830         {
831           p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
832           p -= sec->entsize;
833           while (p >= secinfo->contents)
834             {
835               unsigned int i;
836
837               for (i = 0; i < sec->entsize; ++i)
838                 if (p[i] != '\0')
839                   break;
840               if (i == sec->entsize)
841                 break;
842               p -= sec->entsize;
843             }
844           p += sec->entsize;
845         }
846     }
847   else
848     {
849       p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
850     }
851   entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
852   if (!entry)
853     {
854       if (! secinfo->htab->strings)
855         abort ();
856       /* This should only happen if somebody points into the padding
857          after a NUL character but before next entity.  */
858       if (*p)
859         abort ();
860       if (! secinfo->htab->first)
861         abort ();
862       entry = secinfo->htab->first;
863       p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
864            - entry->len);
865     }
866
867   *psec = entry->secinfo->sec;
868   return entry->u.index + (secinfo->contents + offset - p);
869 }