From patchwork Wed Apr 12 01:32:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 82224 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2974910vqo; Tue, 11 Apr 2023 18:33:02 -0700 (PDT) X-Google-Smtp-Source: AKy350b7cSmL6YR9F2GZitS9QpbFFJXFutoYlmMfoDXkgFDvnKfB6BaPEg3tk7SLnUxDCUqF9axv X-Received: by 2002:a17:906:ae9a:b0:94a:5c6c:e19a with SMTP id md26-20020a170906ae9a00b0094a5c6ce19amr1077136ejb.19.1681263181915; Tue, 11 Apr 2023 18:33:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681263181; cv=none; d=google.com; s=arc-20160816; b=V+QgrQ+4aRswDJDb11H2WP2tMwfzGGeDrloNEguwn/c05PtLgbqBYpwA7zsnH8Jilc 9NvpSsL+lk9m0zROGCcmiTIuOKIuB70VJUe8UGM2K0rB98up+Mx3SmgNzzt8vha7cxJt W3jbyojil5MOZ2/g0Gy8MnHY/qe+tYIvf/pdzKTFexA/6fcg/T1GwQ+1hQB4Pw5dKAme z292V6MzgYNpZ0o0MIKhWGgkd6fyrfOoDz9cBMgn0EjgObgWiDy+EQuN6Y9GF0uSw4vH fiYgdS3eXaBwruGluwGtu/fiEEIbUe/YReUrvgeBvqZQ/QpyTH8DmNIDgiSULa2BLYWl uOrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PbfcTjL8PfaD9plon9xOs/btcjKHjJ73TSI+uR/JfKo=; b=VWqTLZBc11Y0JtdtY1fuBhRNv6gKNpaHoSu2yJXPKpEOfvI2A2Xeo9p5D81NqsHnSE JOR3aSRs4MtuQAL+Z+150c5SzaMR10lTNCn6AfB6LtT/LBxrTSYqcYLuHDk8MCOAzsmC /BgC7JRvOFKfQ9ANZKB1EXl0o3RIw/CSrjCCmrQz8ns42eVtt0XMqeeZxtrFB49Zyeoz UJ9X45ZlcSsC5oOUQjpd9EATVXF2K55xh53M0b9xs+vtTBgJawmFouCf0jTXJWzE97nB wPLTJ28riMWfZ+iRcELkmXw50xZLd0b/KL+TAy01R7R3byBncn2Y3VpK+bjQ6jh+8SlE tD0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ZkEqvKc3; 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=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s19-20020a17090699d300b0094aa4526ad4si2853225ejn.1055.2023.04.11.18.33.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 18:33:01 -0700 (PDT) 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=@sourceware.org header.s=default header.b=ZkEqvKc3; 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=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A40143858439 for ; Wed, 12 Apr 2023 01:32:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A40143858439 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681263178; bh=PbfcTjL8PfaD9plon9xOs/btcjKHjJ73TSI+uR/JfKo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ZkEqvKc3M6DNtvJsF5O1wVspfjLQOqXdDerTQN2vmJMeEgYizaQCPtFhl3nASBoM0 sjo0zsKgnof+Gs+Jmlyz/SEAM/ho3k1Y+M/Z1080FQoG8eeJner7Cm0x3aSvs6hX0Y 5B2bdVBtOsU2zMewrvTZ1CwPjx8Y/wi3FpAriA3w= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 8C3A1385840D for ; Wed, 12 Apr 2023 01:32:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C3A1385840D Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6323e36064aso956499b3a.1 for ; Tue, 11 Apr 2023 18:32:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681263168; x=1683855168; 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=PbfcTjL8PfaD9plon9xOs/btcjKHjJ73TSI+uR/JfKo=; b=yPJ8VIOW/cSVmhtRMiStQhcWS9GIA2HasOBtgB0dpiTb8zT0Vzwy0sWVco4DQjQK57 +wkiF3rZBA4LljCZUdHdKBcUG8Ow9OiJXq8K2IfxVd1r7vHg0aRIh7BheGtvo6tQEApN EWsnxrTOKR8dDnGXKR8NUBw+X9AZkhMp7gy4BAfwBa4pzBD3bLzAgS8s284DODaGWYLA rZ1dj3+nrCHnJuBcNYPKwyDebwPyDWFvITr1EtzpbXSl/yGft30Pcu3bnKI2mxSe98Ec 7q9lbm95q2ZBHpigZ0x8EjbsdG4BhuJurI6Pd4AR9RTukJx+mo1xaIY0yMMFtrhRBHbj mTTA== X-Gm-Message-State: AAQBX9dAF5x9+4FE3eEzU2LX940IeYMVvNLdEBWbBVh40yZvi304Ersl MMOlx5XQKIrNhQHUfzpsI2ngK53609o= X-Received: by 2002:aa7:980c:0:b0:5a8:b2bf:26ac with SMTP id e12-20020aa7980c000000b005a8b2bf26acmr861241pfl.20.1681263167926; Tue, 11 Apr 2023 18:32:47 -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 v15-20020aa7850f000000b0063aa1763146sm2743483pfn.17.2023.04.11.18.32.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 18:32:47 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id CBB8511432A7; Wed, 12 Apr 2023 11:02:44 +0930 (ACST) Date: Wed, 12 Apr 2023 11:02:44 +0930 To: binutils@sourceware.org Subject: pe_ILF_object_p and bfd_check_format_matches Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.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 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+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762932222173487165?= X-GMAIL-MSGID: =?utf-8?q?1762932222173487165?= If pe_ILF_object_p succeeds, pe_ILF_build_a_bfd will have changed the bfd from being file backed to in-memory. This can have unfortunate results for targets checked by bfd_check_format_matches after that point as they will be matching against the created in-memory image rather than the file. bfd_preserve_restore also has a problem if it flips the BFD_IN_MEMORY flag, because the flag affects iostream meaning and should be set if using _bfd_memory_iovec. To fix these problems, save and restore iostream and iovec along with flags, and modify bfd_reinit to make the bfd file backed again. Restoring the iovec and iostream allows the hack in bfd_reinit keeping BFD_IN_MEMORY (part of BFD_FLAGS_SAVED) to be removed. One more detail: If restoring from file backed to in-memory then the bfd needs to be forcibly removed from the cache lru list, since after the bfd becomes in-memory a bfd_close will delete the bfd's memory leaving the lru list pointing into freed memory. * cache.c (bfd_cache_init): Clear BFD_CLOSED_BY_CACHE here.. (bfd_cache_lookup_worker): ..rather than here. (bfd_cache_close): Comment. * format.c (struct bfd_preserve): Add iovec and iostream fields. (bfd_preserve_save): Save them.. (bfd_preserve_restore): ..and restore them, calling bfd_cache_close if the iovec differs. (bfd_reinit): Add preserve param. If the bfd has been flipped to in-memory, reopen the file. Restore flags. * peicode.h (pe_ILF_cleanup): New function. (pe_ILF_object_p): Return it. * bfd.c (BFD_FLAGS_SAVED): Delete. * bfd-in2.h: Regenerate. diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index b60ff960f08..a04e97eda67 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -6622,12 +6622,6 @@ struct bfd /* Compress sections in this BFD with SHF_COMPRESSED zstd. */ #define BFD_COMPRESS_ZSTD 0x400000 - /* Flags bits to be saved in bfd_preserve_save. */ -#define BFD_FLAGS_SAVED \ - (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \ - | BFD_PLUGIN | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON \ - | BFD_USE_ELF_STT_COMMON | BFD_COMPRESS_ZSTD) - /* Flags bits which are for BFD use only. */ #define BFD_FLAGS_FOR_BFD_USE_MASK \ (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \ diff --git a/bfd/bfd.c b/bfd/bfd.c index 3624bfbc9a5..650df1c79ed 100644 --- a/bfd/bfd.c +++ b/bfd/bfd.c @@ -181,12 +181,6 @@ CODE_FRAGMENT . {* Compress sections in this BFD with SHF_COMPRESSED zstd. *} .#define BFD_COMPRESS_ZSTD 0x400000 . -. {* Flags bits to be saved in bfd_preserve_save. *} -.#define BFD_FLAGS_SAVED \ -. (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \ -. | BFD_PLUGIN | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON \ -. | BFD_USE_ELF_STT_COMMON | BFD_COMPRESS_ZSTD) -. . {* Flags bits which are for BFD use only. *} .#define BFD_FLAGS_FOR_BFD_USE_MASK \ . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \ diff --git a/bfd/cache.c b/bfd/cache.c index ab36c8506bd..3b91cce2307 100644 --- a/bfd/cache.c +++ b/bfd/cache.c @@ -266,10 +266,7 @@ bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag) && !(flag & CACHE_NO_SEEK_ERROR)) bfd_set_error (bfd_error_system_call); else - { - abfd->flags &= ~BFD_CLOSED_BY_CACHE; - return (FILE *) abfd->iostream; - } + return (FILE *) abfd->iostream; /* xgettext:c-format */ _bfd_error_handler (_("reopening %pB: %s"), @@ -506,6 +503,7 @@ bfd_cache_init (bfd *abfd) } abfd->iovec = &cache_iovec; insert (abfd); + abfd->flags &= ~BFD_CLOSED_BY_CACHE; ++open_files; return true; } @@ -528,6 +526,7 @@ DESCRIPTION bool bfd_cache_close (bfd *abfd) { + /* Don't remove this test. bfd_reinit depends on it. */ if (abfd->iovec != &cache_iovec) return true; diff --git a/bfd/format.c b/bfd/format.c index 5ad4190d5c4..dd50b5e653a 100644 --- a/bfd/format.c +++ b/bfd/format.c @@ -99,6 +99,8 @@ struct bfd_preserve void *marker; void *tdata; flagword flags; + const struct bfd_iovec *iovec; + void *iostream; const struct bfd_arch_info *arch_info; struct bfd_section *sections; struct bfd_section *section_last; @@ -125,6 +127,8 @@ bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, preserve->tdata = abfd->tdata.any; preserve->arch_info = abfd->arch_info; preserve->flags = abfd->flags; + preserve->iovec = abfd->iovec; + preserve->iostream = abfd->iostream; preserve->sections = abfd->sections; preserve->section_last = abfd->section_last; preserve->section_count = abfd->section_count; @@ -143,14 +147,24 @@ bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, /* Clear out a subset of BFD state. */ static void -bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup) +bfd_reinit (bfd *abfd, unsigned int section_id, + struct bfd_preserve *preserve, bfd_cleanup cleanup) { _bfd_section_id = section_id; if (cleanup) cleanup (abfd); abfd->tdata.any = NULL; abfd->arch_info = &bfd_default_arch_struct; - abfd->flags &= BFD_FLAGS_SAVED; + if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0 + && (abfd->flags & BFD_IN_MEMORY) != 0 + && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0 + && (preserve->flags & BFD_IN_MEMORY) == 0) + { + /* This is to reverse pe_ILF_build_a_bfd, which closes the file + and sets up a bfd in memory. */ + bfd_open_file (abfd); + } + abfd->flags = preserve->flags; abfd->build_id = NULL; bfd_section_list_clear (abfd); } @@ -164,7 +178,11 @@ bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) abfd->tdata.any = preserve->tdata; abfd->arch_info = preserve->arch_info; + if (abfd->iovec != preserve->iovec) + bfd_cache_close (abfd); abfd->flags = preserve->flags; + abfd->iovec = preserve->iovec; + abfd->iostream = preserve->iostream; abfd->section_htab = preserve->section_htab; abfd->sections = preserve->sections; abfd->section_last = preserve->section_last; @@ -368,7 +386,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) /* If we already tried a match, the bfd is modified and may have sections attached, which will confuse the next _bfd_check_format call. */ - bfd_reinit (abfd, initial_section_id, cleanup); + bfd_reinit (abfd, initial_section_id, &preserve, cleanup); /* Free bfd_alloc memory too. If we have matched and preserved a target then the high water mark is that much higher. */ if (preserve_match.marker) @@ -527,7 +545,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) RIGHT_TARG again. */ if (match_targ != right_targ) { - bfd_reinit (abfd, initial_section_id, cleanup); + bfd_reinit (abfd, initial_section_id, &preserve, cleanup); bfd_release (abfd, preserve.marker); if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) goto err_ret; diff --git a/bfd/peicode.h b/bfd/peicode.h index f16aeca7a1b..e2e2be65b5d 100644 --- a/bfd/peicode.h +++ b/bfd/peicode.h @@ -1158,6 +1158,17 @@ pe_ILF_build_a_bfd (bfd * abfd, return false; } +/* Cleanup function, returned from check_format hook. */ + +static void +pe_ILF_cleanup (bfd *abfd) +{ + struct bfd_in_memory *bim = abfd->iostream; + free (bim->buffer); + free (bim); + abfd->iostream = NULL; +} + /* We have detected an Import Library Format archive element. Decode the element and return the appropriate target. */ @@ -1331,7 +1342,7 @@ pe_ILF_object_p (bfd * abfd) return NULL; } - return _bfd_no_cleanup; + return pe_ILF_cleanup; } static void