From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23993 invoked by alias); 6 Mar 2020 10:14:36 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 23984 invoked by uid 89); 6 Mar 2020 10:14:36 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.8 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy= X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-1.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (205.139.110.61) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 06 Mar 2020 10:14:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583489672; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=JUKfGrmdikxsnRm9GEd3KXo1oEYZB3BcTZxm87AHjGk=; b=Uzu8QDWJLbxOkMWaExwuxO1u1cEpbcoNlB7ouT5n40cCzj6iCL0F9870QE3SRSWN1wFNTq JChtQ4RWSW9FeOveWezzj0jy3iXOK9r0sMlCUaHO7OfoLNkt5hHWobuHOy5ap1utJ4vXeA k3mNiOgAI7ejs2iDi7sX8vCbaf+WmIA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-390-47wmd39uM82Xa1D5ztQMUA-1; Fri, 06 Mar 2020 05:14:30 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 08EC018B9FB9 for ; Fri, 6 Mar 2020 10:14:30 +0000 (UTC) Received: from comet.redhat.com (ovpn-117-47.ams2.redhat.com [10.36.117.47]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5890591D7A for ; Fri, 6 Mar 2020 10:14:29 +0000 (UTC) From: Nick Clifton To: binutils@sourceware.org Subject: Commit: objcopy: do not set the 'share' flag on ELF binaries Date: Fri, 06 Mar 2020 10:14:00 -0000 Message-ID: <8736albxj0.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2020-03/txt/msg00143.txt Hi Guys, It was recently pointed out to me that using objcopy to add the "share" section flag to a section in an ELF format binary would trigger an abort in the BFD library. It turns out that objcopy uses the SEC_COFF_SHARE flag bit for "share", which just happens to have the same value as SEC_ELF_COMPRESS. Trying to compress an ELF section just by changing its flag bit is a bad idea and this is what was triggering the abort. So I am checking in the patch below. It replaces the abort with some more helpful error messages and a failure result. It updates the binutils documentation to note that you cannot set the "share" flag bit on non-COFF sections. Plus it fixes objcopy so that it will reject the share flag if the output file is in the ELF format. Cheers Nick binutils/ChangeLog 2020-03-06 Nick Clifton * objcopy.c (check_new_section_flags): New function. Reject the SEC_COFF_SHARED flag if the target is not a COFF binary. (copy_object): Call check_new_section_flags. (setup_section): Likewise. * doc/binutils.texi (objcopy): Add a note that the 'share' section flag cannot be applied to ELF binaries. bfd/ChangeLog 2020-03-06 Nick Clifton * elf.c (_bfd_elf_set_section_contents): Replace call to abort with error messages and failure return values. diff --git a/bfd/elf.c b/bfd/elf.c index 747d120101..e6db2ff64d 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -9181,20 +9180,47 @@ _bfd_elf_set_section_contents (bfd *abfd, hdr =3D &elf_section_data (section)->this_hdr; if (hdr->sh_offset =3D=3D (file_ptr) -1) { + unsigned char *contents; + if (bfd_section_is_ctf (section)) /* Nothing to do with this section: the contents are generated later. */ return TRUE; =20 - /* We must compress this section. Write output to the buffer. */ - unsigned char *contents =3D hdr->contents; - if ((offset + count) > hdr->sh_size - || (section->flags & SEC_ELF_COMPRESS) =3D=3D 0 - || contents =3D=3D NULL) - abort (); + if ((section->flags & SEC_ELF_COMPRESS) =3D=3D 0) + { + _bfd_error_handler + (_("%pB:%pA: error: attempting to write into an unallocated compresse= d section"), + abfd, section); + bfd_set_error (bfd_error_invalid_operation); + return FALSE; + } +=20=20=20=20=20=20 + if ((offset + count) > hdr->sh_size) + { + _bfd_error_handler + (_("%pB:%pA: error: attempting to write over the end of the section"), + abfd, section); + + bfd_set_error (bfd_error_invalid_operation); + return FALSE; + } + + contents =3D hdr->contents; + if (contents =3D=3D NULL) + { + _bfd_error_handler + (_("%pB:%pA: error: attempting to write section into an empty buffer"= ), + abfd, section); + + bfd_set_error (bfd_error_invalid_operation); + return FALSE; + } + memcpy (contents + offset, location, count); return TRUE; } + pos =3D hdr->sh_offset + offset; if (bfd_seek (abfd, pos, SEEK_SET) !=3D 0 || bfd_bwrite (location, count, abfd) !=3D count) diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 3099e3f545..de3f1babb2 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -1648,7 +1648,9 @@ recognized names are @samp{alloc}, @samp{contents}, @= samp{load}, @samp{contents} flag for a section which does not have contents, but it is not meaningful to clear the @samp{contents} flag of a section which does have contents--just remove the section instead. Not all flags are -meaningful for all object file formats. +meaningful for all object file formats. In particular the +@samp{share} flag is only meaningful for COFF format files and not for +ELF format files. =20 @item --set-section-alignment @var{sectionpattern}=3D@var{align} Set the alignment for any sections matching @var{sectionpattern}. @@ -1704,7 +1706,8 @@ Rename a section from @var{oldname} to @var{newname},= optionally changing the section's flags to @var{flags} in the process. This has the advantage over using a linker script to perform the rename in that the output stays as an object file and does not become a linked -executable. +executable. This option accepts the same set of flags as the +@option{--sect-section-flags} option. =20 This option is particularly helpful when the input format is binary, since this will always create a section called .data. If for example, diff --git a/binutils/objcopy.c b/binutils/objcopy.c index 16affa9960..09facf0061 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -2562,6 +2562,23 @@ merge_gnu_build_notes (bfd * abfd, return size; } =20 +static flagword +check_new_section_flags (flagword flags, bfd * abfd, const char * secname) +{ + /* Only set the SEC_COFF_SHARED flag on COFF files. + The same bit value is used by ELF targets to indicate + compressed sections, and setting that flag here breaks + things. */ + if ((flags & SEC_COFF_SHARED) + && bfd_get_flavour (abfd) !=3D bfd_target_coff_flavour) + { + non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format = is not COFF"), + bfd_get_filename (abfd), secname); + flags &=3D ~ SEC_COFF_SHARED; + } + return flags; +} + /* Copy object file IBFD onto OBFD. Returns TRUE upon success, FALSE otherwise. */ =20 @@ -2810,7 +2827,10 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_in= fo_type *input_arch) pset =3D find_section_list (padd->name, FALSE, SECTION_CONTEXT_SET_FLAGS); if (pset !=3D NULL) - flags =3D pset->flags | SEC_HAS_CONTENTS; + {=09=20=20=20=20=20=20 + flags =3D pset->flags | SEC_HAS_CONTENTS; + flags =3D check_new_section_flags (flags, obfd, padd->name); + } else flags =3D SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA; =20 @@ -3950,6 +3970,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obf= darg) flagword flags; const char *err; const char * name; + const char * new_name; char *prefix =3D NULL; bfd_boolean make_nobits; unsigned int alignment; @@ -3965,7 +3986,12 @@ setup_section (bfd *ibfd, sec_ptr isection, void *ob= fdarg) flags &=3D bfd_applicable_section_flags (ibfd); flags &=3D bfd_applicable_section_flags (obfd); } - name =3D find_section_rename (name, &flags); + new_name =3D find_section_rename (name, &flags); + if (new_name !=3D name) + { + name =3D new_name; + flags =3D check_new_section_flags (flags, obfd, name); + } =20 /* Prefix sections. */ if (prefix_alloc_sections_string @@ -3989,7 +4015,10 @@ setup_section (bfd *ibfd, sec_ptr isection, void *ob= fdarg) p =3D find_section_list (bfd_section_name (isection), FALSE, SECTION_CONTEXT_SET_FLAGS); if (p !=3D NULL) - flags =3D p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC)); + { + flags =3D p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC)); + flags =3D check_new_section_flags (flags, obfd, bfd_section_name (is= ection)); + } else if (strip_symbols =3D=3D STRIP_NONDEBUG && (flags & (SEC_ALLOC | SEC_GROUP)) !=3D 0 && !is_nondebug_keep_contents_section (ibfd, isection))