* merge.c (sec_merge_emit): Do not zero pad sections that do not
[binutils.git] / bfd / merge.c
blob180af267a219bcaf970e19f0d421f4a75b398899
1 /* SEC_MERGE support.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Written by Jakub Jelinek <jakub@redhat.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 /* This file contains support for merging duplicate entities within sections,
25 as used in ELF SHF_MERGE. */
27 #include "sysdep.h"
28 #include "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)
289 struct sec_merge_sec_info *secinfo = entry->secinfo;
290 asection *sec = secinfo->sec;
291 char *pad = NULL;
292 bfd_size_type off = 0;
293 int alignment_power = sec->output_section->alignment_power;
295 if (alignment_power)
297 pad = (char *) bfd_zmalloc ((bfd_size_type) 1 << alignment_power);
298 if (pad == NULL)
299 return FALSE;
302 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
304 const char *str;
305 bfd_size_type len;
307 len = -off & (entry->alignment - 1);
308 if (len != 0)
310 /* We should never have an entry with an alignment
311 greater than the section's alignment. */
312 BFD_ASSERT (len <= (bfd_size_type) (1 << alignment_power));
313 if (bfd_bwrite (pad, len, abfd) != len)
314 goto err;
315 off += len;
318 str = entry->root.string;
319 len = entry->len;
321 if (bfd_bwrite (str, len, abfd) != len)
322 goto err;
324 off += len;
327 /* Trailing alignment needed? */
328 off = sec->size - off;
329 if (off != 0
330 && alignment_power
331 && bfd_bwrite (pad, off, abfd) != off)
332 goto err;
334 if (pad != NULL)
335 free (pad);
336 return TRUE;
338 err:
339 if (pad != NULL)
340 free (pad);
341 return FALSE;
344 /* Register a SEC_MERGE section as a candidate for merging.
345 This function is called for all non-dynamic SEC_MERGE input sections. */
347 bfd_boolean
348 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
349 void **psecinfo)
351 struct sec_merge_info *sinfo;
352 struct sec_merge_sec_info *secinfo;
353 unsigned int align;
354 bfd_size_type amt;
356 if ((abfd->flags & DYNAMIC) != 0
357 || (sec->flags & SEC_MERGE) == 0)
358 abort ();
360 if (sec->size == 0
361 || (sec->flags & SEC_EXCLUDE) != 0
362 || sec->entsize == 0)
363 return TRUE;
365 if ((sec->flags & SEC_RELOC) != 0)
367 /* We aren't prepared to handle relocations in merged sections. */
368 return TRUE;
371 align = sec->alignment_power;
372 if ((sec->entsize < (unsigned) 1 << align
373 && ((sec->entsize & (sec->entsize - 1))
374 || !(sec->flags & SEC_STRINGS)))
375 || (sec->entsize > (unsigned) 1 << align
376 && (sec->entsize & (((unsigned) 1 << align) - 1))))
378 /* Sanity check. If string character size is smaller than
379 alignment, then we require character size to be a power
380 of 2, otherwise character size must be integer multiple
381 of alignment. For non-string constants, alignment must
382 be smaller than or equal to entity size and entity size
383 must be integer multiple of alignment. */
384 return TRUE;
387 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
388 if ((secinfo = sinfo->chain)
389 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
390 && secinfo->sec->entsize == sec->entsize
391 && secinfo->sec->alignment_power == sec->alignment_power
392 && secinfo->sec->output_section == sec->output_section)
393 break;
395 if (sinfo == NULL)
397 /* Initialize the information we need to keep track of. */
398 sinfo = (struct sec_merge_info *)
399 bfd_alloc (abfd, sizeof (struct sec_merge_info));
400 if (sinfo == NULL)
401 goto error_return;
402 sinfo->next = (struct sec_merge_info *) *psinfo;
403 sinfo->chain = NULL;
404 *psinfo = sinfo;
405 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
406 if (sinfo->htab == NULL)
407 goto error_return;
410 /* Read the section from abfd. */
412 amt = sizeof (struct sec_merge_sec_info) - 1 + sec->size;
413 if (sec->flags & SEC_STRINGS)
414 /* Some versions of gcc may emit a string without a zero terminator.
415 See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
416 Allocate space for an extra zero. */
417 amt += sec->entsize;
418 *psecinfo = bfd_alloc (abfd, amt);
419 if (*psecinfo == NULL)
420 goto error_return;
422 secinfo = (struct sec_merge_sec_info *) *psecinfo;
423 if (sinfo->chain)
425 secinfo->next = sinfo->chain->next;
426 sinfo->chain->next = secinfo;
428 else
429 secinfo->next = secinfo;
430 sinfo->chain = secinfo;
431 secinfo->sec = sec;
432 secinfo->psecinfo = psecinfo;
433 secinfo->htab = sinfo->htab;
434 secinfo->first_str = NULL;
436 sec->rawsize = sec->size;
437 if (sec->flags & SEC_STRINGS)
438 memset (secinfo->contents + sec->size, 0, sec->entsize);
439 if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents,
440 0, sec->size))
441 goto error_return;
443 return TRUE;
445 error_return:
446 *psecinfo = NULL;
447 return FALSE;
450 /* Record one section into the hash table. */
451 static bfd_boolean
452 record_section (struct sec_merge_info *sinfo,
453 struct sec_merge_sec_info *secinfo)
455 asection *sec = secinfo->sec;
456 struct sec_merge_hash_entry *entry;
457 bfd_boolean nul;
458 unsigned char *p, *end;
459 bfd_vma mask, eltalign;
460 unsigned int align, i;
462 align = sec->alignment_power;
463 end = secinfo->contents + sec->size;
464 nul = FALSE;
465 mask = ((bfd_vma) 1 << align) - 1;
466 if (sec->flags & SEC_STRINGS)
468 for (p = secinfo->contents; p < end; )
470 eltalign = p - secinfo->contents;
471 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
472 if (!eltalign || eltalign > mask)
473 eltalign = mask + 1;
474 entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
475 secinfo);
476 if (! entry)
477 goto error_return;
478 p += entry->len;
479 if (sec->entsize == 1)
481 while (p < end && *p == 0)
483 if (!nul && !((p - secinfo->contents) & mask))
485 nul = TRUE;
486 entry = sec_merge_add (sinfo->htab, "",
487 (unsigned) mask + 1, secinfo);
488 if (! entry)
489 goto error_return;
491 p++;
494 else
496 while (p < end)
498 for (i = 0; i < sec->entsize; i++)
499 if (p[i] != '\0')
500 break;
501 if (i != sec->entsize)
502 break;
503 if (!nul && !((p - secinfo->contents) & mask))
505 nul = TRUE;
506 entry = sec_merge_add (sinfo->htab, (char *) p,
507 (unsigned) mask + 1, secinfo);
508 if (! entry)
509 goto error_return;
511 p += sec->entsize;
516 else
518 for (p = secinfo->contents; p < end; p += sec->entsize)
520 entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
521 if (! entry)
522 goto error_return;
526 return TRUE;
528 error_return:
529 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
530 *secinfo->psecinfo = NULL;
531 return FALSE;
534 static int
535 strrevcmp (const void *a, const void *b)
537 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
538 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
539 unsigned int lenA = A->len;
540 unsigned int lenB = B->len;
541 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
542 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
543 int l = lenA < lenB ? lenA : lenB;
545 while (l)
547 if (*s != *t)
548 return (int) *s - (int) *t;
549 s--;
550 t--;
551 l--;
553 return lenA - lenB;
556 /* Like strrevcmp, but for the case where all strings have the same
557 alignment > entsize. */
559 static int
560 strrevcmp_align (const void *a, const void *b)
562 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
563 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
564 unsigned int lenA = A->len;
565 unsigned int lenB = B->len;
566 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
567 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
568 int l = lenA < lenB ? lenA : lenB;
569 int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
571 if (tail_align != 0)
572 return tail_align;
574 while (l)
576 if (*s != *t)
577 return (int) *s - (int) *t;
578 s--;
579 t--;
580 l--;
582 return lenA - lenB;
585 static inline int
586 is_suffix (const struct sec_merge_hash_entry *A,
587 const struct sec_merge_hash_entry *B)
589 if (A->len <= B->len)
590 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
591 not to be equal by the hash table. */
592 return 0;
594 return memcmp (A->root.string + (A->len - B->len),
595 B->root.string, B->len) == 0;
598 /* This is a helper function for _bfd_merge_sections. It attempts to
599 merge strings matching suffixes of longer strings. */
600 static void
601 merge_strings (struct sec_merge_info *sinfo)
603 struct sec_merge_hash_entry **array, **a, *e;
604 struct sec_merge_sec_info *secinfo;
605 bfd_size_type size, amt;
606 unsigned int alignment = 0;
608 /* Now sort the strings */
609 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
610 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
611 if (array == NULL)
612 goto alloc_failure;
614 for (e = sinfo->htab->first, a = array; e; e = e->next)
615 if (e->alignment)
617 *a++ = e;
618 /* Adjust the length to not include the zero terminator. */
619 e->len -= sinfo->htab->entsize;
620 if (alignment != e->alignment)
622 if (alignment == 0)
623 alignment = e->alignment;
624 else
625 alignment = (unsigned) -1;
629 sinfo->htab->size = a - array;
630 if (sinfo->htab->size != 0)
632 qsort (array, (size_t) sinfo->htab->size,
633 sizeof (struct sec_merge_hash_entry *),
634 (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
635 ? strrevcmp_align : strrevcmp));
637 /* Loop over the sorted array and merge suffixes */
638 e = *--a;
639 e->len += sinfo->htab->entsize;
640 while (--a >= array)
642 struct sec_merge_hash_entry *cmp = *a;
644 cmp->len += sinfo->htab->entsize;
645 if (e->alignment >= cmp->alignment
646 && !((e->len - cmp->len) & (cmp->alignment - 1))
647 && is_suffix (e, cmp))
649 cmp->u.suffix = e;
650 cmp->alignment = 0;
652 else
653 e = cmp;
657 alloc_failure:
658 if (array)
659 free (array);
661 /* Now assign positions to the strings we want to keep. */
662 size = 0;
663 secinfo = sinfo->htab->first->secinfo;
664 for (e = sinfo->htab->first; e; e = e->next)
666 if (e->secinfo != secinfo)
668 secinfo->sec->size = size;
669 secinfo = e->secinfo;
671 if (e->alignment)
673 if (e->secinfo->first_str == NULL)
675 e->secinfo->first_str = e;
676 size = 0;
678 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
679 e->u.index = size;
680 size += e->len;
683 secinfo->sec->size = size;
684 if (secinfo->sec->alignment_power != 0)
686 bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
687 secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
690 /* And now adjust the rest, removing them from the chain (but not hashtable)
691 at the same time. */
692 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
693 if (e->alignment)
694 a = &e->next;
695 else
697 *a = e->next;
698 if (e->len)
700 e->secinfo = e->u.suffix->secinfo;
701 e->alignment = e->u.suffix->alignment;
702 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
707 /* This function is called once after all SEC_MERGE sections are registered
708 with _bfd_merge_section. */
710 bfd_boolean
711 _bfd_merge_sections (bfd *abfd,
712 struct bfd_link_info *info ATTRIBUTE_UNUSED,
713 void *xsinfo,
714 void (*remove_hook) (bfd *, asection *))
716 struct sec_merge_info *sinfo;
718 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
720 struct sec_merge_sec_info * secinfo;
722 if (! sinfo->chain)
723 continue;
725 /* Move sinfo->chain to head of the chain, terminate it. */
726 secinfo = sinfo->chain;
727 sinfo->chain = secinfo->next;
728 secinfo->next = NULL;
730 /* Record the sections into the hash table. */
731 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
732 if (secinfo->sec->flags & SEC_EXCLUDE)
734 *secinfo->psecinfo = NULL;
735 if (remove_hook)
736 (*remove_hook) (abfd, secinfo->sec);
738 else if (! record_section (sinfo, secinfo))
739 break;
741 if (secinfo)
742 continue;
744 if (sinfo->htab->first == NULL)
745 continue;
747 if (sinfo->htab->strings)
748 merge_strings (sinfo);
749 else
751 struct sec_merge_hash_entry *e;
752 bfd_size_type size = 0;
754 /* Things are much simpler for non-strings.
755 Just assign them slots in the section. */
756 secinfo = NULL;
757 for (e = sinfo->htab->first; e; e = e->next)
759 if (e->secinfo->first_str == NULL)
761 if (secinfo)
762 secinfo->sec->size = size;
763 e->secinfo->first_str = e;
764 size = 0;
766 size = (size + e->alignment - 1)
767 & ~((bfd_vma) e->alignment - 1);
768 e->u.index = size;
769 size += e->len;
770 secinfo = e->secinfo;
772 secinfo->sec->size = size;
775 /* Finally remove all input sections which have not made it into
776 the hash table at all. */
777 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
778 if (secinfo->first_str == NULL)
779 secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
782 return TRUE;
785 /* Write out the merged section. */
787 bfd_boolean
788 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
790 struct sec_merge_sec_info *secinfo;
791 file_ptr pos;
793 secinfo = (struct sec_merge_sec_info *) psecinfo;
795 if (!secinfo)
796 return FALSE;
798 if (secinfo->first_str == NULL)
799 return TRUE;
801 /* FIXME: octets_per_byte. */
802 pos = sec->output_section->filepos + sec->output_offset;
803 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
804 return FALSE;
806 if (! sec_merge_emit (output_bfd, secinfo->first_str))
807 return FALSE;
809 return TRUE;
812 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
813 *PSEC, this returns the new offset in the adjusted SEC_MERGE
814 section and writes the new section back into *PSEC. */
816 bfd_vma
817 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
818 void *psecinfo, bfd_vma offset)
820 struct sec_merge_sec_info *secinfo;
821 struct sec_merge_hash_entry *entry;
822 unsigned char *p;
823 asection *sec = *psec;
825 secinfo = (struct sec_merge_sec_info *) psecinfo;
827 if (!secinfo)
828 return offset;
830 if (offset >= sec->rawsize)
832 if (offset > sec->rawsize)
834 (*_bfd_error_handler)
835 (_("%s: access beyond end of merged section (%ld)"),
836 bfd_get_filename (sec->owner), (long) offset);
838 return secinfo->first_str ? sec->size : 0;
841 if (secinfo->htab->strings)
843 if (sec->entsize == 1)
845 p = secinfo->contents + offset - 1;
846 while (p >= secinfo->contents && *p)
847 --p;
848 ++p;
850 else
852 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
853 p -= sec->entsize;
854 while (p >= secinfo->contents)
856 unsigned int i;
858 for (i = 0; i < sec->entsize; ++i)
859 if (p[i] != '\0')
860 break;
861 if (i == sec->entsize)
862 break;
863 p -= sec->entsize;
865 p += sec->entsize;
868 else
870 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
872 entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
873 if (!entry)
875 if (! secinfo->htab->strings)
876 abort ();
877 /* This should only happen if somebody points into the padding
878 after a NUL character but before next entity. */
879 if (*p)
880 abort ();
881 if (! secinfo->htab->first)
882 abort ();
883 entry = secinfo->htab->first;
884 p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
885 - entry->len);
888 *psec = entry->secinfo->sec;
889 return entry->u.index + (secinfo->contents + offset - p);