public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Patrick Monnerat <patrick@monnerat.net>
To: binutils@sourceware.org
Cc: Patrick Monnerat <patrick@monnerat.net>
Subject: [PATCH] binutils/objcopy: keep relocation while renaming a section with explicit flags
Date: Sat, 12 Nov 2022 13:44:41 +0100	[thread overview]
Message-ID: <20221112124441.5084-1-patrick@monnerat.net> (raw)

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.
---
 binutils/objcopy.c                                  |  1 +
 binutils/testsuite/binutils-all/objcopy.exp         |  2 ++
 binutils/testsuite/binutils-all/rename-section-01.d | 12 ++++++++++++
 3 files changed, 15 insertions(+)
 create mode 100644 binutils/testsuite/binutils-all/rename-section-01.d

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index d886e3ae343..f195e4d875e 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4052,6 +4052,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
   if (new_name != name)
     {
       name = new_name;
+      flags |= bfd_section_flags (isection) & SEC_RELOC;
       flags = check_new_section_flags (flags, obfd, name);
     }
 
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..b0282fc9552
--- /dev/null
+++ b/binutils/testsuite/binutils-all/rename-section-01.d
@@ -0,0 +1,12 @@
+#PROG: objcopy
+#name: objcopy rename-section with flags - keep relocation
+#source: needed-by-reloc.s
+#objcopy: --rename-section .data=rodata,contents,alloc,load,readonly
+#objdump: -r
+
+.*: +file format .*
+
+RELOCATION RECORDS FOR \[rodata\]:
+OFFSET +TYPE +VALUE
+0+ +[^ ]+ +foo
+
-- 
2.38.1


             reply	other threads:[~2022-11-12 12:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-12 12:44 Patrick Monnerat [this message]
2022-11-14  1:43 ` Alan Modra
2022-11-14  3:14   ` Alan Modra
2022-11-14 14:11     ` Patrick Monnerat

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221112124441.5084-1-patrick@monnerat.net \
    --to=patrick@monnerat.net \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).