+
+/*
+FUNCTION
+ bfd_demangle
+
+SYNOPSIS
+ char *bfd_demangle (bfd *, const char *, int);
+
+DESCRIPTION
+ Wrapper around cplus_demangle. Strips leading underscores and
+ other such chars that would otherwise confuse the demangler.
+ If passed a g++ v3 ABI mangled name, returns a buffer allocated
+ with malloc holding the demangled name. Returns NULL otherwise
+ and on memory alloc failure.
+*/
+
+char *
+bfd_demangle (bfd *abfd, const char *name, int options)
+{
+ char *res, *alloc;
+ const char *pre, *suf;
+ size_t pre_len;
+ bfd_boolean skip_lead;
+
+ skip_lead = (abfd != NULL
+ && *name != '\0'
+ && bfd_get_symbol_leading_char (abfd) == *name);
+ if (skip_lead)
+ ++name;
+
+ /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
+ or the MS PE format. These formats have a number of leading '.'s
+ on at least some symbols, so we remove all dots to avoid
+ confusing the demangler. */
+ pre = name;
+ while (*name == '.' || *name == '$')
+ ++name;
+ pre_len = name - pre;
+
+ /* Strip off @plt and suchlike too. */
+ alloc = NULL;
+ suf = strchr (name, '@');
+ if (suf != NULL)
+ {
+ alloc = (char *) bfd_malloc (suf - name + 1);
+ if (alloc == NULL)
+ return NULL;
+ memcpy (alloc, name, suf - name);
+ alloc[suf - name] = '\0';
+ name = alloc;
+ }
+
+ res = cplus_demangle (name, options);
+
+ free (alloc);
+
+ if (res == NULL)
+ {
+ if (skip_lead)
+ {
+ size_t len = strlen (pre) + 1;
+ alloc = (char *) bfd_malloc (len);
+ if (alloc == NULL)
+ return NULL;
+ memcpy (alloc, pre, len);
+ return alloc;
+ }
+ return NULL;
+ }
+
+ /* Put back any prefix or suffix. */
+ if (pre_len != 0 || suf != NULL)
+ {
+ size_t len;
+ size_t suf_len;
+ char *final;
+
+ len = strlen (res);
+ if (suf == NULL)
+ suf = res + len;
+ suf_len = strlen (suf) + 1;
+ final = (char *) bfd_malloc (pre_len + len + suf_len);
+ if (final != NULL)
+ {
+ memcpy (final, pre, pre_len);
+ memcpy (final + pre_len, res, len);
+ memcpy (final + pre_len + len, suf, suf_len);
+ }
+ free (res);
+ res = final;
+ }
+
+ return res;
+}
+
+/*
+FUNCTION
+ bfd_update_compression_header
+
+SYNOPSIS
+ void bfd_update_compression_header
+ (bfd *abfd, bfd_byte *contents, asection *sec);
+
+DESCRIPTION
+ Set the compression header at CONTENTS of SEC in ABFD and update
+ elf_section_flags for compression.
+*/
+
+void
+bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
+ asection *sec)
+{
+ if ((abfd->flags & BFD_COMPRESS) == 0)
+ abort ();
+
+ switch (bfd_get_flavour (abfd))
+ {
+ case bfd_target_elf_flavour:
+ if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
+ {
+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+
+ /* Set the SHF_COMPRESSED bit. */
+ elf_section_flags (sec) |= SHF_COMPRESSED;
+
+ if (bed->s->elfclass == ELFCLASS32)
+ {
+ Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
+ bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
+ bfd_put_32 (abfd, sec->size, &echdr->ch_size);
+ bfd_put_32 (abfd, 1 << sec->alignment_power,
+ &echdr->ch_addralign);
+ /* bfd_log2 (alignof (Elf32_Chdr)) */
+ bfd_set_section_alignment (sec, 2);
+ }
+ else
+ {
+ Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
+ bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
+ bfd_put_32 (abfd, 0, &echdr->ch_reserved);
+ bfd_put_64 (abfd, sec->size, &echdr->ch_size);
+ bfd_put_64 (abfd, 1 << sec->alignment_power,
+ &echdr->ch_addralign);
+ /* bfd_log2 (alignof (Elf64_Chdr)) */
+ bfd_set_section_alignment (sec, 3);
+ }
+ break;
+ }
+
+ /* Clear the SHF_COMPRESSED bit. */
+ elf_section_flags (sec) &= ~SHF_COMPRESSED;
+ /* Fall through. */
+
+ default:
+ /* Write the zlib header. It should be "ZLIB" followed by
+ the uncompressed section size, 8 bytes in big-endian
+ order. */
+ memcpy (contents, "ZLIB", 4);
+ bfd_putb64 (sec->size, contents + 4);
+ /* No way to keep the original alignment, just use 1 always. */
+ bfd_set_section_alignment (sec, 0);
+ break;
+ }
+}
+
+/*
+ FUNCTION
+ bfd_check_compression_header
+
+ SYNOPSIS
+ bfd_boolean bfd_check_compression_header
+ (bfd *abfd, bfd_byte *contents, asection *sec,
+ bfd_size_type *uncompressed_size,
+ unsigned int *uncompressed_alignment_power);
+
+DESCRIPTION
+ Check the compression header at CONTENTS of SEC in ABFD and
+ store the uncompressed size in UNCOMPRESSED_SIZE and the
+ uncompressed data alignment in UNCOMPRESSED_ALIGNMENT_POWER
+ if the compression header is valid.
+
+RETURNS
+ Return TRUE if the compression header is valid.
+*/
+
+bfd_boolean
+bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
+ asection *sec,
+ bfd_size_type *uncompressed_size,
+ unsigned int *uncompressed_alignment_power)
+{
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+ && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
+ {
+ Elf_Internal_Chdr chdr;
+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+ if (bed->s->elfclass == ELFCLASS32)
+ {
+ Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
+ chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
+ chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
+ chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
+ }
+ else
+ {
+ Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
+ chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
+ chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
+ chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
+ }
+ if (chdr.ch_type == ELFCOMPRESS_ZLIB
+ && chdr.ch_addralign == (chdr.ch_addralign & -chdr.ch_addralign))
+ {
+ *uncompressed_size = chdr.ch_size;
+ *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+/*
+FUNCTION
+ bfd_get_compression_header_size
+
+SYNOPSIS
+ int bfd_get_compression_header_size (bfd *abfd, asection *sec);
+
+DESCRIPTION
+ Return the size of the compression header of SEC in ABFD.
+
+RETURNS
+ Return the size of the compression header in bytes.
+*/
+
+int
+bfd_get_compression_header_size (bfd *abfd, asection *sec)
+{
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ {
+ if (sec == NULL)
+ {
+ if (!(abfd->flags & BFD_COMPRESS_GABI))
+ return 0;
+ }
+ else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
+ return 0;
+
+ if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
+ return sizeof (Elf32_External_Chdr);
+ else
+ return sizeof (Elf64_External_Chdr);
+ }
+
+ return 0;
+}
+
+/*
+FUNCTION
+ bfd_convert_section_size
+
+SYNOPSIS
+ bfd_size_type bfd_convert_section_size
+ (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size);
+
+DESCRIPTION
+ Convert the size @var{size} of the section @var{isec} in input
+ BFD @var{ibfd} to the section size in output BFD @var{obfd}.
+*/
+
+bfd_size_type
+bfd_convert_section_size (bfd *ibfd, sec_ptr isec, bfd *obfd,
+ bfd_size_type size)
+{
+ bfd_size_type hdr_size;
+
+ /* Do nothing if either input or output aren't ELF. */
+ if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
+ || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
+ return size;
+
+ /* Do nothing if ELF classes of input and output are the same. */
+ if (get_elf_backend_data (ibfd)->s->elfclass
+ == get_elf_backend_data (obfd)->s->elfclass)
+ return size;
+
+ /* Convert GNU property size. */
+ if (CONST_STRNEQ (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
+ return _bfd_elf_convert_gnu_property_size (ibfd, obfd);
+
+ /* Do nothing if input file will be decompressed. */
+ if ((ibfd->flags & BFD_DECOMPRESS))
+ return size;
+
+ /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
+ hdr_size = bfd_get_compression_header_size (ibfd, isec);
+ if (hdr_size == 0)
+ return size;
+
+ /* Adjust the size of the output SHF_COMPRESSED section. */
+ if (hdr_size == sizeof (Elf32_External_Chdr))
+ return (size - sizeof (Elf32_External_Chdr)
+ + sizeof (Elf64_External_Chdr));
+ else
+ return (size - sizeof (Elf64_External_Chdr)
+ + sizeof (Elf32_External_Chdr));
+}
+
+/*
+FUNCTION
+ bfd_convert_section_contents
+
+SYNOPSIS
+ bfd_boolean bfd_convert_section_contents
+ (bfd *ibfd, asection *isec, bfd *obfd,
+ bfd_byte **ptr, bfd_size_type *ptr_size);
+
+DESCRIPTION
+ Convert the contents, stored in @var{*ptr}, of the section
+ @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
+ if needed. The original buffer pointed to by @var{*ptr} may
+ be freed and @var{*ptr} is returned with memory malloc'd by this
+ function, and the new size written to @var{ptr_size}.
+*/
+
+bfd_boolean
+bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
+ bfd_byte **ptr, bfd_size_type *ptr_size)
+{
+ bfd_byte *contents;
+ bfd_size_type ihdr_size, ohdr_size, size;
+ Elf_Internal_Chdr chdr;
+ bfd_boolean use_memmove;
+
+ /* Do nothing if either input or output aren't ELF. */
+ if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
+ || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
+ return TRUE;
+
+ /* Do nothing if ELF classes of input and output are the same. */
+ if (get_elf_backend_data (ibfd)->s->elfclass
+ == get_elf_backend_data (obfd)->s->elfclass)
+ return TRUE;
+
+ /* Convert GNU properties. */
+ if (CONST_STRNEQ (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
+ return _bfd_elf_convert_gnu_properties (ibfd, isec, obfd, ptr,
+ ptr_size);
+
+ /* Do nothing if input file will be decompressed. */
+ if ((ibfd->flags & BFD_DECOMPRESS))
+ return TRUE;
+
+ /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
+ ihdr_size = bfd_get_compression_header_size (ibfd, isec);
+ if (ihdr_size == 0)
+ return TRUE;
+
+ /* PR 25221. Check for corrupt input sections. */
+ if (ihdr_size > bfd_get_section_limit (ibfd, isec))
+ /* FIXME: Issue a warning about a corrupt
+ compression header size field ? */
+ return FALSE;
+
+ contents = *ptr;
+
+ /* Convert the contents of the input SHF_COMPRESSED section to
+ output. Get the input compression header and the size of the
+ output compression header. */
+ if (ihdr_size == sizeof (Elf32_External_Chdr))
+ {
+ Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
+ chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
+ chdr.ch_size = bfd_get_32 (ibfd, &echdr->ch_size);
+ chdr.ch_addralign = bfd_get_32 (ibfd, &echdr->ch_addralign);
+
+ ohdr_size = sizeof (Elf64_External_Chdr);
+
+ use_memmove = FALSE;
+ }
+ else if (ihdr_size != sizeof (Elf64_External_Chdr))
+ {
+ /* FIXME: Issue a warning about a corrupt
+ compression header size field ? */
+ return FALSE;
+ }
+ else
+ {
+ Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
+ chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
+ chdr.ch_size = bfd_get_64 (ibfd, &echdr->ch_size);
+ chdr.ch_addralign = bfd_get_64 (ibfd, &echdr->ch_addralign);
+
+ ohdr_size = sizeof (Elf32_External_Chdr);
+ use_memmove = TRUE;
+ }
+
+ size = bfd_section_size (isec) - ihdr_size + ohdr_size;
+ if (!use_memmove)
+ {
+ contents = (bfd_byte *) bfd_malloc (size);
+ if (contents == NULL)
+ return FALSE;
+ }
+
+ /* Write out the output compression header. */
+ if (ohdr_size == sizeof (Elf32_External_Chdr))
+ {
+ Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
+ bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
+ bfd_put_32 (obfd, chdr.ch_size, &echdr->ch_size);
+ bfd_put_32 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
+ }
+ else
+ {
+ Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
+ bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
+ bfd_put_32 (obfd, 0, &echdr->ch_reserved);
+ bfd_put_64 (obfd, chdr.ch_size, &echdr->ch_size);
+ bfd_put_64 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
+ }
+
+ /* Copy the compressed contents. */
+ if (use_memmove)
+ memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
+ else
+ {
+ memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
+ free (*ptr);
+ *ptr = contents;
+ }
+
+ *ptr_size = size;
+ return TRUE;
+}