]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/ctfread.c
CTF: handle forward reference type
[thirdparty/binutils-gdb.git] / gdb / ctfread.c
index dfd543a39421fd7c3547d35bb6f3b78c5454497f..e2b65b622de20d6f5a0268931a64371264fba101 100644 (file)
@@ -115,6 +115,7 @@ struct ctf_context
 {
   ctf_dict_t *fp;
   struct objfile *of;
+  psymtab_storage *partial_symtabs;
   partial_symtab *pst;
   struct buildsym_compunit *builder;
 };
@@ -122,8 +123,11 @@ struct ctf_context
 /* A partial symtab, specialized for this module.  */
 struct ctf_psymtab : public standard_psymtab
 {
-  ctf_psymtab (const char *filename, struct objfile *objfile, CORE_ADDR addr)
-    : standard_psymtab (filename, objfile, addr)
+  ctf_psymtab (const char *filename,
+              psymtab_storage *partial_symtabs,
+              objfile_per_bfd_storage *objfile_per_bfd,
+              CORE_ADDR addr)
+    : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
   {
   }
 
@@ -186,6 +190,8 @@ static void process_structure_type (struct ctf_context *cp, ctf_id_t tid);
 static void process_struct_members (struct ctf_context *cp, ctf_id_t tid,
                                    struct type *type);
 
+static struct type *read_forward_type (struct ctf_context *cp, ctf_id_t tid);
+
 static struct symbol *new_symbol (struct ctf_context *cp, struct type *type,
                                  ctf_id_t tid);
 
@@ -269,6 +275,25 @@ get_tid_type (struct objfile *of, ctf_id_t tid)
     return nullptr;
 }
 
+/* Fetch the type for TID in CCP OF's tid_and_type hash, add the type to
+ *    context CCP if hash is empty or TID does not have a saved type.  */
+
+static struct type *
+fetch_tid_type (struct ctf_context *ccp, ctf_id_t tid)
+{
+  struct objfile *of = ccp->of;
+  struct type *typ;
+
+  typ = get_tid_type (of, tid);
+  if (typ == nullptr)
+    {
+      ctf_add_type_cb (tid, ccp);
+      typ = get_tid_type (of, tid);
+    }
+
+  return typ;
+}
+
 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM.  */
 
 static int
