From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id 3F0083877227 for ; Tue, 6 Dec 2022 21:02:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3F0083877227 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-x22a.google.com with SMTP id s187so12195077oie.10 for ; Tue, 06 Dec 2022 13:02:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=V01+XuATqi15JQV8PClrtu6CgnKNQVdhUqqKzm21NU0=; b=W7vgDA6uBafe5l2GNwCABbM/D5xNvQOfYuTDtuK/na1Rs+dg8mwQeEtIaMlG7yzeqz HmF9eXqN+K9FbUHi/t+5tyMqxMlamGiirVgfZk8FV7bqYhiCziy/aKg6PjwRvaEwFmo0 yYDcaO1O8QOjyRXt+P7G1WuryAP4BhkF1oUJ1eQ5M9rE7tBBAitm2YkQ+Sk256jejOlH 2Sa7q86EzpbHEO3YUrY27c9vYynQs1r+Z65E0QaQdRQ+coakv0a5HcSvIsNbNCJCxOpH VgtANV8AJEuoJPaXC9pnUGl/gV1vUAHYhpHHY+1iUeI4pNGENs1loSsmJPT4vH99qdyP zdwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=V01+XuATqi15JQV8PClrtu6CgnKNQVdhUqqKzm21NU0=; b=cMsqxUJOqkSzKNnKcsqchTtJodHMKT8BS5oxY0N+/P/Ct3rtq08CFqL0x2iX1HNte/ ommVTt9ygz0KUyH02BB+6yCLYxMmMKOo/+mbiPWzsqm4pnGi0jPWcXNghyL8K1VKiLxj pvu47uS0SjVK14l8oJwvfx23Xqp2TlN7mFeRRXu6qGF8MXDM6lZinkZfFWPI0P5hTJA5 XWjbbkNPxfk2TU1Ye0jEEdj0teJHpe8VUrUaYJXnAM98gM66Ayq5LoA9GwColpwyqsE+ JIfin7CP6x5KOyGWyPoHzQmdbtK5zGd2OizGEiMzPq4VaNldyyLzcipKaNa3IroDx72L C2JQ== X-Gm-Message-State: ANoB5pmjIzgqW215qyorUhKy+nx5QqqdpI9nsH8weNW4t9BVFzcRaGbh VAES+l+J96wPG3dVVS+jog44oyrLglAGXaTyjkQ= X-Google-Smtp-Source: AA0mqf4KhAuEV7Dta3ZkkEql3MtyZw/Cmvo2mJvz0tNEcNr9eYxMS1Aomu8aKjlJKp/VDO+p3IsRS4gpvVSSuC9Npe4= X-Received: by 2002:a05:6808:1309:b0:359:d97b:3f6f with SMTP id y9-20020a056808130900b00359d97b3f6fmr37316944oiv.298.1670360538346; Tue, 06 Dec 2022 13:02:18 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: "H.J. Lu" Date: Tue, 6 Dec 2022 13:01:42 -0800 Message-ID: Subject: [PATCH] bfd: Avoid signed overflow for new_size adjustment To: Alan Modra Cc: Binutils Content-Type: multipart/mixed; boundary="000000000000893f9005ef2f1ee3" X-Spam-Status: No, score=-3022.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000893f9005ef2f1ee3 Content-Type: text/plain; charset="UTF-8" On Mon, Dec 5, 2022 at 9:04 PM Alan Modra via Binutils wrote: > > SEC_ELF_RENAME is a flag used to effect section name changes when > compressing/decompressing zlib-gnu debug sections. This can be > accomplished more directly in one of the objcopy specific bfd > functions. Renaming for ld input is simplified too. Ld input object > files always have BFD_DECOMPRESS set. > > bfd/ > * compress.c (bfd_convert_section_size): Rename to.. > (bfd_convert_section_setup): ..this. Handle objcopy renaming > of compressed/decompressed debug sections. > * elf.c (_bfd_elf_make_section_from_shdr): Only rename zdebug > input for linker. > (elf_fake_sections): Don't handle renaming of debug sections for > objcopy here. > * section.c (SEC_ELF_RENAME): Delete. > * bfd-in2.h: Regenerate. > binutils/ > * objcopy.c (setup_section): Call bfd_convert_section_setup. > Don't call bfd_convert_section_size. > > diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h > index 24f9305c47c..d983268563d 100644 > --- a/bfd/bfd-in2.h > +++ b/bfd/bfd-in2.h > @@ -932,10 +932,6 @@ typedef struct bfd_section > TMS320C54X only. */ > #define SEC_TIC54X_BLOCK 0x10000000 > > - /* This section should be renamed. This is for ELF linker > - internal use only. */ > -#define SEC_ELF_RENAME 0x10000000 > - > /* Conditionally link this section; do not link if there are no > references found to any symbol in the section. This is for TI > TMS320C54X only. */ > @@ -7982,8 +7978,9 @@ void bfd_update_compression_header > > int bfd_get_compression_header_size (bfd *abfd, asection *sec); > > -bfd_size_type bfd_convert_section_size > - (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size); > +bool bfd_convert_section_setup > + (bfd *ibfd, asection *isec, bfd *obfd, > + const char **new_name, bfd_size_type *new_size); > > bool bfd_convert_section_contents > (bfd *ibfd, asection *isec, bfd *obfd, > diff --git a/bfd/compress.c b/bfd/compress.c > index a4e6a8ee7b5..bb55a6ec0ac 100644 > --- a/bfd/compress.c > +++ b/bfd/compress.c > @@ -225,53 +225,89 @@ bfd_get_compression_header_size (bfd *abfd, asection *sec) > > /* > FUNCTION > - bfd_convert_section_size > + bfd_convert_section_setup > > SYNOPSIS > - bfd_size_type bfd_convert_section_size > - (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size); > + bool bfd_convert_section_setup > + (bfd *ibfd, asection *isec, bfd *obfd, > + const char **new_name, bfd_size_type *new_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}. > + Do early setup for objcopy, when copying @var{isec} in input > + BFD @var{ibfd} to output BFD @var{obfd}. Returns the name and > + size of the output section. > */ > > -bfd_size_type > -bfd_convert_section_size (bfd *ibfd, sec_ptr isec, bfd *obfd, > - bfd_size_type size) > +bool > +bfd_convert_section_setup (bfd *ibfd, asection *isec, bfd *obfd, > + const char **new_name, bfd_size_type *new_size) > { > bfd_size_type hdr_size; > > + if ((isec->flags & SEC_DEBUGGING) != 0 > + && (isec->flags & SEC_HAS_CONTENTS) != 0) > + { > + const char *name = *new_name; > + > + if ((ibfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)) != 0) > + { > + /* When we decompress or compress with SHF_COMPRESSED, > + convert section name from .zdebug_* to .debug_*. */ > + if (startswith (name, ".zdebug_")) > + { > + name = bfd_zdebug_name_to_debug (obfd, name); > + if (name == NULL) > + return false; > + } > + } > + > + /* PR binutils/18087: Compression does not always make a > + section smaller. So only rename the section when > + compression has actually taken place. If input section > + name is .zdebug_*, we should never compress it again. */ > + else if (isec->compress_status == COMPRESS_SECTION_DONE > + && startswith (name, ".debug_")) > + { > + name = bfd_debug_name_to_zdebug (obfd, name); > + if (name == NULL) > + return false; > + } > + *new_name = name; > + } > + *new_size = bfd_section_size (isec); > + > /* 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; > + 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 size; > + return true; > > /* Convert GNU property size. */ > if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME)) > - return _bfd_elf_convert_gnu_property_size (ibfd, obfd); > + { > + *new_size = _bfd_elf_convert_gnu_property_size (ibfd, obfd); > + return true; > + } > > /* Do nothing if input file will be decompressed. */ > if ((ibfd->flags & BFD_DECOMPRESS)) > - return size; > + return true; > > /* 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; > + return true; > > /* 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)); > + *new_size += sizeof (Elf64_External_Chdr) - sizeof (Elf32_External_Chdr); > else > - return (size - sizeof (Elf64_External_Chdr) > - + sizeof (Elf32_External_Chdr)); > + *new_size += sizeof (Elf32_External_Chdr) - sizeof (Elf64_External_Chdr); This doesn't work for 32-bit program since sizeof (Elf32_External_Chdr) - sizeof (Elf64_External_Chdr); is negative and will overflow. We should use *new_size -= sizeof (Elf64_External_Chdr) - sizeof (Elf32_External_Chdr); instead. OK for master? -- H.J. --000000000000893f9005ef2f1ee3 Content-Type: application/x-patch; name="0001-bfd-Avoid-signed-overflow-for-new_size-adjustment.patch" Content-Disposition: attachment; filename="0001-bfd-Avoid-signed-overflow-for-new_size-adjustment.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_lbcpgvgo0 RnJvbSA1MGRkNDUyZTI1MjhjNDMzZWE2N2UwYTIwNWY0ODBkMDYyNjA1MzU4IE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiAiSC5KLiBMdSIgPGhqbC50b29sc0BnbWFpbC5jb20+CkRhdGU6 IFR1ZSwgNiBEZWMgMjAyMiAxMjo1NDo0MyAtMDgwMApTdWJqZWN0OiBbUEFUQ0hdIGJmZDogQXZv aWQgc2lnbmVkIG92ZXJmbG93IGZvciBuZXdfc2l6ZSBhZGp1c3RtZW50CgpXaGVuIGJmZF9zaXpl X3R5cGUgaXM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