From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 476473858D32; Wed, 21 Jun 2023 16:15:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 476473858D32 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: H.J. Lu To: bfd-cvs@sourceware.org Subject: [binutils-gdb] x86: Free the symbol buffer and the relocation buffer after use X-Act-Checkin: binutils-gdb X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: c9097e37c7bfdb407e1a5f137b69802a61390def X-Git-Newrev: d8bcb8723602f03351ea583cbc9ba21f39054eb1 Message-Id: <20230621161556.476473858D32@sourceware.org> Date: Wed, 21 Jun 2023 16:15:56 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Jun 2023 16:15:56 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dd8bcb8723602= f03351ea583cbc9ba21f39054eb1 commit d8bcb8723602f03351ea583cbc9ba21f39054eb1 Author: H.J. Lu Date: Tue Jun 20 15:10:11 2023 -0700 x86: Free the symbol buffer and the relocation buffer after use =20 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. =20 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; =20 @@ -1057,6 +1057,8 @@ elf_x86_relative_reloc_record_add { relative_reloc->data[newidx].sym =3D sym; relative_reloc->data[newidx].u.sym_sec =3D sym_sec; + /* We must keep the symbol buffer since SYM will be used later. */ + *keep_symbuf_p =3D true; } relative_reloc->data[newidx].offset =3D offset; relative_reloc->data[newidx].address =3D 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 =3D false; + bool keep_symbuf =3D false; =20 if (bfd_link_relocatable (info)) return true; @@ -1120,9 +1124,9 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_= UNUSED, =20 /* Load the relocations for this section. */ internal_relocs =3D - _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 =3D=3D NULL) return false; =20 @@ -1163,11 +1167,13 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUT= E_UNUSED, { isymbuf =3D (Elf_Internal_Sym *) symtab_hdr->contents; if (isymbuf =3D=3D NULL) - isymbuf =3D bfd_elf_get_elf_syms (abfd, symtab_hdr, - symtab_hdr->sh_info, 0, - NULL, NULL, NULL); - if (isymbuf =3D=3D NULL) - goto error_return; + { + isymbuf =3D bfd_elf_get_elf_syms (abfd, symtab_hdr, + symtab_hdr->sh_info, + 0, NULL, NULL, NULL); + if (isymbuf =3D=3D NULL) + goto error_return; + } } =20 isym =3D 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; =20 continue; @@ -1336,21 +1343,28 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUT= E_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; } } =20 input_section->relative_reloc_packed =3D 1; =20 - return true; + return_status =3D true; =20 error_return: if ((unsigned char *) isymbuf !=3D symtab_hdr->contents) - free (isymbuf); + { + /* Cache the symbol buffer if it must be kept. */ + if (keep_symbuf) + symtab_hdr->contents =3D (unsigned char *) isymbuf; + else + free (isymbuf); + } if (elf_section_data (input_section)->relocs !=3D internal_relocs) free (internal_relocs); - return false; + return return_status; } =20 /* Add an entry to the 64-bit DT_RELR bitmap. */