2 Copyright 2001 Free Software Foundation, Inc.
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. */
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. */
38 /* Start of this string needs to be aligned to
39 alignment octets (not 1 << align). */
40 unsigned int alignment;
42 /* Index within the merged section. */
44 /* Entity size (if present in suffix hash tables). */
46 /* Entry this is a suffix of (if alignment is 0). */
47 struct sec_merge_hash_entry *suffix;
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. */
59 struct bfd_hash_table table;
60 /* Next available index. */
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;
68 /* Are entries fixed size or zero terminated strings? */
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. */
88 /* Pointer to merge_info pointing to us. */
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;
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
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)
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));
140 /* Initialize the local fields. */
141 ret->u.suffix = 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;
156 unsigned int alignment;
159 register const unsigned char *s;
160 register unsigned long hash;
161 register unsigned int c;
162 struct sec_merge_hash_entry *hashp;
168 s = (const unsigned char *) string;
171 if (table->entsize == 1)
173 while ((c = *s++) != '\0')
175 hash += c + (c << 17);
179 hash += len + (len << 17);
185 for (i = 0; i < table->entsize; ++i)
188 if (i == table->entsize)
190 for (i = 0; i < table->entsize; ++i)
193 hash += c + (c << 17);
198 hash += len + (len << 17);
199 len *= table->entsize;
202 len += table->entsize;
206 for (i = 0; i < table->entsize; ++i)
209 hash += c + (c << 17);
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
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. */
230 hashp->alignment = 0;
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;
248 hashp->alignment = alignment;
249 hashp->root.next = table->table.table[index];
250 table->table.table[index] = (struct bfd_hash_entry *) hashp;
255 /* Create a new hash table. */
257 static struct sec_merge_hash *
258 sec_merge_init (entsize, strings)
259 unsigned int entsize;
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);
269 if (! bfd_hash_table_init (&table->table, sec_merge_hash_newfunc))
278 table->entsize = entsize;
279 table->strings = strings;
284 /* Get the index of an entity in a hash table, adding it if it is not
287 static struct sec_merge_hash_entry *
288 sec_merge_add (tab, str, alignment, secinfo)
289 struct sec_merge_hash *tab;
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);
300 if (entry->secinfo == NULL)
303 entry->secinfo = secinfo;
304 if (tab->first == NULL)
307 tab->last->next = entry;
315 sec_merge_emit (abfd, entry)
317 struct sec_merge_hash_entry *entry;
319 struct sec_merge_sec_info *secinfo = entry->secinfo;
320 asection *sec = secinfo->sec;
322 bfd_size_type off = 0;
323 int alignment_power = bfd_get_section_alignment (abfd, sec->output_section);
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;
333 len = off & (entry->alignment - 1);
336 len = entry->alignment - len;
337 if (bfd_bwrite ((PTR) pad, (bfd_size_type) len, abfd) != len)
342 str = entry->root.string;
345 if (bfd_bwrite ((PTR) str, (bfd_size_type) len, abfd) != len)
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. */
361 _bfd_merge_section (abfd, psinfo, sec, psecinfo)
367 struct sec_merge_info *sinfo;
368 struct sec_merge_sec_info *secinfo;
372 if (sec->_raw_size == 0
373 || (sec->flags & SEC_EXCLUDE)
374 || (sec->flags & SEC_MERGE) == 0
375 || sec->entsize == 0)
378 if ((sec->flags & SEC_RELOC) != 0)
380 /* We aren't prepared to handle relocations in merged sections. */
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
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. */
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))
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));
422 sinfo->next = (struct sec_merge_info *) *psinfo;
424 *psinfo = (PTR) sinfo;
426 sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
427 if (sinfo->htab == NULL)
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)
438 secinfo = (struct sec_merge_sec_info *)*psecinfo;
441 secinfo->next = sinfo->chain->next;
442 sinfo->chain->next = secinfo;
445 secinfo->next = secinfo;
446 sinfo->chain = secinfo;
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))
463 /* Compare two sec_merge_hash_entry structures. This is called via qsort. */
466 cmplengthentry (a, 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;
475 else if (A->len > B->len)
478 return memcmp (A->root.string, B->root.string, A->len);
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. */
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. */
500 if (A->alignment < B->alignment
501 || ((A->len - B->len) & (B->alignment - 1)))
502 /* The suffix is not sufficiently aligned. */
505 return memcmp (A->root.string + (A->len - B->len),
506 B->root.string, B->len - 5 * A->u.entsize) == 0;
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. */
522 if (memcmp (A->root.string + A->len - 2 * A->u.entsize,
523 B->root.string + B->len - 2 * A->u.entsize,
525 /* This was a hashtable collision. */
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. */
533 if (A->alignment < B->alignment
534 || ((A->len - B->len) & (B->alignment - 1)))
535 /* The suffix is not sufficiently aligned. */
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. */
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;
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;
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)
567 entry = sec_merge_add (sinfo->htab, p, (unsigned) eltalign, secinfo);
571 if (sec->entsize == 1)
573 while (p < end && *p == 0)
575 if (!nul && !((p - secinfo->contents) & mask))
578 entry = sec_merge_add (sinfo->htab, "",
579 (unsigned) mask + 1, secinfo);
590 for (i = 0; i < sec->entsize; i++)
593 if (i != sec->entsize)
595 if (!nul && !((p - secinfo->contents) & mask))
598 entry = sec_merge_add (sinfo->htab, p,
599 (unsigned) mask + 1, secinfo);
610 for (p = secinfo->contents; p < end; p += sec->entsize)
612 entry = sec_merge_add (sinfo->htab, p, 1, secinfo);
621 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
622 *secinfo->psecinfo = NULL;
626 /* This is a helper function for _bfd_merge_sections. It attempts to
627 merge strings matching suffixes of longer strings. */
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. */
639 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
640 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
644 for (e = sinfo->htab->first, a = array; e; e = e->next)
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)
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
661 for (a = array, end = array + sinfo->htab->size; a < end; a++)
663 register hashval_t hash;
666 const unsigned char *s;
670 e->u.entsize = sinfo->htab->entsize;
671 if (e->len <= e->u.entsize)
673 if (e->len > 4 * e->u.entsize)
675 s = e->root.string + e->len - e->u.entsize;
677 for (i = 0; i < 4 * e->u.entsize; i++)
680 hash += c + (c << 17);
683 p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
688 struct sec_merge_hash_entry *ent;
690 ent = (struct sec_merge_hash_entry *) *p;
698 s = e->root.string + e->len - e->u.entsize;
700 for (i = 0; i < e->u.entsize; i++)
703 hash += c + (c << 17);
706 p = htab_find_slot_with_hash (lasttab, e, hash, INSERT);
711 struct sec_merge_hash_entry *ent;
713 ent = (struct sec_merge_hash_entry *) *p;
725 htab_delete (lasttab);
727 htab_delete (last4tab);
729 /* Now assign positions to the strings we want to keep. */
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;
741 if (e->secinfo->first == NULL)
743 e->secinfo->first = e;
746 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
751 secinfo->sec->_cooked_size = size;
753 /* And now adjust the rest, removing them from the chain (but not hashtable)
755 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
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. */
774 _bfd_merge_sections (abfd, xsinfo)
775 bfd *abfd ATTRIBUTE_UNUSED;
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)
795 *secinfo->psecinfo = NULL;
796 else if (! record_section (sinfo, secinfo))
802 if (sinfo->htab->strings)
803 merge_strings (sinfo);
806 struct sec_merge_hash_entry *e;
807 bfd_size_type size = 0;
809 /* Things are much simpler for non-strings.
810 Just assign them slots in the section. */
812 for (e = sinfo->htab->first; e; e = e->next)
814 if (e->secinfo->first == NULL)
817 secinfo->sec->_cooked_size = size;
818 e->secinfo->first = e;
821 size = (size + e->alignment - 1)
822 & ~((bfd_vma) e->alignment - 1);
825 secinfo = e->secinfo;
827 secinfo->sec->_cooked_size = size;
830 /* Finally shrink all input sections which have not made it into
831 the hash table at all. */
832 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
833 if (secinfo->first == NULL)
835 secinfo->sec->_cooked_size = 0;
836 secinfo->sec->flags |= SEC_EXCLUDE;
843 /* Write out the merged section. */
846 _bfd_write_merged_section (output_bfd, sec, psecinfo)
851 struct sec_merge_sec_info *secinfo;
854 secinfo = (struct sec_merge_sec_info *) psecinfo;
859 pos = sec->output_section->filepos + sec->output_offset;
860 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
863 if (! sec_merge_emit (output_bfd, secinfo->first))
869 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
870 *PSEC, this returns the new offset in the adjusted SEC_MERGE
871 section and writes the new section back into *PSEC. */
874 _bfd_merged_section_offset (output_bfd, psec, psecinfo, offset, addend)
875 bfd *output_bfd ATTRIBUTE_UNUSED;
878 bfd_vma offset, addend;
880 struct sec_merge_sec_info *secinfo;
881 struct sec_merge_hash_entry *entry;
883 asection *sec = *psec;
885 secinfo = (struct sec_merge_sec_info *) psecinfo;
887 if (offset + addend >= sec->_raw_size)
889 if (offset + addend > sec->_raw_size)
890 (*_bfd_error_handler) (_("%s: access beyond end of merged section (%ld + %ld)"),
891 bfd_get_filename (sec->owner), (long)offset,
893 return (secinfo->first ? sec->_cooked_size : 0);
896 if (secinfo->htab->strings)
898 if (sec->entsize == 1)
900 p = secinfo->contents + offset + addend - 1;
901 while (*p && p >= secinfo->contents)
907 p = secinfo->contents
908 + ((offset + addend) / sec->entsize) * sec->entsize;
910 while (p >= secinfo->contents)
914 for (i = 0; i < sec->entsize; ++i)
917 if (i == sec->entsize)
926 p = secinfo->contents
927 + ((offset + addend) / sec->entsize) * sec->entsize;
929 entry = sec_merge_hash_lookup (secinfo->htab, p, 0, false);
932 if (! secinfo->htab->strings)
934 /* This should only happen if somebody points into the padding
935 after a NUL character but before next entity. */
938 if (! secinfo->htab->first)
940 entry = secinfo->htab->first;
941 p = secinfo->contents
942 + ((offset + addend) / sec->entsize + 1) * sec->entsize
946 *psec = entry->secinfo->sec;
947 return entry->u.index + (secinfo->contents + offset - p);