[RFC 6/6] aarch64 gas: Free reg alias entries before exit

Alice Carlotti alice.carlotti@arm.com
Tue Mar 3 12:41:55 GMT 2026


Add a deletion hook to aarch64_reg_hsh, so that dynamically allocated
data in alias entries is freed at exit.  This removes the allocations
from LeakSanitizer reports, and also allows the explicit 'free' calls to
be removed from s_unreq ().


diff --git a/gas/config/tc-aarch64.c b/gas/config/tc-aarch64.c
index 3dde65e3d91e346bd271189f11d119f78355a92d..2c78f990502fb06c61b3d85142ded7036051d7db 100644
--- a/gas/config/tc-aarch64.c
+++ b/gas/config/tc-aarch64.c
@@ -1711,6 +1711,21 @@ create_register_alias (char *newname, char *p)
   return true;
 }
 
+/* Delete handler for aarch64_reg_hsh.  Builtin reg entries are static values
+   added at startup, while aliases are dynamically allocated.  */
+
+static void
+free_register_entry (void *entry)
+{
+  string_tuple_t *tuple = entry;
+  reg_entry *reg = (reg_entry *) tuple->value;
+  if (!reg->builtin)
+  {
+    free ((char *) reg->name);
+    free (reg);
+  }
+}
+
 /* Should never be called, as .req goes between the alias and the
    register name, not at the beginning of the line.  */
 static void
@@ -1753,8 +1768,6 @@ s_unreq (int a ATTRIBUTE_UNUSED)
 	  char *nbuf;
 
 	  str_hash_delete (aarch64_reg_hsh, name);
-	  free ((char *) reg->name);
-	  free (reg);
 
 	  /* Also locate the all upper case and all lower case versions.
 	     Do not complain if we cannot find one or the other as it
@@ -1767,8 +1780,6 @@ s_unreq (int a ATTRIBUTE_UNUSED)
 	  if (reg)
 	    {
 	      str_hash_delete (aarch64_reg_hsh, nbuf);
-	      free ((char *) reg->name);
-	      free (reg);
 	    }
 
 	  for (p = nbuf; *p; p++)
@@ -1777,8 +1788,6 @@ s_unreq (int a ATTRIBUTE_UNUSED)
 	  if (reg)
 	    {
 	      str_hash_delete (aarch64_reg_hsh, nbuf);
-	      free ((char *) reg->name);
-	      free (reg);
 	    }
 
 	  free (nbuf);
@@ -10583,7 +10592,6 @@ md_begin (void)
   aarch64_sys_regs_plbi_hsh = str_htab_create ();
   aarch64_sys_regs_mlbi_hsh = str_htab_create ();
   aarch64_sys_regs_sr_hsh = str_htab_create ();
-  aarch64_reg_hsh = str_htab_create ();
   aarch64_barrier_opt_hsh = str_htab_create ();
   aarch64_nzcv_hsh = str_htab_create ();
   aarch64_pldop_hsh = str_htab_create ();
@@ -10592,6 +10600,11 @@ md_begin (void)
   aarch64_sys_ins_gicr_hsh = str_htab_create ();
   aarch64_sys_ins_gsb_hsh = str_htab_create ();
 
+  /* This is equivalent to str_htab_create with an added deletion hook.  */
+  aarch64_reg_hsh = htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
+				       free_register_entry, notes_calloc,
+				       NULL);
+
   fill_instruction_hash_table ();
 
   for (i = 0; aarch64_sys_regs[i].name != NULL; ++i)


More information about the Binutils mailing list