2 Copyright 2001, 2002, 2003 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. */
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
;
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 struct sec_merge_hash_entry
*ret
= (struct sec_merge_hash_entry
*) entry
;
107 /* Allocate the structure if it has not already been allocated by a
109 if (ret
== (struct sec_merge_hash_entry
*) NULL
)
110 ret
= ((struct sec_merge_hash_entry
*)
111 bfd_hash_allocate (table
, sizeof (struct sec_merge_hash_entry
)));
112 if (ret
== (struct sec_merge_hash_entry
*) NULL
)
115 /* Call the allocation method of the superclass. */
116 ret
= ((struct sec_merge_hash_entry
*)
117 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
121 /* Initialize the local fields. */
122 ret
->u
.suffix
= NULL
;
128 return (struct bfd_hash_entry
*) ret
;
131 /* Look up an entry in a section merge hash table. */
133 static struct sec_merge_hash_entry
*
134 sec_merge_hash_lookup (struct sec_merge_hash
*table
, const char *string
,
135 unsigned int alignment
, bfd_boolean create
)
137 register const unsigned char *s
;
138 register unsigned long hash
;
139 register unsigned int c
;
140 struct sec_merge_hash_entry
*hashp
;
146 s
= (const unsigned char *) string
;
149 if (table
->entsize
== 1)
151 while ((c
= *s
++) != '\0')
153 hash
+= c
+ (c
<< 17);
157 hash
+= len
+ (len
<< 17);
163 for (i
= 0; i
< table
->entsize
; ++i
)
166 if (i
== table
->entsize
)
168 for (i
= 0; i
< table
->entsize
; ++i
)
171 hash
+= c
+ (c
<< 17);
176 hash
+= len
+ (len
<< 17);
177 len
*= table
->entsize
;
180 len
+= table
->entsize
;
184 for (i
= 0; i
< table
->entsize
; ++i
)
187 hash
+= c
+ (c
<< 17);
190 len
= table
->entsize
;
193 index
= hash
% table
->table
.size
;
194 for (hashp
= (struct sec_merge_hash_entry
*) table
->table
.table
[index
];
195 hashp
!= (struct sec_merge_hash_entry
*) NULL
;
196 hashp
= (struct sec_merge_hash_entry
*) hashp
->root
.next
)
198 if (hashp
->root
.hash
== hash
200 && memcmp (hashp
->root
.string
, string
, len
) == 0)
202 /* If the string we found does not have at least the required
203 alignment, we need to insert another copy. */
204 if (hashp
->alignment
< alignment
)
208 /* Mark the less aligned copy as deleted. */
210 hashp
->alignment
= 0;
219 return (struct sec_merge_hash_entry
*) NULL
;
221 hashp
= (struct sec_merge_hash_entry
*)
222 sec_merge_hash_newfunc ((struct bfd_hash_entry
*) NULL
,
223 (struct bfd_hash_table
*) table
, string
);
224 if (hashp
== (struct sec_merge_hash_entry
*) NULL
)
225 return (struct sec_merge_hash_entry
*) NULL
;
226 hashp
->root
.string
= string
;
227 hashp
->root
.hash
= hash
;
229 hashp
->alignment
= alignment
;
230 hashp
->root
.next
= table
->table
.table
[index
];
231 table
->table
.table
[index
] = (struct bfd_hash_entry
*) hashp
;
236 /* Create a new hash table. */
238 static struct sec_merge_hash
*
239 sec_merge_init (unsigned int entsize
, bfd_boolean strings
)
241 struct sec_merge_hash
*table
;
242 bfd_size_type amt
= sizeof (struct sec_merge_hash
);
244 table
= (struct sec_merge_hash
*) bfd_malloc (amt
);
248 if (! bfd_hash_table_init (&table
->table
, sec_merge_hash_newfunc
))
257 table
->entsize
= entsize
;
258 table
->strings
= strings
;
263 /* Get the index of an entity in a hash table, adding it if it is not
266 static struct sec_merge_hash_entry
*
267 sec_merge_add (struct sec_merge_hash
*tab
, const char *str
,
268 unsigned int alignment
, struct sec_merge_sec_info
*secinfo
)
270 register struct sec_merge_hash_entry
*entry
;
272 entry
= sec_merge_hash_lookup (tab
, str
, alignment
, TRUE
);
276 if (entry
->secinfo
== NULL
)
279 entry
->secinfo
= secinfo
;
280 if (tab
->first
== NULL
)
283 tab
->last
->next
= entry
;
291 sec_merge_emit (bfd
*abfd
, struct sec_merge_hash_entry
*entry
)
293 struct sec_merge_sec_info
*secinfo
= entry
->secinfo
;
294 asection
*sec
= secinfo
->sec
;
296 bfd_size_type off
= 0;
297 int alignment_power
= bfd_get_section_alignment (abfd
, sec
->output_section
);
300 pad
= bfd_zmalloc ((bfd_size_type
) 1 << alignment_power
);
302 for (; entry
!= NULL
&& entry
->secinfo
== secinfo
; entry
= entry
->next
)
304 register const char *str
;
307 len
= off
& (entry
->alignment
- 1);
310 len
= entry
->alignment
- len
;
311 if (bfd_bwrite (pad
, (bfd_size_type
) len
, abfd
) != len
)
316 str
= entry
->root
.string
;
319 if (bfd_bwrite (str
, (bfd_size_type
) len
, abfd
) != len
)
328 return entry
== NULL
|| entry
->secinfo
!= secinfo
;
331 /* This function is called for each input file from the add_symbols
332 pass of the linker. */
335 _bfd_merge_section (bfd
*abfd
, void **psinfo
, asection
*sec
, void **psecinfo
)
337 struct sec_merge_info
*sinfo
;
338 struct sec_merge_sec_info
*secinfo
;
342 if (sec
->_raw_size
== 0
343 || (sec
->flags
& SEC_EXCLUDE
)
344 || (sec
->flags
& SEC_MERGE
) == 0
345 || sec
->entsize
== 0)
348 if ((sec
->flags
& SEC_RELOC
) != 0)
350 /* We aren't prepared to handle relocations in merged sections. */
354 align
= bfd_get_section_alignment (sec
->owner
, sec
);
355 if ((sec
->entsize
< (unsigned int)(1 << align
)
356 && ((sec
->entsize
& (sec
->entsize
- 1))
357 || !(sec
->flags
& SEC_STRINGS
)))
358 || (sec
->entsize
> (unsigned int)(1 << align
)
359 && (sec
->entsize
& ((1 << align
) - 1))))
361 /* Sanity check. If string character size is smaller than
362 alignment, then we require character size to be a power
363 of 2, otherwise character size must be integer multiple
364 of alignment. For non-string constants, alignment must
365 be smaller than or equal to entity size and entity size
366 must be integer multiple of alignment. */
370 for (sinfo
= (struct sec_merge_info
*) *psinfo
; sinfo
; sinfo
= sinfo
->next
)
371 if ((secinfo
= sinfo
->chain
)
372 && ! ((secinfo
->sec
->flags
^ sec
->flags
) & (SEC_MERGE
| SEC_STRINGS
))
373 && secinfo
->sec
->entsize
== sec
->entsize
374 && ! strcmp (secinfo
->sec
->name
, sec
->name
))
379 /* Initialize the information we need to keep track of. */
380 amt
= sizeof (struct sec_merge_info
);
381 sinfo
= (struct sec_merge_info
*) bfd_alloc (abfd
, amt
);
384 sinfo
->next
= (struct sec_merge_info
*) *psinfo
;
387 sinfo
->htab
= sec_merge_init (sec
->entsize
, (sec
->flags
& SEC_STRINGS
));
388 if (sinfo
->htab
== NULL
)
392 /* Read the section from abfd. */
394 amt
= sizeof (struct sec_merge_sec_info
) + sec
->_raw_size
- 1;
395 *psecinfo
= bfd_alloc (abfd
, amt
);
396 if (*psecinfo
== NULL
)
399 secinfo
= (struct sec_merge_sec_info
*)*psecinfo
;
402 secinfo
->next
= sinfo
->chain
->next
;
403 sinfo
->chain
->next
= secinfo
;
406 secinfo
->next
= secinfo
;
407 sinfo
->chain
= secinfo
;
409 secinfo
->psecinfo
= psecinfo
;
410 secinfo
->htab
= sinfo
->htab
;
411 secinfo
->first
= NULL
;
413 if (! bfd_get_section_contents (sec
->owner
, sec
, secinfo
->contents
,
414 (bfd_vma
) 0, sec
->_raw_size
))
424 /* Record one section into the hash table. */
426 record_section (struct sec_merge_info
*sinfo
,
427 struct sec_merge_sec_info
*secinfo
)
429 asection
*sec
= secinfo
->sec
;
430 struct sec_merge_hash_entry
*entry
;
432 unsigned char *p
, *end
;
433 bfd_vma mask
, eltalign
;
434 unsigned int align
, i
;
436 align
= bfd_get_section_alignment (sec
->owner
, sec
);
437 end
= secinfo
->contents
+ sec
->_raw_size
;
439 mask
= ((bfd_vma
) 1 << align
) - 1;
440 if (sec
->flags
& SEC_STRINGS
)
442 for (p
= secinfo
->contents
; p
< end
; )
444 eltalign
= p
- secinfo
->contents
;
445 eltalign
= ((eltalign
^ (eltalign
- 1)) + 1) >> 1;
446 if (!eltalign
|| eltalign
> mask
)
448 entry
= sec_merge_add (sinfo
->htab
, p
, (unsigned) eltalign
, secinfo
);
452 if (sec
->entsize
== 1)
454 while (p
< end
&& *p
== 0)
456 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
459 entry
= sec_merge_add (sinfo
->htab
, "",
460 (unsigned) mask
+ 1, secinfo
);
471 for (i
= 0; i
< sec
->entsize
; i
++)
474 if (i
!= sec
->entsize
)
476 if (!nul
&& !((p
- secinfo
->contents
) & mask
))
479 entry
= sec_merge_add (sinfo
->htab
, p
,
480 (unsigned) mask
+ 1, secinfo
);
491 for (p
= secinfo
->contents
; p
< end
; p
+= sec
->entsize
)
493 entry
= sec_merge_add (sinfo
->htab
, p
, 1, secinfo
);
502 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
503 *secinfo
->psecinfo
= NULL
;
508 strrevcmp (const void *a
, const void *b
)
510 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
511 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
512 unsigned int lenA
= A
->len
;
513 unsigned int lenB
= B
->len
;
514 const unsigned char *s
= A
->root
.string
+ lenA
- 1;
515 const unsigned char *t
= B
->root
.string
+ lenB
- 1;
516 int l
= lenA
< lenB
? lenA
: lenB
;
521 return (int) *s
- (int) *t
;
529 /* Like strrevcmp, but for the case where all strings have the same
530 alignment > entsize. */
533 strrevcmp_align (const void *a
, const void *b
)
535 struct sec_merge_hash_entry
*A
= *(struct sec_merge_hash_entry
**) a
;
536 struct sec_merge_hash_entry
*B
= *(struct sec_merge_hash_entry
**) b
;
537 unsigned int lenA
= A
->len
;
538 unsigned int lenB
= B
->len
;
539 const unsigned char *s
= A
->root
.string
+ lenA
- 1;
540 const unsigned char *t
= B
->root
.string
+ lenB
- 1;
541 int l
= lenA
< lenB
? lenA
: lenB
;
542 int tail_align
= (lenA
& (A
->alignment
- 1)) - (lenB
& (A
->alignment
- 1));
550 return (int) *s
- (int) *t
;
559 is_suffix (const struct sec_merge_hash_entry
*A
,
560 const struct sec_merge_hash_entry
*B
)
562 if (A
->len
<= B
->len
)
563 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
564 not to be equal by the hash table. */
567 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
568 B
->root
.string
, B
->len
) == 0;
571 /* This is a helper function for _bfd_merge_sections. It attempts to
572 merge strings matching suffixes of longer strings. */
574 merge_strings (struct sec_merge_info
*sinfo
)
576 struct sec_merge_hash_entry
**array
, **a
, *e
;
577 struct sec_merge_sec_info
*secinfo
;
578 bfd_size_type size
, amt
;
579 unsigned int alignment
= 0;
581 /* Now sort the strings */
582 amt
= sinfo
->htab
->size
* sizeof (struct sec_merge_hash_entry
*);
583 array
= (struct sec_merge_hash_entry
**) bfd_malloc (amt
);
587 for (e
= sinfo
->htab
->first
, a
= array
; e
; e
= e
->next
)
591 /* Adjust the length to not include the zero terminator. */
592 e
->len
-= sinfo
->htab
->entsize
;
593 if (alignment
!= e
->alignment
)
596 alignment
= e
->alignment
;
598 alignment
= (unsigned) -1;
602 sinfo
->htab
->size
= a
- array
;
603 if (sinfo
->htab
->size
!= 0)
605 qsort (array
, (size_t) sinfo
->htab
->size
,
606 sizeof (struct sec_merge_hash_entry
*),
607 (alignment
!= (unsigned) -1 && alignment
> sinfo
->htab
->entsize
608 ? strrevcmp_align
: strrevcmp
));
610 /* Loop over the sorted array and merge suffixes */
612 e
->len
+= sinfo
->htab
->entsize
;
615 struct sec_merge_hash_entry
*cmp
= *a
;
617 cmp
->len
+= sinfo
->htab
->entsize
;
618 if (e
->alignment
>= cmp
->alignment
619 && !((e
->len
- cmp
->len
) & (cmp
->alignment
- 1))
620 && is_suffix (e
, cmp
))
634 /* Now assign positions to the strings we want to keep. */
636 secinfo
= sinfo
->htab
->first
->secinfo
;
637 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
639 if (e
->secinfo
!= secinfo
)
641 secinfo
->sec
->_cooked_size
= size
;
642 secinfo
= e
->secinfo
;
646 if (e
->secinfo
->first
== NULL
)
648 e
->secinfo
->first
= e
;
651 size
= (size
+ e
->alignment
- 1) & ~((bfd_vma
) e
->alignment
- 1);
656 secinfo
->sec
->_cooked_size
= size
;
658 /* And now adjust the rest, removing them from the chain (but not hashtable)
660 for (a
= &sinfo
->htab
->first
, e
= *a
; e
; e
= e
->next
)
668 e
->secinfo
= e
->u
.suffix
->secinfo
;
669 e
->alignment
= e
->u
.suffix
->alignment
;
670 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
- e
->len
);
675 /* This function is called once after all SEC_MERGE sections are registered
676 with _bfd_merge_section. */
679 _bfd_merge_sections (bfd
*abfd ATTRIBUTE_UNUSED
, void *xsinfo
,
680 void (*remove_hook
) (bfd
*, asection
*))
682 struct sec_merge_info
*sinfo
;
684 for (sinfo
= (struct sec_merge_info
*) xsinfo
; sinfo
; sinfo
= sinfo
->next
)
686 struct sec_merge_sec_info
* secinfo
;
691 /* Move sinfo->chain to head of the chain, terminate it. */
692 secinfo
= sinfo
->chain
;
693 sinfo
->chain
= secinfo
->next
;
694 secinfo
->next
= NULL
;
696 /* Record the sections into the hash table. */
697 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
698 if (secinfo
->sec
->flags
& SEC_EXCLUDE
)
700 *secinfo
->psecinfo
= NULL
;
702 (*remove_hook
) (abfd
, secinfo
->sec
);
704 else if (! record_section (sinfo
, secinfo
))
710 if (sinfo
->htab
->first
== NULL
)
713 if (sinfo
->htab
->strings
)
714 merge_strings (sinfo
);
717 struct sec_merge_hash_entry
*e
;
718 bfd_size_type size
= 0;
720 /* Things are much simpler for non-strings.
721 Just assign them slots in the section. */
723 for (e
= sinfo
->htab
->first
; e
; e
= e
->next
)
725 if (e
->secinfo
->first
== NULL
)
728 secinfo
->sec
->_cooked_size
= size
;
729 e
->secinfo
->first
= e
;
732 size
= (size
+ e
->alignment
- 1)
733 & ~((bfd_vma
) e
->alignment
- 1);
736 secinfo
= e
->secinfo
;
738 secinfo
->sec
->_cooked_size
= size
;
741 /* Finally remove all input sections which have not made it into
742 the hash table at all. */
743 for (secinfo
= sinfo
->chain
; secinfo
; secinfo
= secinfo
->next
)
744 if (secinfo
->first
== NULL
)
746 secinfo
->sec
->_cooked_size
= 0;
747 secinfo
->sec
->flags
|= SEC_EXCLUDE
;
754 /* Write out the merged section. */
757 _bfd_write_merged_section (bfd
*output_bfd
, asection
*sec
, void *psecinfo
)
759 struct sec_merge_sec_info
*secinfo
;
762 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
767 pos
= sec
->output_section
->filepos
+ sec
->output_offset
;
768 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0)
771 if (! sec_merge_emit (output_bfd
, secinfo
->first
))
777 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
778 *PSEC, this returns the new offset in the adjusted SEC_MERGE
779 section and writes the new section back into *PSEC. */
782 _bfd_merged_section_offset (bfd
*output_bfd ATTRIBUTE_UNUSED
, asection
**psec
,
783 void *psecinfo
, bfd_vma offset
, bfd_vma addend
)
785 struct sec_merge_sec_info
*secinfo
;
786 struct sec_merge_hash_entry
*entry
;
788 asection
*sec
= *psec
;
790 secinfo
= (struct sec_merge_sec_info
*) psecinfo
;
792 if (offset
+ addend
>= sec
->_raw_size
)
794 if (offset
+ addend
> sec
->_raw_size
)
796 (*_bfd_error_handler
)
797 (_("%s: access beyond end of merged section (%ld + %ld)"),
798 bfd_get_filename (sec
->owner
), (long) offset
, (long) addend
);
800 return (secinfo
->first
? sec
->_cooked_size
: 0);
803 if (secinfo
->htab
->strings
)
805 if (sec
->entsize
== 1)
807 p
= secinfo
->contents
+ offset
+ addend
- 1;
808 while (p
>= secinfo
->contents
&& *p
)
814 p
= secinfo
->contents
815 + ((offset
+ addend
) / sec
->entsize
) * sec
->entsize
;
817 while (p
>= secinfo
->contents
)
821 for (i
= 0; i
< sec
->entsize
; ++i
)
824 if (i
== sec
->entsize
)
833 p
= secinfo
->contents
834 + ((offset
+ addend
) / sec
->entsize
) * sec
->entsize
;
836 entry
= sec_merge_hash_lookup (secinfo
->htab
, p
, 0, FALSE
);
839 if (! secinfo
->htab
->strings
)
841 /* This should only happen if somebody points into the padding
842 after a NUL character but before next entity. */
845 if (! secinfo
->htab
->first
)
847 entry
= secinfo
->htab
->first
;
848 p
= secinfo
->contents
849 + ((offset
+ addend
) / sec
->entsize
+ 1) * sec
->entsize
853 *psec
= entry
->secinfo
->sec
;
854 return entry
->u
.index
+ (secinfo
->contents
+ offset
- p
);