public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: binutils@sourceware.org
Cc: Nick Clifton <nickc@redhat.com>, Alan Modra <amodra@gmail.com>,
	libc-alpha@sourceware.org
Subject: [PATCH 06/10] elf: Add size_relative_relocs and finish_relative_relocs
Date: Fri,  7 Jan 2022 11:06:27 -0800	[thread overview]
Message-ID: <20220107190631.309790-7-hjl.tools@gmail.com> (raw)
In-Reply-To: <20220107190631.309790-1-hjl.tools@gmail.com>

On some targets, the DT_RELR section size can be computed only after all
symbols addresses can be determined.  Set the preliminary DT_RELR section
size before mapping sections to segments and set the final DT_RELR section
size after regular symbol processing is done.

	* elf-bfd.h (elf_backend_data): Add size_relative_relocs and
	finish_relative_relocs.
	* elf.c (_bfd_elf_map_sections_to_segments): Call
	size_relative_relocs if DT_RELR is enabled.
	* elflink.c (bfd_elf_final_link): Call finish_relative_relocs
	after regular symbol processing is finished if DT_RELR is enabled.
	* elfxx-target.h (elf_backend_size_relative_relocs): New.
	(elf_backend_finish_relative_relocs): Likewise.
	(elfNN_bed): Add elf_backend_size_relative_relocs and
	elf_backend_finish_relative_relocs.
---
 bfd/elf-bfd.h      | 10 ++++++++++
 bfd/elf.c          | 14 ++++++++++++--
 bfd/elflink.c      |  8 ++++++++
 bfd/elfxx-target.h |  8 ++++++++
 4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 4e73d79ee77..c006008ab7e 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -1105,6 +1105,16 @@ struct elf_backend_data
     (bfd *abfd, struct bfd_link_info *info, asection *o,
      const Elf_Internal_Rela *relocs);
 