@@ -368,7 +393,7 @@ ctf_add_member_cb (const char *name,
   FIELD_NAME (*fp) = name;
 
   kind = ctf_type_kind (ccp->fp, tid);
-  t = get_tid_type (ccp->of, tid);
+  t = fetch_tid_type (ccp, tid);
   if (t == nullptr)
     {
       t = read_type_record (ccp, tid);
@@ -663,7 +688,7 @@ read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
 
   type->set_code (TYPE_CODE_FUNC);
   ctf_func_type_info (fp, tid, &cfi);
-  rettype = get_tid_type (of, cfi.ctc_return);
+  rettype = fetch_tid_type (ccp, cfi.ctc_return);
   TYPE_TARGET_TYPE (type) = rettype;
   set_type_align (type, ctf_type_align (fp, tid));
 
@@ -790,11 +815,11 @@ read_array_type (struct ctf_context *ccp, ctf_id_t tid)
       return nullptr;
     }
 
-  element_type = get_tid_type (objfile, ar.ctr_contents);
+  element_type = fetch_tid_type (ccp, ar.ctr_contents);
   if (element_type == nullptr)
     return nullptr;
 
-  idx_type = get_tid_type (objfile, ar.ctr_index);
+  idx_type = fetch_tid_type (ccp, ar.ctr_index);
   if (idx_type == nullptr)
     idx_type = objfile_type (objfile)->builtin_int;
 
@@ -822,7 +847,7 @@ read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
   struct objfile *objfile = ccp->of;
   struct type *base_type, *cv_type;
 
-  base_type = get_tid_type (objfile, btid);
+  base_type = fetch_tid_type (ccp, btid);
   if (base_type == nullptr)
     {
       base_type = read_type_record (ccp, btid);
@@ -846,7 +871,7 @@ read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
   ctf_dict_t *fp = ccp->fp;
   struct type *base_type, *cv_type;
 
-  base_type = get_tid_type (objfile, btid);
+  base_type = fetch_tid_type (ccp, btid);
   if (base_type == nullptr)
     {
       base_type = read_type_record (ccp, btid);
@@ -872,7 +897,7 @@ read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
   struct objfile *objfile = ccp->of;
   struct type *base_type, *cv_type;
 
-  base_type = get_tid_type (objfile, btid);
+  base_type = fetch_tid_type (ccp, btid);
   if (base_type == nullptr)
     {
       base_type = read_type_record (ccp, btid);
@@ -899,7 +924,7 @@ read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
   char *aname = obstack_strdup (&objfile->objfile_obstack, name);
   this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, aname);
   set_tid_type (objfile, tid, this_type);
-  target_type = get_tid_type (objfile, btid);
+  target_type = fetch_tid_type (ccp, btid);
   if (target_type != this_type)
     TYPE_TARGET_TYPE (this_type) = target_type;
   else
@@ -918,7 +943,7 @@ read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
   struct objfile *of = ccp->of;
   struct type *target_type, *type;
 
-  target_type = get_tid_type (of, btid);
+  target_type = fetch_tid_type (ccp, btid);
   if (target_type == nullptr)
     {
       target_type = read_type_record (ccp, btid);
@@ -935,6 +960,34 @@ read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
   return set_tid_type (of, tid, type);
 }
 
+/* Read information from a TID of CTF_K_FORWARD.  */
+
+static struct type *
+read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
+{
+  struct objfile *of = ccp->of;
+  ctf_dict_t *fp = ccp->fp;
+  struct type *type;
+  uint32_t kind;
+
+  type = alloc_type (of);
+
+  gdb::unique_xmalloc_ptr<char> name (ctf_type_aname_raw (fp, tid));
+  if (name != NULL && strlen (name.get()) != 0)
+    type->set_name (obstack_strdup (&of->objfile_obstack, name.get ()));
+
+  kind = ctf_type_kind_forwarded (fp, tid);
+  if (kind == CTF_K_UNION)
+    type->set_code (TYPE_CODE_UNION);
+  else
+    type->set_code (TYPE_CODE_STRUCT);
+
+  TYPE_LENGTH (type) = 0;
+  type->set_is_stub (true);
+
+  return set_tid_type (of, tid, type);
+}
+
 /* Read information associated with type TID.  */
 
 static struct type *
@@ -988,6 +1041,9 @@ read_type_record (struct ctf_context *ccp, ctf_id_t tid)
       case CTF_K_ARRAY:
        type = read_array_type (ccp, tid);
        break;
+      case CTF_K_FORWARD:
+       type = read_forward_type (ccp, tid);
+       break;
       case CTF_K_UNKNOWN:
        break;
       default:
@@ -1134,7 +1190,7 @@ add_stt_obj (struct ctf_context *ccp, unsigned long idx)
   if ((tid = ctf_lookup_by_symbol (ccp->fp, idx)) == CTF_ERR)
     return nullptr;
 
-  type = get_tid_type (ccp->of, tid);
+  type = fetch_tid_type (ccp, tid);
   if (type == nullptr)
     return nullptr;
 
@@ -1168,7 +1224,7 @@ add_stt_func (struct ctf_context *ccp, unsigned long idx)
     return nullptr;
 
   tid = ctf_lookup_by_symbol (ccp->fp, idx);
-  ftype = get_tid_type (ccp->of, tid);
+  ftype = fetch_tid_type (ccp, tid);
   if ((finfo.ctc_flags & CTF_FUNC_VARARG) != 0)
     ftype->set_has_varargs (true);
   ftype->set_num_fields (argc);
@@ -1182,7 +1238,7 @@ add_stt_func (struct ctf_context *ccp, unsigned long idx)
      to find the argument type.  */
   for (int iparam = 0; iparam < argc; iparam++)
     {
-      atyp = get_tid_type (ccp->of, argv[iparam]);
+      atyp = fetch_tid_type (ccp, argv[iparam]);
       if (atyp)
        ftype->field (iparam).set_type (atyp);
       else
@@ -1190,7 +1246,7 @@ add_stt_func (struct ctf_context *ccp, unsigned long idx)
     }
 
   sym = new_symbol (ccp, ftype, tid);
-  rettyp = get_tid_type (ccp->of, finfo.ctc_return);
+  rettyp = fetch_tid_type (ccp, finfo.ctc_return);
   if (rettyp != nullptr)
     SYMBOL_TYPE (sym) = rettyp;
   else
@@ -1245,6 +1301,27 @@ ctf_end_symtab (ctf_psymtab *pst,
   return result;
 }
 
+/* Add all members of an enum with type TID to partial symbol table.  */
+
+static void
+ctf_psymtab_add_enums (struct ctf_context *ccp, ctf_id_t tid)
+{
+  int val;
+  const char *ename;
+  ctf_next_t *i = nullptr;
+
+  while ((ename = ctf_enum_next (ccp->fp, tid, &i, &val)) != nullptr)
+    {
+      ccp->pst->add_psymbol (ename, true,
+                            VAR_DOMAIN, LOC_CONST, -1,
+                            psymbol_placement::GLOBAL,
+                            0, language_c, ccp->partial_symtabs, ccp->of);
+    }
+  if (ctf_errno (ccp->fp) != ECTF_NEXT_END)
+    complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
+              ctf_errmsg (ctf_errno (ccp->fp)));
+}
+
 /* Read in full symbols for PST, and anything it depends on.  */
 
 void
@@ -1338,16 +1415,18 @@ ctf_psymtab::read_symtab (struct objfile *objfile)
 static ctf_psymtab *
 create_partial_symtab (const char *name,
                       ctf_dict_t *cfp,
+                      psymtab_storage *partial_symtabs,
                       struct objfile *objfile)
 {
   ctf_psymtab *pst;
   struct ctf_context *ccx;
 
-  pst = new ctf_psymtab (name, objfile, 0);
+  pst = new ctf_psymtab (name, partial_symtabs, objfile->per_bfd, 0);
 
   ccx = XOBNEW (&objfile->objfile_obstack, struct ctf_context);
   ccx->fp = cfp;
   ccx->of = objfile;
+  ccx->partial_symtabs = partial_symtabs;
   ccx->pst = pst;
   ccx->builder = nullptr;
   pst->context = ccx;
@@ -1366,17 +1445,17 @@ ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
 
   ccp = (struct ctf_context *) arg;
   gdb::unique_xmalloc_ptr<char> name (ctf_type_aname_raw (ccp->fp, tid));
-  if (name == nullptr || strlen (name.get ()) == 0)
-    return 0;
 
   domain_enum domain = UNDEF_DOMAIN;
   enum address_class aclass = LOC_UNDEF;
   kind = ctf_type_kind (ccp->fp, tid);
   switch (kind)
     {
+      case CTF_K_ENUM:
+       ctf_psymtab_add_enums (ccp, tid);
+       /* FALL THROUGH */
       case CTF_K_STRUCT:
       case CTF_K_UNION:
-      case CTF_K_ENUM:
        domain = STRUCT_DOMAIN;
        aclass = LOC_TYPEDEF;
        break;
@@ -1407,10 +1486,13 @@ ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
        return 0;
     }
 
+  if (name == nullptr || strlen (name.get ()) == 0)
+    return 0;
+
   ccp->pst->add_psymbol (name.get (), true,
                         domain, aclass, section,
                         psymbol_placement::GLOBAL,
-                        0, language_c, ccp->of);
+                        0, language_c, ccp->partial_symtabs, ccp->of);
 
   return 0;
 }
@@ -1425,7 +1507,7 @@ ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
   ccp->pst->add_psymbol (name, true,
                         VAR_DOMAIN, LOC_STATIC, -1,
                         psymbol_placement::GLOBAL,
-                        0, language_c, ccp->of);
+                        0, language_c, ccp->partial_symtabs, ccp->of);
   return 0;
 }
 
