From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 09C9B3858C53; Sat, 6 Apr 2024 07:29:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09C9B3858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1712388560; bh=+nb3XNBAMHkKuR67foyvxgvx9o3WA/tY+w5tba+oGn4=; h=From:To:Subject:Date:From; b=kHFlm7k5nv+P5qm+W/EXNAb1QwFtoVqbDkHgglLpxtRaH7dwAS+h3A7fuEyIlMuOM KVPQPCigC1QFSqCA6gMDlL9cB22Zn1GQg7BgjW903Jq3Tls+Zw+hRqqgcQpBfJygxb D1HMSF7n349Za8dWXNUFHiiI13AcT2TjOK5MMs/Y= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: binutils-cvs@sourceware.org Subject: [binutils-gdb] Don't have first_hash entries of strings that can be freed. X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 82475fa2fa916677e4c578b4e3e35b49160656fa X-Git-Newrev: e7e05a9dd0c93038fdd5ed1904ca660e52beabdc Message-Id: <20240406072920.09C9B3858C53@sourceware.org> Date: Sat, 6 Apr 2024 07:29:19 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3De7e05a9dd0c9= 3038fdd5ed1904ca660e52beabdc commit e7e05a9dd0c93038fdd5ed1904ca660e52beabdc Author: Alan Modra Date: Sat Apr 6 15:49:44 2024 +1030 Don't have first_hash entries of strings that can be freed. =20 Seen running "LTO 1" under valgrind. =3D=3D1443263=3D=3D Invalid read of size 1 =3D=3D1443263=3D=3D at 0x484CFE4: strcmp (vg_replace_strmem.c:939) =3D=3D1443263=3D=3D by 0x56E16C: bfd_hash_lookup (hash.c:564) =3D=3D1443263=3D=3D by 0x5A3C8F: elf_link_add_to_first_hash (elflink= .c:4316) =3D=3D1443263=3D=3D by 0x5AE60F: elf_link_add_object_symbols (elflin= k.c:5663) =3D=3D1443263=3D=3D by 0x5B0672: bfd_elf_link_add_symbols (elflink.c= :6333) =3D=3D1443263=3D=3D by 0x41448F: load_symbols (ldlang.c:3129) =3D=3D1443263=3D=3D by 0x4149D8: open_input_bfds (ldlang.c:3621) =3D=3D1443263=3D=3D by 0x414968: open_input_bfds (ldlang.c:3569) =3D=3D1443263=3D=3D by 0x4166A2: lang_process (ldlang.c:8162) =3D=3D1443263=3D=3D by 0x4194D5: main (ldmain.c:504) =3D=3D1443263=3D=3D Address 0x525e230 is 192 bytes inside a block of s= ize 4,064 free'd =3D=3D1443263=3D=3D at 0x484810F: free (vg_replace_malloc.c:974) =3D=3D1443263=3D=3D by 0x8D4D87: objalloc_free_block (objalloc.c:248) =3D=3D1443263=3D=3D by 0x5AEACC: elf_link_add_object_symbols (elflin= k.c:5790) =3D=3D1443263=3D=3D by 0x5B0672: bfd_elf_link_add_symbols (elflink.c= :6333) =3D=3D1443263=3D=3D by 0x41448F: load_symbols (ldlang.c:3129) =3D=3D1443263=3D=3D by 0x4149D8: open_input_bfds (ldlang.c:3621) =3D=3D1443263=3D=3D by 0x414968: open_input_bfds (ldlang.c:3569) =3D=3D1443263=3D=3D by 0x4166A2: lang_process (ldlang.c:8162) =3D=3D1443263=3D=3D by 0x4194D5: main (ldmain.c:504) =20 PR ld/31482 PR ld/31489 * elflink.c (elf_link_add_to_first_hash): Add "copy" param. (elf_link_add_object_symbols): Flag that name must be copied when appending version string to symbol name. Diff: --- bfd/elflink.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/bfd/elflink.c b/bfd/elflink.c index e41b3d6dad7..dadac2522d5 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -4304,7 +4304,7 @@ elf_link_first_hash_newfunc (struct bfd_hash_entry *e= ntry, =20 static void elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info, - const char *name) + const char *name, bool copy) { struct elf_link_hash_table *htab =3D elf_hash_table (info); /* Skip if there is no first hash. */ @@ -4313,7 +4313,7 @@ elf_link_add_to_first_hash (bfd *abfd, struct bfd_lin= k_info *info, =20 struct elf_link_first_hash_entry *e =3D ((struct elf_link_first_hash_entry *) - bfd_hash_lookup (htab->first_hash, name, true, false)); + bfd_hash_lookup (htab->first_hash, name, true, copy)); if (e =3D=3D NULL) info->callbacks->einfo (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name); @@ -4920,6 +4920,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_li= nk_info *info) asection *sec, *new_sec; flagword flags; const char *name; + bool must_copy_name =3D false; struct elf_link_hash_entry *h; struct elf_link_hash_entry *hi; bool definition; @@ -5217,6 +5218,11 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_l= ink_info *info) memcpy (p, verstr, verlen + 1); =20 name =3D newname; + /* Since bfd_hash_alloc is used for "name", the string + must be copied if added to first_hash. The string + memory can be freed when an --as-needed library is + not needed. */ + must_copy_name =3D true; } =20 /* If this symbol has default visibility and the user has @@ -5660,7 +5666,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_li= nk_info *info) && h->root.u.def.section->owner =3D=3D abfd) /* Add this symbol to first hash if this shared object has the first definition. */ - elf_link_add_to_first_hash (abfd, info, name); + elf_link_add_to_first_hash (abfd, info, name, must_copy_name); } } } @@ -6108,7 +6114,7 @@ _bfd_elf_archive_symbol_lookup (bfd *abfd, { /* Add this symbol to first hash if this archive has the first definition. */ - elf_link_add_to_first_hash (abfd, info, name); + elf_link_add_to_first_hash (abfd, info, name, false); return h; }