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. */
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. */
39 /* Start of this string needs to be aligned to
40 alignment octets (not 1 << align). */
41 unsigned int alignment;
44 /* Index within the merged section. */
46 /* Entity size (if present in suffix hash tables). */
48 /* Entry this is a suffix of (if alignment is 0). */
49 struct sec_merge_hash_entry *suffix;
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. */
61 struct bfd_hash_table table;
62 /* Next available index. */
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;
70 /* Are entries fixed size or zero terminated strings? */
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. */
90 /* Pointer to merge_info pointing to us. */
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));
114 PARAMS ((const PTR, const PTR));
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;
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
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)
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));
146 /* Initialize the local fields. */
147 ret->u.suffix = 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;
162 unsigned int alignment;
165 register const unsigned char *s;
166 register unsigned long hash;
167 register unsigned int c;
168 struct sec_merge_hash_entry *hashp;
174 s = (const unsigned char *) string;
177 if (table->entsize == 1)
179 while ((c = *s++) != '\0')
181 hash += c + (c << 17);
185 hash += len + (len << 17);
191 for (i = 0; i < table->entsize; ++i)
194 if (i == table->entsize)
196 for (i = 0; i < table->entsize; ++i)
199 hash += c + (c << 17);
204 hash += len + (len << 17);
205 len *= table->entsize;
208 len += table->entsize;
212 for (i = 0; i < table->entsize; ++i)
215 hash += c + (c << 17);
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
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. */
236 hashp->alignment = 0;
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;
254 hashp->alignment = alignment;
255 hashp->root.next = table->table.table[index];
256 table->table.table[index] = (struct bfd_hash_entry *) hashp;
261 /* Create a new hash table. */
263 static struct sec_merge_hash *
264 sec_merge_init (entsize, strings)
265 unsigned int entsize;
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);
275 if (! bfd_hash_table_init (&table->table, sec_merge_hash_newfunc))
284 table->entsize = entsize;
285 table->strings = strings;
290 /* Get the index of an entity in a hash table, adding it if it is not
293 static struct sec_merge_hash_entry *
294 sec_merge_add (tab, str, alignment, secinfo)
295 struct sec_merge_hash *tab;
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);
306 if (entry->secinfo == NULL)
309 entry->secinfo = secinfo;
310 if (tab->first == NULL)
313 tab->last->next = entry;
321 sec_merge_emit (abfd, entry)
323 struct sec_merge_hash_entry *entry;
325 struct sec_merge_sec_info *secinfo = entry->secinfo;
326 asection *sec = secinfo->sec;
328 bfd_size_type off = 0;
329 int alignment_power = bfd_get_section_alignment (abfd, sec->output_section);
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;
339 len = off & (entry->alignment - 1);
342 len = entry->alignment - len;
343 if (bfd_bwrite ((PTR) pad, (bfd_size_type) len, abfd) != len)
348 str = entry->root.string;
351 if (bfd_bwrite ((PTR) str, (bfd_size_type) len, abfd) != len)
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. */
367 _bfd_merge_section (abfd, psinfo, sec, psecinfo)
373 struct sec_merge_info *sinfo;
374 struct sec_merge_sec_info *secinfo;
378 if (sec->_raw_size == 0
379 || (sec->flags & SEC_EXCLUDE)
380 || (sec->flags & SEC_MERGE) == 0
381 || sec->entsize == 0)
384 if ((sec->flags & SEC_RELOC) != 0)
386 /* We aren't prepared to handle relocations in merged sections. */
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. */
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))
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);
420 sinfo->next = (struct sec_merge_info *) *psinfo;
422 *psinfo = (PTR) sinfo;
423 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
424 if (sinfo->htab == NULL)
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)
435 secinfo = (struct sec_merge_sec_info *)*psecinfo;
438 secinfo->next = sinfo->chain->next;
439 sinfo->chain->next = secinfo;
442 secinfo->next = secinfo;
443 sinfo->chain = secinfo;
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))
460 /* Compare two sec_merge_hash_entry structures. This is called via qsort. */
463 cmplengthentry (a, 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;
472 else if (A->len > B->len)
475 return memcmp (A->root.string, B->root.string, A->len);
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. */
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. */
497 if (A->alignment < B->alignment
498 || ((A->len - B->len) & (B->alignment - 1)))
499 /* The suffix is not sufficiently aligned. */
502 return memcmp (A->root.string + (A->len - B->len),
503 B->root.string, B->len - 5 * A->u.entsize) == 0;
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. */
519 if (memcmp (A->root.string + A->len - 2 * A->u.entsize,
520 B->root.string + B->len - 2 * A->u.entsize,
522 /* This was a hashtable collision. */
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. */
530 if (A->alignment < B->alignment
531 || ((A->len - B->len) & (B->alignment - 1)))
532 /* The suffix is not sufficiently aligned. */
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. */
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;
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;
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)
564 entry = sec_merge_add (sinfo->htab, p, (unsigned) eltalign, secinfo);
568 if (sec->entsize == 1)
570 while (p < end && *p == 0)
572 if (!nul && !((p - secinfo->contents) & mask))
575 entry = sec_merge_add (sinfo->htab, "",
576 (unsigned) mask + 1, secinfo);
587 for (i = 0; i < sec->entsize; i++)
590 if (i != sec->entsize)
592 if (!nul && !((p - secinfo->contents) & mask))
595 entry = sec_merge_add (sinfo->htab, p,
596 (unsigned) mask + 1, secinfo);
607 for (p = secinfo->contents; p < end; p += sec->entsize)
609 entry = sec_merge_add (sinfo->htab, p, 1, secinfo);
618 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
619 *secinfo->psecinfo = NULL;
623 /* This is a helper function for _bfd_merge_sections. It attempts to
624 merge strings matching suffixes of longer strings. */
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. */
636 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
637 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
641 for (e = sinfo->htab->first, a = array; e; e = e->next)
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)
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
660 for (a = array, end = array + sinfo->htab->size; a < end; a++)
662 register hashval_t hash;
665 const unsigned char *s;
669 e->u.entsize = sinfo->htab->entsize;
670 if (e->len <= e->u.entsize)
672 if (e->len > 4 * e->u.entsize)
674 s = (const unsigned char *) (e->root.string + e->len - e->u.entsize);
676 for (i = 0; i < 4 * e->u.entsize; i++)
679 hash += c + (c << 17);
682 p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
687 struct sec_merge_hash_entry *ent;
689 ent = (struct sec_merge_hash_entry *) *p;
697 s = (const unsigned char *) (e->root.string + e->len - e->u.entsize);
699 for (i = 0; i < e->u.entsize; i++)
702 hash += c + (c << 17);
705 p = htab_find_slot_with_hash (lasttab, e, hash, INSERT);
710 struct sec_merge_hash_entry *ent;
712 ent = (struct sec_merge_hash_entry *) *p;
724 htab_delete (lasttab);
726 htab_delete (last4tab);
728 /* Now assign positions to the strings we want to keep. */
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;
740 if (e->secinfo->first == NULL)
742 e->secinfo->first = e;
745 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
750 secinfo->sec->_cooked_size = size;
752 /* And now adjust the rest, removing them from the chain (but not hashtable)
754 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
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. */
773 _bfd_merge_sections (abfd, xsinfo, remove_hook)
774 bfd *abfd ATTRIBUTE_UNUSED;
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;
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;
798 (*remove_hook) (abfd, secinfo->sec);
800 else if (! record_section (sinfo, secinfo))
806 if (sinfo->htab->first == NULL)
809 if (sinfo->htab->strings)
810 merge_strings (sinfo);
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. */
819 for (e = sinfo->htab->first; e; e = e->next)
821 if (e->secinfo->first == NULL)
824 secinfo->sec->_cooked_size = size;
825 e->secinfo->first = e;
828 size = (size + e->alignment - 1)
829 & ~((bfd_vma) e->alignment - 1);
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;
847 /* Write out the merged section. */
850 _bfd_write_merged_section (output_bfd, sec, psecinfo)
855 struct sec_merge_sec_info *secinfo;
858 secinfo = (struct sec_merge_sec_info *) psecinfo;
863 pos = sec->output_section->filepos + sec->output_offset;
864 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
867 if (! sec_merge_emit (output_bfd, secinfo->first))
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. */
878 _bfd_merged_section_offset (output_bfd, psec, psecinfo, offset, addend)
879 bfd *output_bfd ATTRIBUTE_UNUSED;
882 bfd_vma offset, addend;
884 struct sec_merge_sec_info *secinfo;
885 struct sec_merge_hash_entry *entry;
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)
913 p = secinfo->contents
914 + ((offset + addend) / sec->entsize) * sec->entsize;
916 while (p >= secinfo->contents)
920 for (i = 0; i < sec->entsize; ++i)
923 if (i == sec->entsize)
932 p = secinfo->contents
933 + ((offset + addend) / sec->entsize) * sec->entsize;
935 entry = sec_merge_hash_lookup (secinfo->htab, p, 0, FALSE);
938 if (! secinfo->htab->strings)
940 /* This should only happen if somebody points into the padding
941 after a NUL character but before next entity. */
944 if (! secinfo->htab->first)
946 entry = secinfo->htab->first;
947 p = secinfo->contents
948 + ((offset + addend) / sec->entsize + 1) * sec->entsize
952 *psec = entry->secinfo->sec;
953 return entry->u.index + (secinfo->contents + offset - p);