+  /* The SIZE_RELATIVE_RELOCS function is called to size relative
+     relocations when mappig sections to segments.  */
+  bool (*size_relative_relocs)
+    (struct bfd_link_info *info, bool *need_layout);
+
+  /* The FINISH_RELATIVE_RELOCS function is called to finish relative
+     relocations in bfd_elf_final_link.  */
+  bool (*finish_relative_relocs)
+    (struct bfd_link_info *info);
+
   /* The CHECK_DIRECTIVES function is called once per input file by
      the add_symbols phase of the ELF backend linker.  The function
      must inspect the bfd and create any additional symbols according
diff --git a/bfd/elf.c b/bfd/elf.c
index 8b866b63e18..14c2c7ba734 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4615,7 +4615,7 @@ elf_modify_segment_map (bfd *abfd,
 bool
 _bfd_elf_map_sections_to_segments (bfd *abfd,
 				   struct bfd_link_info *info,
-				   bool *need_layout ATTRIBUTE_UNUSED)
+				   bool *need_layout)
 {
   unsigned int count;
   struct elf_segment_map *m;
@@ -4626,7 +4626,17 @@ _bfd_elf_map_sections_to_segments (bfd *abfd,
   no_user_phdrs = elf_seg_map (abfd) == NULL;
 
   if (info != NULL)
-    info->user_phdrs = !no_user_phdrs;
+    {
+      info->user_phdrs = !no_user_phdrs;
+
+      /* Size the relative relocations if DT_RELR is enabled.  */
+      if (info->enable_dt_relr
+	  && need_layout != NULL
+	  && bed->size_relative_relocs
+	  && !bed->size_relative_relocs (info, need_layout))
+	info->callbacks->einfo
+	  (_("%F%P: failed to size relative relocations\n"));
+    }
 
   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
     {
diff --git a/bfd/elflink.c b/bfd/elflink.c
index d51b00b6c10..31b13f5df7a 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -12625,6 +12625,14 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   if (!_bfd_elf_fixup_eh_frame_hdr (info))
     return false;
 
+  /* Finish relative relocations here after regular symbol processing
+     is finished if DT_RELR is enabled.  */
+  if (info->enable_dt_relr
+      && bed->finish_relative_relocs
+      && !bed->finish_relative_relocs (info))
+    info->callbacks->einfo
+      (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
+
   /* Since ELF permits relocations to be against local symbols, we
      must have the local symbols available when we do the relocations.
      Since we would rather only read the local symbols once, and we
diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h
index 360b056ff58..e31985ef777 100644
--- a/bfd/elfxx-target.h
+++ b/bfd/elfxx-target.h
@@ -478,6 +478,12 @@
 #ifndef elf_backend_check_relocs
 #define elf_backend_check_relocs	0
 #endif
+#ifndef elf_backend_size_relative_relocs
+#define elf_backend_size_relative_relocs 0
+#endif
+#ifndef elf_backend_finish_relative_relocs
+#define elf_backend_finish_relative_relocs 0
+#endif
 #ifndef elf_backend_check_directives
 #define elf_backend_check_directives	0
 #endif
@@ -842,6 +848,8 @@ static const struct elf_backend_data elfNN_bed =
   elf_backend_omit_section_dynsym,
   elf_backend_relocs_compatible,
   elf_backend_check_relocs,
+  elf_backend_size_relative_relocs,
+  elf_backend_finish_relative_relocs,
   elf_backend_check_directives,
   elf_backend_notice_as_needed,
   elf_backend_adjust_dynamic_symbol,
-- 
2.33.1


  parent reply	other threads:[~2022-01-07 19:06 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-07 19:06 [PATCH 0/10] ld: Implement DT_RELR for x86 H.J. Lu
2022-01-07 19:06 ` [PATCH 01/10] ld: Extract _bfd_elf_link_iterate_on_relocs H.J. Lu
2022-01-07 19:06 ` [PATCH 02/10] elf: Add .relr.dyn to special_sections_r H.J. Lu
2022-01-07 19:06 ` [PATCH 03/10] elf: Extract _bfd_elf_process_reverse_copy H.J. Lu
2022-01-07 19:06 ` [PATCH 04/10] elf: Pass need_layout to _bfd_elf_map_sections_to_segments H.J. Lu
2022-01-07 19:06 ` [PATCH 05/10] ld: Initial DT_RELR support H.J. Lu
2022-01-08  2:10   ` Fangrui Song
2022-01-08  2:42     ` H.J. Lu
2022-01-08 18:32       ` H.J. Lu
2022-01-08  8:30   ` Fangrui Song
2022-01-08 13:19     ` H.J. Lu
2022-01-07 19:06 ` H.J. Lu [this message]
2022-01-07 19:06 ` [PATCH 07/10] elf: Support DT_RELR in linker tests H.J. Lu
2022-01-08  2:42   ` Fangrui Song
2022-01-08 13:46     ` H.J. Lu
2022-01-07 19:06 ` [PATCH 08/10] x86: Add DT_RELR support H.J. Lu
2022-01-07 19:06 ` [PATCH 09/10] ld: Add simple DT_RELR tests H.J. Lu
2022-01-08  2:39   ` Fangrui Song
2022-01-08  2:44     ` H.J. Lu
2022-01-08  8:37   ` Fangrui Song
2022-01-08 18:33     ` H.J. Lu
2022-01-07 19:06 ` [PATCH 10/10] ld: Add glibc dependency for DT_RELR H.J. Lu
2022-01-08  4:43   ` Fangrui Song
2022-01-08  4:51     ` H.J. Lu
2022-01-08  8:19       ` Fangrui Song
2022-01-08 18:37         ` H.J. Lu
2022-01-07 23:45 ` [PATCH 0/10] ld: Implement DT_RELR for x86 Fangrui Song
2022-01-07 23:56   ` H.J. Lu
2022-01-08  1:31     ` Fangrui Song
2022-01-08  1:55       ` H.J. Lu
2022-01-08 18:30         ` H.J. Lu

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=20220107190631.309790-7-hjl.tools@gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=libc-alpha@sourceware.org \
    --cc=nickc@redhat.com \
    /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).