Remove some explicit memory management from dwarf2read.c
[deliverable/binutils-gdb.git] / libctf / ctf-link.c
index 8f18a4927163027379a64bc9e35262e875ac69e8..31179ae13d7c2f349a6997baf79357bd6f8d86b1 100644 (file)
@@ -1,5 +1,5 @@
 /* CTF linking.
-   Copyright (C) 2019 Free Software Foundation, Inc.
+   Copyright (C) 2019-2020 Free Software Foundation, Inc.
 
    This file is part of libctf.
 
 #include <ctf-impl.h>
 #include <string.h>
 
+/* Type tracking machinery.  */
+
+/* Record the correspondence between a source and ctf_add_type()-added
+   destination type: both types are translated into parent type IDs if need be,
+   so they relate to the actual container they are in.  Outside controlled
+   circumstances (like linking) it is probably not useful to do more than
+   compare these pointers, since there is nothing stopping the user closing the
+   source container whenever they want to.
+
+   Our OOM handling here is just to not do anything, because this is called deep
+   enough in the call stack that doing anything useful is painfully difficult:
+   the worst consequence if we do OOM is a bit of type duplication anyway.  */
+
+void
+ctf_add_type_mapping (ctf_file_t *src_fp, ctf_id_t src_type,
+                     ctf_file_t *dst_fp, ctf_id_t dst_type)
+{
+  if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent)
+    src_fp = src_fp->ctf_parent;
+
+  src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
+
+  if (LCTF_TYPE_ISPARENT (dst_fp, dst_type) && dst_fp->ctf_parent)
+    dst_fp = dst_fp->ctf_parent;
+
+  dst_type = LCTF_TYPE_TO_INDEX(dst_fp, dst_type);
+
+  /* This dynhash is a bit tricky: it has a multivalued (structural) key, so we
+     need to use the sized-hash machinery to generate key hashing and equality
+     functions.  */
+
+  if (dst_fp->ctf_link_type_mapping == NULL)
+    {
+      ctf_hash_fun f = ctf_hash_type_mapping_key;
+      ctf_hash_eq_fun e = ctf_hash_eq_type_mapping_key;
+
+      if ((dst_fp->ctf_link_type_mapping = ctf_dynhash_create (f, e, free,
+                                                              NULL)) == NULL)
+       return;
+    }
+
+  ctf_link_type_mapping_key_t *key;
+  key = calloc (1, sizeof (struct ctf_link_type_mapping_key));
+  if (!key)
+    return;
+
+  key->cltm_fp = src_fp;
+  key->cltm_idx = src_type;
+
+  ctf_dynhash_insert (dst_fp->ctf_link_type_mapping, key,
+                     (void *) (uintptr_t) dst_type);
+}
+
+/* Look up a type mapping: return 0 if none.  The DST_FP is modified to point to
+   the parent if need be.  The ID returned is from the dst_fp's perspective.  */
+ctf_id_t
+ctf_type_mapping (ctf_file_t *src_fp, ctf_id_t src_type, ctf_file_t **dst_fp)
+{
+  ctf_link_type_mapping_key_t key;
+  ctf_file_t *target_fp = *dst_fp;
+  ctf_id_t dst_type = 0;
+
+  if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent)
+    src_fp = src_fp->ctf_parent;
+
+  src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
+  key.cltm_fp = src_fp;
+  key.cltm_idx = src_type;
+
+  if (target_fp->ctf_link_type_mapping)
+    dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping,
+                                              &key);
+
+  if (dst_type != 0)
+    {
+      dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
+                                    target_fp->ctf_parent != NULL);
+      *dst_fp = target_fp;
+      return dst_type;
+    }
+
+  if (target_fp->ctf_parent)
+    target_fp = target_fp->ctf_parent;
+  else
+    return 0;
+
+  if (target_fp->ctf_link_type_mapping)
+    dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping,
+                                              &key);
+
+  if (dst_type)
+    dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
+                                  target_fp->ctf_parent != NULL);
+
+  *dst_fp = target_fp;
+  return dst_type;
+}
+
 /* Linker machinery.
 
    CTF linking consists of adding CTF archives full of content to be merged into
@@ -77,6 +175,136 @@ ctf_link_add_ctf (ctf_file_t *fp, ctf_archive_t *ctf, const char *name)
   return (ctf_set_errno (fp, ENOMEM));
 }
 
+/* Return a per-CU output CTF dictionary suitable for the given CU, creating and
+   interning it if need be.  */
+
+static ctf_file_t *
+ctf_create_per_cu (ctf_file_t *fp, const char *filename, const char *cuname)
+{
+  ctf_file_t *cu_fp;
+  const char *ctf_name = NULL;
+  char *dynname = NULL;
+
+  /* First, check the mapping table and translate the per-CU name we use
+     accordingly.  We check both the input filename and the CU name.  Only if
+     neither are set do we fall back to the input filename as the per-CU
+     dictionary name.  We prefer the filename because this is easier for likely
+     callers to determine.  */
+
+  if (fp->ctf_link_cu_mapping)
+    {
+      if (((ctf_name = ctf_dynhash_lookup (fp->ctf_link_cu_mapping, filename)) == NULL) &&
+         ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_cu_mapping, cuname)) == NULL))
+       ctf_name = filename;
+    }
+
+  if (ctf_name == NULL)
+    ctf_name = filename;
+
+  if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL)
+    {
+      int err;
+
+      if ((cu_fp = ctf_create (&err)) == NULL)
+       {
+         ctf_dprintf ("Cannot create per-CU CTF archive for CU %s from "
+                      "input file %s: %s\n", cuname, filename,
+                      ctf_errmsg (err));
+         ctf_set_errno (fp, err);
+         return NULL;
+       }
+
+      if ((dynname = strdup (ctf_name)) == NULL)
+       goto oom;
+      if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
+       goto oom;
+
+      ctf_import (cu_fp, fp);
+      ctf_cuname_set (cu_fp, cuname);
+      ctf_parent_name_set (cu_fp, _CTF_SECTION);
+    }
+  return cu_fp;
+
+ oom:
+  free (dynname);
+  ctf_file_close (cu_fp);
+  ctf_set_errno (fp, ENOMEM);
+  return NULL;
+}
+
+/* Add a mapping directing that the CU named FROM should have its
+   conflicting/non-duplicate types (depending on link mode) go into a container
+   named TO.  Many FROMs can share a TO: in this case, the effect on conflicting
+   types is not yet defined (but in time an auto-renaming algorithm will be
+   added: ugly, but there is really no right thing one can do in this
+   situation).
+
+   We forcibly add a container named TO in every case, even though it may well
+   wind up empty, because clients that use this facility usually expect to find
+   every TO container present, even if empty, and malfunction otherwise.  */
+
+int
+ctf_link_add_cu_mapping (ctf_file_t *fp, const char *from, const char *to)
+{
+  int err;
+  char *f, *t;
+
+  if (fp->ctf_link_cu_mapping == NULL)
+    fp->ctf_link_cu_mapping = ctf_dynhash_create (ctf_hash_string,
+                                                 ctf_hash_eq_string, free,
+                                                 free);
+  if (fp->ctf_link_cu_mapping == NULL)
+    return ctf_set_errno (fp, ENOMEM);
+
+  if (fp->ctf_link_outputs == NULL)
+    fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
+                                              ctf_hash_eq_string, free,
+                                              ctf_file_close_thunk);
+
+  if (fp->ctf_link_outputs == NULL)
+    return ctf_set_errno (fp, ENOMEM);
+
+  f = strdup (from);
+  t = strdup (to);
+  if (!f || !t)
+    goto oom;
+
+  if (ctf_create_per_cu (fp, t, t) == NULL)
+    goto oom_noerrno;                          /* Errno is set for us.  */
+
+  err = ctf_dynhash_insert (fp->ctf_link_cu_mapping, f, t);
+  if (err)
+    {
+      ctf_set_errno (fp, err);
+      goto oom_noerrno;
+    }
+
+  return 0;
+
+ oom:
+  ctf_set_errno (fp, errno);
+ oom_noerrno:
+  free (f);
+  free (t);
+  return -1;
+}
+
+/* Set a function which is called to transform the names of archive members.
+   This is useful for applying regular transformations to many names, where
+   ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
+   names.  The member name changer is applied at ctf_link_write time, so it
+   cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
+   The changer function accepts a name and should return a new
+   dynamically-allocated name, or NULL if the name should be left unchanged.  */
+void
+ctf_link_set_memb_name_changer (ctf_file_t *fp,
+                               ctf_link_memb_name_changer_f *changer,
+                               void *arg)
+{
+  fp->ctf_link_memb_name_changer = changer;
+  fp->ctf_link_memb_name_changer_arg = arg;
+}
+
 typedef struct ctf_link_in_member_cb_arg
 {
   ctf_file_t *out_fp;
@@ -120,42 +348,32 @@ ctf_link_one_type (ctf_id_t type, int isroot _libctf_unused_, void *arg_)
       err = ctf_errno (arg->out_fp);
       if (err != ECTF_CONFLICT)
        {
-         ctf_dprintf ("Cannot link type %lx from archive member %s, input file %s "
-                      "into output link: %s\n", type, arg->arcname, arg->file_name,
-                      ctf_errmsg (err));
-         return -1;
+         if (err != ECTF_NONREPRESENTABLE)
+           ctf_dprintf ("Cannot link type %lx from archive member %s, input file %s "
+                        "into output link: %s\n", type, arg->arcname, arg->file_name,
+                        ctf_errmsg (err));
+         /* We must ignore this problem or we end up losing future types, then
+            trying to link the variables in, then exploding.  Better to link as
+            much as possible.  XXX when we add a proper link warning
+            infrastructure, we should report the error here!  */
+         return 0;
        }
       ctf_set_errno (arg->out_fp, 0);
     }
 
