From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 89DE1382E4F9 for ; Mon, 14 Nov 2022 01:43:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 89DE1382E4F9 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-pf1-x42f.google.com with SMTP id k22so9732425pfd.3 for ; Sun, 13 Nov 2022 17:43:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=o7gA23vIqn8CEOZIaQNe5wmmWKuvds0qjV9ZCYgS24M=; b=Vihi9YTwwnCzgFvQR5SE6eW8JCmPVPea4VGPONvQKO9pjXFonolmxHV9soNOGMnYQN wO+Gpp3vsZCOLUygaiUBcwLzRRUmiVNBEhCXwD6npjsa13Sq3wB/P1O6stRuJEdTDWRa RRCWC7lV1vPzKSIp21HDT2Q8Iu2opMZeHUx4zc2eKp6LVA4UPvDskhm79tn54UO+lfX+ I9ifT6KBPi2MovZ4/0u0tIg3Uq5QYJJAokGv7FGPDn9Gd76t8mLxqcrikZNj8sFrwuVr rM2/mT5SxtkCGoNox/55wkQvZyBaoQkk+LuFtdHlJGRj2+I8eTmdwWHcqvpSMGoM/l9h YSQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=o7gA23vIqn8CEOZIaQNe5wmmWKuvds0qjV9ZCYgS24M=; b=SXMqe6dYcShK77Ncev/GetGL6BYszMRakbhbd6tISR7PscnodPyx0LuSBShEU1S1/o Da1lDES8O1uyEJjF7686nH9afXVl5D+NRT1W21gApzuUrJ1jpEpb+54eNrOslZJMOEBN cEdoLiKrhgU41R2z20UMuVtn3xIJzAFcga50raSlMlRv50cmIDUZ6fQcw/Umjg1jYcjb c1ZGj/9+4XI+JbRZuqGBQzbLm+HRaevtSkUXCG0eHaigjOadT2XL5JIDlQbDu3sbyoBw qTjCeJmYjykN/QVsxu/9xMJRjZ0syb0MjFqSVlAGHy2JjbVyGk0immbAQNNnnsBQQiS4 +S/w== X-Gm-Message-State: ANoB5pk1dTWuCjEgj33nkWriIlylhLqnOaHb/4cYQ0turdRFkxVYNxUQ qQ/5KGGjb++AIKj/YzLJKds/BMU9jaQ= X-Google-Smtp-Source: AA0mqf4mqj54D32WnK1EDYYWCTlZmUEgsN0ZYwxi9gNprL7ZJVKRztEHY/JhNCMDxJOTGkK2eNCcqw== X-Received: by 2002:a62:6385:0:b0:56b:49f7:491d with SMTP id x127-20020a626385000000b0056b49f7491dmr11805894pfb.64.1668390190483; Sun, 13 Nov 2022 17:43:10 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:e362:69b6:8203:5ee6]) by smtp.gmail.com with ESMTPSA id 17-20020a170902e9d100b00177efb56475sm5871328plk.85.2022.11.13.17.43.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 17:43:09 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 3300A1142E77; Mon, 14 Nov 2022 12:13:07 +1030 (ACDT) Date: Mon, 14 Nov 2022 12:13:07 +1030 From: Alan Modra To: Patrick Monnerat Cc: binutils@sourceware.org Subject: Re: [PATCH] binutils/objcopy: keep relocation while renaming a section with explicit flags Message-ID: References: <20221112124441.5084-1-patrick@monnerat.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221112124441.5084-1-patrick@monnerat.net> X-Spam-Status: No, score=-3035.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 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: On Sat, Nov 12, 2022 at 01:44:41PM +0100, Patrick Monnerat via Binutils wrote: > When explicit flags are listed in a --rename-section option, they > override the input section's flags. > There is no way to list the "reloc" flag explicitly, thus the original > one must be kept. Failure to do so causes the section's relocation to > be dropped. > This patch merges the original SEC_RELOC flag within the renamed section > flags. It also introduces a test case for it. Thanks for working on this, but I'm going to apply a different patch. SEC_RELOC is an odd flag, mostly redundant. I think it's better to handle setting or clearing the flag in BFD. The testcase needed tweaking too. Some targets don't support named sections or have different names for .data, and some targets reduce the reloc symbol "foo" to a section symbol. bfd/ * reloc.c (_bfd_generic_set_reloc): Set/clear SEC_RELOC depending on reloc count. * elf64-sparc.c (elf64_sparc_set_reloc): Likewise. binutils/ * objcopy.c (copy_relocations_in_section): Remove now unnecessary clearing of SEC_RELOC. * testsuite/binutils-all/rename-section-01.d: New test. * testsuite/binutils-all/objcopy.exp: Run it. gas/ * write.c (size_seg): Remove unneccesary twiddle of SEC_RELOC. (write_relocs): Likewise. Always call bfd_set_reloc. diff --git a/bfd/elf64-sparc.c b/bfd/elf64-sparc.c index fb4483dcd17..c6d0d3e5b0a 100644 --- a/bfd/elf64-sparc.c +++ b/bfd/elf64-sparc.c @@ -322,6 +322,10 @@ elf64_sparc_set_reloc (bfd *abfd ATTRIBUTE_UNUSED, { asect->orelocation = location; canon_reloc_count (asect) = count; + if (count != 0) + asect->flags |= SEC_RELOC; + else + asect->flags &= ~SEC_RELOC; } /* Write out the relocs. */ diff --git a/bfd/reloc.c b/bfd/reloc.c index 89b6f7fd352..6446acc7a30 100644 --- a/bfd/reloc.c +++ b/bfd/reloc.c @@ -8706,6 +8706,10 @@ _bfd_generic_set_reloc (bfd *abfd ATTRIBUTE_UNUSED, { section->orelocation = relptr; section->reloc_count = count; + if (count != 0) + section->flags |= SEC_RELOC; + else + section->flags &= ~SEC_RELOC; } /* diff --git a/binutils/objcopy.c b/binutils/objcopy.c index d886e3ae343..3d886240ce1 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -4331,10 +4331,7 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) } if (relsize == 0) - { - bfd_set_reloc (obfd, osection, NULL, 0); - osection->flags &= ~SEC_RELOC; - } + bfd_set_reloc (obfd, osection, NULL, 0); else { if (isection->orelocation != NULL) @@ -4377,8 +4374,6 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) } bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount); - if (relcount == 0) - osection->flags &= ~SEC_RELOC; } } diff --git a/binutils/testsuite/binutils-all/objcopy.exp b/binutils/testsuite/binutils-all/objcopy.exp index 5871d431eea..aebfdb2090b 100644 --- a/binutils/testsuite/binutils-all/objcopy.exp +++ b/binutils/testsuite/binutils-all/objcopy.exp @@ -1448,3 +1448,5 @@ if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } { if { ![is_xcoff_format] } { objcopy_test "pr25662" $src executable "" $ldflags } + +run_dump_test "rename-section-01" diff --git a/binutils/testsuite/binutils-all/rename-section-01.d b/binutils/testsuite/binutils-all/rename-section-01.d new file mode 100644 index 00000000000..21d901326c6 --- /dev/null +++ b/binutils/testsuite/binutils-all/rename-section-01.d @@ -0,0 +1,14 @@ +#PROG: objcopy +#name: objcopy rename-section with flags - keep relocation +#source: needed-by-reloc.s +#objcopy: --rename-section .data=myrodata,contents,alloc,load,readonly +#objdump: -r +#notarget: alpha*-*-*vms* rx-*-elf [is_som_format] [is_aout_format] + +.*: +file format .* + +#... +RELOCATION RECORDS FOR .*myrodata.*: +OFFSET +TYPE +VALUE +0+ .* +#pass diff --git a/gas/write.c b/gas/write.c index 1c1b8104222..3014f687949 100644 --- a/gas/write.c +++ b/gas/write.c @@ -579,7 +579,6 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED) if (size > 0 && ! seginfo->bss) flags |= SEC_HAS_CONTENTS; - flags &= ~SEC_RELOC; x = bfd_set_section_flags (sec, flags); gas_assert (x); @@ -1385,13 +1384,7 @@ write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, } #endif - if (n) - { - flagword flags = bfd_section_flags (sec); - flags |= SEC_RELOC; - bfd_set_section_flags (sec, flags); - bfd_set_reloc (stdoutput, sec, relocs, n); - } + bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n); #ifdef SET_SECTION_RELOCS SET_SECTION_RELOCS (sec, relocs, n); -- Alan Modra Australia Development Lab, IBM