33 kx /* SEC_MERGE support.
33 kx Copyright (C) 2001-2023 Free Software Foundation, Inc.
33 kx Written by Jakub Jelinek <jakub@redhat.com>.
33 kx
33 kx This file is part of BFD, the Binary File Descriptor library.
33 kx
33 kx This program is free software; you can redistribute it and/or modify
33 kx it under the terms of the GNU General Public License as published by
33 kx the Free Software Foundation; either version 3 of the License, or
33 kx (at your option) any later version.
33 kx
33 kx This program is distributed in the hope that it will be useful,
33 kx but WITHOUT ANY WARRANTY; without even the implied warranty of
33 kx MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 kx GNU General Public License for more details.
33 kx
33 kx You should have received a copy of the GNU General Public License
33 kx along with this program; if not, write to the Free Software
33 kx Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
33 kx MA 02110-1301, USA. */
33 kx
33 kx
33 kx /* This file contains support for merging duplicate entities within sections,
33 kx as used in ELF SHF_MERGE. */
33 kx
33 kx #include "sysdep.h"
33 kx #include <limits.h>
33 kx #include "bfd.h"
33 kx #include "elf-bfd.h"
33 kx #include "libbfd.h"
33 kx #include "hashtab.h"
33 kx #include "libiberty.h"
33 kx
33 kx struct sec_merge_sec_info;
33 kx
33 kx /* An entry in the section merge hash table. */
33 kx
33 kx struct sec_merge_hash_entry
33 kx {
33 kx struct bfd_hash_entry root;
33 kx /* Length of this entry. This includes the zero terminator. */
33 kx unsigned int len;
33 kx /* Start of this string needs to be aligned to
33 kx alignment octets (not 1 << align). */
33 kx unsigned int alignment;
33 kx union
33 kx {
33 kx /* Index within the merged section. */
33 kx bfd_size_type index;
33 kx /* Entry this is a suffix of (if alignment is 0). */
33 kx struct sec_merge_hash_entry *suffix;
33 kx } u;
33 kx /* Which section is it in. */
33 kx struct sec_merge_sec_info *secinfo;
33 kx /* Next entity in the hash table. */
33 kx struct sec_merge_hash_entry *next;
33 kx };
33 kx
33 kx /* The section merge hash table. */
33 kx
33 kx struct sec_merge_hash
33 kx {
33 kx struct bfd_hash_table table;
33 kx /* Next available index. */
33 kx bfd_size_type size;
33 kx /* First entity in the SEC_MERGE sections of this type. */
33 kx struct sec_merge_hash_entry *first;
33 kx /* Last entity in the SEC_MERGE sections of this type. */
33 kx struct sec_merge_hash_entry *last;
33 kx /* Entity size. */
33 kx unsigned int entsize;
33 kx /* Are entries fixed size or zero terminated strings? */
33 kx bool strings;
33 kx };
33 kx
33 kx struct sec_merge_info
33 kx {
33 kx /* Chain of sec_merge_infos. */
33 kx struct sec_merge_info *next;
33 kx /* Chain of sec_merge_sec_infos. */
33 kx struct sec_merge_sec_info *chain;
33 kx /* A hash table used to hold section content. */
33 kx struct sec_merge_hash *htab;
33 kx };
33 kx
33 kx struct sec_merge_sec_info
33 kx {
33 kx /* Chain of sec_merge_sec_infos. */
33 kx struct sec_merge_sec_info *next;
33 kx /* The corresponding section. */
33 kx asection *sec;
33 kx /* Pointer to merge_info pointing to us. */
33 kx void **psecinfo;
33 kx /* A hash table used to hold section content. */
33 kx struct sec_merge_hash *htab;
33 kx /* First string in this section. */
33 kx struct sec_merge_hash_entry *first_str;
33 kx /* Original section content. */
33 kx unsigned char contents[1];
33 kx };
33 kx
33 kx
33 kx /* Routine to create an entry in a section merge hashtab. */
33 kx
33 kx static struct bfd_hash_entry *
33 kx sec_merge_hash_newfunc (struct bfd_hash_entry *entry,
33 kx struct bfd_hash_table *table, const char *string)
33 kx {
33 kx /* Allocate the structure if it has not already been allocated by a
33 kx subclass. */
33 kx if (entry == NULL)
33 kx entry = (struct bfd_hash_entry *)
33 kx bfd_hash_allocate (table, sizeof (struct sec_merge_hash_entry));
33 kx if (entry == NULL)
33 kx return NULL;
33 kx
33 kx /* Call the allocation method of the superclass. */
33 kx entry = bfd_hash_newfunc (entry, table, string);
33 kx
33 kx if (entry != NULL)
33 kx {
33 kx /* Initialize the local fields. */
33 kx struct sec_merge_hash_entry *ret = (struct sec_merge_hash_entry *) entry;
33 kx
33 kx ret->u.suffix = NULL;
33 kx ret->alignment = 0;
33 kx ret->secinfo = NULL;
33 kx ret->next = NULL;
33 kx }
33 kx
33 kx return entry;
33 kx }
33 kx
33 kx /* Look up an entry in a section merge hash table. */
33 kx
33 kx static struct sec_merge_hash_entry *
33 kx sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
33 kx unsigned int alignment, bool create)
33 kx {
33 kx const unsigned char *s;
33 kx unsigned long hash;
33 kx unsigned int c;
33 kx struct sec_merge_hash_entry *hashp;
33 kx unsigned int len, i;
33 kx unsigned int _index;
33 kx
33 kx hash = 0;
33 kx len = 0;
33 kx s = (const unsigned char *) string;
33 kx if (table->strings)
33 kx {
33 kx if (table->entsize == 1)
33 kx {
33 kx while ((c = *s++) != '\0')
33 kx {
33 kx hash += c + (c << 17);
33 kx hash ^= hash >> 2;
33 kx ++len;
33 kx }
33 kx hash += len + (len << 17);
33 kx }
33 kx else
33 kx {
33 kx for (;;)
33 kx {
33 kx for (i = 0; i < table->entsize; ++i)
33 kx if (s[i] != '\0')
33 kx break;
33 kx if (i == table->entsize)
33 kx break;
33 kx for (i = 0; i < table->entsize; ++i)
33 kx {
33 kx c = *s++;
33 kx hash += c + (c << 17);
33 kx hash ^= hash >> 2;
33 kx }
33 kx ++len;
33 kx }
33 kx hash += len + (len << 17);
33 kx len *= table->entsize;
33 kx }
33 kx hash ^= hash >> 2;
33 kx len += table->entsize;
33 kx }
33 kx else
33 kx {
33 kx for (i = 0; i < table->entsize; ++i)
33 kx {
33 kx c = *s++;
33 kx hash += c + (c << 17);
33 kx hash ^= hash >> 2;
33 kx }
33 kx len = table->entsize;
33 kx }
33 kx
33 kx _index = hash % table->table.size;
33 kx for (hashp = (struct sec_merge_hash_entry *) table->table.table[_index];
33 kx hashp != NULL;
33 kx hashp = (struct sec_merge_hash_entry *) hashp->root.next)
33 kx {
33 kx if (hashp->root.hash == hash
33 kx && len == hashp->len
33 kx && memcmp (hashp->root.string, string, len) == 0)
33 kx {
33 kx /* If the string we found does not have at least the required
33 kx alignment, we need to insert another copy. */
33 kx if (hashp->alignment < alignment)
33 kx {
33 kx if (create)
33 kx {
33 kx /* Mark the less aligned copy as deleted. */
33 kx hashp->len = 0;
33 kx hashp->alignment = 0;
33 kx }
33 kx break;
33 kx }
33 kx return hashp;
33 kx }
33 kx }
33 kx
33 kx if (! create)
33 kx return NULL;
33 kx
33 kx hashp = ((struct sec_merge_hash_entry *)
33 kx bfd_hash_insert (&table->table, string, hash));
33 kx if (hashp == NULL)
33 kx return NULL;
33 kx hashp->len = len;
33 kx hashp->alignment = alignment;
33 kx return hashp;
33 kx }
33 kx
33 kx /* Create a new hash table. */
33 kx
33 kx static struct sec_merge_hash *
33 kx sec_merge_init (unsigned int entsize, bool strings)
33 kx {
33 kx struct sec_merge_hash *table;
33 kx
33 kx table = (struct sec_merge_hash *) bfd_malloc (sizeof (struct sec_merge_hash));
33 kx if (table == NULL)
33 kx return NULL;
33 kx
33 kx if (! bfd_hash_table_init_n (&table->table, sec_merge_hash_newfunc,
33 kx sizeof (struct sec_merge_hash_entry), 16699))
33 kx {
33 kx free (table);
33 kx return NULL;
33 kx }
33 kx
33 kx table->size = 0;
33 kx table->first = NULL;
33 kx table->last = NULL;
33 kx table->entsize = entsize;
33 kx table->strings = strings;
33 kx
33 kx return table;
33 kx }
33 kx
33 kx /* Get the index of an entity in a hash table, adding it if it is not
33 kx already present. */
33 kx
33 kx static struct sec_merge_hash_entry *
33 kx sec_merge_add (struct sec_merge_hash *tab, const char *str,
33 kx unsigned int alignment, struct sec_merge_sec_info *secinfo)
33 kx {
33 kx struct sec_merge_hash_entry *entry;
33 kx
33 kx entry = sec_merge_hash_lookup (tab, str, alignment, true);
33 kx if (entry == NULL)
33 kx return NULL;
33 kx
33 kx if (entry->secinfo == NULL)
33 kx {
33 kx tab->size++;
33 kx entry->secinfo = secinfo;
33 kx if (tab->first == NULL)
33 kx tab->first = entry;
33 kx else
33 kx tab->last->next = entry;
33 kx tab->last = entry;
33 kx }
33 kx
33 kx return entry;
33 kx }
33 kx
33 kx static bool
33 kx sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry,
33 kx unsigned char *contents, file_ptr offset)
33 kx {
33 kx struct sec_merge_sec_info *secinfo = entry->secinfo;
33 kx asection *sec = secinfo->sec;
33 kx char *pad = NULL;
33 kx bfd_size_type off = 0;
33 kx unsigned int opb = bfd_octets_per_byte (abfd, sec);
33 kx int alignment_power = sec->output_section->alignment_power * opb;
33 kx bfd_size_type pad_len; /* Octets. */
33 kx
33 kx /* FIXME: If alignment_power is 0 then really we should scan the
33 kx entry list for the largest required alignment and use that. */
33 kx pad_len = alignment_power ? ((bfd_size_type) 1 << alignment_power) : 16;
33 kx
33 kx pad = (char *) bfd_zmalloc (pad_len);
33 kx if (pad == NULL)
33 kx return false;
33 kx
33 kx for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
33 kx {
33 kx const char *str;
33 kx bfd_size_type len;
33 kx
33 kx len = -off & (entry->alignment - 1);
33 kx if (len != 0)
33 kx {
33 kx BFD_ASSERT (len <= pad_len);
33 kx if (contents)
33 kx {
33 kx memcpy (contents + offset, pad, len);
33 kx offset += len;
33 kx }
33 kx else if (bfd_bwrite (pad, len, abfd) != len)
33 kx goto err;
33 kx off += len;
33 kx }
33 kx
33 kx str = entry->root.string;
33 kx len = entry->len;
33 kx
33 kx if (contents)
33 kx {
33 kx memcpy (contents + offset, str, len);
33 kx offset += len;
33 kx }
33 kx else if (bfd_bwrite (str, len, abfd) != len)
33 kx goto err;
33 kx
33 kx off += len;
33 kx }
33 kx
33 kx /* Trailing alignment needed? */
33 kx off = sec->size - off;
33 kx if (off != 0 && alignment_power)
33 kx {
33 kx BFD_ASSERT (off <= pad_len);
33 kx if (contents)
33 kx memcpy (contents + offset, pad, off);
33 kx else if (bfd_bwrite (pad, off, abfd) != off)
33 kx goto err;
33 kx }
33 kx
33 kx free (pad);
33 kx return true;
33 kx
33 kx err:
33 kx free (pad);
33 kx return false;
33 kx }
33 kx
33 kx /* Register a SEC_MERGE section as a candidate for merging.
33 kx This function is called for all non-dynamic SEC_MERGE input sections. */
33 kx
33 kx bool
33 kx _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec,
33 kx void **psecinfo)
33 kx {
33 kx struct sec_merge_info *sinfo;
33 kx struct sec_merge_sec_info *secinfo;
33 kx unsigned int alignment_power; /* Octets. */
33 kx unsigned int align; /* Octets. */
33 kx bfd_size_type amt;
33 kx bfd_byte *contents;
33 kx unsigned int opb = bfd_octets_per_byte (abfd, sec);
33 kx
33 kx if ((abfd->flags & DYNAMIC) != 0
33 kx || (sec->flags & SEC_MERGE) == 0)
33 kx abort ();
33 kx
33 kx if (sec->size == 0
33 kx || (sec->flags & SEC_EXCLUDE) != 0
33 kx || sec->entsize == 0)
33 kx return true;
33 kx
33 kx if (sec->size % sec->entsize != 0)
33 kx return true;
33 kx
33 kx if ((sec->flags & SEC_RELOC) != 0)
33 kx {
33 kx /* We aren't prepared to handle relocations in merged sections. */
33 kx return true;
33 kx }
33 kx
33 kx #ifndef CHAR_BIT
33 kx #define CHAR_BIT 8
33 kx #endif
33 kx alignment_power = sec->alignment_power * opb;
33 kx if (alignment_power >= sizeof (align) * CHAR_BIT)
33 kx return true;
33 kx
33 kx align = 1u << alignment_power;
33 kx if ((sec->entsize < align
33 kx && ((sec->entsize & (sec->entsize - 1))
33 kx || !(sec->flags & SEC_STRINGS)))
33 kx || (sec->entsize > align
33 kx && (sec->entsize & (align - 1))))
33 kx {
33 kx /* Sanity check. If string character size is smaller than
33 kx alignment, then we require character size to be a power
33 kx of 2, otherwise character size must be integer multiple
33 kx of alignment. For non-string constants, alignment must
33 kx be smaller than or equal to entity size and entity size
33 kx must be integer multiple of alignment. */
33 kx return true;
33 kx }
33 kx
33 kx for (sinfo = (struct sec_merge_info *) *psinfo; sinfo; sinfo = sinfo->next)
33 kx if ((secinfo = sinfo->chain)
33 kx && ! ((secinfo->sec->flags ^ sec->flags) & (SEC_MERGE | SEC_STRINGS))
33 kx && secinfo->sec->entsize == sec->entsize
33 kx && secinfo->sec->alignment_power == sec->alignment_power
33 kx && secinfo->sec->output_section == sec->output_section)
33 kx break;
33 kx
33 kx if (sinfo == NULL)
33 kx {
33 kx /* Initialize the information we need to keep track of. */
33 kx sinfo = (struct sec_merge_info *)
33 kx bfd_alloc (abfd, sizeof (struct sec_merge_info));
33 kx if (sinfo == NULL)
33 kx goto error_return;
33 kx sinfo->next = (struct sec_merge_info *) *psinfo;
33 kx sinfo->chain = NULL;
33 kx *psinfo = sinfo;
33 kx sinfo->htab = sec_merge_init (sec->entsize, (sec->flags & SEC_STRINGS));
33 kx if (sinfo->htab == NULL)
33 kx goto error_return;
33 kx }
33 kx
33 kx /* Read the section from abfd. */
33 kx
33 kx amt = sizeof (struct sec_merge_sec_info) - 1 + sec->size;
33 kx if (sec->flags & SEC_STRINGS)
33 kx /* Some versions of gcc may emit a string without a zero terminator.
33 kx See http://gcc.gnu.org/ml/gcc-patches/2006-06/msg01004.html
33 kx Allocate space for an extra zero. */
33 kx amt += sec->entsize;
33 kx *psecinfo = bfd_alloc (abfd, amt);
33 kx if (*psecinfo == NULL)
33 kx goto error_return;
33 kx
33 kx secinfo = (struct sec_merge_sec_info *) *psecinfo;
33 kx if (sinfo->chain)
33 kx {
33 kx secinfo->next = sinfo->chain->next;
33 kx sinfo->chain->next = secinfo;
33 kx }
33 kx else
33 kx secinfo->next = secinfo;
33 kx sinfo->chain = secinfo;
33 kx secinfo->sec = sec;
33 kx secinfo->psecinfo = psecinfo;
33 kx secinfo->htab = sinfo->htab;
33 kx secinfo->first_str = NULL;
33 kx
33 kx sec->rawsize = sec->size;
33 kx if (sec->flags & SEC_STRINGS)
33 kx memset (secinfo->contents + sec->size, 0, sec->entsize);
33 kx contents = secinfo->contents;
33 kx if (! bfd_get_full_section_contents (sec->owner, sec, &contents))
33 kx goto error_return;
33 kx
33 kx return true;
33 kx
33 kx error_return:
33 kx *psecinfo = NULL;
33 kx return false;
33 kx }
33 kx
33 kx /* Record one section into the hash table. */
33 kx static bool
33 kx record_section (struct sec_merge_info *sinfo,
33 kx struct sec_merge_sec_info *secinfo)
33 kx {
33 kx asection *sec = secinfo->sec;
33 kx struct sec_merge_hash_entry *entry;
33 kx bool nul;
33 kx unsigned char *p, *end;
33 kx bfd_vma mask, eltalign;
33 kx unsigned int align, i;
33 kx
33 kx align = sec->alignment_power;
33 kx end = secinfo->contents + sec->size;
33 kx nul = false;
33 kx mask = ((bfd_vma) 1 << align) - 1;
33 kx if (sec->flags & SEC_STRINGS)
33 kx {
33 kx for (p = secinfo->contents; p < end; )
33 kx {
33 kx eltalign = p - secinfo->contents;
33 kx eltalign = ((eltalign ^ (eltalign - 1)) + 1) >> 1;
33 kx if (!eltalign || eltalign > mask)
33 kx eltalign = mask + 1;
33 kx entry = sec_merge_add (sinfo->htab, (char *) p, (unsigned) eltalign,
33 kx secinfo);
33 kx if (! entry)
33 kx goto error_return;
33 kx p += entry->len;
33 kx if (sec->entsize == 1)
33 kx {
33 kx while (p < end && *p == 0)
33 kx {
33 kx if (!nul && !((p - secinfo->contents) & mask))
33 kx {
33 kx nul = true;
33 kx entry = sec_merge_add (sinfo->htab, "",
33 kx (unsigned) mask + 1, secinfo);
33 kx if (! entry)
33 kx goto error_return;
33 kx }
33 kx p++;
33 kx }
33 kx }
33 kx else
33 kx {
33 kx while (p < end)
33 kx {
33 kx for (i = 0; i < sec->entsize; i++)
33 kx if (p[i] != '\0')
33 kx break;
33 kx if (i != sec->entsize)
33 kx break;
33 kx if (!nul && !((p - secinfo->contents) & mask))
33 kx {
33 kx nul = true;
33 kx entry = sec_merge_add (sinfo->htab, (char *) p,
33 kx (unsigned) mask + 1, secinfo);
33 kx if (! entry)
33 kx goto error_return;
33 kx }
33 kx p += sec->entsize;
33 kx }
33 kx }
33 kx }
33 kx }
33 kx else
33 kx {
33 kx for (p = secinfo->contents; p < end; p += sec->entsize)
33 kx {
33 kx entry = sec_merge_add (sinfo->htab, (char *) p, 1, secinfo);
33 kx if (! entry)
33 kx goto error_return;
33 kx }
33 kx }
33 kx
33 kx return true;
33 kx
33 kx error_return:
33 kx for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
33 kx *secinfo->psecinfo = NULL;
33 kx return false;
33 kx }
33 kx
33 kx /* qsort comparison function. Won't ever return zero as all entries
33 kx differ, so there is no issue with qsort stability here. */
33 kx
33 kx static int
33 kx strrevcmp (const void *a, const void *b)
33 kx {
33 kx struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
33 kx struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
33 kx unsigned int lenA = A->len;
33 kx unsigned int lenB = B->len;
33 kx const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
33 kx const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
33 kx int l = lenA < lenB ? lenA : lenB;
33 kx
33 kx while (l)
33 kx {
33 kx if (*s != *t)
33 kx return (int) *s - (int) *t;
33 kx s--;
33 kx t--;
33 kx l--;
33 kx }
33 kx return lenA - lenB;
33 kx }
33 kx
33 kx /* Like strrevcmp, but for the case where all strings have the same
33 kx alignment > entsize. */
33 kx
33 kx static int
33 kx strrevcmp_align (const void *a, const void *b)
33 kx {
33 kx struct sec_merge_hash_entry *A = *(struct sec_merge_hash_entry **) a;
33 kx struct sec_merge_hash_entry *B = *(struct sec_merge_hash_entry **) b;
33 kx unsigned int lenA = A->len;
33 kx unsigned int lenB = B->len;
33 kx const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
33 kx const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
33 kx int l = lenA < lenB ? lenA : lenB;
33 kx int tail_align = (lenA & (A->alignment - 1)) - (lenB & (A->alignment - 1));
33 kx
33 kx if (tail_align != 0)
33 kx return tail_align;
33 kx
33 kx while (l)
33 kx {
33 kx if (*s != *t)
33 kx return (int) *s - (int) *t;
33 kx s--;
33 kx t--;
33 kx l--;
33 kx }
33 kx return lenA - lenB;
33 kx }
33 kx
33 kx static inline int
33 kx is_suffix (const struct sec_merge_hash_entry *A,
33 kx const struct sec_merge_hash_entry *B)
33 kx {
33 kx if (A->len <= B->len)
33 kx /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
33 kx not to be equal by the hash table. */
33 kx return 0;
33 kx
33 kx return memcmp (A->root.string + (A->len - B->len),
33 kx B->root.string, B->len) == 0;
33 kx }
33 kx
33 kx /* This is a helper function for _bfd_merge_sections. It attempts to
33 kx merge strings matching suffixes of longer strings. */
33 kx static struct sec_merge_sec_info *
33 kx merge_strings (struct sec_merge_info *sinfo)
33 kx {
33 kx struct sec_merge_hash_entry **array, **a, *e;
33 kx struct sec_merge_sec_info *secinfo;
33 kx bfd_size_type size, amt;
33 kx unsigned int alignment = 0;
33 kx
33 kx /* Now sort the strings */
33 kx amt = sinfo->htab->size * sizeof (struct sec_merge_hash_entry *);
33 kx array = (struct sec_merge_hash_entry **) bfd_malloc (amt);
33 kx if (array == NULL)
33 kx return NULL;
33 kx
33 kx for (e = sinfo->htab->first, a = array; e; e = e->next)
33 kx if (e->alignment)
33 kx {
33 kx *a++ = e;
33 kx /* Adjust the length to not include the zero terminator. */
33 kx e->len -= sinfo->htab->entsize;
33 kx if (alignment != e->alignment)
33 kx {
33 kx if (alignment == 0)
33 kx alignment = e->alignment;
33 kx else
33 kx alignment = (unsigned) -1;
33 kx }
33 kx }
33 kx
33 kx sinfo->htab->size = a - array;
33 kx if (sinfo->htab->size != 0)
33 kx {
33 kx qsort (array, (size_t) sinfo->htab->size,
33 kx sizeof (struct sec_merge_hash_entry *),
33 kx (alignment != (unsigned) -1 && alignment > sinfo->htab->entsize
33 kx ? strrevcmp_align : strrevcmp));
33 kx
33 kx /* Loop over the sorted array and merge suffixes */
33 kx e = *--a;
33 kx e->len += sinfo->htab->entsize;
33 kx while (--a >= array)
33 kx {
33 kx struct sec_merge_hash_entry *cmp = *a;
33 kx
33 kx cmp->len += sinfo->htab->entsize;
33 kx if (e->alignment >= cmp->alignment
33 kx && !((e->len - cmp->len) & (cmp->alignment - 1))
33 kx && is_suffix (e, cmp))
33 kx {
33 kx cmp->u.suffix = e;
33 kx cmp->alignment = 0;
33 kx }
33 kx else
33 kx e = cmp;
33 kx }
33 kx }
33 kx
33 kx free (array);
33 kx
33 kx /* Now assign positions to the strings we want to keep. */
33 kx size = 0;
33 kx secinfo = sinfo->htab->first->secinfo;
33 kx for (e = sinfo->htab->first; e; e = e->next)
33 kx {
33 kx if (e->secinfo != secinfo)
33 kx {
33 kx secinfo->sec->size = size;
33 kx secinfo = e->secinfo;
33 kx }
33 kx if (e->alignment)
33 kx {
33 kx if (e->secinfo->first_str == NULL)
33 kx {
33 kx e->secinfo->first_str = e;
33 kx size = 0;
33 kx }
33 kx size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
33 kx e->u.index = size;
33 kx size += e->len;
33 kx }
33 kx }
33 kx secinfo->sec->size = size;
33 kx
33 kx /* And now adjust the rest, removing them from the chain (but not hashtable)
33 kx at the same time. */
33 kx for (a = &sinfo->htab->first, e = *a; e; e = e->next)
33 kx if (e->alignment)
33 kx a = &e->next;
33 kx else
33 kx {
33 kx *a = e->next;
33 kx if (e->len)
33 kx {
33 kx e->secinfo = e->u.suffix->secinfo;
33 kx e->alignment = e->u.suffix->alignment;
33 kx e->u.index = e->u.suffix->u.index + (e->u.suffix->len - e->len);
33 kx }
33 kx }
33 kx return secinfo;
33 kx }
33 kx
33 kx /* This function is called once after all SEC_MERGE sections are registered
33 kx with _bfd_merge_section. */
33 kx
33 kx bool
33 kx _bfd_merge_sections (bfd *abfd,
33 kx struct bfd_link_info *info ATTRIBUTE_UNUSED,
33 kx void *xsinfo,
33 kx void (*remove_hook) (bfd *, asection *))
33 kx {
33 kx struct sec_merge_info *sinfo;
33 kx
33 kx for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
33 kx {
33 kx struct sec_merge_sec_info *secinfo;
33 kx bfd_size_type align; /* Bytes. */
33 kx
33 kx if (! sinfo->chain)
33 kx continue;
33 kx
33 kx /* Move sinfo->chain to head of the chain, terminate it. */
33 kx secinfo = sinfo->chain;
33 kx sinfo->chain = secinfo->next;
33 kx secinfo->next = NULL;
33 kx
33 kx /* Record the sections into the hash table. */
33 kx align = 1;
33 kx for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
33 kx if (secinfo->sec->flags & SEC_EXCLUDE)
33 kx {
33 kx *secinfo->psecinfo = NULL;
33 kx if (remove_hook)
33 kx (*remove_hook) (abfd, secinfo->sec);
33 kx }
33 kx else
33 kx {
33 kx if (!record_section (sinfo, secinfo))
33 kx return false;
33 kx if (align)
33 kx {
33 kx unsigned int opb = bfd_octets_per_byte (abfd, secinfo->sec);
33 kx
33 kx align = (bfd_size_type) 1 << secinfo->sec->alignment_power;
33 kx if (((secinfo->sec->size / opb) & (align - 1)) != 0)
33 kx align = 0;
33 kx }
33 kx }
33 kx
33 kx if (sinfo->htab->first == NULL)
33 kx continue;
33 kx
33 kx if (sinfo->htab->strings)
33 kx {
33 kx secinfo = merge_strings (sinfo);
33 kx if (!secinfo)
33 kx return false;
33 kx }
33 kx else
33 kx {
33 kx struct sec_merge_hash_entry *e;
33 kx bfd_size_type size = 0; /* Octets. */
33 kx
33 kx /* Things are much simpler for non-strings.
33 kx Just assign them slots in the section. */
33 kx secinfo = NULL;
33 kx for (e = sinfo->htab->first; e; e = e->next)
33 kx {
33 kx if (e->secinfo->first_str == NULL)
33 kx {
33 kx if (secinfo)
33 kx secinfo->sec->size = size;
33 kx e->secinfo->first_str = e;
33 kx size = 0;
33 kx }
33 kx size = (size + e->alignment - 1) & ~((bfd_vma) e->alignment - 1);
33 kx e->u.index = size;
33 kx size += e->len;
33 kx secinfo = e->secinfo;
33 kx }
33 kx secinfo->sec->size = size;
33 kx }
33 kx
33 kx /* If the input sections were padded according to their alignments,
33 kx then pad the output too. */
33 kx if (align)
33 kx secinfo->sec->size = (secinfo->sec->size + align - 1) & -align;
33 kx
33 kx /* Finally remove all input sections which have not made it into
33 kx the hash table at all. */
33 kx for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
33 kx if (secinfo->first_str == NULL)
33 kx secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP;
33 kx }
33 kx
33 kx return true;
33 kx }
33 kx
33 kx /* Write out the merged section. */
33 kx
33 kx bool
33 kx _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
33 kx {
33 kx struct sec_merge_sec_info *secinfo;
33 kx file_ptr pos;
33 kx unsigned char *contents;
33 kx Elf_Internal_Shdr *hdr;
33 kx
33 kx secinfo = (struct sec_merge_sec_info *) psecinfo;
33 kx
33 kx if (!secinfo)
33 kx return false;
33 kx
33 kx if (secinfo->first_str == NULL)
33 kx return true;
33 kx
33 kx /* FIXME: octets_per_byte. */
33 kx hdr = &elf_section_data (sec->output_section)->this_hdr;
33 kx if (hdr->sh_offset == (file_ptr) -1)
33 kx {
33 kx /* We must compress this section. Write output to the
33 kx buffer. */
33 kx contents = hdr->contents;
33 kx if (contents == NULL)
33 kx abort ();
33 kx }
33 kx else
33 kx {
33 kx contents = NULL;
33 kx pos = sec->output_section->filepos + sec->output_offset;
33 kx if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
33 kx return false;
33 kx }
33 kx
33 kx if (! sec_merge_emit (output_bfd, secinfo->first_str, contents,
33 kx sec->output_offset))
33 kx return false;
33 kx
33 kx return true;
33 kx }
33 kx
33 kx /* Adjust an address in the SEC_MERGE section. Given OFFSET within
33 kx *PSEC, this returns the new offset in the adjusted SEC_MERGE
33 kx section and writes the new section back into *PSEC. */
33 kx
33 kx bfd_vma
33 kx _bfd_merged_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, asection **psec,
33 kx void *psecinfo, bfd_vma offset)
33 kx {
33 kx struct sec_merge_sec_info *secinfo;
33 kx struct sec_merge_hash_entry *entry;
33 kx unsigned char *p;
33 kx asection *sec = *psec;
33 kx
33 kx secinfo = (struct sec_merge_sec_info *) psecinfo;
33 kx
33 kx if (!secinfo)
33 kx return offset;
33 kx
33 kx if (offset >= sec->rawsize)
33 kx {
33 kx if (offset > sec->rawsize)
33 kx _bfd_error_handler
33 kx /* xgettext:c-format */
33 kx (_("%pB: access beyond end of merged section (%" PRId64 ")"),
33 kx sec->owner, (int64_t) offset);
33 kx return secinfo->first_str ? sec->size : 0;
33 kx }
33 kx
33 kx if (secinfo->htab->strings)
33 kx {
33 kx if (sec->entsize == 1)
33 kx {
33 kx p = secinfo->contents + offset - 1;
33 kx while (p >= secinfo->contents && *p)
33 kx --p;
33 kx ++p;
33 kx }
33 kx else
33 kx {
33 kx p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
33 kx p -= sec->entsize;
33 kx while (p >= secinfo->contents)
33 kx {
33 kx unsigned int i;
33 kx
33 kx for (i = 0; i < sec->entsize; ++i)
33 kx if (p[i] != '\0')
33 kx break;
33 kx if (i == sec->entsize)
33 kx break;
33 kx p -= sec->entsize;
33 kx }
33 kx p += sec->entsize;
33 kx }
33 kx }
33 kx else
33 kx {
33 kx p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
33 kx }
33 kx entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, false);
33 kx if (!entry)
33 kx {
33 kx if (! secinfo->htab->strings)
33 kx abort ();
33 kx /* This should only happen if somebody points into the padding
33 kx after a NUL character but before next entity. */
33 kx if (*p)
33 kx abort ();
33 kx if (! secinfo->htab->first)
33 kx abort ();
33 kx entry = secinfo->htab->first;
33 kx p = (secinfo->contents + (offset / sec->entsize + 1) * sec->entsize
33 kx - entry->len);
33 kx }
33 kx
33 kx *psec = entry->secinfo->sec;
33 kx return entry->u.index + (secinfo->contents + offset - p);
33 kx }
33 kx
33 kx /* Tidy up when done. */
33 kx
33 kx void
33 kx _bfd_merge_sections_free (void *xsinfo)
33 kx {
33 kx struct sec_merge_info *sinfo;
33 kx
33 kx for (sinfo = (struct sec_merge_info *) xsinfo; sinfo; sinfo = sinfo->next)
33 kx {
33 kx bfd_hash_table_free (&sinfo->htab->table);
33 kx free (sinfo->htab);
33 kx }
33 kx }