2 Copyright (C) 2001-2022 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. */
32 #include "libiberty.h"
34 struct sec_merge_sec_info
;
36 /* An entry in the section merge hash table. */
38 struct sec_merge_hash_entry
40 struct bfd_hash_entry root
;
41 /* Length of this entry. This includes the zero terminator. */
43 /* Start of this string needs to be aligned to
44 alignment octets (not 1 << align). */
45 unsigned int alignment
;
48 /* Index within the merged section. */
50 /* Entry this is a suffix of (if alignment is 0). */
51 struct sec_merge_hash_entry
*suffix
;
53 /* Which section is it in. */
54 struct sec_merge_sec_info
*secinfo
;
55 /* Next entity in the hash table. */
56 struct sec_merge_hash_entry
*next
;
59 /* The section merge hash table. */
63 struct bfd_hash_table table
;
64 /* Next available index. */
66 /* First entity in the SEC_MERGE sections of this type. */
67 struct sec_merge_hash_entry
*first
;
68 /* Last entity in the SEC_MERGE sections of this type. */
69 struct sec_merge_hash_entry
*last
;
72 /* Are entries fixed size or zero terminated strings? */
78 /* Chain of sec_merge_infos. */
79 struct sec_merge_info
*next
;
80 /* Chain of sec_merge_sec_infos. */
81 struct sec_merge_sec_info
*chain
;
82 /* A hash table used to hold section content. */
83 struct sec_merge_hash
*htab
;
86 struct sec_merge_sec_info
88 /* Chain of sec_merge_sec_infos. */
89 struct sec_merge_sec_info
*next
;
90 /* The corresponding section. */
92 /* Pointer to merge_info pointing to us. */
94 /* A hash table used to hold section content. */
95 struct sec_merge_hash
*htab
;
96 /* First string in this section. */
97 struct sec_merge_hash_entry
*first_str
;
98 /* Original section content. */
99 unsigned char contents
[1];
103 /* Routine to create an entry in a section merge hashtab. */
105 static struct bfd_hash_entry
*
106 sec_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
107 struct bfd_hash_table
*table
, const char *string
)
109 /* Allocate the structure if it has not already been allocated by a
112 entry
= (struct bfd_hash_entry
*)
113 bfd_hash_allocate (table
, sizeof (struct sec_merge_hash_entry
));
117 /* Call the allocation method of the superclass. */
118 entry
= bfd_hash_newfunc (entry
, table
, string
);
122 /* Initialize the local fields. */
123 struct sec_merge_hash_entry
*ret
= (struct sec_merge_hash_entry
*) entry
;
125 ret
->u
.suffix
= NULL
;
134 /* Look up an entry in a section merge hash table. */
136 static struct sec_merge_hash_entry
*
137 sec_merge_hash_lookup (struct sec_merge_hash
*table
, const char *string
,
138 unsigned int alignment
, bool create
)
140 const unsigned char *s
;
143 struct sec_merge_hash_entry
*hashp
;
149 s
= (const unsigned char *) string
;
152 if (table
->entsize
== 1)
154 while ((c
= *s
++) != '\0')
156 hash
+= c
+ (c
<< 17);
160 hash
+= len
+ (len
<< 17);
166 for (i
= 0; i
< table
->entsize
; ++i
)
169 if (i
== table
->entsize
)
171 for (i
= 0; i
< table
->entsize
; ++i
)
174 hash
+= c
+ (c
<< 17);
179 hash
+= len
+ (len
<< 17);
180 len
*= table
->entsize
;
183 len
+= table
->entsize
;
187 for (i
= 0; i
< table
->entsize
; ++i
)
190 hash
+= c
+ (c
<< 17);
193 len
= table
->entsize
;
196 _index
= hash
% table
->table
.size
;
197 for (hashp
= (struct sec_merge_hash_entry
*) table
->table
.table
[_index
];
199 hashp
= (struct sec_merge_hash_entry
*) hashp
->root
.next
)
201 if (hashp
->root
.hash
== hash
203 && memcmp (hashp
->root
.string
, string
, len
) == 0)
205 /* If the string we found does not have at least the required
206 alignment, we need to insert another copy. */
207 if (hashp
->alignment
< alignment
)
211 /* Mark the less aligned copy as deleted. */
213 hashp
->alignment
= 0;
224 hashp
= ((struct sec_merge_hash_entry
*)
225 bfd_hash_insert (&table
->table
, string
, hash
));
229 hashp
->alignment
= alignment
;
233 /* Create a new hash table. */
235 static struct sec_merge_hash
*
236 sec_merge_init (unsigned int entsize
, bool strings
)
238 struct sec_merge_hash
*table
;
240 table
= (struct sec_merge_hash
*) bfd_malloc (sizeof (struct sec_merge_hash
));
244 if (! bfd_hash_table_init_n (&table
->table
, sec_merge_hash_newfunc
,
245 sizeof (struct sec_merge_hash_entry
), 16699))
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 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
,
289 unsigned char *contents
, file_ptr offset
)
291 struct sec_merge_sec_info
*secinfo
= entry
->secinfo
;
292 asection
*sec
= secinfo
->sec
;
294 bfd_size_type off
= 0;
295 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
296 int alignment_power
= sec
->output_section
->alignment_power
* opb
;
297 bfd_size_type pad_len
; /* Octets. */
299 /* FIXME: If alignment_power is 0 then really we should scan the
300 entry list for the largest required alignment and use that. */
301 pad_len
= alignment_power
? ((bfd_size_type
) 1 << alignment_power
) : 16;
303 pad
= (char *) bfd_zmalloc (pad_len
);
307 for (; entry
!= NULL
&& entry
->secinfo
== secinfo
; entry
= entry
->next
)
312 len
= -off
& (entry
->alignment
- 1);
315 BFD_ASSERT (len
<= pad_len
);
318 memcpy (contents
+ offset
, pad
, len
);
321 else if (bfd_bwrite (pad
, len
, abfd
) != len
)
326 str
= entry
->root
.string
;
331 memcpy (contents
+ offset
, str
, len
);
334 else if (bfd_bwrite (str
, len
, abfd
) != len
)
340 /* Trailing alignment needed? */
341 off
= sec
->size
- off
;
344 BFD_ASSERT (off
<= pad_len
);
346 memcpy (contents
+ offset
, pad
, off
);
347 else if (bfd_bwrite (pad
, off
, abfd
) != off
)
359 /* Register a SEC_MERGE section as a candidate for merging.
360 This function is called for all non-dynamic SEC_MERGE input sections. */
363 _bfd_add_merge_section (bfd
*abfd
, void **psinfo
, asection
*sec
,
366 struct sec_merge_info
*sinfo
;
367 struct sec_merge_sec_info
*secinfo
;
368 unsigned int alignment_power
; /* Octets. */
369 unsigned int align
; /* Octets. */
372 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
374 if ((abfd
->flags
& DYNAMIC
) != 0
375 || (sec
->flags
& SEC_MERGE
) == 0)
379 || (sec
->flags
& SEC_EXCLUDE
) != 0
380 || sec
->entsize
== 0)
383 if (sec
->size
% sec
->entsize
!= 0)
386 if ((sec
->flags
& SEC_RELOC
) != 0)
388 /* We aren't prepared to handle relocations in merged sections. */
395 alignment_power
= sec
->alignment_power
* opb
;
396 if (alignment_power
>= sizeof (align
) * CHAR_BIT
)
399 align
= 1u << alignment_power
;
400 if ((sec
->entsize
< align
401 && ((sec
->entsize
& (sec
->entsize
- 1))
402 || !(sec
->flags
& SEC_STRINGS
)))
403 || (sec
->entsize
> align
404 && (sec
->entsize
& (align
- 1))))
406 /* Sanity check. If string character size is smaller than
407 alignment, then we require character size to be a power
408 of 2, otherwise character size must be integer multiple
409 of alignment. For non-string constants, alignment must
410 be smaller than or equal to entity size and entity size
411 must be integer multiple of alignment. */
415 for (sinfo
= (struct sec_merge_info
*) *psinfo
; sinfo
; sinfo
= sinfo
->next
)
416 if ((secinfo
= sinfo
->chain
)
417 && ! ((secinfo
->sec
->flags
^ sec
->flags
) & (SEC_MERGE
| SEC_STRINGS
))
418 && secinfo
->sec
->entsize
== sec
->entsize
419 && secinfo
->sec
->alignment_power
== sec
->alignment_power
420 && secinfo
->sec
->output_section
== sec
->output_section
)
425 /* Initialize the information we need to keep track of. */
426 sinfo
= (struct sec_merge_info
*)
427 bfd_alloc (abfd
, sizeof (struct sec_merge_info
));
430 sinfo
->next
= (struct sec_merge_info
*) *psinfo
;
433 sinfo
->htab
= sec_merge_init (sec
->entsize
, (sec
->flags
& SEC_STRINGS
));
434 if (sinfo
->htab
== NULL
)
438 /* Read the section from abfd. */
440 amt
= sizeof (struct sec_merge_sec_info
) - 1 + sec
->size
;
441 if (sec
->flags
& SEC_STRINGS
)
442 /* Some versions of gcc may emit a string without a zero terminator.
443 See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
444 Allocate space for an extra zero. */
446 *psecinfo
= bfd_alloc (abfd
, amt
);
447 if (*psecinfo
== NULL
)
450 secinfo
= (struct sec_merge_sec_info
*) *psecinfo
;
453 secinfo
->next
= sinfo
->chain
->next
;
454 sinfo
->chain
->next
= secinfo
;
457 secinfo
->next
= secinfo
;
458 sinfo
->chain
= secinfo
;
460 secinfo
->psecinfo
= psecinfo
;
461 secinfo
->htab
= sinfo
->htab
;
462 secinfo
->first_str
= NULL
;
464 sec
->rawsize
= sec
->size
;
465 if (sec
->flags
& SEC_STRINGS
)
466 memset (secinfo
->contents
+ sec
->size
, 0, sec
->entsize
);
467 contents
= secinfo
->contents
;
468 if (! bfd_get_full_section_contents (sec
->owner
, sec
, &contents
))
478 /* Record one section into the hash table. */
480 record_section (struct sec_merge_info
*sinfo
,
481 struct sec_merge_sec_info
*secinfo
)
483 asection
*sec
= secinfo
->sec
;
484 struct sec_merge_hash_entry
*entry
;
486 unsigned char *p
, *end
;
487 bfd_vma mask
, eltalign
;
488 unsigned int align
, i
;
490 align
= sec
->alignment_power
;
491 end
= secinfo
->contents
+ sec
->size
;
493 mask
= ((bfd_vma
) 1 << align
) - 1;
494 if (sec
->flags
& SEC_STRINGS
)
496 for (p
= secinfo
->contents
; p
< end
; )
498 eltalign
= p
- secinfo
->contents
;
499 eltalign
= ((eltalign
^ (eltalign
- 1)) + 1) >> 1;
500 if (!eltalign
|| eltalign
> mask
)
502 entry
= sec_merge_add (sinfo
->htab
, (char *) p
, (unsigned) eltalign
,
507 if (sec
->entsize
== 1)
509 while (p
< end
&& *p
== 0)
511 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
514 entry
= sec_merge_add (sinfo
->htab
, "",
515 (unsigned) mask
+ 1, secinfo
);
526 for (i
= 0; i
< sec
->entsize
; i
++)
529 if (i
!= sec
->entsize
)
531 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
534 entry
= sec_merge_add (sinfo
->htab
, (char *) p
,
535 (unsigned) mask
+ 1, secinfo
);
546 for (p
= secinfo
->contents
; p
< end
; p
+= sec
->entsize
)
548 entry
= sec_merge_add (sinfo
->htab
, (char *) p
, 1, secinfo
);
557 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
558 *secinfo
->psecinfo
= NULL
;
562 /* qsort comparison function. Won't ever return zero as all entries
563 differ, so there is no issue with qsort stability here. */
566 strrevcmp (const void *a
, const void *b
)
568 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
569 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
570 unsigned int lenA
= A
->len
;
571 unsigned int lenB
= B
->len
;
572 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
573 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
574 int l
= lenA
< lenB
? lenA
: lenB
;
579 return (int) *s
- (int) *t
;
587 /* Like strrevcmp, but for the case where all strings have the same
588 alignment > entsize. */
591 strrevcmp_align (const void *a
, const void *b
)
593 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
594 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
595 unsigned int lenA
= A
->len
;
596 unsigned int lenB
= B
->len
;
597 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
598 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
599 int l
= lenA
< lenB
? lenA
: lenB
;
600 int tail_align
= (lenA
& (A
->alignment
- 1)) - (lenB
& (A
->alignment
- 1));
608 return (int) *s
- (int) *t
;
617 is_suffix (const struct sec_merge_hash_entry
*A
,
618 const struct sec_merge_hash_entry
*B
)
620 if (A
->len
<= B
->len
)
621 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
622 not to be equal by the hash table. */
625 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
626 B
->root
.string
, B
->len
) == 0;
629 /* This is a helper function for _bfd_merge_sections. It attempts to
630 merge strings matching suffixes of longer strings. */
631 static struct sec_merge_sec_info
*
632 merge_strings (struct sec_merge_info
*sinfo
)
634 struct sec_merge_hash_entry
**array
, **a
, *e
;
635 struct sec_merge_sec_info
*secinfo
;
636 bfd_size_type size
, amt
;
637 unsigned int alignment
= 0;
639 /* Now sort the strings */
640 amt
= sinfo
->htab
->size
* sizeof (struct sec_merge_hash_entry
*);
641 array
= (struct sec_merge_hash_entry
**) bfd_malloc (amt
);
645 for (e
= sinfo
->htab
->first
, a
= array
; e
; e
= e
->next
)
649 /* Adjust the length to not include the zero terminator. */
650 e
->len
-= sinfo
->htab
->entsize
;
651 if (alignment
!= e
->alignment
)
654 alignment
= e
->alignment
;
656 alignment
= (unsigned) -1;
660 sinfo
->htab
->size
= a
- array
;
661 if (sinfo
->htab
->size
!= 0)
663 qsort (array
, (size_t) sinfo
->htab
->size
,
664 sizeof (struct sec_merge_hash_entry
*),
665 (alignment
!= (unsigned) -1 && alignment
> sinfo
->htab
->entsize
666 ? strrevcmp_align
: strrevcmp
));
668 /* Loop over the sorted array and merge suffixes */
670 e
->len
+= sinfo
->htab
->entsize
;
673 struct sec_merge_hash_entry
*cmp
= *a
;
675 cmp
->len
+= sinfo
->htab
->entsize
;
676 if (e
->alignment
>= cmp
->alignment
677 && !((e
->len
- cmp
->len
) & (cmp
->alignment
- 1))
678 && is_suffix (e
, cmp
))
690 /* Now assign positions to the strings we want to keep. */
692 secinfo
= sinfo
->htab
->first
->secinfo
;
693 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
695 if (e
->secinfo
!= secinfo
)
697 secinfo
->sec
->size
= size
;
698 secinfo
= e
->secinfo
;
702 if (e
->secinfo
->first_str
== NULL
)
704 e
->secinfo
->first_str
= e
;
707 size
= (size
+ e
->alignment
- 1) & ~((bfd_vma
) e
->alignment
- 1);
712 secinfo
->sec
->size
= size
;
714 /* And now adjust the rest, removing them from the chain (but not hashtable)
716 for (a
= &sinfo
->htab
->first
, e
= *a
; e
; e
= e
->next
)
724 e
->secinfo
= e
->u
.suffix
->secinfo
;
725 e
->alignment
= e
->u
.suffix
->alignment
;
726 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
- e
->len
);
732 /* This function is called once after all SEC_MERGE sections are registered
733 with _bfd_merge_section. */
736 _bfd_merge_sections (bfd
*abfd
,
737 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
739 void (*remove_hook
) (bfd
*, asection
*))
741 struct sec_merge_info
*sinfo
;
743 for (sinfo
= (struct sec_merge_info
*) xsinfo
; sinfo
; sinfo
= sinfo
->next
)
745 struct sec_merge_sec_info
*secinfo
;
746 bfd_size_type align
; /* Bytes. */
751 /* Move sinfo->chain to head of the chain, terminate it. */
752 secinfo
= sinfo
->chain
;
753 sinfo
->chain
= secinfo
->next
;
754 secinfo
->next
= NULL
;
756 /* Record the sections into the hash table. */
758 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
759 if (secinfo
->sec
->flags
& SEC_EXCLUDE
)
761 *secinfo
->psecinfo
= NULL
;
763 (*remove_hook
) (abfd
, secinfo
->sec
);
767 if (!record_section (sinfo
, secinfo
))
771 unsigned int opb
= bfd_octets_per_byte (abfd
, secinfo
->sec
);
773 align
= (bfd_size_type
) 1 << secinfo
->sec
->alignment_power
;
774 if (((secinfo
->sec
->size
/ opb
) & (align
- 1)) != 0)
779 if (sinfo
->htab
->first
== NULL
)
782 if (sinfo
->htab
->strings
)
784 secinfo
= merge_strings (sinfo
);
790 struct sec_merge_hash_entry
*e
;
791 bfd_size_type size
= 0; /* Octets. */
793 /* Things are much simpler for non-strings.
794 Just assign them slots in the section. */
796 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
798 if (e
->secinfo
->first_str
== NULL
)
801 secinfo
->sec
->size
= size
;
802 e
->secinfo
->first_str
= e
;
805 size
= (size
+ e
->alignment
- 1) & ~((bfd_vma
) e
->alignment
- 1);
808 secinfo
= e
->secinfo
;
810 secinfo
->sec
->size
= size
;
813 /* If the input sections were padded according to their alignments,
814 then pad the output too. */
816 secinfo
->sec
->size
= (secinfo
->sec
->size
+ align
- 1) & -align
;
818 /* Finally remove all input sections which have not made it into
819 the hash table at all. */
820 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
821 if (secinfo
->first_str
== NULL
)
822 secinfo
->sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
828 /* Write out the merged section. */
831 _bfd_write_merged_section (bfd
*output_bfd
, asection
*sec
, void *psecinfo
)
833 struct sec_merge_sec_info
*secinfo
;
835 unsigned char *contents
;
836 Elf_Internal_Shdr
*hdr
;
838 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
843 if (secinfo
->first_str
== NULL
)
846 /* FIXME: octets_per_byte. */
847 hdr
= &elf_section_data (sec
->output_section
)->this_hdr
;
848 if (hdr
->sh_offset
== (file_ptr
) -1)
850 /* We must compress this section. Write output to the
852 contents
= hdr
->contents
;
853 if ((sec
->output_section
->flags
& SEC_ELF_COMPRESS
) == 0
860 pos
= sec
->output_section
->filepos
+ sec
->output_offset
;
861 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0)
865 if (! sec_merge_emit (output_bfd
, secinfo
->first_str
, contents
,
872 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
873 *PSEC, this returns the new offset in the adjusted SEC_MERGE
874 section and writes the new section back into *PSEC. */
877 _bfd_merged_section_offset (bfd
*output_bfd ATTRIBUTE_UNUSED
, asection
**psec
,
878 void *psecinfo
, bfd_vma offset
)
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
;
890 if (offset
>= sec
->rawsize
)
892 if (offset
> sec
->rawsize
)
894 /* xgettext:c-format */
895 (_("%pB: access beyond end of merged section (%" PRId64
")"),
896 sec
->owner
, (int64_t) offset
);
897 return secinfo
->first_str
? sec
->size
: 0;
900 if (secinfo
->htab
->strings
)
902 if (sec
->entsize
== 1)
904 p
= secinfo
->contents
+ offset
- 1;
905 while (p
>= secinfo
->contents
&& *p
)
911 p
= secinfo
->contents
+ (offset
/ sec
->entsize
) * sec
->entsize
;
913 while (p
>= secinfo
->contents
)
917 for (i
= 0; i
< sec
->entsize
; ++i
)
920 if (i
== sec
->entsize
)
929 p
= secinfo
->contents
+ (offset
/ sec
->entsize
) * sec
->entsize
;
931 entry
= sec_merge_hash_lookup (secinfo
->htab
, (char *) p
, 0, false);
934 if (! secinfo
->htab
->strings
)
936 /* This should only happen if somebody points into the padding
937 after a NUL character but before next entity. */
940 if (! secinfo
->htab
->first
)
942 entry
= secinfo
->htab
->first
;
943 p
= (secinfo
->contents
+ (offset
/ sec
->entsize
+ 1) * sec
->entsize
947 *psec
= entry
->secinfo
->sec
;
948 return entry
->u
.index
+ (secinfo
->contents
+ offset
- p
);
951 /* Tidy up when done. */
954 _bfd_merge_sections_free (void *xsinfo
)
956 struct sec_merge_info
*sinfo
;
958 for (sinfo
= (struct sec_merge_info
*) xsinfo
; sinfo
; sinfo
= sinfo
->next
)
960 bfd_hash_table_free (&sinfo
->htab
->table
);