@@ -1433,11 +1515,12 @@ ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
    debugging information is available.  */
 
 static void
-scan_partial_symbols (ctf_dict_t *cfp, struct objfile *of)
+scan_partial_symbols (ctf_dict_t *cfp, psymtab_storage *partial_symtabs,
+                     struct objfile *of)
 {
   bfd *abfd = of->obfd;
   const char *name = bfd_get_filename (abfd);
-  ctf_psymtab *pst = create_partial_symtab (name, cfp, of);
+  ctf_psymtab *pst = create_partial_symtab (name, cfp, partial_symtabs, of);
 
   struct ctf_context *ccx = pst->context;
 
@@ -1488,7 +1571,7 @@ scan_partial_symbols (ctf_dict_t *cfp, struct objfile *of)
       pst->add_psymbol (tname.get (), true,
                        tdomain, aclass, -1,
                        psymbol_placement::STATIC,
-                       0, language_c, of);
+                       0, language_c, partial_symtabs, of);
     }
 
   pst->end ();
@@ -1515,7 +1598,10 @@ elfctf_build_psymtabs (struct objfile *of)
           bfd_get_filename (abfd), ctf_errmsg (err));
   ctf_dict_key.emplace (of, fp);
 
-  scan_partial_symbols (fp, of);
+  psymbol_functions *psf = new psymbol_functions ();
+  psymtab_storage *partial_symtabs = psf->get_partial_symtabs ().get ();
+  of->qf.emplace_front (psf);
+  scan_partial_symbols (fp, partial_symtabs, of);
 }
 
 #else