* tc-mips.c (RELAX_BRANCH_ENCODE): Remove reloc_s2 argument.
[binutils.git] / bfd / merge.c
blobec75a7be7e9dec27cafb91e9004d87dfd399b91c
1 /* SEC_MERGE support.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 /* This file contains support for merging duplicate entities within sections,
22 as used in ELF SHF_MERGE. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "hashtab.h"
28 #include "libiberty.h"
30 struct sec_merge_sec_info;
32 /* An entry in the section merge hash table. */
34 struct sec_merge_hash_entry
36 struct bfd_hash_entry root;
37 /* Length of this entry. */
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
44 /* Index within the merged section. */
45 bfd_size_type index;
46 /* Entity size (if present in suffix hash tables). */
47 unsigned int entsize;
48 /* Entry this is a suffix of (if alignment is 0). */
49 struct sec_merge_hash_entry *suffix;
50 } u;
51 /* Which section is it in. */
52 struct sec_merge_sec_info *secinfo;
53 /* Next entity in the hash table. */
54 struct sec_merge_hash_entry *next;
57 /* The section merge hash table. */
59 struct sec_merge_hash
61 struct bfd_hash_table table;
62 /* Next available index. */
63 bfd_size_type size;
64 /* First entity in the SEC_MERGE sections of this type. */
65 struct sec_merge_hash_entry *first;
66 /* Last entity in the SEC_MERGE sections of this type. */
67 struct sec_merge_hash_entry *last;
68 /* Entity size. */
69 unsigned int entsize;
70 /* Are entries fixed size or zero terminated strings? */
71 bfd_boolean strings;
74 struct sec_merge_info
76 /* Chain of sec_merge_infos. */
77 struct sec_merge_info *next;
78 /* Chain of sec_merge_sec_infos. */
79 struct sec_merge_sec_info *chain;
80 /* A hash table used to hold section content. */
81 struct sec_merge_hash *htab;
84 struct sec_merge_sec_info
86 /* Chain of sec_merge_sec_infos. */
87 struct sec_merge_sec_info *next;
88 /* The corresponding section. */
89 asection *sec;
90 /* Pointer to merge_info pointing to us. */
91 PTR *psecinfo;
92 /* A hash table used to hold section content. */
93 struct sec_merge_hash *htab;
94 /* First string in this section. */
95 struct sec_merge_hash_entry *first;
96 /* Original section content. */
97 unsigned char contents[1];
100 static struct bfd_hash_entry *sec_merge_hash_newfunc
101 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
102 static struct sec_merge_hash_entry *sec_merge_hash_lookup
103 PARAMS ((struct sec_merge_hash *, const char *, unsigned int, bfd_boolean));
104 static struct sec_merge_hash *sec_merge_init
105 PARAMS ((unsigned int, bfd_boolean));
106 static struct sec_merge_hash_entry *sec_merge_add
107 PARAMS ((struct sec_merge_hash *, const char *, unsigned int,
108 struct sec_merge_sec_info *));
109 static bfd_boolean sec_merge_emit
110 PARAMS ((bfd *, struct sec_merge_hash_entry *));
111 static int cmplengthentry
112 PARAMS ((const PTR, const PTR));
113 static int last4_eq
114 PARAMS ((const PTR, const PTR));
115 static int last_eq
116 PARAMS ((const PTR, const PTR));
117 static bfd_boolean record_section
118 PARAMS ((struct sec_merge_info *, struct sec_merge_sec_info *));
119 static void merge_strings
120 PARAMS ((struct sec_merge_info *));
122 /* Routine to create an entry in a section merge hashtab. */
124 static struct bfd_hash_entry *
125 sec_merge_hash_newfunc (entry, table, string)
126 struct bfd_hash_entry *entry;
127 struct bfd_hash_table *table;
128 const char *string;
130 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
132 /* Allocate the structure if it has not already been allocated by a
133 subclass. */
134 if (ret == (struct sec_merge_hash_entry *) NULL)
135 ret = ((struct sec_merge_hash_entry *)
136 bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry)));
137 if (ret == (struct sec_merge_hash_entry *) NULL)
138 return NULL;
140 /* Call the allocation method of the superclass. */
141 ret = ((struct sec_merge_hash_entry *)
142 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
144 if (ret)
146 /* Initialize the local fields. */
147 ret->u.suffix = NULL;
148 ret->alignment = 0;
149 ret->secinfo = NULL;
150 ret->next = NULL;
153 return (struct bfd_hash_entry *) ret;
156 /* Look up an entry in a section merge hash table. */
158 static struct sec_merge_hash_entry *
159 sec_merge_hash_lookup (table, string, alignment, create)
160 struct sec_merge_hash *table;
161 const char *string;
162 unsigned int alignment;
163 bfd_boolean create;
165 register const unsigned char *s;
166 register unsigned long hash;
167 register unsigned int c;
168 struct sec_merge_hash_entry *hashp;
169 unsigned int len, i;
170 unsigned int index;
172 hash = 0;
173 len = 0;
174 s = (const unsigned char *) string;
175 if (table->strings)
177 if (table->entsize == 1)
179 while ((c = *s++) != '\0')
181 hash += c + (c << 17);
182 hash ^= hash >> 2;
183 ++len;
185 hash += len + (len << 17);
187 else
189 for (;;)
191 for (i = 0; i < table->entsize; ++i)
192 if (s[i] != '\0')
193 break;
194 if (i == table->entsize)
195 break;
196 for (i = 0; i < table->entsize; ++i)
198 c = *s++;
199 hash += c + (c << 17);
200 hash ^= hash >> 2;
202 ++len;
204 hash += len + (len << 17);
205 len *= table->entsize;
207 hash ^= hash >> 2;
208 len += table->entsize;
210 else
212 for (i = 0; i < table->entsize; ++i)
214 c = *s++;
215 hash += c + (c << 17);
216 hash ^= hash >> 2;
218 len = table->entsize;
221 index = hash % table->table.size;
222 for (hashp = (struct sec_merge_hash_entry *) table->table.table[index];
223 hashp != (struct sec_merge_hash_entry *) NULL;
224 hashp = (struct sec_merge_hash_entry *) hashp->root.next)
226 if (hashp->root.hash == hash
227 && len == hashp->len
228 && memcmp (hashp->root.string, string, len) == 0)
230 /* If the string we found does not have at least the required
231 alignment, we need to insert another copy. */
232 if (hashp->alignment < alignment)
234 /* Mark the less aligned copy as deleted. */
235 hashp->len = 0;
236 hashp->alignment = 0;
237 break;
239 return hashp;
243 if (! create)
244 return (struct sec_merge_hash_entry *) NULL;
246 hashp = (struct sec_merge_hash_entry *)
247 sec_merge_hash_newfunc ((struct bfd_hash_entry *) NULL,
248 (struct bfd_hash_table *) table, string);
249 if (hashp == (struct sec_merge_hash_entry *) NULL)
250 return (struct sec_merge_hash_entry *) NULL;
251 hashp->root.string = string;
252 hashp->root.hash = hash;
253 hashp->len = len;
254 hashp->alignment = alignment;
255 hashp->root.next = table->table.table[index];
256 table->table.table[index] = (struct bfd_hash_entry *) hashp;
258 return hashp;
261 /* Create a new hash table. */
263 static struct sec_merge_hash *
264 sec_merge_init (entsize, strings)
265 unsigned int entsize;
266 bfd_boolean strings;
268 struct sec_merge_hash *table;
269 bfd_size_type amt = sizeof (struct sec_merge_hash);
271 table = (struct sec_merge_hash *) bfd_malloc (amt);
272 if (table == NULL)
273 return NULL;
275 if (! bfd_hash_table_init (&table->table, sec_merge_hash_newfunc))
277 free (table);
278 return NULL;
281 table->size = 0;
282 table->first = NULL;
283 table->last = NULL;
284 table->entsize = entsize;
285 table->strings = strings;
287 return table;
290 /* Get the index of an entity in a hash table, adding it if it is not
291 already present. */
293 static struct sec_merge_hash_entry *
294 sec_merge_add (tab, str, alignment, secinfo)
295 struct sec_merge_hash *tab;
296 const char *str;
297 unsigned int alignment;
298 struct sec_merge_sec_info *secinfo;
300 register struct sec_merge_hash_entry *entry;
302 entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
303 if (entry == NULL)
304 return NULL;
306 if (entry->secinfo == NULL)
308 tab->size++;
309 entry->secinfo = secinfo;
310 if (tab->first == NULL)
311 tab->first = entry;
312 else
313 tab->last->next = entry;
314 tab->last = entry;
317 return entry;
320 static bfd_boolean
321 sec_merge_emit (abfd, entry)
322 register bfd *abfd;
323 struct sec_merge_hash_entry *entry;
325 struct sec_merge_sec_info *secinfo = entry->secinfo;
326 asection *sec = secinfo->sec;
327 char *pad = "";
328 bfd_size_type off = 0;
329 int alignment_power = bfd_get_section_alignment (abfd, sec->output_section);
331 if (alignment_power)
332 pad = bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
334 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
336 register const char *str;
337 register size_t len;
339 len = off & (entry->alignment - 1);
340 if (len)
342 len = entry->alignment - len;
343 if (bfd_bwrite ((PTR) pad, (bfd_size_type) len, abfd) != len)
344 break;
345 off += len;
348 str = entry->root.string;
349 len = entry->len;
351 if (bfd_bwrite ((PTR) str, (bfd_size_type) len, abfd) != len)
352 break;
354 off += len;
357 if (alignment_power)
358 free (pad);
360 return entry == NULL || entry->secinfo != secinfo;
363 /* This function is called for each input file from the add_symbols
364 pass of the linker. */
366 bfd_boolean
367 _bfd_merge_section (abfd, psinfo, sec, psecinfo)
368 bfd *abfd;
369 PTR *psinfo;
370 asection *sec;
371 PTR *psecinfo;
373 struct sec_merge_info *sinfo;
374 struct sec_merge_sec_info *secinfo;
375 unsigned int align;
376 bfd_size_type amt;
378 if (sec->_raw_size == 0
379 || (sec->flags & SEC_EXCLUDE)
380 || (sec->flags & SEC_MERGE) == 0
381 || sec->entsize == 0)
382 return TRUE;
384 if ((sec->flags & SEC_RELOC) != 0)
386 /* We aren't prepared to handle relocations in merged sections. */
387 return TRUE;
390 align = bfd_get_section_alignment (sec->owner, sec);
391 if ((sec->entsize < (unsigned int)(1 << align)
392 && ((sec->entsize & (sec->entsize - 1))
393 || !(sec->flags & SEC_STRINGS)))
394 || (sec->entsize > (unsigned int)(1 << align)
395 && (sec->entsize & ((1 << align) - 1))))
397 /* Sanity check. If string character size is smaller than
398 alignment, then we require character size to be a power
399 of 2, otherwise character size must be integer multiple
400 of alignment. For non-string constants, alignment must
401 be smaller than or equal to entity size and entity size
402 must be integer multiple of alignment. */
403 return TRUE;
406 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
407 if ((secinfo = sinfo->chain)
408 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
409 && secinfo->sec->entsize == sec->entsize
410 && ! strcmp (secinfo->sec->name, sec->name))
411 break;
413 if (sinfo == NULL)
415 /* Initialize the information we need to keep track of. */
416 amt = sizeof (struct sec_merge_info);
417 sinfo = (struct sec_merge_info *) bfd_alloc (abfd, amt);
418 if (sinfo == NULL)
419 goto error_return;
420 sinfo->next = (struct sec_merge_info *) *psinfo;
421 sinfo->chain = NULL;
422 *psinfo = (PTR) sinfo;
423 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
424 if (sinfo->htab == NULL)
425 goto error_return;
428 /* Read the section from abfd. */
430 amt = sizeof (struct sec_merge_sec_info) + sec->_raw_size - 1;
431 *psecinfo = bfd_alloc (abfd, amt);
432 if (*psecinfo == NULL)
433 goto error_return;
435 secinfo = (struct sec_merge_sec_info *)*psecinfo;
436 if (sinfo->chain)
438 secinfo->next = sinfo->chain->next;
439 sinfo->chain->next = secinfo;
441 else
442 secinfo->next = secinfo;
443 sinfo->chain = secinfo;
444 secinfo->sec = sec;
445 secinfo->psecinfo = psecinfo;
446 secinfo->htab = sinfo->htab;
447 secinfo->first = NULL;
449 if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents,
450 (bfd_vma) 0, sec->_raw_size))
451 goto error_return;
453 return TRUE;
455 error_return:
456 *psecinfo = NULL;
457 return FALSE;
460 /* Compare two sec_merge_hash_entry structures. This is called via qsort. */
462 static int
463 cmplengthentry (a, b)
464 const PTR a;
465 const PTR b;
467 struct sec_merge_hash_entry * A = *(struct sec_merge_hash_entry **) a;
468 struct sec_merge_hash_entry * B = *(struct sec_merge_hash_entry **) b;
470 if (A->len < B->len)
471 return 1;
472 else if (A->len > B->len)
473 return -1;
475 return memcmp (A->root.string, B->root.string, A->len);
478 static int
479 last4_eq (a, b)
480 const PTR a;
481 const PTR b;
483 struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
484 struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
486 if (memcmp (A->root.string + A->len - 5 * A->u.entsize,
487 B->root.string + B->len - 5 * A->u.entsize,
488 4 * A->u.entsize) != 0)
489 /* This was a hashtable collision. */
490 return 0;
492 if (A->len <= B->len)
493 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
494 not to be equal by the hash table. */
495 return 0;
497 if (A->alignment < B->alignment
498 || ((A->len - B->len) & (B->alignment - 1)))
499 /* The suffix is not sufficiently aligned. */
500 return 0;
502 return memcmp (A->root.string + (A->len - B->len),
503 B->root.string, B->len - 5 * A->u.entsize) == 0;
506 static int
507 last_eq (a, b)
508 const PTR a;
509 const PTR b;
511 struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
512 struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
514 if (B->len >= 5 * A->u.entsize)
515 /* Longer strings are just pushed into the hash table,
516 they'll be used when looking up for very short strings. */
517 return 0;
519 if (memcmp (A->root.string + A->len - 2 * A->u.entsize,
520 B->root.string + B->len - 2 * A->u.entsize,
521 A->u.entsize) != 0)
522 /* This was a hashtable collision. */
523 return 0;
525 if (A->len <= B->len)
526 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
527 not to be equal by the hash table. */
528 return 0;
530 if (A->alignment < B->alignment
531 || ((A->len - B->len) & (B->alignment - 1)))
532 /* The suffix is not sufficiently aligned. */
533 return 0;
535 return memcmp (A->root.string + (A->len - B->len),
536 B->root.string, B->len - 2 * A->u.entsize) == 0;
539 /* Record one section into the hash table. */
540 static bfd_boolean
541 record_section (sinfo, secinfo)
542 struct sec_merge_info *sinfo;
543 struct sec_merge_sec_info *secinfo;
545 asection *sec = secinfo->sec;
546 struct sec_merge_hash_entry *entry;
547 bfd_boolean nul;
548 unsigned char *p, *end;
549 bfd_vma mask, eltalign;
550 unsigned int align, i;
552 align = bfd_get_section_alignment (sec->owner, sec);
553 end = secinfo->contents + sec->_raw_size;
554 nul = FALSE;
555 mask = ((bfd_vma) 1 << align) - 1;
556 if (sec->flags & SEC_STRINGS)
558 for (p = secinfo->contents; p < end; )
560 eltalign = p - secinfo->contents;
561 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
562 if (!eltalign || eltalign > mask)
563 eltalign = mask + 1;
564 entry = sec_merge_add (sinfo->htab, p, (unsigned) eltalign, secinfo);
565 if (! entry)
566 goto error_return;
567 p += entry->len;
568 if (sec->entsize == 1)
570 while (p < end && *p == 0)
572 if (!nul && !((p - secinfo->contents) & mask))
574 nul = TRUE;
575 entry = sec_merge_add (sinfo->htab, "",
576 (unsigned) mask + 1, secinfo);
577 if (! entry)
578 goto error_return;
580 p++;
583 else
585 while (p < end)
587 for (i = 0; i < sec->entsize; i++)
588 if (p[i] != '\0')
589 break;
590 if (i != sec->entsize)
591 break;
592 if (!nul && !((p - secinfo->contents) & mask))
594 nul = TRUE;
595 entry = sec_merge_add (sinfo->htab, p,
596 (unsigned) mask + 1, secinfo);
597 if (! entry)
598 goto error_return;
600 p += sec->entsize;
605 else
607 for (p = secinfo->contents; p < end; p += sec->entsize)
609 entry = sec_merge_add (sinfo->htab, p, 1, secinfo);
610 if (! entry)
611 goto error_return;
615 return TRUE;
617 error_return:
618 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
619 *secinfo->psecinfo = NULL;
620 return FALSE;
623 /* This is a helper function for _bfd_merge_sections. It attempts to
624 merge strings matching suffixes of longer strings. */
625 static void
626 merge_strings (sinfo)
627 struct sec_merge_info *sinfo;
629 struct sec_merge_hash_entry **array, **a, **end, *e;
630 struct sec_merge_sec_info *secinfo;
631 htab_t lasttab = NULL, last4tab = NULL;
632 bfd_size_type size, amt;
634 /* Now sort the strings by length, longest first. */
635 array = NULL;
636 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
637 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
638 if (array == NULL)
639 goto alloc_failure;
641 for (e = sinfo->htab->first, a = array; e; e = e->next)
642 if (e->alignment)
643 *a++ = e;
645 sinfo->htab->size = a - array;
647 qsort (array, (size_t) sinfo->htab->size,
648 sizeof (struct sec_merge_hash_entry *), cmplengthentry);
650 last4tab = htab_create_alloc ((size_t) sinfo->htab->size * 4,
651 NULL, last4_eq, NULL, calloc, free);
652 lasttab = htab_create_alloc ((size_t) sinfo->htab->size * 4,
653 NULL, last_eq, NULL, calloc, free);
654 if (lasttab == NULL || last4tab == NULL)
655 goto alloc_failure;
657 /* Now insert the strings into hash tables (strings with last 4 characters
658 and strings with last character equal), look for longer strings which
659 we're suffix of. */
660 for (a = array, end = array + sinfo->htab->size; a < end; a++)
662 register hashval_t hash;
663 unsigned int c;
664 unsigned int i;
665 const unsigned char *s;
666 PTR *p;
668 e = *a;
669 e->u.entsize = sinfo->htab->entsize;
670 if (e->len <= e->u.entsize)
671 break;
672 if (e->len > 4 * e->u.entsize)
674 s = (const unsigned char *) (e->root.string + e->len - e->u.entsize);
675 hash = 0;
676 for (i = 0; i < 4 * e->u.entsize; i++)
678 c = *--s;
679 hash += c + (c << 17);
680 hash ^= hash >> 2;
682 p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
683 if (p == NULL)
684 goto alloc_failure;
685 if (*p)
687 struct sec_merge_hash_entry *ent;
689 ent = (struct sec_merge_hash_entry *) *p;
690 e->u.suffix = ent;
691 e->alignment = 0;
692 continue;
694 else
695 *p = (PTR) e;
697 s = (const unsigned char *) (e->root.string + e->len - e->u.entsize);
698 hash = 0;
699 for (i = 0; i < e->u.entsize; i++)
701 c = *--s;
702 hash += c + (c << 17);
703 hash ^= hash >> 2;
705 p = htab_find_slot_with_hash (lasttab, e, hash, INSERT);
706 if (p == NULL)
707 goto alloc_failure;
708 if (*p)
710 struct sec_merge_hash_entry *ent;
712 ent = (struct sec_merge_hash_entry *) *p;
713 e->u.suffix = ent;
714 e->alignment = 0;
716 else
717 *p = (PTR) e;
720 alloc_failure:
721 if (array)
722 free (array);
723 if (lasttab)
724 htab_delete (lasttab);
725 if (last4tab)
726 htab_delete (last4tab);
728 /* Now assign positions to the strings we want to keep. */
729 size = 0;
730 secinfo = sinfo->htab->first->secinfo;
731 for (e = sinfo->htab->first; e; e = e->next)
733 if (e->secinfo != secinfo)
735 secinfo->sec->_cooked_size = size;
736 secinfo = e->secinfo;
738 if (e->alignment)
740 if (e->secinfo->first == NULL)
742 e->secinfo->first = e;
743 size = 0;
745 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
746 e->u.index = size;
747 size += e->len;
750 secinfo->sec->_cooked_size = size;
752 /* And now adjust the rest, removing them from the chain (but not hashtable)
753 at the same time. */
754 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
755 if (e->alignment)
756 a = &e->next;
757 else
759 *a = e->next;
760 if (e->len)
762 e->secinfo = e->u.suffix->secinfo;
763 e->alignment = e->u.suffix->alignment;
764 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
769 /* This function is called once after all SEC_MERGE sections are registered
770 with _bfd_merge_section. */
772 bfd_boolean
773 _bfd_merge_sections (abfd, xsinfo, remove_hook)
774 bfd *abfd ATTRIBUTE_UNUSED;
775 PTR xsinfo;
776 void (*remove_hook) PARAMS((bfd *, asection *));
778 struct sec_merge_info *sinfo;
780 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
782 struct sec_merge_sec_info * secinfo;
784 if (! sinfo->chain)
785 continue;
787 /* Move sinfo->chain to head of the chain, terminate it. */
788 secinfo = sinfo->chain;
789 sinfo->chain = secinfo->next;
790 secinfo->next = NULL;
792 /* Record the sections into the hash table. */
793 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
794 if (secinfo->sec->flags & SEC_EXCLUDE)
796 *secinfo->psecinfo = NULL;
797 if (remove_hook)
798 (*remove_hook) (abfd, secinfo->sec);
800 else if (! record_section (sinfo, secinfo))
801 break;
803 if (secinfo)
804 continue;
806 if (sinfo->htab->first == NULL)
807 continue;
809 if (sinfo->htab->strings)
810 merge_strings (sinfo);
811 else
813 struct sec_merge_hash_entry *e;
814 bfd_size_type size = 0;
816 /* Things are much simpler for non-strings.
817 Just assign them slots in the section. */
818 secinfo = NULL;
819 for (e = sinfo->htab->first; e; e = e->next)
821 if (e->secinfo->first == NULL)
823 if (secinfo)
824 secinfo->sec->_cooked_size = size;
825 e->secinfo->first = e;
826 size = 0;
828 size = (size + e->alignment - 1)
829 & ~((bfd_vma) e->alignment - 1);
830 e->u.index = size;
831 size += e->len;
832 secinfo = e->secinfo;
834 secinfo->sec->_cooked_size = size;
837 /* Finally shrink all input sections which have not made it into
838 the hash table at all. */
839 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
840 if (secinfo->first == NULL)
841 secinfo->sec->_cooked_size = 0;
844 return TRUE;
847 /* Write out the merged section. */
849 bfd_boolean
850 _bfd_write_merged_section (output_bfd, sec, psecinfo)
851 bfd *output_bfd;
852 asection *sec;
853 PTR psecinfo;
855 struct sec_merge_sec_info *secinfo;
856 file_ptr pos;
858 secinfo = (struct sec_merge_sec_info *) psecinfo;
860 if (!secinfo->first)
861 return TRUE;
863 pos = sec->output_section->filepos + sec->output_offset;
864 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
865 return FALSE;
867 if (! sec_merge_emit (output_bfd, secinfo->first))
868 return FALSE;
870 return TRUE;
873 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
874 *PSEC, this returns the new offset in the adjusted SEC_MERGE
875 section and writes the new section back into *PSEC. */
877 bfd_vma
878 _bfd_merged_section_offset (output_bfd, psec, psecinfo, offset, addend)
879 bfd *output_bfd ATTRIBUTE_UNUSED;
880 asection **psec;
881 PTR psecinfo;
882 bfd_vma offset, addend;
884 struct sec_merge_sec_info *secinfo;
885 struct sec_merge_hash_entry *entry;
886 unsigned char *p;
887 asection *sec = *psec;
889 secinfo = (struct sec_merge_sec_info *) psecinfo;
891 if (offset + addend >= sec->_raw_size)
893 if (offset + addend > sec->_raw_size)
895 (*_bfd_error_handler)
896 (_("%s: access beyond end of merged section (%ld + %ld)"),
897 bfd_get_filename (sec->owner), (long) offset, (long) addend);
899 return (secinfo->first ? sec->_cooked_size : 0);
902 if (secinfo->htab->strings)
904 if (sec->entsize == 1)
906 p = secinfo->contents + offset + addend - 1;
907 while (p >= secinfo->contents && *p)
908 --p;
909 ++p;
911 else
913 p = secinfo->contents
914 + ((offset + addend) / sec->entsize) * sec->entsize;
915 p -= sec->entsize;
916 while (p >= secinfo->contents)
918 unsigned int i;
920 for (i = 0; i < sec->entsize; ++i)
921 if (p[i] != '\0')
922 break;
923 if (i == sec->entsize)
924 break;
925 p -= sec->entsize;
927 p += sec->entsize;
930 else
932 p = secinfo->contents
933 + ((offset + addend) / sec->entsize) * sec->entsize;
935 entry = sec_merge_hash_lookup (secinfo->htab, p, 0, FALSE);
936 if (!entry)
938 if (! secinfo->htab->strings)
939 abort ();
940 /* This should only happen if somebody points into the padding
941 after a NUL character but before next entity. */
942 if (*p)
943 abort ();
944 if (! secinfo->htab->first)
945 abort ();
946 entry = secinfo->htab->first;
947 p = secinfo->contents
948 + ((offset + addend) / sec->entsize + 1) * sec->entsize
949 - entry->len;
952 *psec = entry->secinfo->sec;
953 return entry->u.index + (secinfo->contents + offset - p);