Touches most files in bfd/, so likely will be blamed for everything..
[binutils.git] / bfd / merge.c
blob150e9365f2dc6ee36c8cdf0908c68de19887fdec
1 /* SEC_MERGE support.
2 Copyright 2001 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"
29 #include <ctype.h>
31 struct sec_merge_sec_info;
33 /* An entry in the section merge hash table. */
35 struct sec_merge_hash_entry
37 struct bfd_hash_entry root;
38 /* Length of this entry. */
39 unsigned int len;
40 /* Start of this string needs to be aligned to
41 alignment octets (not 1 << align). */
42 unsigned int alignment;
43 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 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, boolean));
104 static struct sec_merge_hash *sec_merge_init
105 PARAMS ((unsigned int, 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 boolean sec_merge_emit
110 PARAMS ((bfd *, struct sec_merge_hash_entry *));
111 static int cmplengthentry PARAMS ((const PTR, const PTR));
112 static int last4_eq PARAMS ((const PTR, const PTR));
113 static int last_eq PARAMS ((const PTR, const PTR));
114 static boolean record_section
115 PARAMS ((struct sec_merge_info *, struct sec_merge_sec_info *));
116 static void merge_strings PARAMS ((struct sec_merge_info *));
118 /* Routine to create an entry in a section merge hashtab. */
120 static struct bfd_hash_entry *
121 sec_merge_hash_newfunc (entry, table, string)
122 struct bfd_hash_entry *entry;
123 struct bfd_hash_table *table;
124 const char *string;
126 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
128 /* Allocate the structure if it has not already been allocated by a
129 subclass. */
130 if (ret == (struct sec_merge_hash_entry *) NULL)
131 ret = ((struct sec_merge_hash_entry *)
132 bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry)));
133 if (ret == (struct sec_merge_hash_entry *) NULL)
134 return NULL;
136 /* Call the allocation method of the superclass. */
137 ret = ((struct sec_merge_hash_entry *)
138 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
140 if (ret)
142 /* Initialize the local fields. */
143 ret->u.suffix = NULL;
144 ret->alignment = 0;
145 ret->secinfo = NULL;
146 ret->next = NULL;
149 return (struct bfd_hash_entry *)ret;
152 /* Look up an entry in a section merge hash table. */
154 static struct sec_merge_hash_entry *
155 sec_merge_hash_lookup (table, string, alignment, create)
156 struct sec_merge_hash *table;
157 const char *string;
158 unsigned int alignment;
159 boolean create;
161 register const unsigned char *s;
162 register unsigned long hash;
163 register unsigned int c;
164 struct sec_merge_hash_entry *hashp;
165 unsigned int len, i;
166 unsigned int index;
168 hash = 0;
169 len = 0;
170 s = (const unsigned char *) string;
171 if (table->strings)
173 if (table->entsize == 1)
175 while ((c = *s++) != '\0')
177 hash += c + (c << 17);
178 hash ^= hash >> 2;
179 ++len;
181 hash += len + (len << 17);
183 else
185 for (;;)
187 for (i = 0; i < table->entsize; ++i)
188 if (s[i] != '\0')
189 break;
190 if (i == table->entsize)
191 break;
192 for (i = 0; i < table->entsize; ++i)
194 c = *s++;
195 hash += c + (c << 17);
196 hash ^= hash >> 2;
198 ++len;
200 hash += len + (len << 17);
201 len *= table->entsize;
203 hash ^= hash >> 2;
204 len += table->entsize;
206 else
208 for (i = 0; i < table->entsize; ++i)
210 c = *s++;
211 hash += c + (c << 17);
212 hash ^= hash >> 2;
214 len = table->entsize;
217 index = hash % table->table.size;
218 for (hashp = (struct sec_merge_hash_entry *) table->table.table[index];
219 hashp != (struct sec_merge_hash_entry *) NULL;
220 hashp = (struct sec_merge_hash_entry *) hashp->root.next)
222 if (hashp->root.hash == hash
223 && len == hashp->len
224 && memcmp (hashp->root.string, string, len) == 0)
226 /* If the string we found does not have at least the required
227 alignment, we need to insert another copy. */
228 if (hashp->alignment < alignment)
230 /* Mark the less aligned copy as deleted. */
231 hashp->len = 0;
232 hashp->alignment = 0;
233 break;
235 return hashp;
239 if (! create)
240 return (struct sec_merge_hash_entry *) NULL;
242 hashp = (struct sec_merge_hash_entry *)
243 sec_merge_hash_newfunc ((struct bfd_hash_entry *) NULL,
244 (struct bfd_hash_table *) table, string);
245 if (hashp == (struct sec_merge_hash_entry *) NULL)
246 return (struct sec_merge_hash_entry *) NULL;
247 hashp->root.string = string;
248 hashp->root.hash = hash;
249 hashp->len = len;
250 hashp->alignment = alignment;
251 hashp->root.next = table->table.table[index];
252 table->table.table[index] = (struct bfd_hash_entry *) hashp;
254 return hashp;
257 /* Create a new hash table. */
259 static struct sec_merge_hash *
260 sec_merge_init (entsize, strings)
261 unsigned int entsize;
262 boolean strings;
264 struct sec_merge_hash *table;
265 bfd_size_type amt = sizeof (struct sec_merge_hash);
267 table = (struct sec_merge_hash *) bfd_malloc (amt);
268 if (table == NULL)
269 return NULL;
271 if (! bfd_hash_table_init (&table->table, sec_merge_hash_newfunc))
273 free (table);
274 return NULL;
277 table->size = 0;
278 table->first = NULL;
279 table->last = NULL;
280 table->entsize = entsize;
281 table->strings = strings;
283 return table;
286 /* Get the index of an entity in a hash table, adding it if it is not
287 already present. */
289 static struct sec_merge_hash_entry *
290 sec_merge_add (tab, str, alignment, secinfo)
291 struct sec_merge_hash *tab;
292 const char *str;
293 unsigned int alignment;
294 struct sec_merge_sec_info *secinfo;
296 register struct sec_merge_hash_entry *entry;
298 entry = sec_merge_hash_lookup (tab, str, alignment, true);
299 if (entry == NULL)
300 return NULL;
302 if (entry->secinfo == NULL)
304 tab->size++;
305 entry->secinfo = secinfo;
306 if (tab->first == NULL)
307 tab->first = entry;
308 else
309 tab->last->next = entry;
310 tab->last = entry;
313 return entry;
316 static boolean
317 sec_merge_emit (abfd, entry)
318 register bfd *abfd;
319 struct sec_merge_hash_entry *entry;
321 struct sec_merge_sec_info *secinfo = entry->secinfo;
322 asection *sec = secinfo->sec;
323 char *pad = "";
324 bfd_size_type off = 0;
325 int alignment_power = bfd_get_section_alignment (abfd, sec->output_section);
327 if (alignment_power)
328 pad = bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
330 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
332 register const char *str;
333 register size_t len;
335 len = off & (entry->alignment - 1);
336 if (len)
338 len = entry->alignment - len;
339 if (bfd_bwrite ((PTR) pad, (bfd_size_type) len, abfd) != len)
340 break;
341 off += len;
344 str = entry->root.string;
345 len = entry->len;
347 if (bfd_bwrite ((PTR) str, (bfd_size_type) len, abfd) != len)
348 break;
350 off += len;
353 if (alignment_power)
354 free (pad);
356 return entry == NULL || entry->secinfo != secinfo;
359 /* This function is called for each input file from the add_symbols
360 pass of the linker. */
362 boolean
363 _bfd_merge_section (abfd, psinfo, sec, psecinfo)
364 bfd *abfd;
365 PTR *psinfo;
366 asection *sec;
367 PTR *psecinfo;
369 struct sec_merge_info *sinfo;
370 struct sec_merge_sec_info *secinfo;
371 unsigned int align;
372 bfd_size_type amt;
374 if (sec->_raw_size == 0
375 || (sec->flags & SEC_EXCLUDE)
376 || (sec->flags & SEC_MERGE) == 0
377 || sec->entsize == 0)
378 return true;
380 if ((sec->flags & SEC_RELOC) != 0)
382 /* We aren't prepared to handle relocations in merged sections. */
383 return true;
386 if (sec->output_section != NULL
387 && bfd_is_abs_section (sec->output_section))
389 /* The section is being discarded from the link, so we should
390 just ignore it. */
391 return true;
394 align = bfd_get_section_alignment (sec->owner, sec);
395 if ((sec->entsize < (unsigned int)(1 << align)
396 && ((sec->entsize & (sec->entsize - 1))
397 || !(sec->flags & SEC_STRINGS)))
398 || (sec->entsize > (unsigned int)(1 << align)
399 && (sec->entsize & ((1 << align) - 1))))
401 /* Sanity check. If string character size is smaller than
402 alignment, then we require character size to be a power
403 of 2, otherwise character size must be integer multiple
404 of alignment. For non-string constants, alignment must
405 be smaller than or equal to entity size and entity size
406 must be integer multiple of alignment. */
407 return true;
410 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
411 if ((secinfo = sinfo->chain)
412 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
413 && secinfo->sec->entsize == sec->entsize
414 && ! strcmp (secinfo->sec->name, sec->name))
415 break;
417 if (sinfo == NULL)
419 /* Initialize the information we need to keep track of. */
420 sinfo = (struct sec_merge_info *)
421 bfd_alloc (abfd, (bfd_size_type) sizeof (struct sec_merge_info));
422 if (sinfo == NULL)
423 goto error_return;
424 sinfo->next = (struct sec_merge_info *) *psinfo;
425 sinfo->chain = NULL;
426 *psinfo = (PTR) sinfo;
427 sinfo->htab =
428 sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
429 if (sinfo->htab == NULL)
430 goto error_return;
433 /* Read the section from abfd. */
435 amt = sizeof (struct sec_merge_sec_info) + sec->_raw_size - 1;
436 *psecinfo = bfd_alloc (abfd, amt);
437 if (*psecinfo == NULL)
438 goto error_return;
440 secinfo = (struct sec_merge_sec_info *)*psecinfo;
441 if (sinfo->chain)
443 secinfo->next = sinfo->chain->next;
444 sinfo->chain->next = secinfo;
446 else
447 secinfo->next = secinfo;
448 sinfo->chain = secinfo;
449 secinfo->sec = sec;
450 secinfo->psecinfo = psecinfo;
451 secinfo->htab = sinfo->htab;
452 secinfo->first = NULL;
454 if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents,
455 (bfd_vma) 0, sec->_raw_size))
456 goto error_return;
458 return true;
460 error_return:
461 *psecinfo = NULL;
462 return false;
465 /* Compare two sec_merge_hash_entry structures. This is called via qsort. */
467 static int
468 cmplengthentry (a, b)
469 const PTR a;
470 const PTR b;
472 struct sec_merge_hash_entry * A = *(struct sec_merge_hash_entry **) a;
473 struct sec_merge_hash_entry * B = *(struct sec_merge_hash_entry **) b;
475 if (A->len < B->len)
476 return 1;
477 else if (A->len > B->len)
478 return -1;
480 return memcmp (A->root.string, B->root.string, A->len);
483 static int
484 last4_eq (a, b)
485 const PTR a;
486 const PTR b;
488 struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
489 struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
491 if (memcmp (A->root.string + A->len - 5 * A->u.entsize,
492 B->root.string + B->len - 5 * A->u.entsize,
493 4 * A->u.entsize) != 0)
494 /* This was a hashtable collision. */
495 return 0;
497 if (A->len <= B->len)
498 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
499 not to be equal by the hash table. */
500 return 0;
502 if (A->alignment < B->alignment
503 || ((A->len - B->len) & (B->alignment - 1)))
504 /* The suffix is not sufficiently aligned. */
505 return 0;
507 return memcmp (A->root.string + (A->len - B->len),
508 B->root.string, B->len - 5 * A->u.entsize) == 0;
511 static int
512 last_eq (a, b)
513 const PTR a;
514 const PTR b;
516 struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
517 struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
519 if (B->len >= 5 * A->u.entsize)
520 /* Longer strings are just pushed into the hash table,
521 they'll be used when looking up for very short strings. */
522 return 0;
524 if (memcmp (A->root.string + A->len - 2 * A->u.entsize,
525 B->root.string + B->len - 2 * A->u.entsize,
526 A->u.entsize) != 0)
527 /* This was a hashtable collision. */
528 return 0;
530 if (A->len <= B->len)
531 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
532 not to be equal by the hash table. */
533 return 0;
535 if (A->alignment < B->alignment
536 || ((A->len - B->len) & (B->alignment - 1)))
537 /* The suffix is not sufficiently aligned. */
538 return 0;
540 return memcmp (A->root.string + (A->len - B->len),
541 B->root.string, B->len - 2 * A->u.entsize) == 0;
544 /* Record one section into the hash table. */
545 static boolean
546 record_section (sinfo, secinfo)
547 struct sec_merge_info *sinfo;
548 struct sec_merge_sec_info *secinfo;
550 asection *sec = secinfo->sec;
551 struct sec_merge_hash_entry *entry;
552 boolean nul;
553 unsigned char *p, *end;
554 bfd_vma mask, eltalign;
555 unsigned int align, i;
557 align = bfd_get_section_alignment (sec->owner, sec);
558 end = secinfo->contents + sec->_raw_size;
559 nul = false;
560 mask = ((bfd_vma) 1 << align) - 1;
561 if (sec->flags & SEC_STRINGS)
563 for (p = secinfo->contents; p < end; )
565 eltalign = p - secinfo->contents;
566 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
567 if (!eltalign || eltalign > mask)
568 eltalign = mask + 1;
569 entry = sec_merge_add (sinfo->htab, p, (unsigned) eltalign, secinfo);
570 if (! entry)
571 goto error_return;
572 p += entry->len;
573 if (sec->entsize == 1)
575 while (p < end && *p == 0)
577 if (!nul && !((p - secinfo->contents) & mask))
579 nul = true;
580 entry = sec_merge_add (sinfo->htab, "",
581 (unsigned) mask + 1, secinfo);
582 if (! entry)
583 goto error_return;
585 p++;
588 else
590 while (p < end)
592 for (i = 0; i < sec->entsize; i++)
593 if (p[i] != '\0')
594 break;
595 if (i != sec->entsize)
596 break;
597 if (!nul && !((p - secinfo->contents) & mask))
599 nul = true;
600 entry = sec_merge_add (sinfo->htab, p,
601 (unsigned) mask + 1, secinfo);
602 if (! entry)
603 goto error_return;
605 p += sec->entsize;
610 else
612 for (p = secinfo->contents; p < end; p += sec->entsize)
614 entry = sec_merge_add (sinfo->htab, p, 1, secinfo);
615 if (! entry)
616 goto error_return;
620 return true;
622 error_return:
623 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
624 *secinfo->psecinfo = NULL;
625 return false;
628 /* This is a helper function for _bfd_merge_sections. It attempts to
629 merge strings matching suffixes of longer strings. */
630 static void
631 merge_strings (sinfo)
632 struct sec_merge_info *sinfo;
634 struct sec_merge_hash_entry **array, **a, **end, *e;
635 struct sec_merge_sec_info *secinfo;
636 htab_t lasttab = NULL, last4tab = NULL;
637 bfd_size_type size, amt;
639 /* Now sort the strings by length, longest first. */
640 array = NULL;
641 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
642 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
643 if (array == NULL)
644 goto alloc_failure;
646 for (e = sinfo->htab->first, a = array; e; e = e->next)
647 if (e->alignment)
648 *a++ = e;
650 sinfo->htab->size = a - array;
652 qsort (array, (size_t) sinfo->htab->size,
653 sizeof (struct sec_merge_hash_entry *), cmplengthentry);
655 last4tab = htab_create ((size_t) sinfo->htab->size * 4, NULL, last4_eq, NULL);
656 lasttab = htab_create ((size_t) sinfo->htab->size * 4, NULL, last_eq, NULL);
657 if (lasttab == NULL || last4tab == NULL)
658 goto alloc_failure;
660 /* Now insert the strings into hash tables (strings with last 4 characters
661 and strings with last character equal), look for longer strings which
662 we're suffix of. */
663 for (a = array, end = array + sinfo->htab->size; a < end; a++)
665 register hashval_t hash;
666 unsigned int c;
667 unsigned int i;
668 const unsigned char *s;
669 PTR *p;
671 e = *a;
672 e->u.entsize = sinfo->htab->entsize;
673 if (e->len <= e->u.entsize)
674 break;
675 if (e->len > 4 * e->u.entsize)
677 s = e->root.string + e->len - e->u.entsize;
678 hash = 0;
679 for (i = 0; i < 4 * e->u.entsize; i++)
681 c = *--s;
682 hash += c + (c << 17);
683 hash ^= hash >> 2;
685 p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
686 if (p == NULL)
687 goto alloc_failure;
688 if (*p)
690 struct sec_merge_hash_entry *ent;
692 ent = (struct sec_merge_hash_entry *) *p;
693 e->u.suffix = ent;
694 e->alignment = 0;
695 continue;
697 else
698 *p = (PTR) e;
700 s = e->root.string + e->len - e->u.entsize;
701 hash = 0;
702 for (i = 0; i < e->u.entsize; i++)
704 c = *--s;
705 hash += c + (c << 17);
706 hash ^= hash >> 2;
708 p = htab_find_slot_with_hash (lasttab, e, hash, INSERT);
709 if (p == NULL)
710 goto alloc_failure;
711 if (*p)
713 struct sec_merge_hash_entry *ent;
715 ent = (struct sec_merge_hash_entry *) *p;
716 e->u.suffix = ent;
717 e->alignment = 0;
719 else
720 *p = (PTR) e;
723 alloc_failure:
724 if (array)
725 free (array);
726 if (lasttab)
727 htab_delete (lasttab);
728 if (last4tab)
729 htab_delete (last4tab);
731 /* Now assign positions to the strings we want to keep. */
732 size = 0;
733 secinfo = sinfo->htab->first->secinfo;
734 for (e = sinfo->htab->first; e; e = e->next)
736 if (e->secinfo != secinfo)
738 secinfo->sec->_cooked_size = size;
739 secinfo = e->secinfo;
741 if (e->alignment)
743 if (e->secinfo->first == NULL)
745 e->secinfo->first = e;
746 size = 0;
748 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
749 e->u.index = size;
750 size += e->len;
753 secinfo->sec->_cooked_size = size;
755 /* And now adjust the rest, removing them from the chain (but not hashtable)
756 at the same time. */
757 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
758 if (e->alignment)
759 a = &e->next;
760 else
762 *a = e->next;
763 if (e->len)
765 e->secinfo = e->u.suffix->secinfo;
766 e->alignment = e->u.suffix->alignment;
767 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
772 /* This function is called once after all SEC_MERGE sections are registered
773 with _bfd_merge_section. */
775 boolean
776 _bfd_merge_sections (abfd, xsinfo)
777 bfd *abfd ATTRIBUTE_UNUSED;
778 PTR xsinfo;
780 struct sec_merge_info *sinfo;
782 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
784 struct sec_merge_sec_info * secinfo;
786 if (! sinfo->chain)
787 continue;
789 /* Move sinfo->chain to head of the chain, terminate it. */
790 secinfo = sinfo->chain;
791 sinfo->chain = secinfo->next;
792 secinfo->next = NULL;
794 /* Record the sections into the hash table. */
795 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
796 if (secinfo->sec->flags & SEC_EXCLUDE)
797 *secinfo->psecinfo = NULL;
798 else if (! record_section (sinfo, secinfo))
799 break;
801 if (secinfo)
802 continue;
804 if (sinfo->htab->strings)
805 merge_strings (sinfo);
806 else
808 struct sec_merge_hash_entry *e;
809 bfd_size_type size = 0;
811 /* Things are much simpler for non-strings.
812 Just assign them slots in the section. */
813 secinfo = NULL;
814 for (e = sinfo->htab->first; e; e = e->next)
816 if (e->secinfo->first == NULL)
818 if (secinfo)
819 secinfo->sec->_cooked_size = size;
820 e->secinfo->first = e;
821 size = 0;
823 size = (size + e->alignment - 1)
824 & ~((bfd_vma) e->alignment - 1);
825 e->u.index = size;
826 size += e->len;
827 secinfo = e->secinfo;
829 secinfo->sec->_cooked_size = size;
832 /* Finally shrink all input sections which have not made it into
833 the hash table at all. */
834 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
835 if (secinfo->first == NULL)
837 secinfo->sec->_cooked_size = 0;
838 secinfo->sec->flags |= SEC_EXCLUDE;
842 return true;
845 /* Write out the merged section. */
847 boolean
848 _bfd_write_merged_section (output_bfd, sec, psecinfo)
849 bfd *output_bfd;
850 asection *sec;
851 PTR psecinfo;
853 struct sec_merge_sec_info *secinfo;
854 file_ptr pos;
856 secinfo = (struct sec_merge_sec_info *) psecinfo;
858 if (!secinfo->first)
859 return true;
861 pos = sec->output_section->filepos + sec->output_offset;
862 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
863 return false;
865 if (! sec_merge_emit (output_bfd, secinfo->first))
866 return false;
868 return true;
871 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
872 *PSEC, this returns the new offset in the adjusted SEC_MERGE
873 section and writes the new section back into *PSEC. */
875 bfd_vma
876 _bfd_merged_section_offset (output_bfd, psec, psecinfo, offset, addend)
877 bfd *output_bfd ATTRIBUTE_UNUSED;
878 asection **psec;
879 PTR psecinfo;
880 bfd_vma offset, addend;
882 struct sec_merge_sec_info *secinfo;
883 struct sec_merge_hash_entry *entry;
884 unsigned char *p;
885 asection *sec = *psec;
887 secinfo = (struct sec_merge_sec_info *) psecinfo;
889 if (offset + addend >= sec->_raw_size)
891 if (offset + addend > sec->_raw_size)
892 (*_bfd_error_handler) (_("%s: access beyond end of merged section (%ld + %ld)"),
893 bfd_get_filename (sec->owner), (long)offset,
894 (long) addend);
895 return (secinfo->first ? sec->_cooked_size : 0);
898 if (secinfo->htab->strings)
900 if (sec->entsize == 1)
902 p = secinfo->contents + offset + addend - 1;
903 while (*p && p >= secinfo->contents)
904 --p;
905 ++p;
907 else
909 p = secinfo->contents
910 + ((offset + addend) / sec->entsize) * sec->entsize;
911 p -= sec->entsize;
912 while (p >= secinfo->contents)
914 unsigned int i;
916 for (i = 0; i < sec->entsize; ++i)
917 if (p[i] != '\0')
918 break;
919 if (i == sec->entsize)
920 break;
921 p -= sec->entsize;
923 p += sec->entsize;
926 else
928 p = secinfo->contents
929 + ((offset + addend) / sec->entsize) * sec->entsize;
931 entry = sec_merge_hash_lookup (secinfo->htab, p, 0, false);
932 if (!entry)
934 if (! secinfo->htab->strings)
935 abort ();
936 /* This should only happen if somebody points into the padding
937 after a NUL character but before next entity. */
938 if (*p)
939 abort ();
940 if (! secinfo->htab->first)
941 abort ();
942 entry = secinfo->htab->first;
943 p = secinfo->contents
944 + ((offset + addend) / sec->entsize + 1) * sec->entsize
945 - entry->len;
948 *psec = entry->secinfo->sec;
949 return entry->u.index + (secinfo->contents + offset - p);