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