1 /* SEC_MERGE support. 2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 3 Free Software Foundation, Inc. 4 Written by Jakub Jelinek <jakub@redhat.com>. 5 6 This file is part of BFD, the Binary File Descriptor library. 7 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 2 of the License, or 11 (at your option) any later version. 12 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. 17 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, MA 02110-1301, USA. */ 21 22 /* This file contains support for merging duplicate entities within sections, 23 as used in ELF SHF_MERGE. */ 24 25 #include "bfd.h" 26 #include "sysdep.h" 27 #include "libbfd.h" 28 #include "hashtab.h" 29 #include "libiberty.h" 30 31 struct sec_merge_sec_info; 32 33 /* An entry in the section merge hash table. */ 34 35 struct sec_merge_hash_entry 36 { 37 struct bfd_hash_entry root; 38 /* Length of this entry. This includes the zero terminator. */ 39 unsigned int len; 40 /* Start of this string needs to be aligned to 41 alignment octets (not 1 << align). */ 42 unsigned int alignment; 43 union 44 { 45 /* Index within the merged section. */ 46 bfd_size_type index; 47 /* Entry this is a suffix of (if alignment is 0). */ 48 struct sec_merge_hash_entry *suffix; 49 } u; 50 /* Which section is it in. */ 51 struct sec_merge_sec_info *secinfo; 52 /* Next entity in the hash table. */ 53 struct sec_merge_hash_entry *next; 54 }; 55 56 /* The section merge hash table. */ 57 58 struct sec_merge_hash 59 { 60 struct bfd_hash_table table; 61 /* Next available index. */ 62 bfd_size_type size; 63 /* First entity in the SEC_MERGE sections of this type. */ 64 struct sec_merge_hash_entry *first; 65 /* Last entity in the SEC_MERGE sections of this type. */ 66 struct sec_merge_hash_entry *last; 67 /* Entity size. */ 68 unsigned int entsize; 69 /* Are entries fixed size or zero terminated strings? */ 70 bfd_boolean strings; 71 }; 72 73 struct sec_merge_info 74 { 75 /* Chain of sec_merge_infos. */ 76 struct sec_merge_info *next; 77 /* Chain of sec_merge_sec_infos. */ 78 struct sec_merge_sec_info *chain; 79 /* A hash table used to hold section content. */ 80 struct sec_merge_hash *htab; 81 }; 82 83 struct sec_merge_sec_info 84 { 85 /* Chain of sec_merge_sec_infos. */ 86 struct sec_merge_sec_info *next; 87 /* The corresponding section. */ 88 asection *sec; 89 /* Pointer to merge_info pointing to us. */ 90 void **psecinfo; 91 /* A hash table used to hold section content. */ 92 struct sec_merge_hash *htab; 93 /* First string in this section. */ 94 struct sec_merge_hash_entry *first_str; 95 /* Original section content. */ 96 unsigned char contents[1]; 97 }; 98 99 100 /* Routine to create an entry in a section merge hashtab. */ 101 102 static struct bfd_hash_entry * 103 sec_merge_hash_newfunc (struct bfd_hash_entry *entry, 104 struct bfd_hash_table *table, const char *string) 105 { 106 /* Allocate the structure if it has not already been allocated by a 107 subclass. */ 108 if (entry == NULL) 109 entry = bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry)); 110 if (entry == NULL) 111 return NULL; 112 113 /* Call the allocation method of the superclass. */ 114 entry = bfd_hash_newfunc (entry, table, string); 115 116 if (entry != NULL) 117 { 118 /* Initialize the local fields. */ 119 struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry; 120 121 ret->u.suffix = NULL; 122 ret->alignment = 0; 123 ret->secinfo = NULL; 124 ret->next = NULL; 125 } 126 127 return entry; 128 } 129 130 /* Look up an entry in a section merge hash table. */ 131 132 static struct sec_merge_hash_entry * 133 sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string, 134 unsigned int alignment, bfd_boolean create) 135 { 136 register const unsigned char *s; 137 register unsigned long hash; 138 register unsigned int c; 139 struct sec_merge_hash_entry *hashp; 140 unsigned int len, i; 141 unsigned int index; 142 143 hash = 0; 144 len = 0; 145 s = (const unsigned char *) string; 146 if (table->strings) 147 { 148 if (table->entsize == 1) 149 { 150 while ((c = *s++) != '\0') 151 { 152 hash += c + (c << 17); 153 hash ^= hash >> 2; 154 ++len; 155 } 156 hash += len + (len << 17); 157 } 158 else 159 { 160 for (;;) 161 { 162 for (i = 0; i < table->entsize; ++i) 163 if (s[i] != '\0') 164 break; 165 if (i == table->entsize) 166 break; 167 for (i = 0; i < table->entsize; ++i) 168 { 169 c = *s++; 170 hash += c + (c << 17); 171 hash ^= hash >> 2; 172 } 173 ++len; 174 } 175 hash += len + (len << 17); 176 len *= table->entsize; 177 } 178 hash ^= hash >> 2; 179 len += table->entsize; 180 } 181 else 182 { 183 for (i = 0; i < table->entsize; ++i) 184 { 185 c = *s++; 186 hash += c + (c << 17); 187 hash ^= hash >> 2; 188 } 189 len = table->entsize; 190 } 191 192 index = hash % table->table.size; 193 for (hashp = (struct sec_merge_hash_entry *) table->table.table[index]; 194 hashp != NULL; 195 hashp = (struct sec_merge_hash_entry *) hashp->root.next) 196 { 197 if (hashp->root.hash == hash 198 && len == hashp->len 199 && memcmp (hashp->root.string, string, len) == 0) 200 { 201 /* If the string we found does not have at least the required 202 alignment, we need to insert another copy. */ 203 if (hashp->alignment < alignment) 204 { 205 if (create) 206 { 207 /* Mark the less aligned copy as deleted. */ 208 hashp->len = 0; 209 hashp->alignment = 0; 210 } 211 break; 212 } 213 return hashp; 214 } 215 } 216 217 if (! create) 218 return NULL; 219 220 hashp = ((struct sec_merge_hash_entry *) 221 sec_merge_hash_newfunc (NULL, &table->table, string)); 222 if (hashp == NULL) 223 return NULL; 224 hashp->root.string = string; 225 hashp->root.hash = hash; 226 hashp->len = len; 227 hashp->alignment = alignment; 228 hashp->root.next = table->table.table[index]; 229 table->table.table[index] = (struct bfd_hash_entry *) hashp; 230 231 return hashp; 232 } 233 234 /* Create a new hash table. */ 235 236 static struct sec_merge_hash * 237 sec_merge_init (unsigned int entsize, bfd_boolean strings) 238 { 239 struct sec_merge_hash *table; 240 241 table = bfd_malloc (sizeof (struct sec_merge_hash)); 242 if (table == NULL) 243 return NULL; 244 245 if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc, 246 sizeof (struct sec_merge_hash_entry), 16699)) 247 { 248 free (table); 249 return NULL; 250 } 251 252 table->size = 0; 253 table->first = NULL; 254 table->last = NULL; 255 table->entsize = entsize; 256 table->strings = strings; 257 258 return table; 259 } 260 261 /* Get the index of an entity in a hash table, adding it if it is not 262 already present. */ 263 264 static struct sec_merge_hash_entry * 265 sec_merge_add (struct sec_merge_hash *tab, const char *str, 266 unsigned int alignment, struct sec_merge_sec_info *secinfo) 267 { 268 register struct sec_merge_hash_entry *entry; 269 270 entry = sec_merge_hash_lookup (tab, str, alignment, TRUE); 271 if (entry == NULL) 272 return NULL; 273 274 if (entry->secinfo == NULL) 275 { 276 tab->size++; 277 entry->secinfo = secinfo; 278 if (tab->first == NULL) 279 tab->first = entry; 280 else 281 tab->last->next = entry; 282 tab->last = entry; 283 } 284 285 return entry; 286 } 287 288 static bfd_boolean 289 sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry) 290 { 291 struct sec_merge_sec_info *secinfo = entry->secinfo; 292 asection *sec = secinfo->sec; 293 char *pad = NULL; 294 bfd_size_type off = 0; 295 int alignment_power = sec->output_section->alignment_power; 296 297 if (alignment_power) 298 { 299 pad = bfd_zmalloc ((bfd_size_type) 1 << alignment_power); 300 if (pad == NULL) 301 return FALSE; 302 } 303 304 for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next) 305 { 306 const char *str; 307 bfd_size_type len; 308 309 len = -off & (entry->alignment - 1); 310 if (len != 0) 311 { 312 if (bfd_bwrite (pad, len, abfd) != len) 313 goto err; 314 off += len; 315 } 316 317 str = entry->root.string; 318 len = entry->len; 319 320 if (bfd_bwrite (str, len, abfd) != len) 321 goto err; 322 323 off += len; 324 } 325 326 /* Trailing alignment needed? */ 327 off = sec->size - off; 328 if (off != 0 329 && bfd_bwrite (pad, off, abfd) != off) 330 goto err; 331 332 if (pad != NULL) 333 free (pad); 334 return TRUE; 335 336 err: 337 if (pad != NULL) 338 free (pad); 339 return FALSE; 340 } 341 342 /* Register a SEC_MERGE section as a candidate for merging. 343 This function is called for all non-dynamic SEC_MERGE input sections. */ 344 345 bfd_boolean 346 _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec, 347 void **psecinfo) 348 { 349 struct sec_merge_info *sinfo; 350 struct sec_merge_sec_info *secinfo; 351 unsigned int align; 352 bfd_size_type amt; 353 354 if ((abfd->flags & DYNAMIC) != 0 355 || (sec->flags & SEC_MERGE) == 0) 356 abort (); 357 358 if (sec->size == 0 359 || (sec->flags & SEC_EXCLUDE) != 0 360 || sec->entsize == 0) 361 return TRUE; 362 363 if ((sec->flags & SEC_RELOC) != 0) 364 { 365 /* We aren't prepared to handle relocations in merged sections. */ 366 return TRUE; 367 } 368 369 align = sec->alignment_power; 370 if ((sec->entsize < (unsigned) 1 << align 371 && ((sec->entsize & (sec->entsize - 1)) 372 || !(sec->flags & SEC_STRINGS))) 373 || (sec->entsize > (unsigned) 1 << align 374 && (sec->entsize & (((unsigned) 1 << align) - 1)))) 375 { 376 /* Sanity check. If string character size is smaller than 377 alignment, then we require character size to be a power 378 of 2, otherwise character size must be integer multiple 379 of alignment. For non-string constants, alignment must 380 be smaller than or equal to entity size and entity size 381 must be integer multiple of alignment. */ 382 return TRUE; 383 } 384 385 for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next) 386 if ((secinfo = sinfo->chain) 387 && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS)) 388 && secinfo->sec->entsize == sec->entsize 389 && secinfo->sec->alignment_power == sec->alignment_power 390 && secinfo->sec->output_section == sec->output_section) 391 break; 392 393 if (sinfo == NULL) 394 { 395 /* Initialize the information we need to keep track of. */ 396 sinfo = bfd_alloc (abfd, sizeof (struct sec_merge_info)); 397 if (sinfo == NULL) 398 goto error_return; 399 sinfo->next = (struct sec_merge_info *) *psinfo; 400 sinfo->chain = NULL; 401 *psinfo = sinfo; 402 sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS)); 403 if (sinfo->htab == NULL) 404 goto error_return; 405 } 406 407 /* Read the section from abfd. */ 408 409 amt = sizeof (struct sec_merge_sec_info) + sec->size - 1; 410 *psecinfo = bfd_alloc (abfd, amt); 411 if (*psecinfo == NULL) 412 goto error_return; 413 414 secinfo = (struct sec_merge_sec_info *) *psecinfo; 415 if (sinfo->chain) 416 { 417 secinfo->next = sinfo->chain->next; 418 sinfo->chain->next = secinfo; 419 } 420 else 421 secinfo->next = secinfo; 422 sinfo->chain = secinfo; 423 secinfo->sec = sec; 424 secinfo->psecinfo = psecinfo; 425 secinfo->htab = sinfo->htab; 426 secinfo->first_str = NULL; 427 428 sec->rawsize = sec->size; 429 if (! bfd_get_section_contents (sec->owner, sec, secinfo->contents, 430 0, sec->size)) 431 goto error_return; 432 433 return TRUE; 434 435 error_return: 436 *psecinfo = NULL; 437 return FALSE; 438 } 439 440 /* Record one section into the hash table. */ 441 static bfd_boolean 442 record_section (struct sec_merge_info *sinfo, 443 struct sec_merge_sec_info *secinfo) 444 { 445 asection *sec = secinfo->sec; 446 struct sec_merge_hash_entry *entry; 447 bfd_boolean nul; 448 unsigned char *p, *end; 449 bfd_vma mask, eltalign; 450 unsigned int align, i; 451 452 align = sec->alignment_power; 453 end = secinfo->contents + sec->size; 454 nul = FALSE; 455 mask = ((bfd_vma) 1 << align) - 1; 456 if (sec->flags & SEC_STRINGS) 457 { 458 for (p = secinfo->contents; p < end; ) 459 { 460 eltalign = p - secinfo->contents; 461 eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1; 462 if (!eltalign || eltalign > mask) 463 eltalign = mask + 1; 464 entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign, 465 secinfo); 466 if (! entry) 467 goto error_return; 468 p += entry->len; 469 if (sec->entsize == 1) 470 { 471 while (p < end && *p == 0) 472 { 473 if (!nul && !((p - secinfo->contents) & mask)) 474 { 475 nul = TRUE; 476 entry = sec_merge_add (sinfo->htab, "", 477 (unsigned) mask + 1, secinfo); 478 if (! entry) 479 goto error_return; 480 } 481 p++; 482 } 483 } 484 else 485 { 486 while (p < end) 487 { 488 for (i = 0; i < sec->entsize; i++) 489 if (p[i] != '\0') 490 break; 491 if (i != sec->entsize) 492 break; 493 if (!nul && !((p - secinfo->contents) & mask)) 494 { 495 nul = TRUE; 496 entry = sec_merge_add (sinfo->htab, (char *) p, 497 (unsigned) mask + 1, secinfo); 498 if (! entry) 499 goto error_return; 500 } 501 p += sec->entsize; 502 } 503 } 504 } 505 } 506 else 507 { 508 for (p = secinfo->contents; p < end; p += sec->entsize) 509 { 510 entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo); 511 if (! entry) 512 goto error_return; 513 } 514 } 515 516 return TRUE; 517 518 error_return: 519 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next) 520 *secinfo->psecinfo = NULL; 521 return FALSE; 522 } 523 524 static int 525 strrevcmp (const void *a, const void *b) 526 { 527 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a; 528 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b; 529 unsigned int lenA = A->len; 530 unsigned int lenB = B->len; 531 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1; 532 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1; 533 int l = lenA < lenB ? lenA : lenB; 534 535 while (l) 536 { 537 if (*s != *t) 538 return (int) *s - (int) *t; 539 s--; 540 t--; 541 l--; 542 } 543 return lenA - lenB; 544 } 545 546 /* Like strrevcmp, but for the case where all strings have the same 547 alignment > entsize. */ 548 549 static int 550 strrevcmp_align (const void *a, const void *b) 551 { 552 struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a; 553 struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b; 554 unsigned int lenA = A->len; 555 unsigned int lenB = B->len; 556 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1; 557 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1; 558 int l = lenA < lenB ? lenA : lenB; 559 int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1)); 560 561 if (tail_align != 0) 562 return tail_align; 563 564 while (l) 565 { 566 if (*s != *t) 567 return (int) *s - (int) *t; 568 s--; 569 t--; 570 l--; 571 } 572 return lenA - lenB; 573 } 574 575 static inline int 576 is_suffix (const struct sec_merge_hash_entry *A, 577 const struct sec_merge_hash_entry *B) 578 { 579 if (A->len <= B->len) 580 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed 581 not to be equal by the hash table. */ 582 return 0; 583 584 return memcmp (A->root.string + (A->len - B->len), 585 B->root.string, B->len) == 0; 586 } 587 588 /* This is a helper function for _bfd_merge_sections. It attempts to 589 merge strings matching suffixes of longer strings. */ 590 static void 591 merge_strings (struct sec_merge_info *sinfo) 592 { 593 struct sec_merge_hash_entry **array, **a, *e; 594 struct sec_merge_sec_info *secinfo; 595 bfd_size_type size, amt; 596 unsigned int alignment = 0; 597 598 /* Now sort the strings */ 599 amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *); 600 array = bfd_malloc (amt); 601 if (array == NULL) 602 goto alloc_failure; 603 604 for (e = sinfo->htab->first, a = array; e; e = e->next) 605 if (e->alignment) 606 { 607 *a++ = e; 608 /* Adjust the length to not include the zero terminator. */ 609 e->len -= sinfo->htab->entsize; 610 if (alignment != e->alignment) 611 { 612 if (alignment == 0) 613 alignment = e->alignment; 614 else 615 alignment = (unsigned) -1; 616 } 617 } 618 619 sinfo->htab->size = a - array; 620 if (sinfo->htab->size != 0) 621 { 622 qsort (array, (size_t) sinfo->htab->size, 623 sizeof (struct sec_merge_hash_entry *), 624 (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize 625 ? strrevcmp_align : strrevcmp)); 626 627 /* Loop over the sorted array and merge suffixes */ 628 e = *--a; 629 e->len += sinfo->htab->entsize; 630 while (--a >= array) 631 { 632 struct sec_merge_hash_entry *cmp = *a; 633 634 cmp->len += sinfo->htab->entsize; 635 if (e->alignment >= cmp->alignment 636 && !((e->len - cmp->len) & (cmp->alignment - 1)) 637 && is_suffix (e, cmp)) 638 { 639 cmp->u.suffix = e; 640 cmp->alignment = 0; 641 } 642 else 643 e = cmp; 644 } 645 } 646 647 alloc_failure: 648 if (array) 649 free (array); 650 651 /* Now assign positions to the strings we want to keep. */ 652 size = 0; 653 secinfo = sinfo->htab->first->secinfo; 654 for (e = sinfo->htab->first; e; e = e->next) 655 { 656 if (e->secinfo != secinfo) 657 { 658 secinfo->sec->size = size; 659 secinfo = e->secinfo; 660 } 661 if (e->alignment) 662 { 663 if (e->secinfo->first_str == NULL) 664 { 665 e->secinfo->first_str = e; 666 size = 0; 667 } 668 size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1); 669 e->u.index = size; 670 size += e->len; 671 } 672 } 673 secinfo->sec->size = size; 674 if (secinfo->sec->alignment_power != 0) 675 { 676 bfd_size_type align = (bfd_size_type) 1 << secinfo->sec->alignment_power; 677 secinfo->sec->size = (secinfo->sec->size + align - 1) & -align; 678 } 679 680 /* And now adjust the rest, removing them from the chain (but not hashtable) 681 at the same time. */ 682 for (a = &sinfo->htab->first, e = *a; e; e = e->next) 683 if (e->alignment) 684 a = &e->next; 685 else 686 { 687 *a = e->next; 688 if (e->len) 689 { 690 e->secinfo = e->u.suffix->secinfo; 691 e->alignment = e->u.suffix->alignment; 692 e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len); 693 } 694 } 695 } 696 697 /* This function is called once after all SEC_MERGE sections are registered 698 with _bfd_merge_section. */ 699 700 bfd_boolean 701 _bfd_merge_sections (bfd *abfd ATTRIBUTE_UNUSED, 702 struct bfd_link_info *info ATTRIBUTE_UNUSED, 703 void *xsinfo, 704 void (*remove_hook) (bfd *, asection *)) 705 { 706 struct sec_merge_info *sinfo; 707 708 for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next) 709 { 710 struct sec_merge_sec_info * secinfo; 711 712 if (! sinfo->chain) 713 continue; 714 715 /* Move sinfo->chain to head of the chain, terminate it. */ 716 secinfo = sinfo->chain; 717 sinfo->chain = secinfo->next; 718 secinfo->next = NULL; 719 720 /* Record the sections into the hash table. */ 721 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next) 722 if (secinfo->sec->flags & SEC_EXCLUDE) 723 { 724 *secinfo->psecinfo = NULL; 725 if (remove_hook) 726 (*remove_hook) (abfd, secinfo->sec); 727 } 728 else if (! record_section (sinfo, secinfo)) 729 break; 730 731 if (secinfo) 732 continue; 733 734 if (sinfo->htab->first == NULL) 735 continue; 736 737 if (sinfo->htab->strings) 738 merge_strings (sinfo); 739 else 740 { 741 struct sec_merge_hash_entry *e; 742 bfd_size_type size = 0; 743 744 /* Things are much simpler for non-strings. 745 Just assign them slots in the section. */ 746 secinfo = NULL; 747 for (e = sinfo->htab->first; e; e = e->next) 748 { 749 if (e->secinfo->first_str == NULL) 750 { 751 if (secinfo) 752 secinfo->sec->size = size; 753 e->secinfo->first_str = e; 754 size = 0; 755 } 756 size = (size + e->alignment - 1) 757 & ~((bfd_vma) e->alignment - 1); 758 e->u.index = size; 759 size += e->len; 760 secinfo = e->secinfo; 761 } 762 secinfo->sec->size = size; 763 } 764 765 /* Finally remove all input sections which have not made it into 766 the hash table at all. */ 767 for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next) 768 if (secinfo->first_str == NULL) 769 secinfo->sec->flags |= SEC_EXCLUDE; 770 } 771 772 return TRUE; 773 } 774 775 /* Write out the merged section. */ 776 777 bfd_boolean 778 _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo) 779 { 780 struct sec_merge_sec_info *secinfo; 781 file_ptr pos; 782 783 secinfo = (struct sec_merge_sec_info *) psecinfo; 784 785 if (secinfo->first_str == NULL) 786 return TRUE; 787 788 pos = sec->output_section->filepos + sec->output_offset; 789 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0) 790 return FALSE; 791 792 if (! sec_merge_emit (output_bfd, secinfo->first_str)) 793 return FALSE; 794 795 return TRUE; 796 } 797 798 /* Adjust an address in the SEC_MERGE section. Given OFFSET within 799 *PSEC, this returns the new offset in the adjusted SEC_MERGE 800 section and writes the new section back into *PSEC. */ 801 802 bfd_vma 803 _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec, 804 void *psecinfo, bfd_vma offset) 805 { 806 struct sec_merge_sec_info *secinfo; 807 struct sec_merge_hash_entry *entry; 808 unsigned char *p; 809 asection *sec = *psec; 810 811 secinfo = (struct sec_merge_sec_info *) psecinfo; 812 813 if (offset >= sec->rawsize) 814 { 815 if (offset > sec->rawsize) 816 { 817 (*_bfd_error_handler) 818 (_("%s: access beyond end of merged section (%ld)"), 819 bfd_get_filename (sec->owner), (long) offset); 820 } 821 return secinfo->first_str ? sec->size : 0; 822 } 823 824 if (secinfo->htab->strings) 825 { 826 if (sec->entsize == 1) 827 { 828 p = secinfo->contents + offset - 1; 829 while (p >= secinfo->contents && *p) 830 --p; 831 ++p; 832 } 833 else 834 { 835 p = secinfo->contents + (offset / sec->entsize) * sec->entsize; 836 p -= sec->entsize; 837 while (p >= secinfo->contents) 838 { 839 unsigned int i; 840 841 for (i = 0; i < sec->entsize; ++i) 842 if (p[i] != '\0') 843 break; 844 if (i == sec->entsize) 845 break; 846 p -= sec->entsize; 847 } 848 p += sec->entsize; 849 } 850 } 851 else 852 { 853 p = secinfo->contents + (offset / sec->entsize) * sec->entsize; 854 } 855 entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE); 856 if (!entry) 857 { 858 if (! secinfo->htab->strings) 859 abort (); 860 /* This should only happen if somebody points into the padding 861 after a NUL character but before next entity. */ 862 if (*p) 863 abort (); 864 if (! secinfo->htab->first) 865 abort (); 866 entry = secinfo->htab->first; 867 p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize 868 - entry->len); 869 } 870 871 *psec = entry->secinfo->sec; 872 return entry->u.index + (secinfo->contents + offset - p); 873 } 874