mmo.c leak in mmo_make_section
[binutils-gdb.git] / libctf / ctf-dedup.c
index 649a76a91b62a55d5ac86214fd5651f3664bfea1..dcde3e88c6f150485fb9fce646b423bdaed30122 100644 (file)
@@ -1,5 +1,5 @@
 /* CTF type deduplication.
-   Copyright (C) 2019-2021 Free Software Foundation, Inc.
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
 
    This file is part of libctf.
 
@@ -352,7 +352,7 @@ make_set_element (ctf_dynhash_t *set, const void *key)
   if ((element = ctf_dynhash_lookup (set, key)) == NULL)
     {
       if ((element = ctf_dynset_create (htab_hash_string,
-                                       ctf_dynset_eq_string,
+                                       htab_eq_string,
                                        NULL)) == NULL)
        return NULL;
 
@@ -376,7 +376,7 @@ ctf_dedup_atoms_init (ctf_dict_t *fp)
   if (!fp->ctf_dedup_atoms_alloc)
     {
       if ((fp->ctf_dedup_atoms_alloc
-          = ctf_dynset_create (htab_hash_string, ctf_dynset_eq_string,
+          = ctf_dynset_create (htab_hash_string, htab_eq_string,
                                free)) == NULL)
        return ctf_set_errno (fp, ENOMEM);
     }
@@ -584,8 +584,8 @@ ctf_dedup_rhash_type (ctf_dict_t *fp, ctf_dict_t *input, ctf_dict_t **inputs,
       whaterr = N_("error updating citers");                           \
       if (!citers)                                                     \
        if ((citers = ctf_dynset_create (htab_hash_string,              \
-                                         ctf_dynset_eq_string,         \
-                                         NULL)) == NULL)               \
+                                        htab_eq_string,                \
+                                        NULL)) == NULL)                \
          goto oom;                                                     \
       if (ctf_dynset_cinsert (citers, hval) < 0)                       \
        goto oom;                                                       \
@@ -1502,12 +1502,17 @@ ctf_dedup_detect_name_ambiguity (ctf_dict_t *fp, ctf_dict_t **inputs)
             the most-popular type on insertion, and we want conflicting structs
             et al to have all forwards left intact, so the user is notified
             that this type is conflicting.  TODO: improve this in future by
-            setting such forwards non-root-visible.)  */
+            setting such forwards non-root-visible.)
+
+            If multiple distinct types are "most common", pick the one that
+            appears first on the link line, and within that, the one with the
+            lowest type ID.  (See sort_output_mapping.)  */
 
          const void *key;
          const void *count;
          const char *hval;
          long max_hcount = -1;
+         void *max_gid = NULL;
          const char *max_hval = NULL;
 
          if (ctf_dynhash_elements (name_counts) <= 1)
@@ -1517,10 +1522,24 @@ ctf_dedup_detect_name_ambiguity (ctf_dict_t *fp, ctf_dict_t **inputs)
          while ((err = ctf_dynhash_cnext (name_counts, &j, &key, &count)) == 0)
            {
              hval = (const char *) key;
+
              if ((long int) (uintptr_t) count > max_hcount)
                {
                  max_hcount = (long int) (uintptr_t) count;
                  max_hval = hval;
+                 max_gid = ctf_dynhash_lookup (d->cd_output_first_gid, hval);
+               }
+             else if ((long int) (uintptr_t) count == max_hcount)
+               {
+                 void *gid = ctf_dynhash_lookup (d->cd_output_first_gid, hval);
+
+                 if (CTF_DEDUP_GID_TO_INPUT(gid) < CTF_DEDUP_GID_TO_INPUT(max_gid)
+                     || (CTF_DEDUP_GID_TO_INPUT(gid) == CTF_DEDUP_GID_TO_INPUT(max_gid)
+                         && CTF_DEDUP_GID_TO_TYPE(gid) < CTF_DEDUP_GID_TO_TYPE(max_gid)))
+                   {
+                     max_hval = hval;
+                     max_gid = ctf_dynhash_lookup (d->cd_output_first_gid, hval);
+                   }
                }
            }
          if (err != ECTF_NEXT_END)
@@ -1656,7 +1675,7 @@ ctf_dedup_init (ctf_dict_t *fp)
 
   if ((d->cd_conflicting_types
        = ctf_dynset_create (htab_hash_string,
-                           ctf_dynset_eq_string, NULL)) == NULL)
+                           htab_eq_string, NULL)) == NULL)
     goto oom;
 
   return 0;
@@ -1814,7 +1833,7 @@ ctf_dedup_conflictify_unshared (ctf_dict_t *output, ctf_dict_t **inputs)
   const void *k;
   ctf_dynset_t *to_mark = NULL;
 
-  if ((to_mark = ctf_dynset_create (htab_hash_string, ctf_dynset_eq_string,
+  if ((to_mark = ctf_dynset_create (htab_hash_string, htab_eq_string,
                                    NULL)) == NULL)
     goto err_no;
 
@@ -2351,7 +2370,7 @@ ctf_dedup_walk_output_mapping (ctf_dict_t *output, ctf_dict_t **inputs,
   void *k;
 
   if ((already_visited = ctf_dynset_create (htab_hash_string,
-                                           ctf_dynset_eq_string,
+                                           htab_eq_string,
                                            NULL)) == NULL)
     return ctf_set_errno (output, ENOMEM);
 
@@ -2652,6 +2671,8 @@ ctf_dedup_emit_type (const char *hval, ctf_dict_t *output, ctf_dict_t **inputs,
          ctf_parent_name_set (target, _CTF_SECTION);
 
          input->ctf_dedup.cd_output = target;
+         input->ctf_link_in_out = target;
+         target->ctf_link_in_out = input;
        }
       output_num = input_num;
     }