From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id C7DBA3858417; Thu, 11 Apr 2024 02:52:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7DBA3858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1712803920; bh=Zwjb1bAi9wbfla/2Ld8VYK5CZP8ydGTabZrPsYFZxzE=; h=From:To:Subject:Date:From; b=NySy+Spz9uXEMD18eOQ96Mto1ZsVyV11GhjCxGC+71dNDMLG532n/+I9173ALf2Oi nzlLC5L/I15e7Jc/TJQkH2mjIQikNvUFhk+6ngxBhuTpGiJZOtAQsQ54LU8VmYBofP TDHS5SKo0ejgtIFIMhqu+G7qjT5OUrJojhyBPuno= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: H.J. Lu To: binutils-cvs@sourceware.org Subject: [binutils-gdb] elf: Fix a memory leak in _bfd_elf_add_dynamic_entry X-Act-Checkin: binutils-gdb X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: ea3002bc4d2a3c8ad284041f8a7dd08472c3f5fa X-Git-Newrev: c3460201a64b641e3a2089b7fca7ae17a9ddfb85 Message-Id: <20240411025200.C7DBA3858417@sourceware.org> Date: Thu, 11 Apr 2024 02:52:00 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc3460201a64b= 641e3a2089b7fca7ae17a9ddfb85 commit c3460201a64b641e3a2089b7fca7ae17a9ddfb85 Author: H.J. Lu Date: Tue Apr 9 18:41:59 2024 -0700 elf: Fix a memory leak in _bfd_elf_add_dynamic_entry =20 Normally, the section contents is allocated by bfd_alloc which is freed when the object is closed. But the .dynamic section contents is alloca= ted by bfd_realloc, which should be freed by calling free. Add a dynamic field to elf_link_hash_table for the .dynamic section and free its contents in _bfd_elf_link_hash_table_free. =20 * elf-bfd.h (elf_link_hash_table): Add dynamic. * elflink.c (_bfd_elf_link_create_dynamic_sections): Set the dynamic field in elf_link_hash_table. (_bfd_elf_add_dynamic_entry): Use hash_table->dynamic. (_bfd_elf_strip_zero_sized_dynamic_sections): Likewise. (bfd_elf_add_dt_needed_tag): Likewise. (elf_finalize_dynstr): Likewise. (_bfd_elf_link_hash_table_free): Free htab->dynamic->contents. (bfd_elf_final_link): Use htab->dynamic. * elfxx-x86.c (_bfd_x86_elf_finish_dynamic_sections): Use htab->elf.dynamic. Diff: --- bfd/elf-bfd.h | 1 + bfd/elflink.c | 16 ++++++++++------ bfd/elfxx-x86.c | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 85e66488955..ef5dcb55e72 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -757,6 +757,7 @@ struct elf_link_hash_table asection *irelifunc; asection *dynsym; asection *srelrdyn; + asection *dynamic; }; =20 /* Returns TRUE if the hash table is a struct elf_link_hash_table. */ diff --git a/bfd/elflink.c b/bfd/elflink.c index c73470276cd..22fddb8f56b 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -355,6 +355,7 @@ _bfd_elf_link_create_dynamic_sections (bfd *abfd, struc= t bfd_link_info *info) if (s =3D=3D NULL || !bfd_set_section_alignment (s, bed->s->log_file_align)) return false; + elf_hash_table (info)->dynamic =3D s; =20 /* The special symbol _DYNAMIC is always set to the start of the .dynamic section. We could set _DYNAMIC in a linker script, but we @@ -3729,7 +3730,7 @@ _bfd_elf_add_dynamic_entry (struct bfd_link_info *inf= o, hash_table->dynamic_relocs =3D true; =20 bed =3D get_elf_backend_data (hash_table->dynobj); - s =3D bfd_get_linker_section (hash_table->dynobj, ".dynamic"); + s =3D hash_table->dynamic; BFD_ASSERT (s !=3D NULL); =20 newsize =3D s->size + bed->s->sizeof_dyn; @@ -3772,7 +3773,7 @@ _bfd_elf_strip_zero_sized_dynamic_sections (struct bf= d_link_info *info) if (!hash_table->dynobj) return true; =20 - sdynamic=3D bfd_get_linker_section (hash_table->dynobj, ".dynamic"); + sdynamic=3D hash_table->dynamic; if (!sdynamic) return true; =20 @@ -3872,7 +3873,7 @@ bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link= _info *info) bfd_byte *extdyn; =20 bed =3D get_elf_backend_data (hash_table->dynobj); - sdyn =3D bfd_get_linker_section (hash_table->dynobj, ".dynamic"); + sdyn =3D hash_table->dynamic; if (sdyn !=3D NULL && sdyn->size !=3D 0) for (extdyn =3D sdyn->contents; extdyn < sdyn->contents + sdyn->size; @@ -4017,7 +4018,7 @@ elf_finalize_dynstr (bfd *output_bfd, struct bfd_link= _info *info) info->callbacks->examine_strtab (dynstr); =20 bed =3D get_elf_backend_data (dynobj); - sdyn =3D bfd_get_linker_section (dynobj, ".dynamic"); + sdyn =3D hash_table->dynamic; BFD_ASSERT (sdyn !=3D NULL); =20 /* Update all .dynamic entries referencing .dynstr strings. */ @@ -8352,6 +8353,9 @@ _bfd_elf_link_hash_table_free (bfd *obfd) if (htab->dynstr !=3D NULL) _bfd_elf_strtab_free (htab->dynstr); _bfd_merge_sections_free (htab->merge_info); + /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */ + if (htab->dynamic !=3D NULL) + free (htab->dynamic->contents); if (htab->first_hash !=3D NULL) { bfd_hash_table_free (htab->first_hash); @@ -13420,7 +13424,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info= *info) bfd_byte *dyncon, *dynconend; =20 /* Fix up .dynamic entries. */ - o =3D bfd_get_linker_section (dynobj, ".dynamic"); + o =3D htab->dynamic; BFD_ASSERT (o !=3D NULL); =20 dyncon =3D o->contents; @@ -13654,7 +13658,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info= *info) =20 /* Check for DT_TEXTREL (late, in case the backend removes it). */ if (bfd_link_textrel_check (info) - && (o =3D bfd_get_linker_section (dynobj, ".dynamic")) !=3D NULL + && (o =3D htab->dynamic) !=3D NULL && o->size !=3D 0) { bfd_byte *dyncon, *dynconend; diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c index 7d189bc66d1..725884e78c1 100644 --- a/bfd/elfxx-x86.c +++ b/bfd/elfxx-x86.c @@ -2715,7 +2715,7 @@ _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd, return htab; =20 dynobj =3D htab->elf.dynobj; - sdyn =3D bfd_get_linker_section (dynobj, ".dynamic"); + sdyn =3D htab->elf.dynamic; =20 /* GOT is always created in setup_gnu_properties. But it may not be needed. .got.plt section may be needed for static IFUNC. */