public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
* [binutils-gdb] x86: Free the symbol buffer and the relocation buffer after use
@ 2023-06-21 16:15 H.J. Lu
  0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2023-06-21 16:15 UTC (permalink / raw)
  To: bfd-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d8bcb8723602f03351ea583cbc9ba21f39054eb1

commit d8bcb8723602f03351ea583cbc9ba21f39054eb1
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jun 20 15:10:11 2023 -0700

    x86: Free the symbol buffer and the relocation buffer after use
    
    When --no-keep-memory is used, the symbol buffer and the relocation
    buffer aren't cached.  When packing relative relocations, we may
    allocate a new symbol buffer and a new relocation buffer for each
    eligible section in an object file.  If there are many sections,
    memory may be exhausted.  In this case, we should free the symbol
    buffer and the relocation buffer after use.  If symbol buffer entries
    are used to track relative relocations against local symbols for later
    use, the symbol buffer should be cached.
    
            PR ld/30566
            * elfxx-x86.c (elf_x86_relative_reloc_record_add): Add an
            argument to inform caller if the symbol buffer should be kept.
            (_bfd_x86_elf_link_relax_section): Call
            _bfd_elf_link_info_read_relocs instead of
            _bfd_elf_link_read_relocs.  Free the symbol buffer and the
            relocation buffer after use.  Cache the symbol buffer if it
            is used.

Diff:
---
 bfd/elfxx-x86.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index 132fb791ac6..8e13a92e7f9 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -1013,7 +1013,7 @@ elf_x86_relative_reloc_record_add
    struct elf_x86_relative_reloc_data *relative_reloc,
    Elf_Internal_Rela *rel, asection *sec,
    asection *sym_sec, struct elf_link_hash_entry *h,
-   Elf_Internal_Sym *sym, bfd_vma offset)
+   Elf_Internal_Sym *sym, bfd_vma offset, bool *keep_symbuf_p)
 {
   bfd_size_type newidx;
 
@@ -1057,6 +1057,8 @@ elf_x86_relative_reloc_record_add
     {
       relative_reloc->data[newidx].sym = sym;
       relative_reloc->data[newidx].u.sym_sec = sym_sec;
+      /* We must keep the symbol buffer since SYM will be used later.  */
+      *keep_symbuf_p = true;
     }
   relative_reloc->data[newidx].offset = offset;
   relative_reloc->data[newidx].address = 0;
@@ -1086,6 +1088,8 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
   bfd_vma *local_got_offsets;
   bool is_x86_64;
   bool unaligned_section;
+  bool return_status = false;
+  bool keep_symbuf = false;
 
   if (bfd_link_relocatable (info))
     return true;
@@ -1120,9 +1124,9 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
 
   /* Load the relocations for this section.  */
   internal_relocs =
-    _bfd_elf_link_read_relocs (abfd, input_section, NULL,
-			       (Elf_Internal_Rela *) NULL,
-			       info->keep_memory);
+    _bfd_elf_link_info_read_relocs (abfd, info, input_section, NULL,
+				    (Elf_Internal_Rela *) NULL,
+				    info->keep_memory);
   if (internal_relocs == NULL)
     return false;
 
@@ -1163,11 +1167,13 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
 	    {
 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
 	      if (isymbuf == NULL)
-		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
-						symtab_hdr->sh_info, 0,
-						NULL, NULL, NULL);
-	      if (isymbuf == NULL)
-		goto error_return;
+		{
+		  isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
+						  symtab_hdr->sh_info,
+						  0, NULL, NULL, NULL);
+		  if (isymbuf == NULL)
+		    goto error_return;
+		}
 	    }
 
 	  isym = isymbuf + r_symndx;
@@ -1267,7 +1273,8 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
 	  if (!elf_x86_relative_reloc_record_add (info,
 						  &htab->relative_reloc,
 						  irel, htab->elf.sgot,
-						  sec, h, isym, offset))
+						  sec, h, isym, offset,
+						  &keep_symbuf))
 	    goto error_return;
 
 	  continue;
@@ -1336,21 +1343,28 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
 		 ((unaligned_section || unaligned_offset)
 		  ? &htab->unaligned_relative_reloc
 		  : &htab->relative_reloc),
-		 irel, input_section, sec, h, isym, offset))
+		 irel, input_section, sec, h, isym, offset,
+		 &keep_symbuf))
 	    goto error_return;
 	}
     }
 
   input_section->relative_reloc_packed = 1;
 
-  return true;
+  return_status = true;
 
 error_return:
   if ((unsigned char *) isymbuf != symtab_hdr->contents)
-    free (isymbuf);
+    {
+      /* Cache the symbol buffer if it must be kept.  */
+      if (keep_symbuf)
+	symtab_hdr->contents = (unsigned char *) isymbuf;
+      else
+	free (isymbuf);
+    }
   if (elf_section_data (input_section)->relocs != internal_relocs)
     free (internal_relocs);
-  return false;
+  return return_status;
 }
 
 /* Add an entry to the 64-bit DT_RELR bitmap.  */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-21 16:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-21 16:15 [binutils-gdb] x86: Free the symbol buffer and the relocation buffer after use H.J. Lu

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).