From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id E00C23858D1E; Fri, 5 Apr 2024 23:53:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E00C23858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1712361206; bh=vt/lv8KF5X+UO7g97UiwPTML5fp3dsO1qaKOLtdJNTY=; h=From:To:Subject:Date:From; b=Qk3ToOotHjOefjVyNyeFoKh5nLO/BUU2oJ1atTNuUY2k8kuKeH1aLDcrPyrSkc+GF lqrJ28ndqvPjAFU5prcQWjhH+HaBhTZnbMF74VvOuMlgJyEj/1+hWV6Rty6eAnNrrZ WLoVBUu57Glhx5lRbsAZHONQLi0vQM7DrKIW9CEI= 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: Use elf_link_first_hash_entry for first_hash X-Act-Checkin: binutils-gdb X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: c0419c024bf922128131671e40de0aed736e38ed X-Git-Newrev: 816fd3dced1b2e94789b6d7d1d1fab1988e8e05d Message-Id: <20240405235326.E00C23858D1E@sourceware.org> Date: Fri, 5 Apr 2024 23:53:26 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D816fd3dced1b= 2e94789b6d7d1d1fab1988e8e05d commit 816fd3dced1b2e94789b6d7d1d1fab1988e8e05d Author: H.J. Lu Date: Fri Apr 5 16:37:58 2024 -0700 elf: Use elf_link_first_hash_entry for first_hash =20 Add elf_link_first_hash_entry and use it for first_hash. Free first_ha= sh before freeing the main hash table. =20 PR ld/31482 PR ld/31489 * elf-bfd.h (elf_link_hash_table): Change first_hash to bfd_hash_table. * elflink.c (elf_link_first_hash_entry): New. (elf_link_first_hash_newfunc): Likewise. (elf_link_add_to_first_hash): Updated. (elf_link_add_object_symbols): Initialize first_hash with elf_link_first_hash_newfunc. (elf_link_add_object_symbols): Updated. (elf_link_add_archive_symbols): Likewise. (_bfd_elf_link_hash_table_free): Free first_hash before freeing the main hash table. Diff: --- bfd/elf-bfd.h | 2 +- bfd/elflink.c | 87 ++++++++++++++++++++++++++++++++++++++++++-------------= ---- 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index cf043d6d154..85e66488955 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -739,7 +739,7 @@ struct elf_link_hash_table =20 /* Hash table of symbols which are first defined in archives or shared objects when there are any IR inputs. */ - struct bfd_link_hash_table *first_hash; + struct bfd_hash_table *first_hash; =20 /* Short-cuts to get to dynamic linker sections. */ asection *sgot; diff --git a/bfd/elflink.c b/bfd/elflink.c index 3db517ca1cd..e41b3d6dad7 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -4261,6 +4261,45 @@ _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_li= nk_info *info) return true; } =20 +/* An entry in the first definition hash table. */ + +struct elf_link_first_hash_entry +{ + struct bfd_hash_entry root; + /* The object of the first definition. */ + bfd *abfd; +}; + +/* The function to create a new entry in the first definition hash + table. */ + +static struct bfd_hash_entry * +elf_link_first_hash_newfunc (struct bfd_hash_entry *entry, + struct bfd_hash_table *table, + const char *string) +{ + struct elf_link_first_hash_entry *ret =3D + (struct elf_link_first_hash_entry *) entry; + + /* Allocate the structure if it has not already been allocated by a + subclass. */ + if (ret =3D=3D NULL) + ret =3D (struct elf_link_first_hash_entry *) + bfd_hash_allocate (table, + sizeof (struct elf_link_first_hash_entry)); + if (ret =3D=3D NULL) + return NULL; + + /* Call the allocation method of the superclass. */ + ret =3D ((struct elf_link_first_hash_entry *) + bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, + string)); + if (ret !=3D NULL) + ret->abfd =3D NULL; + + return (struct bfd_hash_entry *) ret; +} + /* Add the symbol NAME from ABFD to first hash. */ =20 static void @@ -4272,19 +4311,16 @@ elf_link_add_to_first_hash (bfd *abfd, struct bfd_l= ink_info *info, if (htab->first_hash =3D=3D NULL) return; =20 - struct bfd_link_hash_entry *e - =3D bfd_link_hash_lookup (htab->first_hash, name, true, false, true); + struct elf_link_first_hash_entry *e + =3D ((struct elf_link_first_hash_entry *) + bfd_hash_lookup (htab->first_hash, name, true, false)); if (e =3D=3D NULL) info->callbacks->einfo (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name); =20 - if (e->type =3D=3D bfd_link_hash_new) - { - /* Change the type to bfd_link_hash_undefined and store ABFD in - u.undef->abfd. */ - e->type =3D bfd_link_hash_undefined; - e->u.undef.abfd =3D abfd; - } + if (e->abfd =3D=3D NULL) + /* Store ABFD in abfd. */ + e->abfd =3D abfd; } =20 /* Add symbols from an ELF object file to the linker hash table. */ @@ -4341,11 +4377,11 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_= link_info *info) && htab->first_hash =3D=3D NULL) { /* Initialize first_hash for an IR input. */ - htab->first_hash =3D (struct bfd_link_hash_table *) - xmalloc (sizeof (struct bfd_link_hash_table)); - if (!bfd_hash_table_init (&htab->first_hash->table, - _bfd_link_hash_newfunc, - sizeof (struct bfd_link_hash_entry))) + htab->first_hash =3D (struct bfd_hash_table *) + xmalloc (sizeof (struct bfd_hash_table)); + if (!bfd_hash_table_init + (htab->first_hash, elf_link_first_hash_newfunc, + sizeof (struct elf_link_first_hash_entry))) info->callbacks->einfo (_("%F%P: first_hash failed to initialize: %E\n")); } @@ -5219,10 +5255,11 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_= link_info *info) /* When reloading --as-needed shared objects for new symbols added from IR inputs, if this shared object has the first definition, use it. */ - struct bfd_link_hash_entry *e - =3D bfd_link_hash_lookup (htab->first_hash, name, - false, false, true); - if (e !=3D NULL && e->u.undef.abfd =3D=3D abfd) + struct elf_link_first_hash_entry *e + =3D ((struct elf_link_first_hash_entry *) + bfd_hash_lookup (htab->first_hash, name, false, + false)); + if (e !=3D NULL && e->abfd =3D=3D abfd) definition =3D true; } } @@ -6222,11 +6259,11 @@ elf_link_add_archive_symbols (bfd *abfd, struct bfd= _link_info *info) struct elf_link_hash_table *htab =3D elf_hash_table (info); if (htab->first_hash =3D=3D NULL) continue; - struct bfd_link_hash_entry *e - =3D bfd_link_hash_lookup (htab->first_hash, - symdef->name, false, false, - true); - if (e =3D=3D NULL || e->u.undef.abfd !=3D abfd) + struct elf_link_first_hash_entry *e + =3D ((struct elf_link_first_hash_entry *) + bfd_hash_lookup (htab->first_hash, symdef->name, + false, false)); + if (e =3D=3D NULL || e->abfd !=3D abfd) continue; } =20 @@ -8308,12 +8345,12 @@ _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); - _bfd_generic_link_hash_table_free (obfd); if (htab->first_hash !=3D NULL) { - bfd_hash_table_free (&htab->first_hash->table); + bfd_hash_table_free (htab->first_hash); free (htab->first_hash); } + _bfd_generic_link_hash_table_free (obfd); } =20 /* This is a hook for the ELF emulation code in the generic linker to