elf: Store __ehdr_start hash in elf_link_hash_table

Message ID 20250203040942.2447898-1-hjl.tools@gmail.com
State New
Headers
Series elf: Store __ehdr_start hash in elf_link_hash_table |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

H.J. Lu Feb. 3, 2025, 4:09 a.m. UTC
  Store __ehdr_start hash in elf_link_hash_table so that we just need to
lookup it up once.

bfd/

	* elf-bfd.h (elf_link_hash_table): Add hehdr_start.
	* elf.c (assign_file_positions_for_load_sections): Use
	hehdr_start.

ld/

	* ldelf.c (ldelf_before_allocation): Store __ehdr_start hash in
	hehdr_start.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elf-bfd.h | 3 +++
 bfd/elf.c     | 6 +++---
 ld/ldelf.c    | 1 +
 3 files changed, 7 insertions(+), 3 deletions(-)
  

Comments

H.J. Lu Feb. 3, 2025, 9:33 a.m. UTC | #1
On Mon, Feb 3, 2025 at 12:09 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Store __ehdr_start hash in elf_link_hash_table so that we just need to
> lookup it up once.
>
> bfd/
>
>         * elf-bfd.h (elf_link_hash_table): Add hehdr_start.
>         * elf.c (assign_file_positions_for_load_sections): Use
>         hehdr_start.
>
> ld/
>
>         * ldelf.c (ldelf_before_allocation): Store __ehdr_start hash in
>         hehdr_start.
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  bfd/elf-bfd.h | 3 +++
>  bfd/elf.c     | 6 +++---
>  ld/ldelf.c    | 1 +
>  3 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
> index c89327fdc9c..785a37dd7fd 100644
> --- a/bfd/elf-bfd.h
> +++ b/bfd/elf-bfd.h
> @@ -699,6 +699,9 @@ struct elf_link_hash_table
>    /* The _DYNAMIC symbol.  */
>    struct elf_link_hash_entry *hdynamic;
>
> +  /* The __ehdr_start symbol.  */
> +  struct elf_link_hash_entry *hehdr_start;
> +
>    /* A pointer to information used to merge SEC_MERGE sections.  */
>    void *merge_info;
>
> diff --git a/bfd/elf.c b/bfd/elf.c
> index 0fa580d8d78..0e40d6d1d30 100644
> --- a/bfd/elf.c
> +++ b/bfd/elf.c
> @@ -6419,9 +6419,9 @@ assign_file_positions_for_load_sections (bfd *abfd,
>          program headers, so provide a symbol __ehdr_start pointing there.
>          A program can use this to examine itself robustly.  */
>
> -      struct elf_link_hash_entry *hash
> -       = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
> -                               false, false, true);
> +      struct elf_link_hash_table *htab = elf_hash_table (link_info);
> +      struct elf_link_hash_entry *hash = htab->hehdr_start;
> +
>        /* If the symbol was referenced and not defined, define it.  */
>        if (hash != NULL
>           && (hash->root.type == bfd_link_hash_new
> diff --git a/ld/ldelf.c b/ld/ldelf.c
> index dbc3d77d47b..624b32d6dcd 100644
> --- a/ld/ldelf.c
> +++ b/ld/ldelf.c
> @@ -1797,6 +1797,7 @@ ldelf_before_allocation (char **audit, char **depaudit,
>               /* It will be converted to section-relative later.  */
>               ehdr_start->u.def.section = bfd_abs_section_ptr;
>               ehdr_start->u.def.value = 0;
> +             htab->hehdr_start = h;
>             }
>         }
>
> --
> 2.48.1
>

It turns out that

commit 97da0e2677c4a38df2406576428ec27d1da26e7c
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Jan 12 23:42:23 2022 +1030

    tweak __ehdr_start visibility and flags for check_relocs

creates __ehdr_start hash in lang_symbol_tweaks.   Here is the v2 patch.
  
Alan Modra Feb. 4, 2025, 4:59 a.m. UTC | #2
On Mon, Feb 03, 2025 at 05:33:41PM +0800, H.J. Lu wrote:
> creates __ehdr_start hash in lang_symbol_tweaks.   Here is the v2 patch.

OK.
  

Patch

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index c89327fdc9c..785a37dd7fd 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -699,6 +699,9 @@  struct elf_link_hash_table
   /* The _DYNAMIC symbol.  */
   struct elf_link_hash_entry *hdynamic;
 
+  /* The __ehdr_start symbol.  */
+  struct elf_link_hash_entry *hehdr_start;
+
   /* A pointer to information used to merge SEC_MERGE sections.  */
   void *merge_info;
 
diff --git a/bfd/elf.c b/bfd/elf.c
index 0fa580d8d78..0e40d6d1d30 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6419,9 +6419,9 @@  assign_file_positions_for_load_sections (bfd *abfd,
 	 program headers, so provide a symbol __ehdr_start pointing there.
 	 A program can use this to examine itself robustly.  */
 
-      struct elf_link_hash_entry *hash
-	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
-				false, false, true);
+      struct elf_link_hash_table *htab = elf_hash_table (link_info);
+      struct elf_link_hash_entry *hash = htab->hehdr_start;
+
       /* If the symbol was referenced and not defined, define it.  */
       if (hash != NULL
 	  && (hash->root.type == bfd_link_hash_new
diff --git a/ld/ldelf.c b/ld/ldelf.c
index dbc3d77d47b..624b32d6dcd 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1797,6 +1797,7 @@  ldelf_before_allocation (char **audit, char **depaudit,
 	      /* It will be converted to section-relative later.  */
 	      ehdr_start->u.def.section = bfd_abs_section_ptr;
 	      ehdr_start->u.def.value = 0;
+	      htab->hehdr_start = h;
 	    }
 	}