PR23804, buffer overflow in sec_merge_hash_lookup
[deliverable/binutils-gdb.git] / bfd / merge.c
1 /* SEC_MERGE support.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22
23 /* This file contains support for merging duplicate entities within sections,
24 as used in ELF SHF_MERGE. */
25
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"
32
33 struct sec_merge_sec_info;
34
35 /* An entry in the section merge hash table. */
36
37 struct sec_merge_hash_entry
38 {
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
46 {
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;
56 };
57
58 /* The section merge hash table. */
59
60 struct sec_merge_hash
61 {
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;
73 };
74
75 struct sec_merge_info
76 {
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;
83 };
84
85 struct sec_merge_sec_info
86 {
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];
99 };
100
101
102 /* Routine to create an entry in a section merge hashtab. */
103
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)
107 {
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;
115
116 /* Call the allocation method of the superclass. */
117 entry = bfd_hash_newfunc (entry, table, string);
118
119 if (entry != NULL)
120 {
121 /* Initialize the local fields. */
122 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
123
124 ret->u.suffix = NULL;
125 ret->alignment = 0;
126 ret->secinfo = NULL;
127 ret->next = NULL;
128 }
129
130 return entry;
131 }
132
133 /* Look up an entry in a section merge hash table. */
134
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)
138 {
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;
145
146 hash = 0;
147 len = 0;
148 s = (const unsigned char *) string;
149 if (table->strings)
150 {
151 if (table->entsize == 1)
152 {
153 while ((c = *s++) != '\0')
154 {
155 hash += c + (c << 17);
156 hash ^= hash >> 2;
157 ++len;
158 }
159 hash += len + (len << 17);
160 }
161 else
162 {
163 for (;;)
164 {
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)
171 {
172 c = *s++;
173 hash += c + (c << 17);
174 hash ^= hash >> 2;
175 }
176 ++len;
177 }
178 hash += len + (len << 17);
179 len *= table->entsize;
180 }
181 hash ^= hash >> 2;
182 len += table->entsize;
183 }
184 else
185 {
186 for (i = 0; i < table->entsize; ++i)
187 {
188 c = *s++;
189 hash += c + (c << 17);
190 hash ^= hash >> 2;
191 }
192 len = table->entsize;
193 }
194
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)
199 {
200 if (hashp->root.hash == hash
201 && len == hashp->len
202 && memcmp (hashp->root.string, string, len) == 0)
203 {
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)
207 {
208 if (create)
209 {
210 /* Mark the less aligned copy as deleted. */
211 hashp->len = 0;
212 hashp->alignment = 0;
213 }
214 break;
215 }
216 return hashp;
217 }
218 }
219
220 if (! create)
221 return NULL;
222
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;
230 }
231
232 /* Create a new hash table. */
233
234 static struct sec_merge_hash *
235 sec_merge_init (unsigned int entsize, bfd_boolean strings)
236 {
237 struct sec_merge_hash *table;
238
239 table = (struct sec_merge_hash *) bfd_malloc (sizeof (struct sec_merge_hash));
240 if (table == NULL)
241 return NULL;
242
243 if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc,
244 sizeof (struct sec_merge_hash_entry), 16699))
245 {
246 free (table);
247 return NULL;
248 }
249
250 table->size = 0;
251 table->first = NULL;
252 table->last = NULL;
253 table->entsize = entsize;
254 table->strings = strings;
255
256 return table;
257 }
258
259 /* Get the index of an entity in a hash table, adding it if it is not
260 already present. */
261
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)
265 {
266 struct sec_merge_hash_entry *entry;
267
268 entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
269 if (entry == NULL)
270 return NULL;
271
272 if (entry->secinfo == NULL)
273 {
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;
281 }
282
283 return entry;
284 }
285
286 static bfd_boolean
287 sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry,
288 unsigned char *contents, file_ptr offset)
289 {
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;
295 bfd_size_type pad_len;
296
297 /* FIXME: If alignment_power is 0 then really we should scan the
298 entry list for the largest required alignment and use that. */
299 pad_len = alignment_power ? ((bfd_size_type) 1 << alignment_power) : 16;
300
301 pad = (char *) bfd_zmalloc (pad_len);
302 if (pad == NULL)
303 return FALSE;
304
305 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
306 {
307 const char *str;
308 bfd_size_type len;
309
310 len = -off & (entry->alignment - 1);
311 if (len != 0)
312 {
313 BFD_ASSERT (len <= pad_len);
314 if (contents)
315 {
316 memcpy (contents + offset, pad, len);
317 offset += len;
318 }
319 else if (bfd_bwrite (pad, len, abfd) != len)
320 goto err;
321 off += len;
322 }
323
324 str = entry->root.string;
325 len = entry->len;
326
327 if (contents)
328 {
329 memcpy (contents + offset, str, len);
330 offset += len;
331 }
332 else if (bfd_bwrite (str, len, abfd) != len)
333 goto err;
334
335 off += len;
336 }
337
338 /* Trailing alignment needed? */
339 off = sec->size - off;
340 if (off != 0)
341 {
342 BFD_ASSERT (off <= pad_len);
343 if (contents)
344 memcpy (contents + offset, pad, off);
345 else if (bfd_bwrite (pad, off, abfd) != off)
346 goto err;
347 }
348
349 free (pad);
350 return TRUE;
351
352 err:
353 free (pad);
354 return FALSE;
355 }
356
357 /* Register a SEC_MERGE section as a candidate for merging.
358 This function is called for all non-dynamic SEC_MERGE input sections. */
359
360 bfd_boolean
361 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
362 void **psecinfo)
363 {
364 struct sec_merge_info *sinfo;
365 struct sec_merge_sec_info *secinfo;
366 unsigned int align;
367 bfd_size_type amt;
368 bfd_byte *contents;
369
370 if ((abfd->flags & DYNAMIC) != 0
371 || (sec->flags & SEC_MERGE) == 0)
372 abort ();
373
374 if (sec->size == 0
375 || (sec->flags & SEC_EXCLUDE) != 0
376 || sec->entsize == 0)
377 return TRUE;
378
379 if (sec->size % sec->entsize != 0)
380 return TRUE;
381
382 if ((sec->flags & SEC_RELOC) != 0)
383 {
384 /* We aren't prepared to handle relocations in merged sections. */
385 return TRUE;
386 }
387
388 align = sec->alignment_power;
389 if ((sec->entsize < (unsigned) 1 << align
390 && ((sec->entsize & (sec->entsize - 1))
391 || !(sec->flags & SEC_STRINGS)))
392 || (sec->entsize > (unsigned) 1 << align
393 && (sec->entsize & (((unsigned) 1 << align) - 1))))
394 {
395 /* Sanity check. If string character size is smaller than
396 alignment, then we require character size to be a power
397 of 2, otherwise character size must be integer multiple
398 of alignment. For non-string constants, alignment must
399 be smaller than or equal to entity size and entity size
400 must be integer multiple of alignment. */
401 return TRUE;
402 }
403
404 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
405 if ((secinfo = sinfo->chain)
406 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
407 && secinfo->sec->entsize == sec->entsize
408 && secinfo->sec->alignment_power == sec->alignment_power
409 && secinfo->sec->output_section == sec->output_section)
410 break;
411
412 if (sinfo == NULL)
413 {
414 /* Initialize the information we need to keep track of. */
415 sinfo = (struct sec_merge_info *)
416 bfd_alloc (abfd, sizeof (struct sec_merge_info));
417 if (sinfo == NULL)
418 goto error_return;
419 sinfo->next = (struct sec_merge_info *) *psinfo;
420 sinfo->chain = NULL;
421 *psinfo = sinfo;
422 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
423 if (sinfo->htab == NULL)
424 goto error_return;
425 }
426
427 /* Read the section from abfd. */
428
429 amt = sizeof (struct sec_merge_sec_info) - 1 + sec->size;
430 if (sec->flags & SEC_STRINGS)
431 /* Some versions of gcc may emit a string without a zero terminator.
432 See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
433 Allocate space for an extra zero. */
434 amt += sec->entsize;
435 *psecinfo = bfd_alloc (abfd, amt);
436 if (*psecinfo == NULL)
437 goto error_return;
438
439 secinfo = (struct sec_merge_sec_info *) *psecinfo;
440 if (sinfo->chain)
441 {
442 secinfo->next = sinfo->chain->next;
443 sinfo->chain->next = secinfo;
444 }
445 else
446 secinfo->next = secinfo;
447 sinfo->chain = secinfo;
448 secinfo->sec = sec;
449 secinfo->psecinfo = psecinfo;
450 secinfo->htab = sinfo->htab;
451 secinfo->first_str = NULL;
452
453 sec->rawsize = sec->size;
454 if (sec->flags & SEC_STRINGS)
455 memset (secinfo->contents + sec->size, 0, sec->entsize);
456 contents = secinfo->contents;
457 if (! bfd_get_full_section_contents (sec->owner, sec, &contents))
458 goto error_return;
459
460 return TRUE;
461
462 error_return:
463 *psecinfo = NULL;
464 return FALSE;
465 }
466
467 /* Record one section into the hash table. */
468 static bfd_boolean
469 record_section (struct sec_merge_info *sinfo,
470 struct sec_merge_sec_info *secinfo)
471 {
472 asection *sec = secinfo->sec;
473 struct sec_merge_hash_entry *entry;
474 bfd_boolean nul;
475 unsigned char *p, *end;
476 bfd_vma mask, eltalign;
477 unsigned int align, i;
478
479 align = sec->alignment_power;
480 end = secinfo->contents + sec->size;
481 nul = FALSE;
482 mask = ((bfd_vma) 1 << align) - 1;
483 if (sec->flags & SEC_STRINGS)
484 {
485 for (p = secinfo->contents; p < end; )
486 {
487 eltalign = p - secinfo->contents;
488 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
489 if (!eltalign || eltalign > mask)
490 eltalign = mask + 1;
491 entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
492 secinfo);
493 if (! entry)
494 goto error_return;
495 p += entry->len;
496 if (sec->entsize == 1)
497 {
498 while (p < end && *p == 0)
499 {
500 if (!nul && !((p - secinfo->contents) & mask))
501 {
502 nul = TRUE;
503 entry = sec_merge_add (sinfo->htab, "",
504 (unsigned) mask + 1, secinfo);
505 if (! entry)
506 goto error_return;
507 }
508 p++;
509 }
510 }
511 else
512 {
513 while (p < end)
514 {
515 for (i = 0; i < sec->entsize; i++)
516 if (p[i] != '\0')
517 break;
518 if (i != sec->entsize)
519 break;
520 if (!nul && !((p - secinfo->contents) & mask))
521 {
522 nul = TRUE;
523 entry = sec_merge_add (sinfo->htab, (char *) p,
524 (unsigned) mask + 1, secinfo);
525 if (! entry)
526 goto error_return;
527 }
528 p += sec->entsize;
529 }
530 }
531 }
532 }
533 else
534 {
535 for (p = secinfo->contents; p < end; p += sec->entsize)
536 {
537 entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
538 if (! entry)
539 goto error_return;
540 }
541 }
542
543 return TRUE;
544
545 error_return:
546 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
547 *secinfo->psecinfo = NULL;
548 return FALSE;
549 }
550
551 static int
552 strrevcmp (const void *a, const void *b)
553 {
554 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
555 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
556 unsigned int lenA = A->len;
557 unsigned int lenB = B->len;
558 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
559 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
560 int l = lenA < lenB ? lenA : lenB;
561
562 while (l)
563 {
564 if (*s != *t)
565 return (int) *s - (int) *t;
566 s--;
567 t--;
568 l--;
569 }
570 return lenA - lenB;
571 }
572
573 /* Like strrevcmp, but for the case where all strings have the same
574 alignment > entsize. */
575
576 static int
577 strrevcmp_align (const void *a, const void *b)
578 {
579 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
580 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
581 unsigned int lenA = A->len;
582 unsigned int lenB = B->len;
583 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
584 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
585 int l = lenA < lenB ? lenA : lenB;
586 int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
587
588 if (tail_align != 0)
589 return tail_align;
590
591 while (l)
592 {
593 if (*s != *t)
594 return (int) *s - (int) *t;
595 s--;
596 t--;
597 l--;
598 }
599 return lenA - lenB;
600 }
601
602 static inline int
603 is_suffix (const struct sec_merge_hash_entry *A,
604 const struct sec_merge_hash_entry *B)
605 {
606 if (A->len <= B->len)
607 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
608 not to be equal by the hash table. */
609 return 0;
610
611 return memcmp (A->root.string + (A->len - B->len),
612 B->root.string, B->len) == 0;
613 }
614
615 /* This is a helper function for _bfd_merge_sections. It attempts to
616 merge strings matching suffixes of longer strings. */
617 static bfd_boolean
618 merge_strings (struct sec_merge_info *sinfo)
619 {
620 struct sec_merge_hash_entry **array, **a, *e;
621 struct sec_merge_sec_info *secinfo;
622 bfd_size_type size, amt;
623 unsigned int alignment = 0;
624
625 /* Now sort the strings */
626 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
627 array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
628 if (array == NULL)
629 return FALSE;
630
631 for (e = sinfo->htab->first, a = array; e; e = e->next)
632 if (e->alignment)
633 {
634 *a++ = e;
635 /* Adjust the length to not include the zero terminator. */
636 e->len -= sinfo->htab->entsize;
637 if (alignment != e->alignment)
638 {
639 if (alignment == 0)
640 alignment = e->alignment;
641 else
642 alignment = (unsigned) -1;
643 }
644 }
645
646 sinfo->htab->size = a - array;
647 if (sinfo->htab->size != 0)
648 {
649 qsort (array, (size_t) sinfo->htab->size,
650 sizeof (struct sec_merge_hash_entry *),
651 (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
652 ? strrevcmp_align : strrevcmp));
653
654 /* Loop over the sorted array and merge suffixes */
655 e = *--a;
656 e->len += sinfo->htab->entsize;
657 while (--a >= array)
658 {
659 struct sec_merge_hash_entry *cmp = *a;
660
661 cmp->len += sinfo->htab->entsize;
662 if (e->alignment >= cmp->alignment
663 && !((e->len - cmp->len) & (cmp->alignment - 1))
664 && is_suffix (e, cmp))
665 {
666 cmp->u.suffix = e;
667 cmp->alignment = 0;
668 }
669 else
670 e = cmp;
671 }
672 }
673
674 free (array);
675
676 /* Now assign positions to the strings we want to keep. */
677 size = 0;
678 secinfo = sinfo->htab->first->secinfo;
679 for (e = sinfo->htab->first; e; e = e->next)
680 {
681 if (e->secinfo != secinfo)
682 {
683 secinfo->sec->size = size;
684 secinfo = e->secinfo;
685 }
686 if (e->alignment)
687 {
688 if (e->secinfo->first_str == NULL)
689 {
690 e->secinfo->first_str = e;
691 size = 0;
692 }
693 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
694 e->u.index = size;
695 size += e->len;
696 }
697 }
698 secinfo->sec->size = size;
699 if (secinfo->sec->alignment_power != 0)
700 {
701 bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
702 secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
703 }
704
705 /* And now adjust the rest, removing them from the chain (but not hashtable)
706 at the same time. */
707 for (a = &sinfo->htab->first, e = *a; e; e = e->next)
708 if (e->alignment)
709 a = &e->next;
710 else
711 {
712 *a = e->next;
713 if (e->len)
714 {
715 e->secinfo = e->u.suffix->secinfo;
716 e->alignment = e->u.suffix->alignment;
717 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
718 }
719 }
720 return TRUE;
721 }
722
723 /* This function is called once after all SEC_MERGE sections are registered
724 with _bfd_merge_section. */
725
726 bfd_boolean
727 _bfd_merge_sections (bfd *abfd,
728 struct bfd_link_info *info ATTRIBUTE_UNUSED,
729 void *xsinfo,
730 void (*remove_hook) (bfd *, asection *))
731 {
732 struct sec_merge_info *sinfo;
733
734 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
735 {
736 struct sec_merge_sec_info * secinfo;
737
738 if (! sinfo->chain)
739 continue;
740
741 /* Move sinfo->chain to head of the chain, terminate it. */
742 secinfo = sinfo->chain;
743 sinfo->chain = secinfo->next;
744 secinfo->next = NULL;
745
746 /* Record the sections into the hash table. */
747 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
748 if (secinfo->sec->flags & SEC_EXCLUDE)
749 {
750 *secinfo->psecinfo = NULL;
751 if (remove_hook)
752 (*remove_hook) (abfd, secinfo->sec);
753 }
754 else if (! record_section (sinfo, secinfo))
755 return FALSE;
756
757 if (secinfo)
758 continue;
759
760 if (sinfo->htab->first == NULL)
761 continue;
762
763 if (sinfo->htab->strings)
764 {
765 if (!merge_strings (sinfo))
766 return FALSE;
767 }
768 else
769 {
770 struct sec_merge_hash_entry *e;
771 bfd_size_type size = 0;
772
773 /* Things are much simpler for non-strings.
774 Just assign them slots in the section. */
775 secinfo = NULL;
776 for (e = sinfo->htab->first; e; e = e->next)
777 {
778 if (e->secinfo->first_str == NULL)
779 {
780 if (secinfo)
781 secinfo->sec->size = size;
782 e->secinfo->first_str = e;
783 size = 0;
784 }
785 size = (size + e->alignment - 1)
786 & ~((bfd_vma) e->alignment - 1);
787 e->u.index = size;
788 size += e->len;
789 secinfo = e->secinfo;
790 }
791 secinfo->sec->size = size;
792 }
793
794 /* Finally remove all input sections which have not made it into
795 the hash table at all. */
796 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
797 if (secinfo->first_str == NULL)
798 secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
799 }
800
801 return TRUE;
802 }
803
804 /* Write out the merged section. */
805
806 bfd_boolean
807 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
808 {
809 struct sec_merge_sec_info *secinfo;
810 file_ptr pos;
811 unsigned char *contents;
812 Elf_Internal_Shdr *hdr;
813
814 secinfo = (struct sec_merge_sec_info *) psecinfo;
815
816 if (!secinfo)
817 return FALSE;
818
819 if (secinfo->first_str == NULL)
820 return TRUE;
821
822 /* FIXME: octets_per_byte. */
823 hdr = &elf_section_data (sec->output_section)->this_hdr;
824 if (hdr->sh_offset == (file_ptr) -1)
825 {
826 /* We must compress this section. Write output to the
827 buffer. */
828 contents = hdr->contents;
829 if ((sec->output_section->flags & SEC_ELF_COMPRESS) == 0
830 || contents == NULL)
831 abort ();
832 }
833 else
834 {
835 contents = NULL;
836 pos = sec->output_section->filepos + sec->output_offset;
837 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
838 return FALSE;
839 }
840
841 if (! sec_merge_emit (output_bfd, secinfo->first_str, contents,
842 sec->output_offset))
843 return FALSE;
844
845 return TRUE;
846 }
847
848 /* Adjust an address in the SEC_MERGE section. Given OFFSET within
849 *PSEC, this returns the new offset in the adjusted SEC_MERGE
850 section and writes the new section back into *PSEC. */
851
852 bfd_vma
853 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
854 void *psecinfo, bfd_vma offset)
855 {
856 struct sec_merge_sec_info *secinfo;
857 struct sec_merge_hash_entry *entry;
858 unsigned char *p;
859 asection *sec = *psec;
860
861 secinfo = (struct sec_merge_sec_info *) psecinfo;
862
863 if (!secinfo)
864 return offset;
865
866 if (offset >= sec->rawsize)
867 {
868 if (offset > sec->rawsize)
869 _bfd_error_handler
870 /* xgettext:c-format */
871 (_("%pB: access beyond end of merged section (%" PRId64 ")"),
872 sec->owner, (int64_t) offset);
873 return secinfo->first_str ? sec->size : 0;
874 }
875
876 if (secinfo->htab->strings)
877 {
878 if (sec->entsize == 1)
879 {
880 p = secinfo->contents + offset - 1;
881 while (p >= secinfo->contents && *p)
882 --p;
883 ++p;
884 }
885 else
886 {
887 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
888 p -= sec->entsize;
889 while (p >= secinfo->contents)
890 {
891 unsigned int i;
892
893 for (i = 0; i < sec->entsize; ++i)
894 if (p[i] != '\0')
895 break;
896 if (i == sec->entsize)
897 break;
898 p -= sec->entsize;
899 }
900 p += sec->entsize;
901 }
902 }
903 else
904 {
905 p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
906 }
907 entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
908 if (!entry)
909 {
910 if (! secinfo->htab->strings)
911 abort ();
912 /* This should only happen if somebody points into the padding
913 after a NUL character but before next entity. */
914 if (*p)
915 abort ();
916 if (! secinfo->htab->first)
917 abort ();
918 entry = secinfo->htab->first;
919 p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
920 - entry->len);
921 }
922
923 *psec = entry->secinfo->sec;
924 return entry->u.index + (secinfo->contents + offset - p);
925 }
926
927 /* Tidy up when done. */
928
929 void
930 _bfd_merge_sections_free (void *xsinfo)
931 {
932 struct sec_merge_info *sinfo;
933
934 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
935 {
936 bfd_hash_table_free (&sinfo->htab->table);
937 free (sinfo->htab);
938 }
939 }
This page took 0.04881 seconds and 5 git commands to generate.