2 Copyright (C) 2001-2015 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* This file contains support for merging duplicate entities within sections,
24 as used in ELF SHF_MERGE. */
30 #include "libiberty.h"
32 struct sec_merge_sec_info;
34 /* An entry in the section merge hash table. */
36 struct sec_merge_hash_entry
38 struct bfd_hash_entry root;
39 /* Length of this entry. This includes the zero terminator. */
41 /* Start of this string needs to be aligned to
42 alignment octets (not 1 << align). */
43 unsigned int alignment;
46 /* Index within the merged section. */
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_str;
96 /* Original section content. */
97 unsigned char contents[1];
101 /* Routine to create an entry in a section merge hashtab. */
103 static struct bfd_hash_entry *
104 sec_merge_hash_newfunc (struct bfd_hash_entry *entry,
105 struct bfd_hash_table *table, const char *string)
107 /* Allocate the structure if it has not already been allocated by a
110 entry = (struct bfd_hash_entry *)
111 bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry));
115 /* Call the allocation method of the superclass. */
116 entry = bfd_hash_newfunc (entry, table, string);
120 /* Initialize the local fields. */
121 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
123 ret->u.suffix = NULL;
132 /* Look up an entry in a section merge hash table. */
134 static struct sec_merge_hash_entry *
135 sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
136 unsigned int alignment, bfd_boolean create)
138 const unsigned char *s;
141 struct sec_merge_hash_entry *hashp;
147 s = (const unsigned char *) string;
150 if (table->entsize == 1)
152 while ((c = *s++) != '\0')
154 hash += c + (c << 17);
158 hash += len + (len << 17);
164 for (i = 0; i < table->entsize; ++i)
167 if (i == table->entsize)
169 for (i = 0; i < table->entsize; ++i)
172 hash += c + (c << 17);
177 hash += len + (len << 17);
178 len *= table->entsize;
181 len += table->entsize;
185 for (i = 0; i < table->entsize; ++i)
188 hash += c + (c << 17);
191 len = table->entsize;
194 _index = hash % table->table.size;
195 for (hashp = (struct sec_merge_hash_entry *) table->table.table[_index];
197 hashp = (struct sec_merge_hash_entry *) hashp->root.next)
199 if (hashp->root.hash == hash
201 && memcmp (hashp->root.string, string, len) == 0)
203 /* If the string we found does not have at least the required
204 alignment, we need to insert another copy. */
205 if (hashp->alignment < alignment)
209 /* Mark the less aligned copy as deleted. */
211 hashp->alignment = 0;
222 hashp = ((struct sec_merge_hash_entry *)
223 bfd_hash_insert (&table->table, string, hash));
227 hashp->alignment = alignment;
231 /* Create a new hash table. */
233 static struct sec_merge_hash *
234 sec_merge_init (unsigned int entsize, bfd_boolean strings)
236 struct sec_merge_hash *table;
238 table = (struct sec_merge_hash *) bfd_malloc (sizeof (struct sec_merge_hash));
242 if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc,
243 sizeof (struct sec_merge_hash_entry), 16699))
252 table->entsize = entsize;
253 table->strings = strings;
258 /* Get the index of an entity in a hash table, adding it if it is not
261 static struct sec_merge_hash_entry *
262 sec_merge_add (struct sec_merge_hash *tab, const char *str,
263 unsigned int alignment, struct sec_merge_sec_info *secinfo)
265 struct sec_merge_hash_entry *entry;
267 entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
271 if (entry->secinfo == NULL)
274 entry->secinfo = secinfo;
275 if (tab->first == NULL)
278 tab->last->next = entry;
286 sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
288 struct sec_merge_sec_info *secinfo = entry->secinfo;
289 asection *sec = secinfo->sec;
291 bfd_size_type off = 0;
292 int alignment_power = sec->output_section->alignment_power;
296 pad = (char *) bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
301 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
306 len = -off & (entry->alignment - 1);
309 if (bfd_bwrite (pad, len, abfd) != len)
314 str = entry->root.string;
317 if (bfd_bwrite (str, len, abfd) != len)
323 /* Trailing alignment needed? */
324 off = sec->size - off;
326 && bfd_bwrite (pad, off, abfd) != off)
339 /* Register a SEC_MERGE section as a candidate for merging.
340 This function is called for all non-dynamic SEC_MERGE input sections. */
343 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
346 struct sec_merge_info *sinfo;
347 struct sec_merge_sec_info *secinfo;
352 if ((abfd->flags & DYNAMIC) != 0
353 || (sec->flags & SEC_MERGE) == 0)
357 || (sec->flags & SEC_EXCLUDE) != 0
358 || sec->entsize == 0)
361 if ((sec->flags & SEC_RELOC) != 0)
363 /* We aren't prepared to handle relocations in merged sections. */
367 align = sec->alignment_power;
368 if ((sec->entsize < (unsigned) 1 << align
369 && ((sec->entsize & (sec->entsize - 1))
370 || !(sec->flags & SEC_STRINGS)))
371 || (sec->entsize > (unsigned) 1 << align
372 && (sec->entsize & (((unsigned) 1 << align) - 1))))
374 /* Sanity check. If string character size is smaller than
375 alignment, then we require character size to be a power
376 of 2, otherwise character size must be integer multiple
377 of alignment. For non-string constants, alignment must
378 be smaller than or equal to entity size and entity size
379 must be integer multiple of alignment. */
383 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
384 if ((secinfo = sinfo->chain)
385 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
386 && secinfo->sec->entsize == sec->entsize
387 && secinfo->sec->alignment_power == sec->alignment_power
388 && secinfo->sec->output_section == sec->output_section)
393 /* Initialize the information we need to keep track of. */
394 sinfo = (struct sec_merge_info *)
395 bfd_alloc (abfd, sizeof (struct sec_merge_info));
398 sinfo->next = (struct sec_merge_info *) *psinfo;
401 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
402 if (sinfo->htab == NULL)
406 /* Read the section from abfd. */
408 amt = sizeof (struct sec_merge_sec_info) - 1 + sec->size;
409 if (sec->flags & SEC_STRINGS)
410 /* Some versions of gcc may emit a string without a zero terminator.
411 See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
412 Allocate space for an extra zero. */
414 *psecinfo = bfd_alloc (abfd, amt);
415 if (*psecinfo == NULL)
418 secinfo = (struct sec_merge_sec_info *) *psecinfo;
421 secinfo->next = sinfo->chain->next;
422 sinfo->chain->next = secinfo;
425 secinfo->next = secinfo;
426 sinfo->chain = secinfo;
428 secinfo->psecinfo = psecinfo;
429 secinfo->htab = sinfo->htab;
430 secinfo->first_str = NULL;
432 sec->rawsize = sec->size;
433 if (sec->flags & SEC_STRINGS)
434 memset (secinfo->contents + sec->size, 0, sec->entsize);
435 contents = secinfo->contents;
436 if (! bfd_get_full_section_contents (sec->owner, sec, &contents))
446 /* Record one section into the hash table. */
448 record_section (struct sec_merge_info *sinfo,
449 struct sec_merge_sec_info *secinfo)
451 asection *sec = secinfo->sec;
452 struct sec_merge_hash_entry *entry;
454 unsigned char *p, *end;
455 bfd_vma mask, eltalign;
456 unsigned int align, i;
458 align = sec->alignment_power;
459 end = secinfo->contents + sec->size;
461 mask = ((bfd_vma) 1 << align) - 1;
462 if (sec->flags & SEC_STRINGS)
464 for (p = secinfo->contents; p < end; )
466 eltalign = p - secinfo->contents;
467 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
468 if (!eltalign || eltalign > mask)
470 entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
475 if (sec->entsize == 1)
477 while (p < end && *p == 0)
479 if (!nul && !((p - secinfo->contents) & mask))
482 entry = sec_merge_add (sinfo->htab, "",
483 (unsigned) mask + 1, secinfo);
494 for (i = 0; i < sec->entsize; i++)
497 if (i != sec->entsize)
499 if (!nul && !((p - secinfo->contents) & mask))
502 entry = sec_merge_add (sinfo->htab, (char *) p,
503 (unsigned) mask + 1, secinfo);
514 for (p = secinfo->contents; p < end; p += sec->entsize)
516 entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
525 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
526 *secinfo->psecinfo = NULL;
531 strrevcmp (const void *a, const void *b)
533 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
534 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
535 unsigned int lenA = A->len;
536 unsigned int lenB = B->len;
537 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
538 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
539 int l = lenA < lenB ? lenA : lenB;
544 return (int) *s - (int) *t;
552 /* Like strrevcmp, but for the case where all strings have the same
553 alignment > entsize. */
556 strrevcmp_align (const void *a, const void *b)
558 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
559 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
560 unsigned int lenA = A->len;
561 unsigned int lenB = B->len;
562 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
563 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
564 int l = lenA < lenB ? lenA : lenB;
565 int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
573 return (int) *s - (int) *t;
582 is_suffix (const struct sec_merge_hash_entry *A,
583 const struct sec_merge_hash_entry *B)
585 if (A->len <= B->len)
586 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
587 not to be equal by the hash table. */
590 return memcmp (A->root.string + (A->len - B->len),
591 B->root.string, B->len) == 0;
594 /* This is a helper function for _bfd_merge_sections. It attempts to
595 merge strings matching suffixes of longer strings. */
597 merge_strings (struct sec_merge_info *sinfo)
599 struct sec_merge_hash_entry **array, **a, *e;
600 struct sec_merge_sec_info *secinfo;
601 bfd_size_type size, amt;
602 unsigned int alignment = 0;
604 /* Now sort the strings */
605 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
606 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
610 for (e = sinfo->htab->first, a = array; e; e = e->next)
614 /* Adjust the length to not include the zero terminator. */
615 e->len -= sinfo->htab->entsize;
616 if (alignment != e->alignment)
619 alignment = e->alignment;
621 alignment = (unsigned) -1;
625 sinfo->htab->size = a - array;
626 if (sinfo->htab->size != 0)
628 qsort (array, (size_t) sinfo->htab->size,
629 sizeof (struct sec_merge_hash_entry *),
630 (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
631 ? strrevcmp_align : strrevcmp));
633 /* Loop over the sorted array and merge suffixes */
635 e->len += sinfo->htab->entsize;
638 struct sec_merge_hash_entry *cmp = *a;
640 cmp->len += sinfo->htab->entsize;
641 if (e->alignment >= cmp->alignment
642 && !((e->len - cmp->len) & (cmp->alignment - 1))
643 && is_suffix (e, cmp))
657 /* Now assign positions to the strings we want to keep. */
659 secinfo = sinfo->htab->first->secinfo;
660 for (e = sinfo->htab->first; e; e = e->next)
662 if (e->secinfo != secinfo)
664 secinfo->sec->size = size;
665 secinfo = e->secinfo;
669 if (e->secinfo->first_str == NULL)
671 e->secinfo->first_str = e;
674 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
679 secinfo->sec->size = size;
680 if (secinfo->sec->alignment_power != 0)
682 bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
683 secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
686 /* And now adjust the rest, removing them from the chain (but not hashtable)
688 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
696 e->secinfo = e->u.suffix->secinfo;
697 e->alignment = e->u.suffix->alignment;
698 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
703 /* This function is called once after all SEC_MERGE sections are registered
704 with _bfd_merge_section. */
707 _bfd_merge_sections (bfd *abfd,
708 struct bfd_link_info *info ATTRIBUTE_UNUSED,
710 void (*remove_hook) (bfd *, asection *))
712 struct sec_merge_info *sinfo;
714 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
716 struct sec_merge_sec_info * secinfo;
721 /* Move sinfo->chain to head of the chain, terminate it. */
722 secinfo = sinfo->chain;
723 sinfo->chain = secinfo->next;
724 secinfo->next = NULL;
726 /* Record the sections into the hash table. */
727 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
728 if (secinfo->sec->flags & SEC_EXCLUDE)
730 *secinfo->psecinfo = NULL;
732 (*remove_hook) (abfd, secinfo->sec);
734 else if (! record_section (sinfo, secinfo))
740 if (sinfo->htab->first == NULL)
743 if (sinfo->htab->strings)
744 merge_strings (sinfo);
747 struct sec_merge_hash_entry *e;
748 bfd_size_type size = 0;
750 /* Things are much simpler for non-strings.
751 Just assign them slots in the section. */
753 for (e = sinfo->htab->first; e; e = e->next)
755 if (e->secinfo->first_str == NULL)
758 secinfo->sec->size = size;
759 e->secinfo->first_str = e;
762 size = (size + e->alignment - 1)
763 & ~((bfd_vma) e->alignment - 1);
766 secinfo = e->secinfo;
768 secinfo->sec->size = size;
771 /* Finally remove all input sections which have not made it into
772 the hash table at all. */
773 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
774 if (secinfo->first_str == NULL)
775 secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
781 /* Write out the merged section. */
784 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
786 struct sec_merge_sec_info *secinfo;
789 secinfo = (struct sec_merge_sec_info *) psecinfo;
794 if (secinfo->first_str == NULL)
797 /* FIXME: octets_per_byte. */
798 pos = sec->output_section->filepos + sec->output_offset;
799 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
802 if (! sec_merge_emit (output_bfd, secinfo->first_str))
808 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
809 *PSEC, this returns the new offset in the adjusted SEC_MERGE
810 section and writes the new section back into *PSEC. */
813 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
814 void *psecinfo, bfd_vma offset)
816 struct sec_merge_sec_info *secinfo;
817 struct sec_merge_hash_entry *entry;
819 asection *sec = *psec;
821 secinfo = (struct sec_merge_sec_info *) psecinfo;
826 if (offset >= sec->rawsize)
828 if (offset > sec->rawsize)
830 (*_bfd_error_handler)
831 (_("%s: access beyond end of merged section (%ld)"),
832 bfd_get_filename (sec->owner), (long) offset);
834 return secinfo->first_str ? sec->size : 0;
837 if (secinfo->htab->strings)
839 if (sec->entsize == 1)
841 p = secinfo->contents + offset - 1;
842 while (p >= secinfo->contents && *p)
848 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
850 while (p >= secinfo->contents)
854 for (i = 0; i < sec->entsize; ++i)
857 if (i == sec->entsize)
866 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
868 entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
871 if (! secinfo->htab->strings)
873 /* This should only happen if somebody points into the padding
874 after a NUL character but before next entity. */
877 if (! secinfo->htab->first)
879 entry = secinfo->htab->first;
880 p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
884 *psec = entry->secinfo->sec;
885 return entry->u.index + (secinfo->contents + offset - p);
888 /* Tidy up when done. */
891 _bfd_merge_sections_free (void *xsinfo)
893 struct sec_merge_info *sinfo;
895 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
897 bfd_hash_table_free (&sinfo->htab->table);