2002-02-18 David O'Brien <obrien@FreeBSD.org>
[binutils.git] / bfd / merge.c
blob7b06c10debeec6c73bd937703a609530324ce87f
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 struct sec_merge_sec_info;
31 /* An entry in the section merge hash table. */
33 struct sec_merge_hash_entry
35 struct bfd_hash_entry root;
36 /* Length of this entry. */
37 unsigned int len;
38 /* Start of this string needs to be aligned to
39 alignment octets (not 1 << align). */
40 unsigned int alignment;
41 union {
42 /* Index within the merged section. */
43 bfd_size_type index;
44 /* Entity size (if present in suffix hash tables). */
45 unsigned int entsize;
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;
55 /* The section merge hash table. */
57 struct sec_merge_hash
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 boolean strings;
72 struct sec_merge_info
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;
82 struct sec_merge_sec_info
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 PTR *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;
94 /* Original section content. */
95 unsigned char contents[1];
98 static struct bfd_hash_entry *sec_merge_hash_newfunc
99 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
100 static struct sec_merge_hash_entry *sec_merge_hash_lookup
101 PARAMS ((struct sec_merge_hash *, const char *, unsigned int, boolean));
102 static struct sec_merge_hash *sec_merge_init
103 PARAMS ((unsigned int, boolean));
104 static struct sec_merge_hash_entry *sec_merge_add
105 PARAMS ((struct sec_merge_hash *, const char *, unsigned int,
106 struct sec_merge_sec_info *));
107 static boolean sec_merge_emit
108 PARAMS ((bfd *, struct sec_merge_hash_entry *));
109 static int cmplengthentry PARAMS ((const PTR, const PTR));
110 static int last4_eq PARAMS ((const PTR, const PTR));
111 static int last_eq PARAMS ((const PTR, const PTR));
112 static boolean record_section
113 PARAMS ((struct sec_merge_info *, struct sec_merge_sec_info *));
114 static void merge_strings PARAMS ((struct sec_merge_info *));
116 /* Routine to create an entry in a section merge hashtab. */
118 static struct bfd_hash_entry *
119 sec_merge_hash_newfunc (entry, table, string)
120 struct bfd_hash_entry *entry;
121 struct bfd_hash_table *table;
122 const char *string;
124 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
126 /* Allocate the structure if it has not already been allocated by a
127 subclass. */
128 if (ret == (struct sec_merge_hash_entry *) NULL)
129 ret = ((struct sec_merge_hash_entry *)
130 bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry)));
131 if (ret == (struct sec_merge_hash_entry *) NULL)
132 return NULL;
134 /* Call the allocation method of the superclass. */
135 ret = ((struct sec_merge_hash_entry *)
136 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
138 if (ret)
140 /* Initialize the local fields. */
141 ret->u.suffix = NULL;
142 ret->alignment = 0;
143 ret->secinfo = NULL;
144 ret->next = NULL;
147 return (struct bfd_hash_entry *)ret;
150 /* Look up an entry in a section merge hash table. */
152 static struct sec_merge_hash_entry *
153 sec_merge_hash_lookup (table, string, alignment, create)
154 struct sec_merge_hash *table;
155 const char *string;
156 unsigned int alignment;
157 boolean create;
159 register const unsigned char *s;
160 register unsigned long hash;
161 register unsigned int c;
162 struct sec_merge_hash_entry *hashp;
163 unsigned int len, i;
164 unsigned int index;
166 hash = 0;
167 len = 0;
168 s = (const unsigned char *) string;
169 if (table->strings)
171 if (table->entsize == 1)
173 while ((c = *s++) != '\0')
175 hash += c + (c << 17);
176 hash ^= hash >> 2;
177 ++len;
179 hash += len + (len << 17);
181 else
183 for (;;)
185 for (i = 0; i < table->entsize; ++i)
186 if (s[i] != '\0')
187 break;
188 if (i == table->entsize)
189 break;
190 for (i = 0; i < table->entsize; ++i)
192 c = *s++;
193 hash += c + (c << 17);
194 hash ^= hash >> 2;
196 ++len;
198 hash += len + (len << 17);
199 len *= table->entsize;
201 hash ^= hash >> 2;
202 len += table->entsize;
204 else
206 for (i = 0; i < table->entsize; ++i)
208 c = *s++;
209 hash += c + (c << 17);
210 hash ^= hash >> 2;
212 len = table->entsize;
215 index = hash % table->table.size;
216 for (hashp = (struct sec_merge_hash_entry *) table->table.table[index];
217 hashp != (struct sec_merge_hash_entry *) NULL;
218 hashp = (struct sec_merge_hash_entry *) hashp->root.next)
220 if (hashp->root.hash == hash
221 && len == hashp->len
222 && memcmp (hashp->root.string, string, len) == 0)
224 /* If the string we found does not have at least the required
225 alignment, we need to insert another copy. */
226 if (hashp->alignment < alignment)
228 /* Mark the less aligned copy as deleted. */
229 hashp->len = 0;
230 hashp->alignment = 0;
231 break;
233 return hashp;
237 if (! create)
238 return (struct sec_merge_hash_entry *) NULL;
240 hashp = (struct sec_merge_hash_entry *)
241 sec_merge_hash_newfunc ((struct bfd_hash_entry *) NULL,
242 (struct bfd_hash_table *) table, string);
243 if (hashp == (struct sec_merge_hash_entry *) NULL)
244 return (struct sec_merge_hash_entry *) NULL;
245 hashp->root.string = string;
246 hashp->root.hash = hash;
247 hashp->len = len;
248 hashp->alignment = alignment;
249 hashp->root.next = table->table.table[index];
250 table->table.table[index] = (struct bfd_hash_entry *) hashp;
252 return hashp;
255 /* Create a new hash table. */
257 static struct sec_merge_hash *
258 sec_merge_init (entsize, strings)
259 unsigned int entsize;
260 boolean strings;
262 struct sec_merge_hash *table;
263 bfd_size_type amt = sizeof (struct sec_merge_hash);
265 table = (struct sec_merge_hash *) bfd_malloc (amt);
266 if (table == NULL)
267 return NULL;
269 if (! bfd_hash_table_init (&table->table, sec_merge_hash_newfunc))
271 free (table);
272 return NULL;
275 table->size = 0;
276 table->first = NULL;
277 table->last = NULL;
278 table->entsize = entsize;
279 table->strings = strings;
281 return table;
284 /* Get the index of an entity in a hash table, adding it if it is not
285 already present. */
287 static struct sec_merge_hash_entry *
288 sec_merge_add (tab, str, alignment, secinfo)
289 struct sec_merge_hash *tab;
290 const char *str;
291 unsigned int alignment;
292 struct sec_merge_sec_info *secinfo;
294 register struct sec_merge_hash_entry *entry;
296 entry = sec_merge_hash_lookup (tab, str, alignment, true);
297 if (entry == NULL)
298 return NULL;
300 if (entry->secinfo == NULL)
302 tab->size++;
303 entry->secinfo = secinfo;
304 if (tab->first == NULL)
305 tab->first = entry;
306 else
307 tab->last->next = entry;
308 tab->last = entry;
311 return entry;
314 static boolean
315 sec_merge_emit (abfd, entry)
316 register bfd *abfd;
317 struct sec_merge_hash_entry *entry;
319 struct sec_merge_sec_info *secinfo = entry->secinfo;
320 asection *sec = secinfo->sec;
321 char *pad = "";
322 bfd_size_type off = 0;
323 int alignment_power = bfd_get_section_alignment (abfd, sec->output_section);
325 if (alignment_power)
326 pad = bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
328 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
330 register const char *str;
331 register size_t len;
333 len = off & (entry->alignment - 1);
334 if (len)
336 len = entry->alignment - len;
337 if (bfd_bwrite ((PTR) pad, (bfd_size_type) len, abfd) != len)
338 break;
339 off += len;
342 str = entry->root.string;
343 len = entry->len;
345 if (bfd_bwrite ((PTR) str, (bfd_size_type) len, abfd) != len)
346 break;
348 off += len;
351 if (alignment_power)
352 free (pad);
354 return entry == NULL || entry->secinfo != secinfo;
357 /* This function is called for each input file from the add_symbols
358 pass of the linker. */
360 boolean
361 _bfd_merge_section (abfd, psinfo, sec, psecinfo)
362 bfd *abfd;
363 PTR *psinfo;
364 asection *sec;
365 PTR *psecinfo;
367 struct sec_merge_info *sinfo;
368 struct sec_merge_sec_info *secinfo;
369 unsigned int align;
370 bfd_size_type amt;
372 if (sec->_raw_size == 0
373 || (sec->flags & SEC_EXCLUDE)
374 || (sec->flags & SEC_MERGE) == 0
375 || sec->entsize == 0)
376 return true;
378 if ((sec->flags & SEC_RELOC) != 0)
380 /* We aren't prepared to handle relocations in merged sections. */
381 return true;
384 if (sec->output_section != NULL
385 && bfd_is_abs_section (sec->output_section))
387 /* The section is being discarded from the link, so we should
388 just ignore it. */
389 return true;
392 align = bfd_get_section_alignment (sec->owner, sec);
393 if ((sec->entsize < (unsigned int)(1 << align)
394 && ((sec->entsize & (sec->entsize - 1))
395 || !(sec->flags & SEC_STRINGS)))
396 || (sec->entsize > (unsigned int)(1 << align)
397 && (sec->entsize & ((1 << align) - 1))))
399 /* Sanity check. If string character size is smaller than
400 alignment, then we require character size to be a power
401 of 2, otherwise character size must be integer multiple
402 of alignment. For non-string constants, alignment must
403 be smaller than or equal to entity size and entity size
404 must be integer multiple of alignment. */
405 return true;
408 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
409 if ((secinfo = sinfo->chain)
410 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
411 && secinfo->sec->entsize == sec->entsize
412 && ! strcmp (secinfo->sec->name, sec->name))
413 break;
415 if (sinfo == NULL)
417 /* Initialize the information we need to keep track of. */
418 sinfo = (struct sec_merge_info *)
419 bfd_alloc (abfd, (bfd_size_type) sizeof (struct sec_merge_info));
420 if (sinfo == NULL)
421 goto error_return;
422 sinfo->next = (struct sec_merge_info *) *psinfo;
423 sinfo->chain = NULL;
424 *psinfo = (PTR) sinfo;
425 sinfo->htab =
426 sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
427 if (sinfo->htab == NULL)
428 goto error_return;
431 /* Read the section from abfd. */
433 amt = sizeof (struct sec_merge_sec_info) + sec->_raw_size - 1;
434 *psecinfo = bfd_alloc (abfd, amt);
435 if (*psecinfo == NULL)
436 goto error_return;
438 secinfo = (struct sec_merge_sec_info *)*psecinfo;
439 if (sinfo->chain)
441 secinfo->next = sinfo->chain->next;
442 sinfo->chain->next = secinfo;
444 else
445 secinfo->next = secinfo;
446 sinfo->chain = secinfo;
447 secinfo->sec = sec;
448 secinfo->psecinfo = psecinfo;
449 secinfo->htab = sinfo->htab;
450 secinfo->first = NULL;
452 if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents,
453 (bfd_vma) 0, sec->_raw_size))
454 goto error_return;
456 return true;
458 error_return:
459 *psecinfo = NULL;
460 return false;
463 /* Compare two sec_merge_hash_entry structures. This is called via qsort. */
465 static int
466 cmplengthentry (a, b)
467 const PTR a;
468 const PTR b;
470 struct sec_merge_hash_entry * A = *(struct sec_merge_hash_entry **) a;
471 struct sec_merge_hash_entry * B = *(struct sec_merge_hash_entry **) b;
473 if (A->len < B->len)
474 return 1;
475 else if (A->len > B->len)
476 return -1;
478 return memcmp (A->root.string, B->root.string, A->len);
481 static int
482 last4_eq (a, b)
483 const PTR a;
484 const PTR b;
486 struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
487 struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
489 if (memcmp (A->root.string + A->len - 5 * A->u.entsize,
490 B->root.string + B->len - 5 * A->u.entsize,
491 4 * A->u.entsize) != 0)
492 /* This was a hashtable collision. */
493 return 0;
495 if (A->len <= B->len)
496 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
497 not to be equal by the hash table. */
498 return 0;
500 if (A->alignment < B->alignment
501 || ((A->len - B->len) & (B->alignment - 1)))
502 /* The suffix is not sufficiently aligned. */
503 return 0;
505 return memcmp (A->root.string + (A->len - B->len),
506 B->root.string, B->len - 5 * A->u.entsize) == 0;
509 static int
510 last_eq (a, b)
511 const PTR a;
512 const PTR b;
514 struct sec_merge_hash_entry * A = (struct sec_merge_hash_entry *) a;
515 struct sec_merge_hash_entry * B = (struct sec_merge_hash_entry *) b;
517 if (B->len >= 5 * A->u.entsize)
518 /* Longer strings are just pushed into the hash table,
519 they'll be used when looking up for very short strings. */
520 return 0;
522 if (memcmp (A->root.string + A->len - 2 * A->u.entsize,
523 B->root.string + B->len - 2 * A->u.entsize,
524 A->u.entsize) != 0)
525 /* This was a hashtable collision. */
526 return 0;
528 if (A->len <= B->len)
529 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
530 not to be equal by the hash table. */
531 return 0;
533 if (A->alignment < B->alignment
534 || ((A->len - B->len) & (B->alignment - 1)))
535 /* The suffix is not sufficiently aligned. */
536 return 0;
538 return memcmp (A->root.string + (A->len - B->len),
539 B->root.string, B->len - 2 * A->u.entsize) == 0;
542 /* Record one section into the hash table. */
543 static boolean
544 record_section (sinfo, secinfo)
545 struct sec_merge_info *sinfo;
546 struct sec_merge_sec_info *secinfo;
548 asection *sec = secinfo->sec;
549 struct sec_merge_hash_entry *entry;
550 boolean nul;
551 unsigned char *p, *end;
552 bfd_vma mask, eltalign;
553 unsigned int align, i;
555 align = bfd_get_section_alignment (sec->owner, sec);
556 end = secinfo->contents + sec->_raw_size;
557 nul = false;
558 mask = ((bfd_vma) 1 << align) - 1;
559 if (sec->flags & SEC_STRINGS)
561 for (p = secinfo->contents; p < end; )
563 eltalign = p - secinfo->contents;
564 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
565 if (!eltalign || eltalign > mask)
566 eltalign = mask + 1;
567 entry = sec_merge_add (sinfo->htab, p, (unsigned) eltalign, secinfo);
568 if (! entry)
569 goto error_return;
570 p += entry->len;
571 if (sec->entsize == 1)
573 while (p < end && *p == 0)
575 if (!nul && !((p - secinfo->contents) & mask))
577 nul = true;
578 entry = sec_merge_add (sinfo->htab, "",
579 (unsigned) mask + 1, secinfo);
580 if (! entry)
581 goto error_return;
583 p++;
586 else
588 while (p < end)
590 for (i = 0; i < sec->entsize; i++)
591 if (p[i] != '\0')
592 break;
593 if (i != sec->entsize)
594 break;
595 if (!nul && !((p - secinfo->contents) & mask))
597 nul = true;
598 entry = sec_merge_add (sinfo->htab, p,
599 (unsigned) mask + 1, secinfo);
600 if (! entry)
601 goto error_return;
603 p += sec->entsize;
608 else
610 for (p = secinfo->contents; p < end; p += sec->entsize)
612 entry = sec_merge_add (sinfo->htab, p, 1, secinfo);
613 if (! entry)
614 goto error_return;
618 return true;
620 error_return:
621 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
622 *secinfo->psecinfo = NULL;
623 return false;
626 /* This is a helper function for _bfd_merge_sections. It attempts to
627 merge strings matching suffixes of longer strings. */
628 static void
629 merge_strings (sinfo)
630 struct sec_merge_info *sinfo;
632 struct sec_merge_hash_entry **array, **a, **end, *e;
633 struct sec_merge_sec_info *secinfo;
634 htab_t lasttab = NULL, last4tab = NULL;
635 bfd_size_type size, amt;
637 /* Now sort the strings by length, longest first. */
638 array = NULL;
639 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
640 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
641 if (array == NULL)
642 goto alloc_failure;
644 for (e = sinfo->htab->first, a = array; e; e = e->next)
645 if (e->alignment)
646 *a++ = e;
648 sinfo->htab->size = a - array;
650 qsort (array, (size_t) sinfo->htab->size,
651 sizeof (struct sec_merge_hash_entry *), cmplengthentry);
653 last4tab = htab_create ((size_t) sinfo->htab->size * 4, NULL, last4_eq, NULL);
654 lasttab = htab_create ((size_t) sinfo->htab->size * 4, NULL, last_eq, NULL);
655 if (lasttab == NULL || last4tab == NULL)
656 goto alloc_failure;
658 /* Now insert the strings into hash tables (strings with last 4 characters
659 and strings with last character equal), look for longer strings which
660 we're suffix of. */
661 for (a = array, end = array + sinfo->htab->size; a < end; a++)
663 register hashval_t hash;
664 unsigned int c;
665 unsigned int i;
666 const unsigned char *s;
667 PTR *p;
669 e = *a;
670 e->u.entsize = sinfo->htab->entsize;
671 if (e->len <= e->u.entsize)
672 break;
673 if (e->len > 4 * e->u.entsize)
675 s = e->root.string + e->len - e->u.entsize;
676 hash = 0;
677 for (i = 0; i < 4 * e->u.entsize; i++)
679 c = *--s;
680 hash += c + (c << 17);
681 hash ^= hash >> 2;
683 p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
684 if (p == NULL)
685 goto alloc_failure;
686 if (*p)
688 struct sec_merge_hash_entry *ent;
690 ent = (struct sec_merge_hash_entry *) *p;
691 e->u.suffix = ent;
692 e->alignment = 0;
693 continue;
695 else
696 *p = (PTR) e;
698 s = e->root.string + e->len - e->u.entsize;
699 hash = 0;
700 for (i = 0; i < e->u.entsize; i++)
702 c = *--s;
703 hash += c + (c << 17);
704 hash ^= hash >> 2;
706 p = htab_find_slot_with_hash (lasttab, e, hash, INSERT);
707 if (p == NULL)
708 goto alloc_failure;
709 if (*p)
711 struct sec_merge_hash_entry *ent;
713 ent = (struct sec_merge_hash_entry *) *p;
714 e->u.suffix = ent;
715 e->alignment = 0;
717 else
718 *p = (PTR) e;
721 alloc_failure:
722 if (array)
723 free (array);
724 if (lasttab)
725 htab_delete (lasttab);
726 if (last4tab)
727 htab_delete (last4tab);
729 /* Now assign positions to the strings we want to keep. */
730 size = 0;
731 secinfo = sinfo->htab->first->secinfo;
732 for (e = sinfo->htab->first; e; e = e->next)
734 if (e->secinfo != secinfo)
736 secinfo->sec->_cooked_size = size;
737 secinfo = e->secinfo;
739 if (e->alignment)
741 if (e->secinfo->first == NULL)
743 e->secinfo->first = e;
744 size = 0;
746 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
747 e->u.index = size;
748 size += e->len;
751 secinfo->sec->_cooked_size = size;
753 /* And now adjust the rest, removing them from the chain (but not hashtable)
754 at the same time. */
755 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
756 if (e->alignment)
757 a = &e->next;
758 else
760 *a = e->next;
761 if (e->len)
763 e->secinfo = e->u.suffix->secinfo;
764 e->alignment = e->u.suffix->alignment;
765 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
770 /* This function is called once after all SEC_MERGE sections are registered
771 with _bfd_merge_section. */
773 boolean
774 _bfd_merge_sections (abfd, xsinfo, remove_hook)
775 bfd *abfd ATTRIBUTE_UNUSED;
776 PTR xsinfo;
777 void (*remove_hook) PARAMS((bfd *, asection *));
779 struct sec_merge_info *sinfo;
781 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
783 struct sec_merge_sec_info * secinfo;
785 if (! sinfo->chain)
786 continue;
788 /* Move sinfo->chain to head of the chain, terminate it. */
789 secinfo = sinfo->chain;
790 sinfo->chain = secinfo->next;
791 secinfo->next = NULL;
793 /* Record the sections into the hash table. */
794 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
795 if (secinfo->sec->flags & SEC_EXCLUDE)
797 *secinfo->psecinfo = NULL;
798 if (remove_hook)
799 (*remove_hook) (abfd, secinfo->sec);
801 else if (! record_section (sinfo, secinfo))
802 break;
804 if (secinfo)
805 continue;
807 if (sinfo->htab->strings)
808 merge_strings (sinfo);
809 else
811 struct sec_merge_hash_entry *e;
812 bfd_size_type size = 0;
814 /* Things are much simpler for non-strings.
815 Just assign them slots in the section. */
816 secinfo = NULL;
817 for (e = sinfo->htab->first; e; e = e->next)
819 if (e->secinfo->first == NULL)
821 if (secinfo)
822 secinfo->sec->_cooked_size = size;
823 e->secinfo->first = e;
824 size = 0;
826 size = (size + e->alignment - 1)
827 & ~((bfd_vma) e->alignment - 1);
828 e->u.index = size;
829 size += e->len;
830 secinfo = e->secinfo;
832 secinfo->sec->_cooked_size = size;
835 /* Finally shrink all input sections which have not made it into
836 the hash table at all. */
837 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
838 if (secinfo->first == NULL)
840 secinfo->sec->_cooked_size = 0;
841 secinfo->sec->flags |= SEC_EXCLUDE;
845 return true;
848 /* Write out the merged section. */
850 boolean
851 _bfd_write_merged_section (output_bfd, sec, psecinfo)
852 bfd *output_bfd;
853 asection *sec;
854 PTR psecinfo;
856 struct sec_merge_sec_info *secinfo;
857 file_ptr pos;
859 secinfo = (struct sec_merge_sec_info *) psecinfo;
861 if (!secinfo->first)
862 return true;
864 pos = sec->output_section->filepos + sec->output_offset;
865 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
866 return false;
868 if (! sec_merge_emit (output_bfd, secinfo->first))
869 return false;
871 return true;
874 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
875 *PSEC, this returns the new offset in the adjusted SEC_MERGE
876 section and writes the new section back into *PSEC. */
878 bfd_vma
879 _bfd_merged_section_offset (output_bfd, psec, psecinfo, offset, addend)
880 bfd *output_bfd ATTRIBUTE_UNUSED;
881 asection **psec;
882 PTR psecinfo;
883 bfd_vma offset, addend;
885 struct sec_merge_sec_info *secinfo;
886 struct sec_merge_hash_entry *entry;
887 unsigned char *p;
888 asection *sec = *psec;
890 secinfo = (struct sec_merge_sec_info *) psecinfo;
892 if (offset + addend >= sec->_raw_size)
894 if (offset + addend > sec->_raw_size)
896 (*_bfd_error_handler)
897 (_("%s: access beyond end of merged section (%ld + %ld)"),
898 bfd_get_filename (sec->owner), (long) offset, (long) addend);
900 return (secinfo->first ? sec->_cooked_size : 0);
903 if (secinfo->htab->strings)
905 if (sec->entsize == 1)
907 p = secinfo->contents + offset + addend - 1;
908 while (*p && p >= secinfo->contents)
909 --p;
910 ++p;
912 else
914 p = secinfo->contents
915 + ((offset + addend) / sec->entsize) * sec->entsize;
916 p -= sec->entsize;
917 while (p >= secinfo->contents)
919 unsigned int i;
921 for (i = 0; i < sec->entsize; ++i)
922 if (p[i] != '\0')
923 break;
924 if (i == sec->entsize)
925 break;
926 p -= sec->entsize;
928 p += sec->entsize;
931 else
933 p = secinfo->contents
934 + ((offset + addend) / sec->entsize) * sec->entsize;
936 entry = sec_merge_hash_lookup (secinfo->htab, p, 0, false);
937 if (!entry)
939 if (! secinfo->htab->strings)
940 abort ();
941 /* This should only happen if somebody points into the padding
942 after a NUL character but before next entity. */
943 if (*p)
944 abort ();
945 if (! secinfo->htab->first)
946 abort ();
947 entry = secinfo->htab->first;
948 p = secinfo->contents
949 + ((offset + addend) / sec->entsize + 1) * sec->entsize
950 - entry->len;
953 *psec = entry->secinfo->sec;
954 return entry->u.index + (secinfo->contents + offset - p);