-  if ((per_cu_out_fp = ctf_dynhash_lookup (arg->out_fp->ctf_link_outputs,
-                                          arg->arcname)) == NULL)
-    {
-      int err;
-
-      if ((per_cu_out_fp = ctf_create (&err)) == NULL)
-       {
-         ctf_dprintf ("Cannot create per-CU CTF archive for member %s: %s\n",
-                      arg->arcname, ctf_errmsg (err));
-         ctf_set_errno (arg->out_fp, err);
-         return -1;
-       }
-
-      if (ctf_dynhash_insert (arg->out_fp->ctf_link_outputs, arg->arcname,
-                             per_cu_out_fp) < 0)
-       {
-         ctf_set_errno (arg->out_fp, ENOMEM);
-         return -1;
-       }
-
-      ctf_import (per_cu_out_fp, arg->out_fp);
-      ctf_cuname_set (per_cu_out_fp, arg->cu_name);
-    }
+  if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->file_name,
+                                         arg->cu_name)) == NULL)
+    return -1;                                 /* Errno is set for us.  */
 
   if (ctf_add_type (per_cu_out_fp, arg->in_fp, type) != CTF_ERR)
     return 0;
 
   err = ctf_errno (per_cu_out_fp);
+  if (err != ECTF_NONREPRESENTABLE)
+    ctf_dprintf ("Cannot link type %lx from CTF archive member %s, input file %s "
+                "into output per-CU CTF archive member %s: %s: skipped\n", type,
+                arg->arcname, arg->file_name, arg->arcname,
+                ctf_errmsg (err));
   if (err == ECTF_CONFLICT)
       /* Conflicts are possible at this stage only if a non-ld user has combined
         multiple TUs into a single output dictionary.  Even in this case we do not
@@ -165,6 +383,95 @@ ctf_link_one_type (ctf_id_t type, int isroot _libctf_unused_, void *arg_)
   return 0;                                    /* As above: do not lose types.  */
 }
 
