From patchwork Sun Aug 27 04:48:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 74786 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C0403858039 for ; Sun, 27 Aug 2023 04:49:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C0403858039 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693111771; bh=ngazLLWPzsvgA4QEaIbUedMmHGpb1MgOso/JWBy4u9o=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Dlegd2gO9r0tR6umoBg/TjMS0GDFkT45dLWqNBgELlzeI3vdHMrnXaUzsf6Gjcdju EvtI8iKiqZInuLYokxUu8bHOzG1H5RTDdjsxLnwhMw8EdnJ7AIgieGlMKlndoUFOMk YkGlu2XFhv2urUHMhOwLIbU4Kghx5SLwfd3SlagQ= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 7CA1B3858D3C for ; Sun, 27 Aug 2023 04:49:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7CA1B3858D3C Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1bf1935f6c2so14555575ad.1 for ; Sat, 26 Aug 2023 21:49:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693111741; x=1693716541; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ngazLLWPzsvgA4QEaIbUedMmHGpb1MgOso/JWBy4u9o=; b=FMVATxrjY04wnfbEiyxK8WfgCo+bFm9u9zXHI2L0fuLGlBHkKYqGVuIukDcD5CKuG5 PnbTHMIGbJSGs9DCHqz7QNaS+FXNeUkwgvbKtzQMbsTKt8MuFtRW2UolDZyYBivfbShv I5rPCCtnOSbUwE83u19wLby8qU/dqRNjZel0ELBfjOv0Q/1oa4wJ0uxsRkMPcCRBmXTT OVTyMgKMIyU51nZZ53j5lTWIkkRUlgPXrBd1F8445ArQh77G/3EN1esxcteUHWqkw3DK 1hPFfRc6KcOCPDjRa0yt0RHb5tNNE6hexqEAAKh3N0wg4mTKghUZFrSZOVIkvIbLPBny lrnw== X-Gm-Message-State: AOJu0Yxizky6pSLurJP7YbuzY48H8AjjI1SDWWGmIKOhL0csSALUfon/ j1yJDgHpdA8EYCr91mXkmfTKWDUkjgUfSA== X-Google-Smtp-Source: AGHT+IGXW6bDYha2dA0ASVMwJLUIRmhZZxlyPga7YQvawvXp0Yvzaa/Cyu+lDWzECFfNlABISXhQ2A== X-Received: by 2002:a17:902:d2cd:b0:1c1:d730:f0cf with SMTP id n13-20020a170902d2cd00b001c1d730f0cfmr2573063plc.20.1693111741007; Sat, 26 Aug 2023 21:49:01 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:bd86:4b0c:f073:c134]) by smtp.gmail.com with ESMTPSA id x2-20020a170902ea8200b001bb97e51ab4sm4553324plb.98.2023.08.26.21.49.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Aug 2023 21:49:00 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 3562E11423B7; Sun, 27 Aug 2023 14:18:58 +0930 (ACST) Date: Sun, 27 Aug 2023 14:18:58 +0930 To: binutils@sourceware.org Subject: comdat_hash memory leaks Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3033.7 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 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-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org Sender: "Binutils" Entries added to the hash table with bfd_malloc ought to be freed when the hash table is deleted. This patch adds the necessary del_f to the htab_create call, and delays creating the table until an IMAGE_SCN_LNK_COMDAT symbol is read. * peicode.h (pe_mkobject): Move comdat_hash creation.. (htab_hash_flags, htab_eq_flags): ..and these support functions.. * coffcode.h (handle_COMDAT): ..to here, renaming support to (comdat_hashf, comdat_eqf): ..this and adding.. (comdat_delf): ..this new function. diff --git a/bfd/coffcode.h b/bfd/coffcode.h index 2d40c5cfcac..03c1788a1c9 100644 --- a/bfd/coffcode.h +++ b/bfd/coffcode.h @@ -853,6 +853,29 @@ styp_to_sec_flags (bfd *abfd, #else /* COFF_WITH_PE */ +static hashval_t +comdat_hashf (const void *entry) +{ + const struct comdat_hash_entry *fe = entry; + return fe->target_index; +} + +static int +comdat_eqf (const void *e1, const void *e2) +{ + const struct comdat_hash_entry *fe1 = e1; + const struct comdat_hash_entry *fe2 = e2; + return fe1->target_index == fe2->target_index; +} + +static void +comdat_delf (void *ent) +{ + struct comdat_hash_entry *e = ent; + free (e->symname); + free (e); +} + static struct comdat_hash_entry * find_flags (htab_t comdat_hash, int target_index) { @@ -1085,6 +1108,14 @@ static bool handle_COMDAT (bfd *abfd, flagword *sec_flags, const char *name, asection *section) { + if (pe_data (abfd)->comdat_hash == NULL) + { + pe_data (abfd)->comdat_hash = htab_create (10, comdat_hashf, comdat_eqf, + comdat_delf); + if (pe_data (abfd)->comdat_hash == NULL) + return false; + } + if (htab_elements (pe_data (abfd)->comdat_hash) == 0) if (! fill_comdat_hash (abfd)) return false; diff --git a/bfd/peicode.h b/bfd/peicode.h index e1d06606575..1ff13b0313d 100644 --- a/bfd/peicode.h +++ b/bfd/peicode.h @@ -255,21 +255,6 @@ coff_swap_scnhdr_in (bfd * abfd, void * ext, void * in) #endif } -static hashval_t -htab_hash_flags (const void *entry) -{ - const struct comdat_hash_entry *fe = entry; - return fe->target_index; -} - -static int -htab_eq_flags (const void *e1, const void *e2) -{ - const struct comdat_hash_entry *fe1 = e1; - const struct comdat_hash_entry *fe2 = e2; - return fe1->target_index == fe2->target_index; -} - static bool pe_mkobject (bfd * abfd) { @@ -306,8 +291,6 @@ pe_mkobject (bfd * abfd) pe->dos_message[14] = 0x24; pe->dos_message[15] = 0x0; - pe->comdat_hash = htab_create (10, htab_hash_flags, htab_eq_flags, NULL); - memset (& pe->pe_opthdr, 0, sizeof pe->pe_opthdr); bfd_coff_long_section_names (abfd)