2 Copyright 2001 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., 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. */
31 struct sec_merge_sec_info
;
33 /* An entry in the section merge hash table. */
35 struct sec_merge_hash_entry
37 struct bfd_hash_entry root
;
38 /* Length of this entry. */
40 /* Start of this string needs to be aligned to
41 alignment octets (not 1 << align). */
42 unsigned int alignment
;
44 /* Index within the merged section. */
46 /* Entity size (if present in suffix hash tables). */
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
;
96 /* Original section content. */
97 unsigned char contents
[1];
100 static struct bfd_hash_entry
*sec_merge_hash_newfunc
101 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
102 static struct sec_merge_hash_entry
*sec_merge_hash_lookup
103 PARAMS ((struct sec_merge_hash
*, const char *, unsigned int, boolean
));
104 static struct sec_merge_hash
*sec_merge_init
105 PARAMS ((unsigned int, boolean
));
106 static struct sec_merge_hash_entry
*sec_merge_add
107 PARAMS ((struct sec_merge_hash
*, const char *, unsigned int,
108 struct sec_merge_sec_info
*));
109 static boolean sec_merge_emit
110 PARAMS ((bfd
*, struct sec_merge_hash_entry
*));
112 /* Routine to create an entry in a section merge hashtab. */
114 static struct bfd_hash_entry
*
115 sec_merge_hash_newfunc (entry
, table
, string
)
116 struct bfd_hash_entry
*entry
;
117 struct bfd_hash_table
*table
;
120 struct sec_merge_hash_entry
*ret
= (struct sec_merge_hash_entry
*) entry
;
122 /* Allocate the structure if it has not already been allocated by a
124 if (ret
== (struct sec_merge_hash_entry
*) NULL
)
125 ret
= ((struct sec_merge_hash_entry
*)
126 bfd_hash_allocate (table
, sizeof (struct sec_merge_hash_entry
)));
127 if (ret
== (struct sec_merge_hash_entry
*) NULL
)
130 /* Call the allocation method of the superclass. */
131 ret
= ((struct sec_merge_hash_entry
*)
132 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
136 /* Initialize the local fields. */
137 ret
->u
.suffix
= NULL
;
143 return (struct bfd_hash_entry
*)ret
;
146 /* Look up an entry in a section merge hash table. */
148 static struct sec_merge_hash_entry
*
149 sec_merge_hash_lookup (table
, string
, alignment
, create
)
150 struct sec_merge_hash
*table
;
152 unsigned int alignment
;
155 register const unsigned char *s
;
156 register unsigned long hash
;
157 register unsigned int c
;
158 struct sec_merge_hash_entry
*hashp
;
164 s
= (const unsigned char *) string
;
167 if (table
->entsize
== 1)
169 while ((c
= *s
++) != '\0')
171 hash
+= c
+ (c
<< 17);
175 hash
+= len
+ (len
<< 17);
181 for (i
= 0; i
< table
->entsize
; ++i
)
184 if (i
== table
->entsize
)
186 for (i
= 0; i
< table
->entsize
; ++i
)
189 hash
+= c
+ (c
<< 17);
194 hash
+= len
+ (len
<< 17);
195 len
*= table
->entsize
;
198 len
+= table
->entsize
;
202 for (i
= 0; i
< table
->entsize
; ++i
)
205 hash
+= c
+ (c
<< 17);
208 len
= table
->entsize
;
211 index
= hash
% table
->table
.size
;
212 for (hashp
= (struct sec_merge_hash_entry
*) table
->table
.table
[index
];
213 hashp
!= (struct sec_merge_hash_entry
*) NULL
;
214 hashp
= (struct sec_merge_hash_entry
*) hashp
->root
.next
)
216 if (hashp
->root
.hash
== hash
218 && memcmp (hashp
->root
.string
, string
, len
) == 0)
220 /* If the string we found does not have at least the required
221 alignment, we need to insert another copy. */
222 if (hashp
->alignment
< alignment
)
224 /* Mark the less aligned copy as deleted. */
226 hashp
->alignment
= 0;
234 return (struct sec_merge_hash_entry
*) NULL
;
236 hashp
= (struct sec_merge_hash_entry
*)
237 sec_merge_hash_newfunc ((struct bfd_hash_entry
*) NULL
,
238 (struct bfd_hash_table
*) table
, string
);
239 if (hashp
== (struct sec_merge_hash_entry
*) NULL
)
240 return (struct sec_merge_hash_entry
*) NULL
;
241 hashp
->root
.string
= string
;
242 hashp
->root
.hash
= hash
;
244 hashp
->alignment
= alignment
;
245 hashp
->root
.next
= table
->table
.table
[index
];
246 table
->table
.table
[index
] = (struct bfd_hash_entry
*) hashp
;
251 /* Create a new hash table. */
253 static struct sec_merge_hash
*
254 sec_merge_init (entsize
, strings
)
255 unsigned int entsize
;
258 struct sec_merge_hash
*table
;
260 table
= ((struct sec_merge_hash
*)
261 bfd_malloc (sizeof (struct sec_merge_hash
)));
265 if (! bfd_hash_table_init (&table
->table
, sec_merge_hash_newfunc
))
274 table
->entsize
= entsize
;
275 table
->strings
= strings
;
280 /* Get the index of an entity in a hash table, adding it if it is not
283 static struct sec_merge_hash_entry
*
284 sec_merge_add (tab
, str
, alignment
, secinfo
)
285 struct sec_merge_hash
*tab
;
287 unsigned int alignment
;
288 struct sec_merge_sec_info
*secinfo
;
290 register struct sec_merge_hash_entry
*entry
;
292 entry
= sec_merge_hash_lookup (tab
, str
, alignment
, true);
296 if (entry
->secinfo
== NULL
)
299 entry
->secinfo
= secinfo
;
300 if (tab
->first
== NULL
)
303 tab
->last
->next
= entry
;
311 sec_merge_emit (abfd
, entry
)
313 struct sec_merge_hash_entry
*entry
;
315 struct sec_merge_sec_info
*secinfo
= entry
->secinfo
;
316 asection
*sec
= secinfo
->sec
;
318 bfd_size_type off
= 0;
319 int alignment_power
= bfd_get_section_alignment (abfd
, sec
->output_section
);
322 pad
= bfd_zmalloc (1 << alignment_power
);
324 for (; entry
!= NULL
&& entry
->secinfo
== secinfo
; entry
= entry
->next
)
326 register const char *str
;
329 len
= off
& (entry
->alignment
- 1);
332 len
= entry
->alignment
- len
;
333 if (bfd_write ((PTR
) pad
, 1, len
, abfd
) != len
)
338 str
= entry
->root
.string
;
341 if (bfd_write ((PTR
) str
, 1, len
, abfd
) != len
)
350 return entry
== NULL
|| entry
->secinfo
!= secinfo
;
353 /* This function is called for each input file from the add_symbols
354 pass of the linker. */
357 _bfd_merge_section (abfd
, psinfo
, sec
, psecinfo
)
363 struct sec_merge_info
*sinfo
;
364 struct sec_merge_sec_info
*secinfo
;
367 if (sec
->_raw_size
== 0
368 || (sec
->flags
& SEC_EXCLUDE
)
369 || (sec
->flags
& SEC_MERGE
) == 0
370 || sec
->entsize
== 0)
373 if ((sec
->flags
& SEC_RELOC
) != 0)
375 /* We aren't prepared to handle relocations in merged sections. */
379 if (sec
->output_section
!= NULL
380 && bfd_is_abs_section (sec
->output_section
))
382 /* The section is being discarded from the link, so we should
387 align
= bfd_get_section_alignment (sec
->owner
, sec
);
388 if ((sec
->entsize
< (unsigned int)(1 << align
)
389 && ((sec
->entsize
& (sec
->entsize
- 1))
390 || !(sec
->flags
& SEC_STRINGS
)))
391 || (sec
->entsize
> (unsigned int)(1 << align
)
392 && (sec
->entsize
& ((1 << align
) - 1))))
394 /* Sanity check. If string character size is smaller than
395 alignment, then we require character size to be a power
396 of 2, otherwise character size must be integer multiple
397 of alignment. For non-string constants, alignment must
398 be smaller than or equal to entity size and entity size
399 must be integer multiple of alignment. */
403 for (sinfo
= (struct sec_merge_info
*) *psinfo
; sinfo
; sinfo
= sinfo
->next
)
404 if ((secinfo
= sinfo
->chain
)
405 && ! ((secinfo
->sec
->flags
^ sec
->flags
) & (SEC_MERGE
| SEC_STRINGS
))
406 && secinfo
->sec
->entsize
== sec
->entsize
407 && ! strcmp (secinfo
->sec
->name
, sec
->name
))
412 /* Initialize the information we need to keep track of. */
413 sinfo
= (struct sec_merge_info
*)
414 bfd_alloc (abfd
, sizeof (struct sec_merge_info
));
417 sinfo
->next
= (struct sec_merge_info
*) *psinfo
;
419 *psinfo
= (PTR
) sinfo
;
421 sec_merge_init (sec
->entsize
, (sec
->flags
& SEC_STRINGS
));
422 if (sinfo
->htab
== NULL
)
426 /* Read the section from abfd. */
428 *psecinfo
= bfd_alloc (abfd
, sizeof (struct sec_merge_sec_info
)
429 + sec
->_raw_size
- 1);
430 if (*psecinfo
== NULL
)
433 secinfo
= (struct sec_merge_sec_info
*)*psecinfo
;
436 secinfo
->next
= sinfo
->chain
->next
;
437 sinfo
->chain
->next
= secinfo
;
440 secinfo
->next
= secinfo
;
441 sinfo
->chain
= secinfo
;
443 secinfo
->psecinfo
= psecinfo
;
444 secinfo
->htab
= sinfo
->htab
;
445 secinfo
->first
= NULL
;
447 if (! bfd_get_section_contents (sec
->owner
, sec
, secinfo
->contents
, 0,
458 /* Compare two sec_merge_hash_entry structures. This is called via qsort. */
461 cmplengthentry (a
, b
)
465 struct sec_merge_hash_entry
* A
= *(struct sec_merge_hash_entry
**) a
;
466 struct sec_merge_hash_entry
* B
= *(struct sec_merge_hash_entry
**) b
;
470 else if (A
->len
> B
->len
)
473 return memcmp (A
->root
.string
, B
->root
.string
, A
->len
);
480 struct sec_merge_hash_entry
* A
= (struct sec_merge_hash_entry
*) a
;
481 struct sec_merge_hash_entry
* B
= (struct sec_merge_hash_entry
*) b
;
483 if (memcmp (A
->root
.string
+ A
->len
- 5 * A
->u
.entsize
,
484 B
->root
.string
+ B
->len
- 5 * A
->u
.entsize
,
485 4 * A
->u
.entsize
) != 0)
486 /* This was a hashtable collision. */
489 if (A
->len
<= B
->len
)
490 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
491 not to be equal by the hash table. */
494 if (A
->alignment
< B
->alignment
495 || ((A
->len
- B
->len
) & (B
->alignment
- 1)))
496 /* The suffix is not sufficiently aligned. */
499 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
500 B
->root
.string
, B
->len
- 5 * A
->u
.entsize
) == 0;
507 struct sec_merge_hash_entry
* A
= (struct sec_merge_hash_entry
*) a
;
508 struct sec_merge_hash_entry
* B
= (struct sec_merge_hash_entry
*) b
;
510 if (B
->len
>= 5 * A
->u
.entsize
)
511 /* Longer strings are just pushed into the hash table,
512 they'll be used when looking up for very short strings. */
515 if (memcmp (A
->root
.string
+ A
->len
- 2 * A
->u
.entsize
,
516 B
->root
.string
+ B
->len
- 2 * A
->u
.entsize
,
518 /* This was a hashtable collision. */
521 if (A
->len
<= B
->len
)
522 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
523 not to be equal by the hash table. */
526 if (A
->alignment
< B
->alignment
527 || ((A
->len
- B
->len
) & (B
->alignment
- 1)))
528 /* The suffix is not sufficiently aligned. */
531 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
532 B
->root
.string
, B
->len
- 2 * A
->u
.entsize
) == 0;
535 /* Record one section into the hash table. */
537 record_section (sinfo
, secinfo
)
538 struct sec_merge_info
*sinfo
;
539 struct sec_merge_sec_info
*secinfo
;
541 asection
*sec
= secinfo
->sec
;
542 struct sec_merge_hash_entry
*entry
;
544 unsigned char *p
, *end
;
545 bfd_vma mask
, eltalign
;
546 unsigned int align
, i
;
548 align
= bfd_get_section_alignment (sec
->owner
, sec
);
549 end
= secinfo
->contents
+ sec
->_raw_size
;
551 mask
= ((bfd_vma
)1 << align
) - 1;
552 if (sec
->flags
& SEC_STRINGS
)
554 for (p
= secinfo
->contents
; p
< end
;)
556 eltalign
= p
- secinfo
->contents
;
557 eltalign
= ((eltalign
^ (eltalign
- 1)) + 1) >> 1;
558 if (!eltalign
|| eltalign
> mask
)
560 entry
= sec_merge_add (sinfo
->htab
, p
, eltalign
, secinfo
);
564 if (sec
->entsize
== 1)
566 while (p
< end
&& *p
== 0)
568 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
571 entry
= sec_merge_add (sinfo
->htab
, "", mask
+ 1,
583 for (i
= 0; i
< sec
->entsize
; i
++)
586 if (i
!= sec
->entsize
)
588 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
591 entry
= sec_merge_add (sinfo
->htab
, p
, mask
+ 1,
603 for (p
= secinfo
->contents
; p
< end
; p
+= sec
->entsize
)
605 entry
= sec_merge_add (sinfo
->htab
, p
, 1, secinfo
);
614 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
615 *secinfo
->psecinfo
= NULL
;
619 /* This is a helper function for _bfd_merge_sections. It attempts to
620 merge strings matching suffixes of longer strings. */
622 merge_strings (sinfo
)
623 struct sec_merge_info
*sinfo
;
625 struct sec_merge_hash_entry
**array
, **a
, **end
, *e
;
626 struct sec_merge_sec_info
*secinfo
;
627 htab_t lasttab
= NULL
, last4tab
= NULL
;
630 /* Now sort the strings by length, longest first. */
631 array
= (struct sec_merge_hash_entry
**)
632 malloc (sinfo
->htab
->size
633 * sizeof (struct sec_merge_hash_entry
*));
637 for (e
= sinfo
->htab
->first
, a
= array
; e
; e
= e
->next
)
641 sinfo
->htab
->size
= a
- array
;
643 qsort (array
, sinfo
->htab
->size
, sizeof (struct sec_merge_hash_entry
*),
646 last4tab
= htab_create (sinfo
->htab
->size
* 4, NULL
, last4_eq
, NULL
);
647 lasttab
= htab_create (sinfo
->htab
->size
* 4, NULL
, last_eq
, NULL
);
648 if (lasttab
== NULL
|| last4tab
== NULL
)
651 /* Now insert the strings into hash tables (strings with last 4 characters
652 and strings with last character equal), look for longer strings which
654 for (a
= array
, end
= array
+ sinfo
->htab
->size
; a
< end
; a
++)
656 register hashval_t hash
;
659 const unsigned char *s
;
663 e
->u
.entsize
= sinfo
->htab
->entsize
;
664 if (e
->len
<= e
->u
.entsize
)
666 if (e
->len
> 4 * e
->u
.entsize
)
668 s
= e
->root
.string
+ e
->len
- e
->u
.entsize
;
670 for (i
= 0; i
< 4 * e
->u
.entsize
; i
++)
673 hash
+= c
+ (c
<< 17);
676 p
= htab_find_slot_with_hash (last4tab
, e
, hash
, INSERT
);
681 struct sec_merge_hash_entry
*ent
;
683 ent
= (struct sec_merge_hash_entry
*) *p
;
691 s
= e
->root
.string
+ e
->len
- e
->u
.entsize
;
693 for (i
= 0; i
< e
->u
.entsize
; i
++)
696 hash
+= c
+ (c
<< 17);
699 p
= htab_find_slot_with_hash (lasttab
, e
, hash
, INSERT
);
704 struct sec_merge_hash_entry
*ent
;
706 ent
= (struct sec_merge_hash_entry
*) *p
;
718 htab_delete (lasttab
);
720 htab_delete (last4tab
);
722 /* Now assign positions to the strings we want to keep. */
724 secinfo
= sinfo
->htab
->first
->secinfo
;
725 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
727 if (e
->secinfo
!= secinfo
)
729 secinfo
->sec
->_cooked_size
= size
;
730 secinfo
= e
->secinfo
;
734 if (e
->secinfo
->first
== NULL
)
736 e
->secinfo
->first
= e
;
739 size
= (size
+ e
->alignment
- 1) & ~((bfd_vma
) e
->alignment
- 1);
744 secinfo
->sec
->_cooked_size
= size
;
746 /* And now adjust the rest, removing them from the chain (but not hashtable)
748 for (a
= &sinfo
->htab
->first
, e
= *a
; e
; e
= e
->next
)
756 e
->secinfo
= e
->u
.suffix
->secinfo
;
757 e
->alignment
= e
->u
.suffix
->alignment
;
758 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
- e
->len
);
763 /* This function is called once after all SEC_MERGE sections are registered
764 with _bfd_merge_section. */
767 _bfd_merge_sections (abfd
, xsinfo
)
768 bfd
*abfd ATTRIBUTE_UNUSED
;
771 struct sec_merge_info
*sinfo
;
773 for (sinfo
= (struct sec_merge_info
*) xsinfo
; sinfo
; sinfo
= sinfo
->next
)
775 struct sec_merge_sec_info
* secinfo
;
780 /* Move sinfo->chain to head of the chain, terminate it. */
781 secinfo
= sinfo
->chain
;
782 sinfo
->chain
= secinfo
->next
;
783 secinfo
->next
= NULL
;
785 /* Record the sections into the hash table. */
786 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
787 if (secinfo
->sec
->flags
& SEC_EXCLUDE
)
788 *secinfo
->psecinfo
= NULL
;
789 else if (! record_section (sinfo
, secinfo
))
795 if (sinfo
->htab
->strings
)
796 merge_strings (sinfo
);
799 struct sec_merge_hash_entry
*e
;
800 bfd_size_type size
= 0;
802 /* Things are much simpler for non-strings.
803 Just assign them slots in the section. */
805 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
807 if (e
->secinfo
->first
== NULL
)
810 secinfo
->sec
->_cooked_size
= size
;
811 e
->secinfo
->first
= e
;
814 size
= (size
+ e
->alignment
- 1)
815 & ~((bfd_vma
) e
->alignment
- 1);
818 secinfo
= e
->secinfo
;
820 secinfo
->sec
->_cooked_size
= size
;
823 /* Finally shrink all input sections which have not made it into
824 the hash table at all. */
825 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
826 if (secinfo
->first
== NULL
)
828 secinfo
->sec
->_cooked_size
= 0;
829 secinfo
->sec
->flags
|= SEC_EXCLUDE
;
836 /* Write out the merged section. */
839 _bfd_write_merged_section (output_bfd
, sec
, psecinfo
)
844 struct sec_merge_sec_info
*secinfo
;
846 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
851 if (bfd_seek (output_bfd
,
852 (sec
->output_section
->filepos
+ sec
->output_offset
),
856 if (! sec_merge_emit (output_bfd
, secinfo
->first
))
862 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
863 *PSEC, this returns the new offset in the adjusted SEC_MERGE
864 section and writes the new section back into *PSEC. */
867 _bfd_merged_section_offset (output_bfd
, psec
, psecinfo
, offset
, addend
)
868 bfd
*output_bfd ATTRIBUTE_UNUSED
;
871 bfd_vma offset
, addend
;
873 struct sec_merge_sec_info
*secinfo
;
874 struct sec_merge_hash_entry
*entry
;
876 asection
*sec
= *psec
;
878 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
880 if (offset
+ addend
>= sec
->_raw_size
)
882 if (offset
+ addend
> sec
->_raw_size
)
883 (*_bfd_error_handler
) (_("%s: access beyond end of merged section (%ld + %ld)"),
884 bfd_get_filename (sec
->owner
), (long)offset
,
886 return (secinfo
->first
? sec
->_cooked_size
: 0);
889 if (secinfo
->htab
->strings
)
891 if (sec
->entsize
== 1)
893 p
= secinfo
->contents
+ offset
+ addend
- 1;
894 while (*p
&& p
>= secinfo
->contents
)
900 p
= secinfo
->contents
901 + ((offset
+ addend
) / sec
->entsize
) * sec
->entsize
;
903 while (p
>= secinfo
->contents
)
907 for (i
= 0; i
< sec
->entsize
; ++i
)
910 if (i
== sec
->entsize
)
919 p
= secinfo
->contents
920 + ((offset
+ addend
) / sec
->entsize
) * sec
->entsize
;
922 entry
= sec_merge_hash_lookup (secinfo
->htab
, p
, 0, false);
925 if (! secinfo
->htab
->strings
)
927 /* This should only happen if somebody points into the padding
928 after a NUL character but before next entity. */
931 if (! secinfo
->htab
->first
)
933 entry
= secinfo
->htab
->first
;
934 p
= secinfo
->contents
935 + ((offset
+ addend
) / sec
->entsize
+ 1) * sec
->entsize
939 *psec
= entry
->secinfo
->sec
;
940 return entry
->u
.index
+ (secinfo
->contents
+ offset
- p
);