[v12,5/6] elf: Always keep symbol table and relocation info for eh_frame

Message ID 20240317121912.799372-6-hjl.tools@gmail.com
State Committed
Headers
Series elf: Use mmap to map in section contents |

Checks

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

Commit Message

H.J. Lu March 17, 2024, 12:19 p.m. UTC
  When --no-keep-memory is used, the symbol table and relocation info for
eh_frame are freed after they are retrieved for each text section in the
input object.  If an input object has many text sections, the same data
is retrieved and freed many times which can take a very long time.
Update _bfd_elf_gc_mark to keep the symbol table and relocation info for
eh_frame to avoid it.  Data to link the 3.5GB clang executable in LLVM
17 debug build on Linux/x86-64 with 32GB RAM is:

		before		after		improvement
user		86.31		86.44		-0.2%
system		8.77		8.63		1.6%
total		95.58		96.81		-1.3%
maximum set(GB)	13.1		13.1		0%
page faults	3024752		3028699		-1.3%

and data to link the 275M cc1plus executable in GCC 14 stage 1 build is:

user		5.49		5.46		-0.5%
system		0.73		0.73		0%
total		6.26		6.25		0.3%
maximum set(MB)	964		964		0%
page faults	235173		235796		-0.3%

The memory usage impact is minimum and the link time of the Rust binary
in

https://sourceware.org/bugzilla/show_bug.cgi?id=31466

is reduced from 500+ seconds to 1.44 seconds, a 300x speedup.

	PR ld/31466
	* elflink.c (init_reloc_cookie): Add a bool argument, keep_memory,
	for keeping memory.  Always keep memory if keep_memory is true.
	(init_reloc_cookie_rels): Likewise
	(init_reloc_cookie_for_section): Add a bool argument for keeping
	memory and pass it to init_reloc_cookie and
	init_reloc_cookie_rels.
	(_bfd_elf_gc_mark_reloc): Pass false to _bfd_elf_gc_mark.
	(_bfd_elf_gc_mark): Pass true to init_reloc_cookie_for_section
	for the eh_frame section.  Pass false to
	init_reloc_cookie_for_section for other sections.
	(_bfd_elf_gc_mark_extra_sections): Add Add a bool argument for
	keeping memory and pass it to _bfd_elf_gc_mark.
	(bfd_elf_parse_eh_frame_entries): Pass false to init_reloc_cookie
	and init_reloc_cookie_rels.
	(bfd_elf_gc_sections): Pass false to init_reloc_cookie_for_section
	and _bfd_elf_gc_mark.
	(bfd_elf_discard_info): Pass false to
	init_reloc_cookie_for_section and init_reloc_cookie.
---
 bfd/elflink.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)
  

Patch

diff --git a/bfd/elflink.c b/bfd/elflink.c
index dcbe6e1a18c..601d15e9cef 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -13621,7 +13621,8 @@  bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
 
 static bool
 init_reloc_cookie (struct elf_reloc_cookie *cookie,
-		   struct bfd_link_info *info, bfd *abfd)
+		   struct bfd_link_info *info, bfd *abfd,
+		   bool keep_memory)
 {
   Elf_Internal_Shdr *symtab_hdr;
   const struct elf_backend_data *bed;
@@ -13659,7 +13660,7 @@  init_reloc_cookie (struct elf_reloc_cookie *cookie,
 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
 	  return false;
 	}
-      if (_bfd_elf_link_keep_memory (info) )
+      if (keep_memory || _bfd_elf_link_keep_memory (info))
 	{
 	  symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
 	  info->cache_size += (cookie->locsymcount
@@ -13687,7 +13688,7 @@  fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
 static bool
 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
 			struct bfd_link_info *info, bfd *abfd,
-			asection *sec)
+			asection *sec, bool keep_memory)
 {
   if (sec->reloc_count == 0)
     {
@@ -13698,7 +13699,7 @@  init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
     {
       cookie->rels = _bfd_elf_link_info_read_relocs
 	(abfd, info, sec, NULL, NULL,
-	 _bfd_elf_link_keep_memory (info));
+	 keep_memory || _bfd_elf_link_keep_memory (info));
       if (cookie->rels == NULL)
 	return false;
       cookie->rel = cookie->rels;
@@ -13724,11 +13725,12 @@  fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
 static bool
 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
 			       struct bfd_link_info *info,
-			       asection *sec)
+			       asection *sec, bool keep_memory)
 {
-  if (!init_reloc_cookie (cookie, info, sec->owner))
+  if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
     goto error1;
-  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
+  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
+			       keep_memory))
     goto error2;
   return true;
 
@@ -13939,7 +13941,7 @@  _bfd_elf_gc_mark (struct bfd_link_info *info,
     {
       struct elf_reloc_cookie cookie;
 
-      if (!init_reloc_cookie_for_section (&cookie, info, sec))
+      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
 	ret = false;
       else
 	{
@@ -13957,7 +13959,14 @@  _bfd_elf_gc_mark (struct bfd_link_info *info,
     {
       struct elf_reloc_cookie cookie;
 
-      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
+      /* NB: When --no-keep-memory is used, the symbol table and
+	 relocation info for eh_frame are freed after they are retrieved
+	 for each text section in the input object.  If an input object
+	 has many text sections, the same data is retrieved and freed
+	 many times which can take a very long time.  Always keep the
+	 symbol table and relocation info for eh_frame to avoid it.  */
+      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
+					  true))
 	ret = false;
       else
 	{
@@ -14397,13 +14406,14 @@  bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
 	continue;
 
-      if (!init_reloc_cookie (&cookie, info, ibfd))
+      if (!init_reloc_cookie (&cookie, info, ibfd, false))
 	return false;
 
       for (sec = ibfd->sections; sec; sec = sec->next)
 	{
 	  if (startswith (bfd_section_name (sec), ".eh_frame_entry")
-	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
+	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
+					 false))
 	    {
 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
 	      fini_reloc_cookie_rels (&cookie, sec);
@@ -14448,7 +14458,8 @@  bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
 	continue;
       sec = bfd_get_section_by_name (sub, ".eh_frame");
-      while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
+      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
+						   false))
 	{
 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
 	  if (elf_section_data (sec)->sec_info
@@ -14960,7 +14971,7 @@  bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
 	    continue;
 
-	  if (!init_reloc_cookie_for_section (&cookie, info, i))
+	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
 	    return -1;
 
 	  if (_bfd_discard_section_stabs (abfd, i,
@@ -14991,7 +15002,7 @@  bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
 	    continue;
 
-	  if (!init_reloc_cookie_for_section (&cookie, info, i))
+	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
 	    return -1;
 
 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
@@ -15056,7 +15067,7 @@  bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
 	    continue;
 
-	  if (!init_reloc_cookie_for_section (&cookie, info, i))
+	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
 	    return -1;
 
 	  if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
@@ -15092,7 +15103,7 @@  bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
 
       if (bed->elf_backend_discard_info != NULL)
 	{
-	  if (!init_reloc_cookie (&cookie, info, abfd))
+	  if (!init_reloc_cookie (&cookie, info, abfd, false))
 	    return -1;
 
 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))