+/* Check if we can safely add a variable with the given type to this container.  */
+
+static int
+check_variable (const char *name, ctf_file_t *fp, ctf_id_t type,
+               ctf_dvdef_t **out_dvd)
+{
+  ctf_dvdef_t *dvd;
+
+  dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
+  *out_dvd = dvd;
+  if (!dvd)
+    return 1;
+
+  if (dvd->dvd_type != type)
+    {
+      /* Variable here.  Wrong type: cannot add.  Just skip it, because there is
+        no way to express this in CTF.  (This might be the parent, in which
+        case we'll try adding in the child first, and only then give up.)  */
+      ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
+    }
+
+  return 0;                                  /* Already exists.  */
+}
+
+/* Link one variable in.  */
+
+static int
+ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_)
+{
+  ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
+  ctf_file_t *per_cu_out_fp;
+  ctf_id_t dst_type = 0;
+  ctf_file_t *check_fp;
+  ctf_dvdef_t *dvd;
+
+  /* In unconflicted link mode, if this type is mapped to a type in the parent
+     container, we want to try to add to that first: if it reports a duplicate,
+     or if the type is in a child already, add straight to the child.  */
+
+  check_fp = arg->out_fp;
+
+  dst_type = ctf_type_mapping (arg->in_fp, type, &check_fp);
+  if (dst_type != 0)
+    {
+      if (check_fp == arg->out_fp)
+       {
+         if (check_variable (name, check_fp, dst_type, &dvd))
+           {
+             /* No variable here: we can add it.  */
+             if (ctf_add_variable (check_fp, name, dst_type) < 0)
+               return (ctf_set_errno (arg->out_fp, ctf_errno (check_fp)));
+             return 0;
+           }
+
+         /* Already present?  Nothing to do.  */
+         if (dvd && dvd->dvd_type == type)
+           return 0;
+       }
+    }
+
+  /* Can't add to the parent due to a name clash, or because it references a
+     type only present in the child.  Try adding to the child, creating if need
+     be.  */
+
+  if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->file_name,
+                                         arg->cu_name)) == NULL)
+    return -1;                                 /* Errno is set for us.  */
+
+  /* If the type was not found, check for it in the child too. */
+  if (dst_type == 0)
+    {
+      check_fp = per_cu_out_fp;
+      dst_type = ctf_type_mapping (arg->in_fp, type, &check_fp);
+
+      if (dst_type == 0)
+       {
+         ctf_dprintf ("Type %lx for variable %s in input file %s not "
+                      "found: skipped.\n", type, name, arg->file_name);
+         /* Do not terminate the link: just skip the variable.  */
+         return 0;
+       }
+    }
+
+  if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
+    if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
+      return (ctf_set_errno (arg->out_fp, ctf_errno (per_cu_out_fp)));
+  return 0;
+}
+
 /* Merge every type and variable in this archive member into the link, so we can
    relink things that have already had ld run on them.  We use the archive
    member name, sans any leading '.ctf.', as the CU name for ambiguous types if
@@ -218,7 +525,8 @@ ctf_link_one_input_archive_member (ctf_file_t *in_fp, const char *name, void *ar
     arg->cu_name += strlen (".ctf.");
   arg->in_fp = in_fp;
 
-  err = ctf_type_iter_all (in_fp, ctf_link_one_type, arg);
+  if ((err = ctf_type_iter_all (in_fp, ctf_link_one_type, arg)) > -1)
+    err = ctf_variable_iter (in_fp, ctf_link_one_variable, arg);
 
   arg->in_input_cu_file = 0;
   free (arg->arcname);
@@ -229,6 +537,17 @@ ctf_link_one_input_archive_member (ctf_file_t *in_fp, const char *name, void *ar
   return 0;
 }
 
+/* Dump the unnecessary link type mapping after one input file is processed.  */
+static void
+empty_link_type_mapping (void *key _libctf_unused_, void *value,
+                        void *arg _libctf_unused_)
+{
+  ctf_file_t *fp = (ctf_file_t *) value;
+
+  if (fp->ctf_link_type_mapping)
+    ctf_dynhash_empty (fp->ctf_link_type_mapping);
+}
+
 /* Link one input file's types into the output file.  */
 static void
 ctf_link_one_input_archive (void *key, void *value, void *arg_)
