Initial import of binutils 2.27 on vendor branch
[dragonfly.git] / contrib / binutils-2.27 / bfd / merge.c
bloba892dde11dac8052e513c53e6ca8863c20b3fa57
1 /* SEC_MERGE support.
2 Copyright (C) 2001-2016 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. */
26 #include "sysdep.h"
27 #include "bfd.h"
28 #include "elf-bfd.h"
29 #include "libbfd.h"
30 #include "hashtab.h"
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. */
41 unsigned int len;
42 /* Start of this string needs to be aligned to
43 alignment octets (not 1 << align). */
44 unsigned int alignment;
45 union
47 /* Index within the merged section. */
48 bfd_size_type index;
49 /* Entry this is a suffix of (if alignment is 0). */
50 struct sec_merge_hash_entry *suffix;
51 } u;
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. */
60 struct sec_merge_hash
62 struct bfd_hash_table table;
63 /* Next available index. */
64 bfd_size_type size;
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;
69 /* Entity size. */
70 unsigned int entsize;
71 /* Are entries fixed size or zero terminated strings? */
72 bfd_boolean strings;
75 struct sec_merge_info
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. */
90 asection *sec;
91 /* Pointer to merge_info pointing to us. */
92 void **psecinfo;
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
109 subclass. */
110 if (entry == NULL)
111 entry = (struct bfd_hash_entry *)
112 bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry));
113 if (entry == NULL)
114 return NULL;
116 /* Call the allocation method of the superclass. */
117 entry = bfd_hash_newfunc (entry, table, string);
119 if (entry != NULL)
121 /* Initialize the local fields. */
122 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
124 ret->u.suffix = NULL;
125 ret->alignment = 0;
126 ret->secinfo = NULL;
127 ret->next = NULL;
130 return entry;
133 /* Look up an entry in a section merge hash table. */
135 static struct sec_merge_hash_entry *
136 sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
137 unsigned int alignment, bfd_boolean create)
139 const unsigned char *s;
140 unsigned long hash;
141 unsigned int c;
142 struct sec_merge_hash_entry *hashp;
143 unsigned int len, i;
144 unsigned int _index;
146 hash = 0;
147 len = 0;
148 s = (const unsigned char *) string;
149 if (table->strings)
151 if (table->entsize == 1)
153 while ((c = *s++) != '\0')
155 hash += c + (c << 17);
156 hash ^= hash >> 2;
157 ++len;
159 hash += len + (len << 17);
161 else
163 for (;;)
165 for (i = 0; i < table->entsize; ++i)
166 if (s[i] != '\0')
167 break;
168 if (i == table->entsize)
169 break;
170 for (i = 0; i < table->entsize; ++i)
172 c = *s++;
173 hash += c + (c << 17);
174 hash ^= hash >> 2;
176 ++len;
178 hash += len + (len << 17);
179 len *= table->entsize;
181 hash ^= hash >> 2;
182 len += table->entsize;
184 else
186 for (i = 0; i < table->entsize; ++i)
188 c = *s++;
189 hash += c + (c << 17);
190 hash ^= hash >> 2;
192 len = table->entsize;
195 _index = hash % table->table.size;
196 for (hashp = (struct sec_merge_hash_entry *) table->table.table[_index];
197 hashp != NULL;
198 hashp = (struct sec_merge_hash_entry *) hashp->root.next)
200 if (hashp->root.hash == hash
201 && len == hashp->len
202 && memcmp (hashp->root.string, string, len) == 0)
204 /* If the string we found does not have at least the required
205 alignment, we need to insert another copy. */
206 if (hashp->alignment < alignment)
208 if (create)
210 /* Mark the less aligned copy as deleted. */
211 hashp->len = 0;
212 hashp->alignment = 0;
214 break;
216 return hashp;
220 if (! create)
221 return NULL;
223 hashp = ((struct sec_merge_hash_entry *)
224 bfd_hash_insert (&table->table, string, hash));
225 if (hashp == NULL)
226 return NULL;
227 hashp->len = len;
228 hashp->alignment = alignment;
229 return hashp;
232 /* Create a new hash table. */
234 static struct sec_merge_hash *
235 sec_merge_init (unsigned int entsize, bfd_boolean strings)
237 struct sec_merge_hash *table;
239 table = (struct sec_merge_hash *) bfd_malloc (sizeof (struct sec_merge_hash));
240 if (table == NULL)
241 return NULL;
243 if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc,
244 sizeof (struct sec_merge_hash_entry), 16699))
246 free (table);
247 return NULL;
250 table->size = 0;
251 table->first = NULL;
252 table->last = NULL;
253 table->entsize = entsize;
254 table->strings = strings;
256 return table;
259 /* Get the index of an entity in a hash table, adding it if it is not
260 already present. */
262 static struct sec_merge_hash_entry *
263 sec_merge_add (struct sec_merge_hash *tab, const char *str,
264 unsigned int alignment, struct sec_merge_sec_info *secinfo)
266 struct sec_merge_hash_entry *entry;
268 entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
269 if (entry == NULL)
270 return NULL;
272 if (entry->secinfo == NULL)
274 tab->size++;
275 entry->secinfo = secinfo;
276 if (tab->first == NULL)
277 tab->first = entry;
278 else
279 tab->last->next = entry;
280 tab->last = entry;
283 return entry;
286 static bfd_boolean
287 sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry,
288 unsigned char *contents, file_ptr offset)
290 struct sec_merge_sec_info *secinfo = entry->secinfo;
291 asection *sec = secinfo->sec;
292 char *pad = NULL;
293 bfd_size_type off = 0;
294 int alignment_power = sec->output_section->alignment_power;
296 if (alignment_power)
298 pad = (char *) bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
299 if (pad == NULL)
300 return FALSE;
303 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
305 const char *str;
306 bfd_size_type len;
308 len = -off & (entry->alignment - 1);
309 if (len != 0)
311 if (contents)
313 memcpy (contents + offset, pad, len);
314 offset += len;
316 else if (bfd_bwrite (pad, len, abfd) != len)
317 goto err;
318 off += len;
321 str = entry->root.string;
322 len = entry->len;
324 if (contents)
326 memcpy (contents + offset, str, len);
327 offset += len;
329 else if (bfd_bwrite (str, len, abfd) != len)
330 goto err;
332 off += len;
335 /* Trailing alignment needed? */
336 off = sec->size - off;
337 if (off != 0)
339 if (contents)
340 memcpy (contents + offset, pad, off);
341 else if (bfd_bwrite (pad, off, abfd) != off)
342 goto err;
345 if (pad != NULL)
346 free (pad);
347 return TRUE;
349 err:
350 if (pad != NULL)
351 free (pad);
352 return FALSE;
355 /* Register a SEC_MERGE section as a candidate for merging.
356 This function is called for all non-dynamic SEC_MERGE input sections. */
358 bfd_boolean
359 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
360 void **psecinfo)
362 struct sec_merge_info *sinfo;
363 struct sec_merge_sec_info *secinfo;
364 unsigned int align;
365 bfd_size_type amt;
366 bfd_byte *contents;
368 if ((abfd->flags & DYNAMIC) != 0
369 || (sec->flags & SEC_MERGE) == 0)
370 abort ();
372 if (sec->size == 0
373 || (sec->flags & SEC_EXCLUDE) != 0
374 || sec->entsize == 0)
375 return TRUE;
377 if ((sec->flags & SEC_RELOC) != 0)
379 /* We aren't prepared to handle relocations in merged sections. */
380 return TRUE;
383 align = sec->alignment_power;
384 if ((sec->entsize < (unsigned) 1 << align
385 && ((sec->entsize & (sec->entsize - 1))
386 || !(sec->flags & SEC_STRINGS)))
387 || (sec->entsize > (unsigned) 1 << align
388 && (sec->entsize & (((unsigned) 1 << align) - 1))))
390 /* Sanity check. If string character size is smaller than
391 alignment, then we require character size to be a power
392 of 2, otherwise character size must be integer multiple
393 of alignment. For non-string constants, alignment must
394 be smaller than or equal to entity size and entity size
395 must be integer multiple of alignment. */
396 return TRUE;
399 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
400 if ((secinfo = sinfo->chain)
401 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
402 && secinfo->sec->entsize == sec->entsize
403 && secinfo->sec->alignment_power == sec->alignment_power
404 && secinfo->sec->output_section == sec->output_section)
405 break;
407 if (sinfo == NULL)
409 /* Initialize the information we need to keep track of. */
410 sinfo = (struct sec_merge_info *)
411 bfd_alloc (abfd, sizeof (struct sec_merge_info));
412 if (sinfo == NULL)
413 goto error_return;
414 sinfo->next = (struct sec_merge_info *) *psinfo;
415 sinfo->chain = NULL;
416 *psinfo = sinfo;
417 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
418 if (sinfo->htab == NULL)
419 goto error_return;
422 /* Read the section from abfd. */
424 amt = sizeof (struct sec_merge_sec_info) - 1 + sec->size;
425 if (sec->flags & SEC_STRINGS)
426 /* Some versions of gcc may emit a string without a zero terminator.
427 See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
428 Allocate space for an extra zero. */
429 amt += sec->entsize;
430 *psecinfo = bfd_alloc (abfd, amt);
431 if (*psecinfo == NULL)
432 goto error_return;
434 secinfo = (struct sec_merge_sec_info *) *psecinfo;
435 if (sinfo->chain)
437 secinfo->next = sinfo->chain->next;
438 sinfo->chain->next = secinfo;
440 else
441 secinfo->next = secinfo;
442 sinfo->chain = secinfo;
443 secinfo->sec = sec;
444 secinfo->psecinfo = psecinfo;
445 secinfo->htab = sinfo->htab;
446 secinfo->first_str = NULL;
448 sec->rawsize = sec->size;
449 if (sec->flags & SEC_STRINGS)
450 memset (secinfo->contents + sec->size, 0, sec->entsize);
451 contents = secinfo->contents;
452 if (! bfd_get_full_section_contents (sec->owner, sec, &contents))
453 goto error_return;
455 return TRUE;
457 error_return:
458 *psecinfo = NULL;
459 return FALSE;
462 /* Record one section into the hash table. */
463 static bfd_boolean
464 record_section (struct sec_merge_info *sinfo,
465 struct sec_merge_sec_info *secinfo)
467 asection *sec = secinfo->sec;
468 struct sec_merge_hash_entry *entry;
469 bfd_boolean nul;
470 unsigned char *p, *end;
471 bfd_vma mask, eltalign;
472 unsigned int align, i;
474 align = sec->alignment_power;
475 end = secinfo->contents + sec->size;
476 nul = FALSE;
477 mask = ((bfd_vma) 1 << align) - 1;
478 if (sec->flags & SEC_STRINGS)
480 for (p = secinfo->contents; p < end; )
482 eltalign = p - secinfo->contents;
483 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
484 if (!eltalign || eltalign > mask)
485 eltalign = mask + 1;
486 entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
487 secinfo);
488 if (! entry)
489 goto error_return;
490 p += entry->len;
491 if (sec->entsize == 1)
493 while (p < end && *p == 0)
495 if (!nul && !((p - secinfo->contents) & mask))
497 nul = TRUE;
498 entry = sec_merge_add (sinfo->htab, "",
499 (unsigned) mask + 1, secinfo);
500 if (! entry)
501 goto error_return;
503 p++;
506 else
508 while (p < end)
510 for (i = 0; i < sec->entsize; i++)
511 if (p[i] != '\0')
512 break;
513 if (i != sec->entsize)
514 break;
515 if (!nul && !((p - secinfo->contents) & mask))
517 nul = TRUE;
518 entry = sec_merge_add (sinfo->htab, (char *) p,
519 (unsigned) mask + 1, secinfo);
520 if (! entry)
521 goto error_return;
523 p += sec->entsize;
528 else
530 for (p = secinfo->contents; p < end; p += sec->entsize)
532 entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
533 if (! entry)
534 goto error_return;
538 return TRUE;
540 error_return:
541 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
542 *secinfo->psecinfo = NULL;
543 return FALSE;
546 static int
547 strrevcmp (const void *a, const void *b)
549 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
550 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
551 unsigned int lenA = A->len;
552 unsigned int lenB = B->len;
553 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
554 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
555 int l = lenA < lenB ? lenA : lenB;
557 while (l)
559 if (*s != *t)
560 return (int) *s - (int) *t;
561 s--;
562 t--;
563 l--;
565 return lenA - lenB;
568 /* Like strrevcmp, but for the case where all strings have the same
569 alignment > entsize. */
571 static int
572 strrevcmp_align (const void *a, const void *b)
574 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
575 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
576 unsigned int lenA = A->len;
577 unsigned int lenB = B->len;
578 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
579 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
580 int l = lenA < lenB ? lenA : lenB;
581 int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
583 if (tail_align != 0)
584 return tail_align;
586 while (l)
588 if (*s != *t)
589 return (int) *s - (int) *t;
590 s--;
591 t--;
592 l--;
594 return lenA - lenB;
597 static inline int
598 is_suffix (const struct sec_merge_hash_entry *A,
599 const struct sec_merge_hash_entry *B)
601 if (A->len <= B->len)
602 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
603 not to be equal by the hash table. */
604 return 0;
606 return memcmp (A->root.string + (A->len - B->len),
607 B->root.string, B->len) == 0;
610 /* This is a helper function for _bfd_merge_sections. It attempts to
611 merge strings matching suffixes of longer strings. */
612 static void
613 merge_strings (struct sec_merge_info *sinfo)
615 struct sec_merge_hash_entry **array, **a, *e;
616 struct sec_merge_sec_info *secinfo;
617 bfd_size_type size, amt;
618 unsigned int alignment = 0;
620 /* Now sort the strings */
621 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
622 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
623 if (array == NULL)
624 goto alloc_failure;
626 for (e = sinfo->htab->first, a = array; e; e = e->next)
627 if (e->alignment)
629 *a++ = e;
630 /* Adjust the length to not include the zero terminator. */
631 e->len -= sinfo->htab->entsize;
632 if (alignment != e->alignment)
634 if (alignment == 0)
635 alignment = e->alignment;
636 else
637 alignment = (unsigned) -1;
641 sinfo->htab->size = a - array;
642 if (sinfo->htab->size != 0)
644 qsort (array, (size_t) sinfo->htab->size,
645 sizeof (struct sec_merge_hash_entry *),
646 (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
647 ? strrevcmp_align : strrevcmp));
649 /* Loop over the sorted array and merge suffixes */
650 e = *--a;
651 e->len += sinfo->htab->entsize;
652 while (--a >= array)
654 struct sec_merge_hash_entry *cmp = *a;
656 cmp->len += sinfo->htab->entsize;
657 if (e->alignment >= cmp->alignment
658 && !((e->len - cmp->len) & (cmp->alignment - 1))
659 && is_suffix (e, cmp))
661 cmp->u.suffix = e;
662 cmp->alignment = 0;
664 else
665 e = cmp;
669 alloc_failure:
670 if (array)
671 free (array);
673 /* Now assign positions to the strings we want to keep. */
674 size = 0;
675 secinfo = sinfo->htab->first->secinfo;
676 for (e = sinfo->htab->first; e; e = e->next)
678 if (e->secinfo != secinfo)
680 secinfo->sec->size = size;
681 secinfo = e->secinfo;
683 if (e->alignment)
685 if (e->secinfo->first_str == NULL)
687 e->secinfo->first_str = e;
688 size = 0;
690 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
691 e->u.index = size;
692 size += e->len;
695 secinfo->sec->size = size;
696 if (secinfo->sec->alignment_power != 0)
698 bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
699 secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
702 /* And now adjust the rest, removing them from the chain (but not hashtable)
703 at the same time. */
704 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
705 if (e->alignment)
706 a = &e->next;
707 else
709 *a = e->next;
710 if (e->len)
712 e->secinfo = e->u.suffix->secinfo;
713 e->alignment = e->u.suffix->alignment;
714 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
719 /* This function is called once after all SEC_MERGE sections are registered
720 with _bfd_merge_section. */
722 bfd_boolean
723 _bfd_merge_sections (bfd *abfd,
724 struct bfd_link_info *info ATTRIBUTE_UNUSED,
725 void *xsinfo,
726 void (*remove_hook) (bfd *, asection *))
728 struct sec_merge_info *sinfo;
730 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
732 struct sec_merge_sec_info * secinfo;
734 if (! sinfo->chain)
735 continue;
737 /* Move sinfo->chain to head of the chain, terminate it. */
738 secinfo = sinfo->chain;
739 sinfo->chain = secinfo->next;
740 secinfo->next = NULL;
742 /* Record the sections into the hash table. */
743 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
744 if (secinfo->sec->flags & SEC_EXCLUDE)
746 *secinfo->psecinfo = NULL;
747 if (remove_hook)
748 (*remove_hook) (abfd, secinfo->sec);
750 else if (! record_section (sinfo, secinfo))
751 break;
753 if (secinfo)
754 continue;
756 if (sinfo->htab->first == NULL)
757 continue;
759 if (sinfo->htab->strings)
760 merge_strings (sinfo);
761 else
763 struct sec_merge_hash_entry *e;
764 bfd_size_type size = 0;
766 /* Things are much simpler for non-strings.
767 Just assign them slots in the section. */
768 secinfo = NULL;
769 for (e = sinfo->htab->first; e; e = e->next)
771 if (e->secinfo->first_str == NULL)
773 if (secinfo)
774 secinfo->sec->size = size;
775 e->secinfo->first_str = e;
776 size = 0;
778 size = (size + e->alignment - 1)
779 & ~((bfd_vma) e->alignment - 1);
780 e->u.index = size;
781 size += e->len;
782 secinfo = e->secinfo;
784 secinfo->sec->size = size;
787 /* Finally remove all input sections which have not made it into
788 the hash table at all. */
789 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
790 if (secinfo->first_str == NULL)
791 secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
794 return TRUE;
797 /* Write out the merged section. */
799 bfd_boolean
800 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
802 struct sec_merge_sec_info *secinfo;
803 file_ptr pos;
804 unsigned char *contents;
805 Elf_Internal_Shdr *hdr;
807 secinfo = (struct sec_merge_sec_info *) psecinfo;
809 if (!secinfo)
810 return FALSE;
812 if (secinfo->first_str == NULL)
813 return TRUE;
815 /* FIXME: octets_per_byte. */
816 hdr = &elf_section_data (sec->output_section)->this_hdr;
817 if (hdr->sh_offset == (file_ptr) -1)
819 /* We must compress this section. Write output to the
820 buffer. */
821 contents = hdr->contents;
822 if ((sec->output_section->flags & SEC_ELF_COMPRESS) == 0
823 || contents == NULL)
824 abort ();
826 else
828 contents = NULL;
829 pos = sec->output_section->filepos + sec->output_offset;
830 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
831 return FALSE;
834 if (! sec_merge_emit (output_bfd, secinfo->first_str, contents,
835 sec->output_offset))
836 return FALSE;
838 return TRUE;
841 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
842 *PSEC, this returns the new offset in the adjusted SEC_MERGE
843 section and writes the new section back into *PSEC. */
845 bfd_vma
846 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
847 void *psecinfo, bfd_vma offset)
849 struct sec_merge_sec_info *secinfo;
850 struct sec_merge_hash_entry *entry;
851 unsigned char *p;
852 asection *sec = *psec;
854 secinfo = (struct sec_merge_sec_info *) psecinfo;
856 if (!secinfo)
857 return offset;
859 if (offset >= sec->rawsize)
861 if (offset > sec->rawsize)
863 (*_bfd_error_handler)
864 (_("%s: access beyond end of merged section (%ld)"),
865 bfd_get_filename (sec->owner), (long) offset);
867 return secinfo->first_str ? sec->size : 0;
870 if (secinfo->htab->strings)
872 if (sec->entsize == 1)
874 p = secinfo->contents + offset - 1;
875 while (p >= secinfo->contents && *p)
876 --p;
877 ++p;
879 else
881 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
882 p -= sec->entsize;
883 while (p >= secinfo->contents)
885 unsigned int i;
887 for (i = 0; i < sec->entsize; ++i)
888 if (p[i] != '\0')
889 break;
890 if (i == sec->entsize)
891 break;
892 p -= sec->entsize;
894 p += sec->entsize;
897 else
899 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
901 entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
902 if (!entry)
904 if (! secinfo->htab->strings)
905 abort ();
906 /* This should only happen if somebody points into the padding
907 after a NUL character but before next entity. */
908 if (*p)
909 abort ();
910 if (! secinfo->htab->first)
911 abort ();
912 entry = secinfo->htab->first;
913 p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
914 - entry->len);
917 *psec = entry->secinfo->sec;
918 return entry->u.index + (secinfo->contents + offset - p);
921 /* Tidy up when done. */
923 void
924 _bfd_merge_sections_free (void *xsinfo)
926 struct sec_merge_info *sinfo;
928 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
930 bfd_hash_table_free (&sinfo->htab->table);
931 free (sinfo->htab);