2 Copyright 2001, 2002, 2003, 2004, 2005 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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. This includes the zero terminator. */
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 /* 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_str
;
94 /* Original section content. */
95 unsigned char contents
[1];
99 /* Routine to create an entry in a section merge hashtab. */
101 static struct bfd_hash_entry
*
102 sec_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
103 struct bfd_hash_table
*table
, const char *string
)
105 /* Allocate the structure if it has not already been allocated by a
108 entry
= bfd_hash_allocate (table
, sizeof (struct sec_merge_hash_entry
));
112 /* Call the allocation method of the superclass. */
113 entry
= bfd_hash_newfunc (entry
, table
, string
);
117 /* Initialize the local fields. */
118 struct sec_merge_hash_entry
*ret
= (struct sec_merge_hash_entry
*) entry
;
120 ret
->u
.suffix
= NULL
;
129 /* Look up an entry in a section merge hash table. */
131 static struct sec_merge_hash_entry
*
132 sec_merge_hash_lookup (struct sec_merge_hash
*table
, const char *string
,
133 unsigned int alignment
, bfd_boolean create
)
135 register const unsigned char *s
;
136 register unsigned long hash
;
137 register unsigned int c
;
138 struct sec_merge_hash_entry
*hashp
;
144 s
= (const unsigned char *) string
;
147 if (table
->entsize
== 1)
149 while ((c
= *s
++) != '\0')
151 hash
+= c
+ (c
<< 17);
155 hash
+= len
+ (len
<< 17);
161 for (i
= 0; i
< table
->entsize
; ++i
)
164 if (i
== table
->entsize
)
166 for (i
= 0; i
< table
->entsize
; ++i
)
169 hash
+= c
+ (c
<< 17);
174 hash
+= len
+ (len
<< 17);
175 len
*= table
->entsize
;
178 len
+= table
->entsize
;
182 for (i
= 0; i
< table
->entsize
; ++i
)
185 hash
+= c
+ (c
<< 17);
188 len
= table
->entsize
;
191 index
= hash
% table
->table
.size
;
192 for (hashp
= (struct sec_merge_hash_entry
*) table
->table
.table
[index
];
194 hashp
= (struct sec_merge_hash_entry
*) hashp
->root
.next
)
196 if (hashp
->root
.hash
== hash
198 && memcmp (hashp
->root
.string
, string
, len
) == 0)
200 /* If the string we found does not have at least the required
201 alignment, we need to insert another copy. */
202 if (hashp
->alignment
< alignment
)
206 /* Mark the less aligned copy as deleted. */
208 hashp
->alignment
= 0;
219 hashp
= ((struct sec_merge_hash_entry
*)
220 sec_merge_hash_newfunc (NULL
, &table
->table
, string
));
223 hashp
->root
.string
= string
;
224 hashp
->root
.hash
= hash
;
226 hashp
->alignment
= alignment
;
227 hashp
->root
.next
= table
->table
.table
[index
];
228 table
->table
.table
[index
] = (struct bfd_hash_entry
*) hashp
;
233 /* Create a new hash table. */
235 static struct sec_merge_hash
*
236 sec_merge_init (unsigned int entsize
, bfd_boolean strings
)
238 struct sec_merge_hash
*table
;
240 table
= bfd_malloc (sizeof (struct sec_merge_hash
));
244 if (! bfd_hash_table_init_n (&table
->table
, sec_merge_hash_newfunc
,
254 table
->entsize
= entsize
;
255 table
->strings
= strings
;
260 /* Get the index of an entity in a hash table, adding it if it is not
263 static struct sec_merge_hash_entry
*
264 sec_merge_add (struct sec_merge_hash
*tab
, const char *str
,
265 unsigned int alignment
, struct sec_merge_sec_info
*secinfo
)
267 register struct sec_merge_hash_entry
*entry
;
269 entry
= sec_merge_hash_lookup (tab
, str
, alignment
, TRUE
);
273 if (entry
->secinfo
== NULL
)
276 entry
->secinfo
= secinfo
;
277 if (tab
->first
== NULL
)
280 tab
->last
->next
= entry
;
288 sec_merge_emit (bfd
*abfd
, struct sec_merge_hash_entry
*entry
)
290 struct sec_merge_sec_info
*secinfo
= entry
->secinfo
;
291 asection
*sec
= secinfo
->sec
;
293 bfd_size_type off
= 0;
294 int alignment_power
= sec
->output_section
->alignment_power
;
298 pad
= bfd_zmalloc ((bfd_size_type
) 1 << alignment_power
);
303 for (; entry
!= NULL
&& entry
->secinfo
== secinfo
; entry
= entry
->next
)
308 len
= -off
& (entry
->alignment
- 1);
311 if (bfd_bwrite (pad
, len
, abfd
) != len
)
316 str
= entry
->root
.string
;
319 if (bfd_bwrite (str
, len
, abfd
) != len
)
325 /* Trailing alignment needed? */
326 off
= sec
->size
- off
;
328 && bfd_bwrite (pad
, off
, abfd
) != off
)
341 /* Register a SEC_MERGE section as a candidate for merging.
342 This function is called for all non-dynamic SEC_MERGE input sections. */
345 _bfd_add_merge_section (bfd
*abfd
, void **psinfo
, asection
*sec
,
348 struct sec_merge_info
*sinfo
;
349 struct sec_merge_sec_info
*secinfo
;
353 if ((abfd
->flags
& DYNAMIC
) != 0
354 || (sec
->flags
& SEC_MERGE
) == 0)
358 || (sec
->flags
& SEC_EXCLUDE
) != 0
359 || sec
->entsize
== 0)
362 if ((sec
->flags
& SEC_RELOC
) != 0)
364 /* We aren't prepared to handle relocations in merged sections. */
368 align
= sec
->alignment_power
;
369 if ((sec
->entsize
< (unsigned) 1 << align
370 && ((sec
->entsize
& (sec
->entsize
- 1))
371 || !(sec
->flags
& SEC_STRINGS
)))
372 || (sec
->entsize
> (unsigned) 1 << align
373 && (sec
->entsize
& (((unsigned) 1 << align
) - 1))))
375 /* Sanity check. If string character size is smaller than
376 alignment, then we require character size to be a power
377 of 2, otherwise character size must be integer multiple
378 of alignment. For non-string constants, alignment must
379 be smaller than or equal to entity size and entity size
380 must be integer multiple of alignment. */
384 for (sinfo
= (struct sec_merge_info
*) *psinfo
; sinfo
; sinfo
= sinfo
->next
)
385 if ((secinfo
= sinfo
->chain
)
386 && ! ((secinfo
->sec
->flags
^ sec
->flags
) & (SEC_MERGE
| SEC_STRINGS
))
387 && secinfo
->sec
->entsize
== sec
->entsize
388 && secinfo
->sec
->alignment_power
== sec
->alignment_power
389 && secinfo
->sec
->output_section
== sec
->output_section
)
394 /* Initialize the information we need to keep track of. */
395 sinfo
= 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
) + sec
->size
- 1;
409 *psecinfo
= bfd_alloc (abfd
, amt
);
410 if (*psecinfo
== NULL
)
413 secinfo
= (struct sec_merge_sec_info
*) *psecinfo
;
416 secinfo
->next
= sinfo
->chain
->next
;
417 sinfo
->chain
->next
= secinfo
;
420 secinfo
->next
= secinfo
;
421 sinfo
->chain
= secinfo
;
423 secinfo
->psecinfo
= psecinfo
;
424 secinfo
->htab
= sinfo
->htab
;
425 secinfo
->first_str
= NULL
;
427 sec
->rawsize
= sec
->size
;
428 if (! bfd_get_section_contents (sec
->owner
, sec
, secinfo
->contents
,
439 /* Record one section into the hash table. */
441 record_section (struct sec_merge_info
*sinfo
,
442 struct sec_merge_sec_info
*secinfo
)
444 asection
*sec
= secinfo
->sec
;
445 struct sec_merge_hash_entry
*entry
;
447 unsigned char *p
, *end
;
448 bfd_vma mask
, eltalign
;
449 unsigned int align
, i
;
451 align
= sec
->alignment_power
;
452 end
= secinfo
->contents
+ sec
->size
;
454 mask
= ((bfd_vma
) 1 << align
) - 1;
455 if (sec
->flags
& SEC_STRINGS
)
457 for (p
= secinfo
->contents
; p
< end
; )
459 eltalign
= p
- secinfo
->contents
;
460 eltalign
= ((eltalign
^ (eltalign
- 1)) + 1) >> 1;
461 if (!eltalign
|| eltalign
> mask
)
463 entry
= sec_merge_add (sinfo
->htab
, (char *) p
, (unsigned) eltalign
,
468 if (sec
->entsize
== 1)
470 while (p
< end
&& *p
== 0)
472 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
475 entry
= sec_merge_add (sinfo
->htab
, "",
476 (unsigned) mask
+ 1, secinfo
);
487 for (i
= 0; i
< sec
->entsize
; i
++)
490 if (i
!= sec
->entsize
)
492 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
495 entry
= sec_merge_add (sinfo
->htab
, (char *) p
,
496 (unsigned) mask
+ 1, secinfo
);
507 for (p
= secinfo
->contents
; p
< end
; p
+= sec
->entsize
)
509 entry
= sec_merge_add (sinfo
->htab
, (char *) p
, 1, secinfo
);
518 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
519 *secinfo
->psecinfo
= NULL
;
524 strrevcmp (const void *a
, const void *b
)
526 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
527 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
528 unsigned int lenA
= A
->len
;
529 unsigned int lenB
= B
->len
;
530 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
531 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
532 int l
= lenA
< lenB
? lenA
: lenB
;
537 return (int) *s
- (int) *t
;
545 /* Like strrevcmp, but for the case where all strings have the same
546 alignment > entsize. */
549 strrevcmp_align (const void *a
, const void *b
)
551 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
552 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
553 unsigned int lenA
= A
->len
;
554 unsigned int lenB
= B
->len
;
555 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
556 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
557 int l
= lenA
< lenB
? lenA
: lenB
;
558 int tail_align
= (lenA
& (A
->alignment
- 1)) - (lenB
& (A
->alignment
- 1));
566 return (int) *s
- (int) *t
;
575 is_suffix (const struct sec_merge_hash_entry
*A
,
576 const struct sec_merge_hash_entry
*B
)
578 if (A
->len
<= B
->len
)
579 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
580 not to be equal by the hash table. */
583 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
584 B
->root
.string
, B
->len
) == 0;
587 /* This is a helper function for _bfd_merge_sections. It attempts to
588 merge strings matching suffixes of longer strings. */
590 merge_strings (struct sec_merge_info
*sinfo
)
592 struct sec_merge_hash_entry
**array
, **a
, *e
;
593 struct sec_merge_sec_info
*secinfo
;
594 bfd_size_type size
, amt
;
595 unsigned int alignment
= 0;
597 /* Now sort the strings */
598 amt
= sinfo
->htab
->size
* sizeof (struct sec_merge_hash_entry
*);
599 array
= bfd_malloc (amt
);
603 for (e
= sinfo
->htab
->first
, a
= array
; e
; e
= e
->next
)
607 /* Adjust the length to not include the zero terminator. */
608 e
->len
-= sinfo
->htab
->entsize
;
609 if (alignment
!= e
->alignment
)
612 alignment
= e
->alignment
;
614 alignment
= (unsigned) -1;
618 sinfo
->htab
->size
= a
- array
;
619 if (sinfo
->htab
->size
!= 0)
621 qsort (array
, (size_t) sinfo
->htab
->size
,
622 sizeof (struct sec_merge_hash_entry
*),
623 (alignment
!= (unsigned) -1 && alignment
> sinfo
->htab
->entsize
624 ? strrevcmp_align
: strrevcmp
));
626 /* Loop over the sorted array and merge suffixes */
628 e
->len
+= sinfo
->htab
->entsize
;
631 struct sec_merge_hash_entry
*cmp
= *a
;
633 cmp
->len
+= sinfo
->htab
->entsize
;
634 if (e
->alignment
>= cmp
->alignment
635 && !((e
->len
- cmp
->len
) & (cmp
->alignment
- 1))
636 && is_suffix (e
, cmp
))
650 /* Now assign positions to the strings we want to keep. */
652 secinfo
= sinfo
->htab
->first
->secinfo
;
653 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
655 if (e
->secinfo
!= secinfo
)
657 secinfo
->sec
->size
= size
;
658 secinfo
= e
->secinfo
;
662 if (e
->secinfo
->first_str
== NULL
)
664 e
->secinfo
->first_str
= e
;
667 size
= (size
+ e
->alignment
- 1) & ~((bfd_vma
) e
->alignment
- 1);
672 secinfo
->sec
->size
= size
;
673 if (secinfo
->sec
->alignment_power
!= 0)
675 bfd_size_type align
= (bfd_size_type
) 1 << secinfo
->sec
->alignment_power
;
676 secinfo
->sec
->size
= (secinfo
->sec
->size
+ align
- 1) & -align
;
679 /* And now adjust the rest, removing them from the chain (but not hashtable)
681 for (a
= &sinfo
->htab
->first
, e
= *a
; e
; e
= e
->next
)
689 e
->secinfo
= e
->u
.suffix
->secinfo
;
690 e
->alignment
= e
->u
.suffix
->alignment
;
691 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
- e
->len
);
696 /* This function is called once after all SEC_MERGE sections are registered
697 with _bfd_merge_section. */
700 _bfd_merge_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
701 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
703 void (*remove_hook
) (bfd
*, asection
*))
705 struct sec_merge_info
*sinfo
;
707 for (sinfo
= (struct sec_merge_info
*) xsinfo
; sinfo
; sinfo
= sinfo
->next
)
709 struct sec_merge_sec_info
* secinfo
;
714 /* Move sinfo->chain to head of the chain, terminate it. */
715 secinfo
= sinfo
->chain
;
716 sinfo
->chain
= secinfo
->next
;
717 secinfo
->next
= NULL
;
719 /* Record the sections into the hash table. */
720 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
721 if (secinfo
->sec
->flags
& SEC_EXCLUDE
)
723 *secinfo
->psecinfo
= NULL
;
725 (*remove_hook
) (abfd
, secinfo
->sec
);
727 else if (! record_section (sinfo
, secinfo
))
733 if (sinfo
->htab
->first
== NULL
)
736 if (sinfo
->htab
->strings
)
737 merge_strings (sinfo
);
740 struct sec_merge_hash_entry
*e
;
741 bfd_size_type size
= 0;
743 /* Things are much simpler for non-strings.
744 Just assign them slots in the section. */
746 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
748 if (e
->secinfo
->first_str
== NULL
)
751 secinfo
->sec
->size
= size
;
752 e
->secinfo
->first_str
= e
;
755 size
= (size
+ e
->alignment
- 1)
756 & ~((bfd_vma
) e
->alignment
- 1);
759 secinfo
= e
->secinfo
;
761 secinfo
->sec
->size
= size
;
764 /* Finally remove all input sections which have not made it into
765 the hash table at all. */
766 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
767 if (secinfo
->first_str
== NULL
)
768 secinfo
->sec
->flags
|= SEC_EXCLUDE
;
774 /* Write out the merged section. */
777 _bfd_write_merged_section (bfd
*output_bfd
, asection
*sec
, void *psecinfo
)
779 struct sec_merge_sec_info
*secinfo
;
782 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
784 if (secinfo
->first_str
== NULL
)
787 pos
= sec
->output_section
->filepos
+ sec
->output_offset
;
788 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0)
791 if (! sec_merge_emit (output_bfd
, secinfo
->first_str
))
797 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
798 *PSEC, this returns the new offset in the adjusted SEC_MERGE
799 section and writes the new section back into *PSEC. */
802 _bfd_merged_section_offset (bfd
*output_bfd ATTRIBUTE_UNUSED
, asection
**psec
,
803 void *psecinfo
, bfd_vma offset
)
805 struct sec_merge_sec_info
*secinfo
;
806 struct sec_merge_hash_entry
*entry
;
808 asection
*sec
= *psec
;
810 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
812 if (offset
>= sec
->rawsize
)
814 if (offset
> sec
->rawsize
)
816 (*_bfd_error_handler
)
817 (_("%s: access beyond end of merged section (%ld)"),
818 bfd_get_filename (sec
->owner
), (long) offset
);
820 return secinfo
->first_str
? sec
->size
: 0;
823 if (secinfo
->htab
->strings
)
825 if (sec
->entsize
== 1)
827 p
= secinfo
->contents
+ offset
- 1;
828 while (p
>= secinfo
->contents
&& *p
)
834 p
= secinfo
->contents
+ (offset
/ sec
->entsize
) * sec
->entsize
;
836 while (p
>= secinfo
->contents
)
840 for (i
= 0; i
< sec
->entsize
; ++i
)
843 if (i
== sec
->entsize
)
852 p
= secinfo
->contents
+ (offset
/ sec
->entsize
) * sec
->entsize
;
854 entry
= sec_merge_hash_lookup (secinfo
->htab
, (char *) p
, 0, FALSE
);
857 if (! secinfo
->htab
->strings
)
859 /* This should only happen if somebody points into the padding
860 after a NUL character but before next entity. */
863 if (! secinfo
->htab
->first
)
865 entry
= secinfo
->htab
->first
;
866 p
= (secinfo
->contents
+ (offset
/ sec
->entsize
+ 1) * sec
->entsize
870 *psec
= entry
->secinfo
->sec
;
871 return entry
->u
.index
+ (secinfo
->contents
+ offset
- p
);