@@ -267,6 +586,11 @@ ctf_link_one_input_archive (void *key, void *value, void *arg_)
       ctf_set_errno (arg->out_fp, 0);
     }
   ctf_file_close (arg->main_input_fp);
+
+  /* Discard the now-unnecessary mapping table data.  */
+  if (arg->out_fp->ctf_link_type_mapping)
+    ctf_dynhash_empty (arg->out_fp->ctf_link_type_mapping);
+  ctf_dynhash_iter (arg->out_fp->ctf_link_outputs, empty_link_type_mapping, NULL);
 }
 
 /* Merge types and variable sections in all files added to the link
@@ -315,7 +639,7 @@ ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
   ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
 
   fp->ctf_flags |= LCTF_DIRTY;
-  if (ctf_str_add_external (fp, arg->str, arg->offset) == NULL)
+  if (!ctf_str_add_external (fp, arg->str, arg->offset))
     arg->err = ENOMEM;
 }
 
@@ -338,7 +662,7 @@ ctf_link_add_strtab (ctf_file_t *fp, ctf_link_strtab_string_f *add_string,
       ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
 
       fp->ctf_flags |= LCTF_DIRTY;
-      if (ctf_str_add_external (fp, str, offset) == NULL)
+      if (!ctf_str_add_external (fp, str, offset))
        err = ENOMEM;
 
       ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
@@ -365,10 +689,11 @@ typedef struct ctf_name_list_accum_cb_arg
   ctf_file_t *fp;
   ctf_file_t **files;
   size_t i;
+  char **dynames;
+  size_t ndynames;
 } ctf_name_list_accum_cb_arg_t;
 
-/* Accumulate the names and a count of the names in the link output hash,
-   and run ctf_update() on them to generate them.  */
+/* Accumulate the names and a count of the names in the link output hash.  */
 static void
 ctf_accumulate_archive_names (void *key, void *value, void *arg_)
 {
@@ -377,13 +702,6 @@ ctf_accumulate_archive_names (void *key, void *value, void *arg_)
   char **names;
   ctf_file_t **files;
   ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
-  int err;
-
-  if ((err = ctf_update (fp)) < 0)
-    {
-      ctf_set_errno (arg->fp, ctf_errno (fp));
-      return;
-    }
 
   if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
     {
@@ -398,12 +716,51 @@ ctf_accumulate_archive_names (void *key, void *value, void *arg_)
       ctf_set_errno (arg->fp, ENOMEM);
       return;
     }
+
+  /* Allow the caller to get in and modify the name at the last minute.  If the
+     caller *does* modify the name, we have to stash away the new name the
+     caller returned so we can free it later on.  (The original name is the key
+     of the ctf_link_outputs hash and is freed by the dynhash machinery.)  */
+
+  if (fp->ctf_link_memb_name_changer)
+    {
+      char **dynames;
+      char *dyname;
+      void *nc_arg = fp->ctf_link_memb_name_changer_arg;
+
+      dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
+
+      if (dyname != NULL)
+       {
+         if ((dynames = realloc (arg->dynames,
+                                 sizeof (char *) * ++(arg->ndynames))) == NULL)
+           {
+             (arg->ndynames)--;
+             ctf_set_errno (arg->fp, ENOMEM);
+             return;
+           }
+           arg->dynames = dynames;
+           name = (const char *) dyname;
+       }
+    }
+
   arg->names = names;
   arg->names[(arg->i) - 1] = (char *) name;
   arg->files = files;
   arg->files[(arg->i) - 1] = fp;
 }
 
