From patchwork Fri Dec 15 03:15:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 179025 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9010725dys; Thu, 14 Dec 2023 19:15:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCdGQ77pm8f8cTiF2bHgsmFIVQRTCDcfy+svy4n13UwqlTgBR+GiiJRsKelVA3CKW6lQ70 X-Received: by 2002:a05:622a:148c:b0:423:a4f0:382 with SMTP id t12-20020a05622a148c00b00423a4f00382mr17546166qtx.21.1702610123037; Thu, 14 Dec 2023 19:15:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702610123; cv=pass; d=google.com; s=arc-20160816; b=xwv6eWh+ilHwN86zRap/LDT8jyjFqZCG4oplbg3Fj5yJylMe091FZcP3K1anwCT+NF BIYhz85U/1evTFBLkGtV+BmysOTXa2L04fpnCdXXqnMnnYGPpQc4nxJpMWBPkZAAcmmW 4zkrZj1B+ZhOFgvvT07WdXIuUzRD29B/4iQVpJwG6fMYHUDEVy7l+6hERx97LRE/5dkC g2C1lRl3CJIb8jDJ5UkF4cYb4c1FicwV4ShDjnLtI3veZiBq76BplCYYdQwA/02e8DIC Yxlf6Po6uFLiXlD6GAHXOEYETmCtuN7iqkHW9A1j/5N9WlU5C/i3zqgK+tq970LD+bCX d4Dg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=lHDwmw2sljOZrj0kQiTAVKTvzkGyhN4kHB6qZNQRxf8=; fh=NLxAvL/bDfPg4AGOtxqvQlND8vazkZrNzKLY8+LAbBY=; b=GMKme6cFL4xcN11gu7/l6LFmwnanVJZN476Rsef2i1NTUmKTX3WiMZf52HmjVcZwZp 5ZMP1QZZ+VrfosX+oqmyZTlv3zc3eY/f9qxroIg2ZtJ/QiH93Eu0S6jJmaICOMYukAtI pgdDKdnAfAkGF30GlA0vhH8brfmTKqbvBtHMoDJg4HauFM/7RPMTMQLi3gAfhESohLKX 8uWPGnf0nUwP4i/0MBDBFmU3NgFLRZeCjmn6iBFvztaortKlqNRviD0OO1cSz78GeDWZ gyQSwSjNyegJrfV2icT9v2+NLDEWk+TJwmjORwzySwANGC+MEUhkyle9wvxfiTGo8hlz FutA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mYkHMlzh; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id fi3-20020a05622a58c300b004237f64dec8si7539789qtb.528.2023.12.14.19.15.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 19:15:23 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mYkHMlzh; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B6E193848345 for ; Fri, 15 Dec 2023 03:15:22 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id CB713384CB83 for ; Fri, 15 Dec 2023 03:15:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB713384CB83 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CB713384CB83 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702610116; cv=none; b=PlL8/lBMbRyv8clXzHH9PbY41hxQdK0+Ey8ZyKmnucZQDBJ1NLDgaL3z+Fi7e1Eo7RMnf8uYadMMEBqYFerMLlZESfIi6yS76HWWIgWtO2hxPxL9QO19gQpXOS9XS8XfA3ZukN33978CNkyWP6qG+nd43BmNLSufDvkKSHceMPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702610116; c=relaxed/simple; bh=0WqkH5KSGqz0ZvM8kKxSOdHgf0uQQBR+YXjmPrw496o=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=p4ofZSRMMfq3QziIN0x7VVtO0LC3c3iVTxX2kuZD9cBojW1fbrcfvE1pRwyoUSwLuBA1R39OO5CunLFABLAjQWbRYbYRP46YYU1plp9rt9rj2kHpO0SYuVkBQnJvA2zK1z3W0ZHElJnh0j625bvmiwpw5H7YqwwC73miEFnjoZo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1d04c097e34so1343615ad.0 for ; Thu, 14 Dec 2023 19:15:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702610113; x=1703214913; darn=sourceware.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=lHDwmw2sljOZrj0kQiTAVKTvzkGyhN4kHB6qZNQRxf8=; b=mYkHMlzhk0U2A0R70mwsK9rvCASoXE5NCTLGEi48d9nYZbRqdpz4BjRvTNFU/V5SGm 82aFcLV0kqrcD8iu/c+Mo3NZLb+Tp1aepmM519kB9BLjzMcjN3ab4kLLe5kKX4F94JJj n/YIu0x+6tyeeqFtWhVYgK/rwgoG54NjZrBzi9qt9EF0BmEcfzEsNVuwHZgwwDyBPjse YZJr821rODoZwQVxysYeBwvWcD59/o89efqM670Rp0t032ICPBRkFse/vxcrtujCtDrh 20PMUSzdcY3umpMn1yV62Ym5jS6U7KVH5i3avnU4w9FQf9UjVA3dUKcdzjmVbU1WEmME /lfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702610113; x=1703214913; 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=lHDwmw2sljOZrj0kQiTAVKTvzkGyhN4kHB6qZNQRxf8=; b=KAD5aNCs/ASnkgCCksT6uF+KPgmUbR8zqaJ710gE8dR8vII+xKHaXl3JMqdgu/VMtY 63wvNUFj2R7yOVUJVeW+dZkCoiFQqO6EMVHMzW3+I8AzQ3guB26vpSuc0mXHkVzh1MrD AAVGZy5xbC8PQZAkZkHA9GX+80F515HwoA4fQkTCpsM3XSxRZD40ABIgcC/MyJi6rZc2 JzG/NXKoJ3KEDZhy7phjigaCwzECgz+6ibVGszC3a7xZhWHOa8KsCnANynOqVJCRz9cq vxlYlaQouyl2A7cno2VvBs0f11sXuC5jIZ57JIZTGrhYa0WIhr4HDe02AGUiuNo8uJ+f 0JfQ== X-Gm-Message-State: AOJu0YxLWRKCMQ/TxJbz9ikAy82olT4MOZKlxltGsrq2TzlvCYd1jBuV XwKvmCYS+S68bZc3K5uMwql0taEMATw= X-Received: by 2002:a17:903:687:b0:1d0:af28:3602 with SMTP id ki7-20020a170903068700b001d0af283602mr10104043plb.59.1702610112890; Thu, 14 Dec 2023 19:15:12 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:6f67:2bbb:cce6:f1f9]) by smtp.gmail.com with ESMTPSA id g1-20020a170902fe0100b001d0ab572458sm1846708plj.121.2023.12.14.19.15.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 19:15:12 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 1C3C811423EB; Fri, 15 Dec 2023 13:45:10 +1030 (ACDT) Date: Fri, 15 Dec 2023 13:45:10 +1030 From: Alan Modra To: binutils@sourceware.org Subject: PR31145, potential memory leak in binutils/ld Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3033.5 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.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785316112296166841 X-GMAIL-MSGID: 1785316112296166841 PR 31145 * bfd.c (BFD_IN_MEMORY): Mention that bim is malloc'd. * format.c (io_reinit): Free BFD_IN_MEMORY iostream. * opncls.c (_bfd_delete_bfd): Likewise. (bfd_make_readable): Delete unnecessary code. * bfd-in2.h: Regenerate. diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 040d5560cdf..2807e694b8f 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -2035,8 +2035,8 @@ struct bfd #define BFD_TRADITIONAL_FORMAT 0x400 /* This flag indicates that the BFD contents are actually cached - in memory. If this is set, iostream points to a bfd_in_memory - struct. */ + in memory. If this is set, iostream points to a malloc'd + bfd_in_memory struct. */ #define BFD_IN_MEMORY 0x800 /* This BFD has been created by the linker and doesn't correspond diff --git a/bfd/bfd.c b/bfd/bfd.c index 616ded3f4be..a5df4ef0311 100644 --- a/bfd/bfd.c +++ b/bfd/bfd.c @@ -160,8 +160,8 @@ CODE_FRAGMENT .#define BFD_TRADITIONAL_FORMAT 0x400 . . {* This flag indicates that the BFD contents are actually cached -. in memory. If this is set, iostream points to a bfd_in_memory -. struct. *} +. in memory. If this is set, iostream points to a malloc'd +. bfd_in_memory struct. *} .#define BFD_IN_MEMORY 0x800 . . {* This BFD has been created by the linker and doesn't correspond diff --git a/bfd/format.c b/bfd/format.c index 66dc2e7e51e..31aeb52861a 100644 --- a/bfd/format.c +++ b/bfd/format.c @@ -166,7 +166,14 @@ io_reinit (bfd *abfd, struct bfd_preserve *preserve) won't do anything unless abfd->iovec is the cache_iovec. */ bfd_cache_close (abfd); abfd->iovec = preserve->iovec; - abfd->iostream = preserve->iostream; + + if (abfd->iostream != preserve->iostream) + { + if ((abfd->flags & BFD_IN_MEMORY) != 0) + free (abfd->iostream); + abfd->iostream = preserve->iostream; + } + /* Handle in-memory to file backed transition. */ if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0 && (abfd->flags & BFD_IN_MEMORY) != 0 diff --git a/bfd/opncls.c b/bfd/opncls.c index 5a77562744c..e7b39593608 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -176,6 +176,8 @@ _bfd_delete_bfd (bfd *abfd) else free ((char *) bfd_get_filename (abfd)); + if ((abfd->flags & BFD_IN_MEMORY) != 0) + free (abfd->iostream); free (abfd->arelt_data); free (abfd); } @@ -1064,7 +1066,6 @@ bfd_make_readable (bfd *abfd) abfd->section_count = 0; abfd->usrdata = NULL; abfd->cacheable = false; - abfd->flags |= BFD_IN_MEMORY; abfd->mtime_set = false; abfd->target_defaulted = true;