2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Jakub Jelinek <jakub@redhat.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 /* This file contains support for merging duplicate entities within sections,
25 as used in ELF SHF_MERGE. */
31 #include "libiberty.h"
33 struct sec_merge_sec_info
;
35 /* An entry in the section merge hash table. */
37 struct sec_merge_hash_entry
39 struct bfd_hash_entry root
;
40 /* Length of this entry. This includes the zero terminator. */
42 /* Start of this string needs to be aligned to
43 alignment octets (not 1 << align). */
44 unsigned int alignment
;
47 /* Index within the merged section. */
49 /* Entry this is a suffix of (if alignment is 0). */
50 struct sec_merge_hash_entry
*suffix
;
52 /* Which section is it in. */
53 struct sec_merge_sec_info
*secinfo
;
54 /* Next entity in the hash table. */
55 struct sec_merge_hash_entry
*next
;
58 /* The section merge hash table. */
62 struct bfd_hash_table table
;
63 /* Next available index. */
65 /* First entity in the SEC_MERGE sections of this type. */
66 struct sec_merge_hash_entry
*first
;
67 /* Last entity in the SEC_MERGE sections of this type. */
68 struct sec_merge_hash_entry
*last
;
71 /* Are entries fixed size or zero terminated strings? */
77 /* Chain of sec_merge_infos. */
78 struct sec_merge_info
*next
;
79 /* Chain of sec_merge_sec_infos. */
80 struct sec_merge_sec_info
*chain
;
81 /* A hash table used to hold section content. */
82 struct sec_merge_hash
*htab
;
85 struct sec_merge_sec_info
87 /* Chain of sec_merge_sec_infos. */
88 struct sec_merge_sec_info
*next
;
89 /* The corresponding section. */
91 /* Pointer to merge_info pointing to us. */
93 /* A hash table used to hold section content. */
94 struct sec_merge_hash
*htab
;
95 /* First string in this section. */
96 struct sec_merge_hash_entry
*first_str
;
97 /* Original section content. */
98 unsigned char contents
[1];
102 /* Routine to create an entry in a section merge hashtab. */
104 static struct bfd_hash_entry
*
105 sec_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
106 struct bfd_hash_table
*table
, const char *string
)
108 /* Allocate the structure if it has not already been allocated by a
111 entry
= 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 register const unsigned char *s
;
139 register unsigned long hash
;
140 register unsigned int c
;
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
= 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 register 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
= 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
;
351 if ((abfd
->flags
& DYNAMIC
) != 0
352 || (sec
->flags
& SEC_MERGE
) == 0)
356 || (sec
->flags
& SEC_EXCLUDE
) != 0
357 || sec
->entsize
== 0)
360 if ((sec
->flags
& SEC_RELOC
) != 0)
362 /* We aren't prepared to handle relocations in merged sections. */
366 align
= sec
->alignment_power
;
367 if ((sec
->entsize
< (unsigned) 1 << align
368 && ((sec
->entsize
& (sec
->entsize
- 1))
369 || !(sec
->flags
& SEC_STRINGS
)))
370 || (sec
->entsize
> (unsigned) 1 << align
371 && (sec
->entsize
& (((unsigned) 1 << align
) - 1))))
373 /* Sanity check. If string character size is smaller than
374 alignment, then we require character size to be a power
375 of 2, otherwise character size must be integer multiple
376 of alignment. For non-string constants, alignment must
377 be smaller than or equal to entity size and entity size
378 must be integer multiple of alignment. */
382 for (sinfo
= (struct sec_merge_info
*) *psinfo
; sinfo
; sinfo
= sinfo
->next
)
383 if ((secinfo
= sinfo
->chain
)
384 && ! ((secinfo
->sec
->flags
^ sec
->flags
) & (SEC_MERGE
| SEC_STRINGS
))
385 && secinfo
->sec
->entsize
== sec
->entsize
386 && secinfo
->sec
->alignment_power
== sec
->alignment_power
387 && secinfo
->sec
->output_section
== sec
->output_section
)
392 /* Initialize the information we need to keep track of. */
393 sinfo
= bfd_alloc (abfd
, sizeof (struct sec_merge_info
));
396 sinfo
->next
= (struct sec_merge_info
*) *psinfo
;
399 sinfo
->htab
= sec_merge_init (sec
->entsize
, (sec
->flags
& SEC_STRINGS
));
400 if (sinfo
->htab
== NULL
)
404 /* Read the section from abfd. */
406 amt
= sizeof (struct sec_merge_sec_info
) - 1 + sec
->size
;
407 if (sec
->flags
& SEC_STRINGS
)
408 /* Some versions of gcc may emit a string without a zero terminator.
409 See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
410 Allocate space for an extra zero. */
412 *psecinfo
= bfd_alloc (abfd
, amt
);
413 if (*psecinfo
== NULL
)
416 secinfo
= (struct sec_merge_sec_info
*) *psecinfo
;
419 secinfo
->next
= sinfo
->chain
->next
;
420 sinfo
->chain
->next
= secinfo
;
423 secinfo
->next
= secinfo
;
424 sinfo
->chain
= secinfo
;
426 secinfo
->psecinfo
= psecinfo
;
427 secinfo
->htab
= sinfo
->htab
;
428 secinfo
->first_str
= NULL
;
430 sec
->rawsize
= sec
->size
;
431 if (sec
->flags
& SEC_STRINGS
)
432 memset (secinfo
->contents
+ sec
->size
, 0, sec
->entsize
);
433 if (! bfd_get_section_contents (sec
->owner
, sec
, secinfo
->contents
,
444 /* Record one section into the hash table. */
446 record_section (struct sec_merge_info
*sinfo
,
447 struct sec_merge_sec_info
*secinfo
)
449 asection
*sec
= secinfo
->sec
;
450 struct sec_merge_hash_entry
*entry
;
452 unsigned char *p
, *end
;
453 bfd_vma mask
, eltalign
;
454 unsigned int align
, i
;
456 align
= sec
->alignment_power
;
457 end
= secinfo
->contents
+ sec
->size
;
459 mask
= ((bfd_vma
) 1 << align
) - 1;
460 if (sec
->flags
& SEC_STRINGS
)
462 for (p
= secinfo
->contents
; p
< end
; )
464 eltalign
= p
- secinfo
->contents
;
465 eltalign
= ((eltalign
^ (eltalign
- 1)) + 1) >> 1;
466 if (!eltalign
|| eltalign
> mask
)
468 entry
= sec_merge_add (sinfo
->htab
, (char *) p
, (unsigned) eltalign
,
473 if (sec
->entsize
== 1)
475 while (p
< end
&& *p
== 0)
477 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
480 entry
= sec_merge_add (sinfo
->htab
, "",
481 (unsigned) mask
+ 1, secinfo
);
492 for (i
= 0; i
< sec
->entsize
; i
++)
495 if (i
!= sec
->entsize
)
497 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
500 entry
= sec_merge_add (sinfo
->htab
, (char *) p
,
501 (unsigned) mask
+ 1, secinfo
);
512 for (p
= secinfo
->contents
; p
< end
; p
+= sec
->entsize
)
514 entry
= sec_merge_add (sinfo
->htab
, (char *) p
, 1, secinfo
);
523 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
524 *secinfo
->psecinfo
= NULL
;
529 strrevcmp (const void *a
, const void *b
)
531 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
532 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
533 unsigned int lenA
= A
->len
;
534 unsigned int lenB
= B
->len
;
535 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
536 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
537 int l
= lenA
< lenB
? lenA
: lenB
;
542 return (int) *s
- (int) *t
;
550 /* Like strrevcmp, but for the case where all strings have the same
551 alignment > entsize. */
554 strrevcmp_align (const void *a
, const void *b
)
556 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
557 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
558 unsigned int lenA
= A
->len
;
559 unsigned int lenB
= B
->len
;
560 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
561 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
562 int l
= lenA
< lenB
? lenA
: lenB
;
563 int tail_align
= (lenA
& (A
->alignment
- 1)) - (lenB
& (A
->alignment
- 1));
571 return (int) *s
- (int) *t
;
580 is_suffix (const struct sec_merge_hash_entry
*A
,
581 const struct sec_merge_hash_entry
*B
)
583 if (A
->len
<= B
->len
)
584 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
585 not to be equal by the hash table. */
588 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
589 B
->root
.string
, B
->len
) == 0;
592 /* This is a helper function for _bfd_merge_sections. It attempts to
593 merge strings matching suffixes of longer strings. */
595 merge_strings (struct sec_merge_info
*sinfo
)
597 struct sec_merge_hash_entry
**array
, **a
, *e
;
598 struct sec_merge_sec_info
*secinfo
;
599 bfd_size_type size
, amt
;
600 unsigned int alignment
= 0;
602 /* Now sort the strings */
603 amt
= sinfo
->htab
->size
* sizeof (struct sec_merge_hash_entry
*);
604 array
= bfd_malloc (amt
);
608 for (e
= sinfo
->htab
->first
, a
= array
; e
; e
= e
->next
)
612 /* Adjust the length to not include the zero terminator. */
613 e
->len
-= sinfo
->htab
->entsize
;
614 if (alignment
!= e
->alignment
)
617 alignment
= e
->alignment
;
619 alignment
= (unsigned) -1;
623 sinfo
->htab
->size
= a
- array
;
624 if (sinfo
->htab
->size
!= 0)
626 qsort (array
, (size_t) sinfo
->htab
->size
,
627 sizeof (struct sec_merge_hash_entry
*),
628 (alignment
!= (unsigned) -1 && alignment
> sinfo
->htab
->entsize
629 ? strrevcmp_align
: strrevcmp
));
631 /* Loop over the sorted array and merge suffixes */
633 e
->len
+= sinfo
->htab
->entsize
;
636 struct sec_merge_hash_entry
*cmp
= *a
;
638 cmp
->len
+= sinfo
->htab
->entsize
;
639 if (e
->alignment
>= cmp
->alignment
640 && !((e
->len
- cmp
->len
) & (cmp
->alignment
- 1))
641 && is_suffix (e
, cmp
))
655 /* Now assign positions to the strings we want to keep. */
657 secinfo
= sinfo
->htab
->first
->secinfo
;
658 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
660 if (e
->secinfo
!= secinfo
)
662 secinfo
->sec
->size
= size
;
663 secinfo
= e
->secinfo
;
667 if (e
->secinfo
->first_str
== NULL
)
669 e
->secinfo
->first_str
= e
;
672 size
= (size
+ e
->alignment
- 1) & ~((bfd_vma
) e
->alignment
- 1);
677 secinfo
->sec
->size
= size
;
678 if (secinfo
->sec
->alignment_power
!= 0)
680 bfd_size_type align
= (bfd_size_type
) 1 << secinfo
->sec
->alignment_power
;
681 secinfo
->sec
->size
= (secinfo
->sec
->size
+ align
- 1) & -align
;
684 /* And now adjust the rest, removing them from the chain (but not hashtable)
686 for (a
= &sinfo
->htab
->first
, e
= *a
; e
; e
= e
->next
)
694 e
->secinfo
= e
->u
.suffix
->secinfo
;
695 e
->alignment
= e
->u
.suffix
->alignment
;
696 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
- e
->len
);
701 /* This function is called once after all SEC_MERGE sections are registered
702 with _bfd_merge_section. */
705 _bfd_merge_sections (bfd
*abfd
,
706 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
708 void (*remove_hook
) (bfd
*, asection
*))
710 struct sec_merge_info
*sinfo
;
712 for (sinfo
= (struct sec_merge_info
*) xsinfo
; sinfo
; sinfo
= sinfo
->next
)
714 struct sec_merge_sec_info
* secinfo
;
719 /* Move sinfo->chain to head of the chain, terminate it. */
720 secinfo
= sinfo
->chain
;
721 sinfo
->chain
= secinfo
->next
;
722 secinfo
->next
= NULL
;
724 /* Record the sections into the hash table. */
725 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
726 if (secinfo
->sec
->flags
& SEC_EXCLUDE
)
728 *secinfo
->psecinfo
= NULL
;
730 (*remove_hook
) (abfd
, secinfo
->sec
);
732 else if (! record_section (sinfo
, secinfo
))
738 if (sinfo
->htab
->first
== NULL
)
741 if (sinfo
->htab
->strings
)
742 merge_strings (sinfo
);
745 struct sec_merge_hash_entry
*e
;
746 bfd_size_type size
= 0;
748 /* Things are much simpler for non-strings.
749 Just assign them slots in the section. */
751 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
753 if (e
->secinfo
->first_str
== NULL
)
756 secinfo
->sec
->size
= size
;
757 e
->secinfo
->first_str
= e
;
760 size
= (size
+ e
->alignment
- 1)
761 & ~((bfd_vma
) e
->alignment
- 1);
764 secinfo
= e
->secinfo
;
766 secinfo
->sec
->size
= size
;
769 /* Finally remove all input sections which have not made it into
770 the hash table at all. */
771 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
772 if (secinfo
->first_str
== NULL
)
773 secinfo
->sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
779 /* Write out the merged section. */
782 _bfd_write_merged_section (bfd
*output_bfd
, asection
*sec
, void *psecinfo
)
784 struct sec_merge_sec_info
*secinfo
;
787 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
792 if (secinfo
->first_str
== NULL
)
795 pos
= sec
->output_section
->filepos
+ sec
->output_offset
;
796 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0)
799 if (! sec_merge_emit (output_bfd
, secinfo
->first_str
))
805 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
806 *PSEC, this returns the new offset in the adjusted SEC_MERGE
807 section and writes the new section back into *PSEC. */
810 _bfd_merged_section_offset (bfd
*output_bfd ATTRIBUTE_UNUSED
, asection
**psec
,
811 void *psecinfo
, bfd_vma offset
)
813 struct sec_merge_sec_info
*secinfo
;
814 struct sec_merge_hash_entry
*entry
;
816 asection
*sec
= *psec
;
818 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
823 if (offset
>= sec
->rawsize
)
825 if (offset
> sec
->rawsize
)
827 (*_bfd_error_handler
)
828 (_("%s: access beyond end of merged section (%ld)"),
829 bfd_get_filename (sec
->owner
), (long) offset
);
831 return secinfo
->first_str
? sec
->size
: 0;
834 if (secinfo
->htab
->strings
)
836 if (sec
->entsize
== 1)
838 p
= secinfo
->contents
+ offset
- 1;
839 while (p
>= secinfo
->contents
&& *p
)
845 p
= secinfo
->contents
+ (offset
/ sec
->entsize
) * sec
->entsize
;
847 while (p
>= secinfo
->contents
)
851 for (i
= 0; i
< sec
->entsize
; ++i
)
854 if (i
== sec
->entsize
)
863 p
= secinfo
->contents
+ (offset
/ sec
->entsize
) * sec
->entsize
;
865 entry
= sec_merge_hash_lookup (secinfo
->htab
, (char *) p
, 0, FALSE
);
868 if (! secinfo
->htab
->strings
)
870 /* This should only happen if somebody points into the padding
871 after a NUL character but before next entity. */
874 if (! secinfo
->htab
->first
)
876 entry
= secinfo
->htab
->first
;
877 p
= (secinfo
->contents
+ (offset
/ sec
->entsize
+ 1) * sec
->entsize
881 *psec
= entry
->secinfo
->sec
;
882 return entry
->u
.index
+ (secinfo
->contents
+ offset
- p
);