+/* Change the name of the parent CTF section, if the name transformer has got to
+   it.  */
+static void
+ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
+{
+  ctf_file_t *fp = (ctf_file_t *) value;
+  const char *name = (const char *) arg;
+
+  ctf_parent_name_set (fp, name);
+}
+
 /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
    (otherwise) into a new dynamically-allocated string, and return it.
    Members with sizes above THRESHOLD are compressed.  */
@@ -412,6 +769,7 @@ ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
 {
   ctf_name_list_accum_cb_arg_t arg;
   char **names;
+  char *transformed_name = NULL;
   ctf_file_t **files;
   FILE *f = NULL;
   int err;
@@ -422,12 +780,6 @@ ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
   memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
   arg.fp = fp;
 
-  if (ctf_update (fp) < 0)
-    {
-      errloc = "CTF file construction";
-      goto err;
-    }
-
   if (fp->ctf_link_outputs)
     {
       ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
@@ -451,7 +803,22 @@ ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
     }
   arg.names = names;
   memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
+
   arg.names[0] = (char *) _CTF_SECTION;
+  if (fp->ctf_link_memb_name_changer)
+    {
+      void *nc_arg = fp->ctf_link_memb_name_changer_arg;
+
+      transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
+                                                        nc_arg);
+
+      if (transformed_name != NULL)
+       {
+         arg.names[0] = transformed_name;
+         ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
+                           transformed_name);
+       }
+    }
 
   if ((files = realloc (arg.files,
                        sizeof (struct ctf_file *) * (arg.i + 1))) == NULL)
@@ -512,6 +879,14 @@ ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
   *size = fsize;
   free (arg.names);
   free (arg.files);
+  free (transformed_name);
+  if (arg.ndynames)
+    {
+      size_t i;
+      for (i = 0; i < arg.ndynames; i++)
+       free (arg.dynames[i]);
+      free (arg.dynames);
+    }
   return buf;
 
  err_no:
@@ -522,6 +897,14 @@ ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
     fclose (f);
   free (arg.names);
   free (arg.files);
+  free (transformed_name);
+  if (arg.ndynames)
+    {
+      size_t i;
+      for (i = 0; i < arg.ndynames; i++)
+       free (arg.dynames[i]);
+      free (arg.dynames);
+    }
   ctf_dprintf ("Cannot write archive in link: %s failure: %s\n", errloc,
               ctf_errmsg (ctf_errno (fp)));
   return NULL;
This page took 0.049167 seconds and 4 git commands to generate.