From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by sourceware.org (Postfix) with ESMTPS id 6B392385BAF8 for ; Mon, 4 Jul 2022 13:23:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6B392385BAF8 Received: by mail-pj1-x1036.google.com with SMTP id h9-20020a17090a648900b001ecb8596e43so9593073pjj.5 for ; Mon, 04 Jul 2022 06:23:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=ZUvuaoDChMf231KTrxfBjt2Ny4dGpWDtf3WgLSVSNbA=; b=I6PkDsoWOhiTznh27B9SylT2O94w6OkGEIEmR8RXT2+BelAxH9xN5dx+ZZfvquBZ3s LI5CTjs+KrvstFBZ2ccutR/2uwlpVfpHw0VHVUE3191q7K0aAreMErF5LGHHl1LeHoTB gb3mBqCVdMlAA25y36TpQLOfU/SoCNJC/ok1uormkUZ9WgG0pbw3Hm2omhyK/v03EL8W +WTnRBeXrdu2ihsob1ys0Am9tvimL0k0XhkvBCNwNf+WAMu8f2n3ZCN2CG/oxExQnPLG ek/AGv+uOv/q/xo1LBiAHCyRArBVBZyyH/M6qr57y1RY6dcJZ+DQJG3o5Jw9x2hqE/v8 FLNg== X-Gm-Message-State: AJIora/V3gLYgMRh7t4GnEQXuFjSlS6AR0F7XrUurQ8Xi/h4bFmJ6nks P7zgaW+GP8dk+wqS9abLdj7js/KP3lc= X-Google-Smtp-Source: AGRyM1vAGgFE1lU2UfevsWOw6EzCeOvJYcMQnP/6irMpCQe4wDgRjcB9hqu/EvwjgN3dei5/9myfmw== X-Received: by 2002:a17:902:db08:b0:16a:4b7e:af5f with SMTP id m8-20020a170902db0800b0016a4b7eaf5fmr36310957plx.143.1656940981079; Mon, 04 Jul 2022 06:23:01 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id z185-20020a6265c2000000b00528669a770esm2232774pfb.90.2022.07.04.06.23.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:23:00 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id A2B231140459; Mon, 4 Jul 2022 22:52:57 +0930 (ACST) Date: Mon, 4 Jul 2022 22:52:57 +0930 From: Alan Modra To: binutils@sourceware.org Subject: objcopy: bfd_alloc orelocation Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3037.0 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Jul 2022 13:23:04 -0000 This fixes an inconsequential objcopy memory leak. I'd normally ignore reports of leaks like this one, that are merely one block or fewer per section processed, since objcopy soon exits and frees all memory. However I thought it worth providing support for allocating memory on a bfd objalloc in objcopy and other utils. PR 29233 * bucomm.c (bfd_xalloc): New function. * bucomm.h (bfd_xalloc): Declare. * objcopy.c (copy_relocations_in_section): Use it to allocate array of reloc pointers. Rewrite code stripping relocs to do without extra memory allocation. diff --git a/binutils/bucomm.c b/binutils/bucomm.c index 4395cb9f7f5..58341905587 100644 --- a/binutils/bucomm.c +++ b/binutils/bucomm.c @@ -142,6 +142,19 @@ non_fatal (const char *format, ...) va_end (args); } +/* Like xmalloc except that ABFD's objalloc memory is returned. + Use objalloc_free_block to free this memory and all more recently + allocated, or more usually, leave it to bfd_close to free. */ + +void * +bfd_xalloc (bfd *abfd, size_t size) +{ + void *ret = bfd_alloc (abfd, size); + if (ret == NULL) + bfd_fatal (NULL); + return ret; +} + /* Set the default BFD target based on the configured target. Doing this permits the binutils to be configured for a particular target, and linked against a shared BFD library which was configured for a diff --git a/binutils/bucomm.h b/binutils/bucomm.h index 48b2c50aced..a1814cbbcc2 100644 --- a/binutils/bucomm.h +++ b/binutils/bucomm.h @@ -39,6 +39,8 @@ void fatal (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN; void non_fatal (const char *, ...) ATTRIBUTE_PRINTF_1; +void *bfd_xalloc (bfd *, size_t); + void set_default_bfd_target (void); void list_matching_formats (char **); diff --git a/binutils/objcopy.c b/binutils/objcopy.c index df87712df98..37435733d76 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -4336,14 +4336,13 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) } else { - relpp = (arelent **) xmalloc (relsize); + relpp = bfd_xalloc (obfd, relsize); relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); if (relcount < 0) { status = 1; bfd_nonfatal_message (NULL, ibfd, isection, _("relocation count is negative")); - free (relpp); return; } } @@ -4352,34 +4351,24 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) { /* Remove relocations which are not in keep_strip_specific_list. */ - arelent **temp_relpp; - long temp_relcount = 0; + arelent **w_relpp; long i; - temp_relpp = (arelent **) xmalloc (relsize); - for (i = 0; i < relcount; i++) - { - /* PR 17512: file: 9e907e0c. */ - if (relpp[i]->sym_ptr_ptr - /* PR 20096 */ - && * relpp[i]->sym_ptr_ptr) - if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr), - keep_specific_htab)) - temp_relpp [temp_relcount++] = relpp [i]; - } - relcount = temp_relcount; - if (relpp != isection->orelocation) - free (relpp); - relpp = temp_relpp; + for (w_relpp = relpp, i = 0; i < relcount; i++) + /* PR 17512: file: 9e907e0c. */ + if (relpp[i]->sym_ptr_ptr + /* PR 20096 */ + && *relpp[i]->sym_ptr_ptr + && is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr), + keep_specific_htab)) + *w_relpp++ = relpp[i]; + relcount = w_relpp - relpp; + *w_relpp = 0; } bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount); if (relcount == 0) - { - osection->flags &= ~SEC_RELOC; - if (relpp != isection->orelocation) - free (relpp); - } + osection->flags &= ~SEC_RELOC; } } -- Alan Modra Australia Development Lab, IBM