From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id 816B63858D32 for ; Thu, 13 Apr 2023 02:58:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 816B63858D32 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-pj1-x1034.google.com with SMTP id jx2-20020a17090b46c200b002469a9ff94aso11982682pjb.3 for ; Wed, 12 Apr 2023 19:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681354687; x=1683946687; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=bWtgzXHk2LygQS+YfjShXLqNtXwyh9oHET/+AsBavEQ=; b=gQetZU3CJZhzWKDL+Ui52t/Z+Rsn05T1bDnJLDCg0Ff1ZUxbfmMokiVWk/1UTP/wAX DVI1zTvHKgfACz/bEsvSY9kJGUai65sQekx18q7K+XJqr7r5HI2+oS+o/yERikm5NgpN NnOmKPDRmRKojkPIFrWo/tITCc0L6ldq1YTjyEmEfceS6mfO9Pxc/Aqeuf/LvKp+lmOv hdbglr4Ywl4OKIApYeqld3csCHGT5yw8rjJHbjOzX80Wu2zzkWnKeXZs1jahRdwoFPXj FIpMj5CRVjatNlzIKKu/nkbgze+REEhfnSKffB6yhEx5zvV1LBX6WtpowTAtVPL+9t7h DfHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681354687; x=1683946687; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=bWtgzXHk2LygQS+YfjShXLqNtXwyh9oHET/+AsBavEQ=; b=WR0lRfze9CpKRSr2IJTR0CltcapLQR1CS59SvW3W4IzW2shHdG49WjmTkrrbuk0Lmk Xk+SY/FlUuRGFlZAG2q4FQ+0McVfQZ639sDCLx2mDI/ysi0+Q8mBC9PO1/ND2F4lSMdT y1BbxC4NHvWRfNf1P23nPcl2NXxVAYnyuB2t6m2iYvnvlXIi+OR8ULgKJ4ImAqbkfQho WSHIRJk9X30u7qZl2pqKrw0VkIbvIy93L80iKf/9v800cQGCw1277SHfH+P67XxPksNJ sh51epYnywfRLLd6m8YzMLR735Tv/3DKStYyRnq/AQkuTPMDcFZVXu8gELJYr7b+dDr0 RwdQ== X-Gm-Message-State: AAQBX9dJkIHm6MQcY1108fP0JOsgp7xoQ1bgVjgD9mDkfDkc5jN+j5HR UkLkgsRDrhEGPzr3unnv39OYbm69ckY= X-Google-Smtp-Source: AKy350bAV6/AczWa0MF1dhGZ1VOELvEcHLyp97quGxzIWxfFroom52zvoecrvky0wJTc5nh/VFxh8w== X-Received: by 2002:a17:90b:368f:b0:23f:635e:51e9 with SMTP id mj15-20020a17090b368f00b0023f635e51e9mr374770pjb.8.1681354686896; Wed, 12 Apr 2023 19:58:06 -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 x2-20020a17090aa38200b0023b3d80c76csm265211pjp.4.2023.04.12.19.58.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 19:58:05 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id CC4531142C28; Thu, 13 Apr 2023 12:28:02 +0930 (ACST) Date: Thu, 13 Apr 2023 12:28:02 +0930 From: Alan Modra To: binutils@sourceware.org Subject: Re: pe_ILF_object_p and bfd_check_format_matches Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 List-Id: The last patch wasn't quite correct. bfd_preserve_restore also needs to handle an in-memory to file backed transition, seen in a testcase ILF object matching both pei-arm-little and pei-arm-wince-little. There the first match is saved in preserve_match, and restored at the end of the bfd_check_format_matches loop making the bfd in-memory. On finding more than one match the function wants to restore the bfd back to its original state with another bfd_preserve_restore call before exiting with a bfd_error_file_ambiguously_recognized error. It is also not correct to restore abfd->iostream unless the iovec changes. abfd->iostream is a FILE* when using cache_iovec, and if the file has been closed and reopened the iostream may have changed. * format.c (io_reinit): New function. (bfd_reinit, bfd_preserve_restore): Use it. diff --git a/bfd/format.c b/bfd/format.c index dd50b5e653a..66b45ae1979 100644 --- a/bfd/format.c +++ b/bfd/format.c @@ -144,6 +144,33 @@ bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, sizeof (struct section_hash_entry)); } +/* A back-end object_p function may flip a bfd from file backed to + in-memory, eg. pe_ILF_object_p. In that case to restore the + original IO state we need to reopen the file. Conversely, if we + are restoring a previously matched pe ILF format and have been + checking further target matches using file IO then we need to close + the file and detach the bfd from the cache lru list. */ + +static void +io_reinit (bfd *abfd, struct bfd_preserve *preserve) +{ + if (abfd->iovec != preserve->iovec) + { + /* Handle file backed to in-memory transition. bfd_cache_close + won't do anything unless abfd->iovec is the cache_iovec. */ + bfd_cache_close (abfd); + abfd->iovec = preserve->iovec; + 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 + && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0 + && (preserve->flags & BFD_IN_MEMORY) == 0) + bfd_open_file (abfd); + } + abfd->flags = preserve->flags; +} + /* Clear out a subset of BFD state. */ static void @@ -155,16 +182,7 @@ bfd_reinit (bfd *abfd, unsigned int section_id, cleanup (abfd); abfd->tdata.any = NULL; abfd->arch_info = &bfd_default_arch_struct; - 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; + io_reinit (abfd, preserve); abfd->build_id = NULL; bfd_section_list_clear (abfd); } @@ -178,11 +196,7 @@ 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; + io_reinit (abfd, preserve); abfd->section_htab = preserve->section_htab; abfd->sections = preserve->sections; abfd->section_last = preserve->section_last; -- Alan Modra Australia Development Lab, IBM