xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/ctfread.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Compact ANSI-C Type Format (CTF) support in GDB.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 2019-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos /* This file format can be used to compactly represent the information needed
217d62b00eSchristos    by a debugger to interpret the ANSI-C types used by a given program.
227d62b00eSchristos    Traditionally, this kind of information is generated by the compiler when
237d62b00eSchristos    invoked with the -g flag and is stored in "stabs" strings or in the more
247d62b00eSchristos    modern DWARF format. A new -gtLEVEL option has been added in gcc to generate
257d62b00eSchristos    such information. CTF provides a representation of only the information
267d62b00eSchristos    that is relevant to debugging a complex, optimized C program such as the
277d62b00eSchristos    operating system kernel in a form that is significantly more compact than
287d62b00eSchristos    the equivalent stabs or DWARF representation.  The format is data-model
297d62b00eSchristos    independent, so consumers do not need different code depending on whether
307d62b00eSchristos    they are 32-bit or 64-bit programs.  CTF assumes that a standard ELF symbol
317d62b00eSchristos    table is available for use in the debugger, and uses the structure and data
327d62b00eSchristos    of the symbol table to avoid storing redundant information.  The CTF data
337d62b00eSchristos    may be compressed on disk or in memory, indicated by a bit in the header.
347d62b00eSchristos    CTF may be interpreted in a raw disk file, or it may be stored in an ELF
357d62b00eSchristos    section, typically named .ctf.  Data structures are aligned so that a raw
367d62b00eSchristos    CTF file or CTF ELF section may be manipulated using mmap(2).
377d62b00eSchristos 
387d62b00eSchristos    The CTF file or section itself has the following structure:
397d62b00eSchristos 
407d62b00eSchristos    +--------+--------+---------+----------+----------+-------+--------+
417d62b00eSchristos    |  file  |  type  |  data   | function | variable | data  | string |
427d62b00eSchristos    | header | labels | objects |   info   |   info   | types | table  |
437d62b00eSchristos    +--------+--------+---------+----------+----------+-------+--------+
447d62b00eSchristos 
457d62b00eSchristos    The file header stores a magic number and version information, encoding
467d62b00eSchristos    flags, and the byte offset of each of the sections relative to the end of the
477d62b00eSchristos    header itself.  If the CTF data has been uniquified against another set of
487d62b00eSchristos    CTF data, a reference to that data also appears in the header.  This
497d62b00eSchristos    reference is the name of the label corresponding to the types uniquified
507d62b00eSchristos    against.
517d62b00eSchristos 
527d62b00eSchristos    Following the header is a list of labels, used to group the types included in
537d62b00eSchristos    the data types section.  Each label is accompanied by a type ID i.  A given
547d62b00eSchristos    label refers to the group of types whose IDs are in the range [0, i].
557d62b00eSchristos 
567d62b00eSchristos    Data object and function records are stored in the same order as they appear
577d62b00eSchristos    in the corresponding symbol table, except that symbols marked SHN_UNDEF are
587d62b00eSchristos    not stored and symbols that have no type data are padded out with zeroes.
597d62b00eSchristos    For each data object, the type ID (a small integer) is recorded.  For each
607d62b00eSchristos    function, the type ID of the return type and argument types is recorded.
617d62b00eSchristos 
627d62b00eSchristos    Variable records (as distinct from data objects) provide a modicum of support
637d62b00eSchristos    for non-ELF systems, mapping a variable name to a CTF type ID.  The variable
647d62b00eSchristos    names are sorted into ASCIIbetical order, permitting binary searching.
657d62b00eSchristos 
667d62b00eSchristos    The data types section is a list of variable size records that represent each
677d62b00eSchristos    type, in order by their ID.  The types themselves form a directed graph,
687d62b00eSchristos    where each node may contain one or more outgoing edges to other type nodes,
697d62b00eSchristos    denoted by their ID.
707d62b00eSchristos 
717d62b00eSchristos    Strings are recorded as a string table ID (0 or 1) and a byte offset into the
727d62b00eSchristos    string table.  String table 0 is the internal CTF string table.  String table
737d62b00eSchristos    1 is the external string table, which is the string table associated with the
747d62b00eSchristos    ELF symbol table for this object.  CTF does not record any strings that are
757d62b00eSchristos    already in the symbol table, and the CTF string table does not contain any
767d62b00eSchristos    duplicated strings.  */
777d62b00eSchristos 
787d62b00eSchristos #include "defs.h"
797d62b00eSchristos #include "buildsym.h"
807d62b00eSchristos #include "complaints.h"
817d62b00eSchristos #include "block.h"
827d62b00eSchristos #include "ctfread.h"
837d62b00eSchristos #include "psympriv.h"
847d62b00eSchristos 
857d62b00eSchristos #if ENABLE_LIBCTF
867d62b00eSchristos 
877d62b00eSchristos #include "ctf.h"
887d62b00eSchristos #include "ctf-api.h"
897d62b00eSchristos 
90*6881a400Schristos static const registry<objfile>::key<htab, htab_deleter> ctf_tid_key;
917d62b00eSchristos 
927d62b00eSchristos struct ctf_fp_info
937d62b00eSchristos {
94*6881a400Schristos   explicit ctf_fp_info (ctf_dict_t *cfp) : fp (cfp) {}
957d62b00eSchristos   ~ctf_fp_info ();
96*6881a400Schristos   ctf_dict_t *fp;
977d62b00eSchristos };
987d62b00eSchristos 
99*6881a400Schristos /* Cleanup function for the ctf_dict_key data.  */
1007d62b00eSchristos ctf_fp_info::~ctf_fp_info ()
1017d62b00eSchristos {
102*6881a400Schristos   if (fp == nullptr)
1037d62b00eSchristos     return;
1047d62b00eSchristos 
1057d62b00eSchristos   ctf_archive_t *arc = ctf_get_arc (fp);
106*6881a400Schristos   ctf_dict_close (fp);
1077d62b00eSchristos   ctf_close (arc);
1087d62b00eSchristos }
1097d62b00eSchristos 
110*6881a400Schristos static const registry<objfile>::key<ctf_fp_info> ctf_dict_key;
1117d62b00eSchristos 
1127d62b00eSchristos /* A CTF context consists of a file pointer and an objfile pointer.  */
1137d62b00eSchristos 
1147d62b00eSchristos struct ctf_context
1157d62b00eSchristos {
116*6881a400Schristos   ctf_dict_t *fp;
1177d62b00eSchristos   struct objfile *of;
118*6881a400Schristos   psymtab_storage *partial_symtabs;
119*6881a400Schristos   partial_symtab *pst;
120*6881a400Schristos   ctf_archive_t *arc;
1217d62b00eSchristos   struct buildsym_compunit *builder;
1227d62b00eSchristos };
1237d62b00eSchristos 
1247d62b00eSchristos /* A partial symtab, specialized for this module.  */
1257d62b00eSchristos struct ctf_psymtab : public standard_psymtab
1267d62b00eSchristos {
127*6881a400Schristos   ctf_psymtab (const char *filename,
128*6881a400Schristos 	       psymtab_storage *partial_symtabs,
129*6881a400Schristos 	       objfile_per_bfd_storage *objfile_per_bfd,
130*6881a400Schristos 	       CORE_ADDR addr)
131*6881a400Schristos     : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
1327d62b00eSchristos   {
1337d62b00eSchristos   }
1347d62b00eSchristos 
1357d62b00eSchristos   void read_symtab (struct objfile *) override;
1367d62b00eSchristos   void expand_psymtab (struct objfile *) override;
1377d62b00eSchristos 
138*6881a400Schristos   struct ctf_context context;
1397d62b00eSchristos };
1407d62b00eSchristos 
1417d62b00eSchristos /* The routines that read and process fields/members of a C struct, union,
1427d62b00eSchristos    or enumeration, pass lists of data member fields in an instance of a
1437d62b00eSchristos    ctf_field_info structure. It is derived from dwarf2read.c.  */
1447d62b00eSchristos 
1457d62b00eSchristos struct ctf_nextfield
1467d62b00eSchristos {
1477d62b00eSchristos   struct field field {};
1487d62b00eSchristos };
1497d62b00eSchristos 
1507d62b00eSchristos struct ctf_field_info
1517d62b00eSchristos {
1527d62b00eSchristos   /* List of data member fields.  */
1537d62b00eSchristos   std::vector<struct ctf_nextfield> fields;
1547d62b00eSchristos 
1557d62b00eSchristos   /* Context.  */
1567d62b00eSchristos   struct ctf_context *cur_context;
1577d62b00eSchristos 
1587d62b00eSchristos   /* Parent type.  */
1597d62b00eSchristos   struct type *ptype;
1607d62b00eSchristos 
1617d62b00eSchristos   /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head
1627d62b00eSchristos      of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
1637d62b00eSchristos   std::vector<struct decl_field> typedef_field_list;
1647d62b00eSchristos 
1657d62b00eSchristos   /* Nested types defined by this struct and the number of elements in
1667d62b00eSchristos      this list.  */
1677d62b00eSchristos   std::vector<struct decl_field> nested_types_list;
1687d62b00eSchristos };
1697d62b00eSchristos 
170*6881a400Schristos /* Data held for a translation unit.  */
171*6881a400Schristos 
172*6881a400Schristos struct ctf_per_tu_data
173*6881a400Schristos {
174*6881a400Schristos   ctf_dict_t *fp;
175*6881a400Schristos   struct objfile *of;
176*6881a400Schristos   ctf_archive_t *arc;
177*6881a400Schristos   psymtab_storage *pss;
178*6881a400Schristos   psymbol_functions *psf;
179*6881a400Schristos };
1807d62b00eSchristos 
1817d62b00eSchristos /* Local function prototypes */
1827d62b00eSchristos 
1837d62b00eSchristos static int ctf_add_type_cb (ctf_id_t tid, void *arg);
1847d62b00eSchristos 
1857d62b00eSchristos static struct type *read_array_type (struct ctf_context *cp, ctf_id_t tid);
1867d62b00eSchristos 
1877d62b00eSchristos static struct type *read_pointer_type (struct ctf_context *cp, ctf_id_t tid,
1887d62b00eSchristos 				       ctf_id_t btid);
1897d62b00eSchristos 
1907d62b00eSchristos static struct type *read_structure_type (struct ctf_context *cp, ctf_id_t tid);
1917d62b00eSchristos 
1927d62b00eSchristos static struct type *read_enum_type (struct ctf_context *cp, ctf_id_t tid);
1937d62b00eSchristos 
1947d62b00eSchristos static struct type *read_typedef_type (struct ctf_context *cp, ctf_id_t tid,
1957d62b00eSchristos 				       ctf_id_t btid, const char *name);
1967d62b00eSchristos 
1977d62b00eSchristos static struct type *read_type_record (struct ctf_context *cp, ctf_id_t tid);
1987d62b00eSchristos 
1997d62b00eSchristos static void process_structure_type (struct ctf_context *cp, ctf_id_t tid);
2007d62b00eSchristos 
2017d62b00eSchristos static void process_struct_members (struct ctf_context *cp, ctf_id_t tid,
2027d62b00eSchristos 				    struct type *type);
2037d62b00eSchristos 
204*6881a400Schristos static struct type *read_forward_type (struct ctf_context *cp, ctf_id_t tid);
205*6881a400Schristos 
2067d62b00eSchristos static struct symbol *new_symbol (struct ctf_context *cp, struct type *type,
2077d62b00eSchristos 				  ctf_id_t tid);
2087d62b00eSchristos 
2097d62b00eSchristos struct ctf_tid_and_type
2107d62b00eSchristos {
2117d62b00eSchristos   ctf_id_t tid;
2127d62b00eSchristos   struct type *type;
2137d62b00eSchristos };
2147d62b00eSchristos 
2157d62b00eSchristos /* Hash function for a ctf_tid_and_type.  */
2167d62b00eSchristos 
2177d62b00eSchristos static hashval_t
2187d62b00eSchristos tid_and_type_hash (const void *item)
2197d62b00eSchristos {
2207d62b00eSchristos   const struct ctf_tid_and_type *ids
2217d62b00eSchristos     = (const struct ctf_tid_and_type *) item;
2227d62b00eSchristos 
2237d62b00eSchristos   return ids->tid;
2247d62b00eSchristos }
2257d62b00eSchristos 
2267d62b00eSchristos /* Equality function for a ctf_tid_and_type.  */
2277d62b00eSchristos 
2287d62b00eSchristos static int
2297d62b00eSchristos tid_and_type_eq (const void *item_lhs, const void *item_rhs)
2307d62b00eSchristos {
2317d62b00eSchristos   const struct ctf_tid_and_type *ids_lhs
2327d62b00eSchristos     = (const struct ctf_tid_and_type *) item_lhs;
2337d62b00eSchristos   const struct ctf_tid_and_type *ids_rhs
2347d62b00eSchristos     = (const struct ctf_tid_and_type *) item_rhs;
2357d62b00eSchristos 
2367d62b00eSchristos   return ids_lhs->tid == ids_rhs->tid;
2377d62b00eSchristos }
2387d62b00eSchristos 
2397d62b00eSchristos /* Set the type associated with TID to TYP.  */
2407d62b00eSchristos 
2417d62b00eSchristos static struct type *
2427d62b00eSchristos set_tid_type (struct objfile *of, ctf_id_t tid, struct type *typ)
2437d62b00eSchristos {
2447d62b00eSchristos   htab_t htab;
2457d62b00eSchristos 
246*6881a400Schristos   htab = ctf_tid_key.get (of);
2477d62b00eSchristos   if (htab == NULL)
2487d62b00eSchristos     {
2497d62b00eSchristos       htab = htab_create_alloc (1, tid_and_type_hash,
2507d62b00eSchristos 				tid_and_type_eq,
2517d62b00eSchristos 				NULL, xcalloc, xfree);
2527d62b00eSchristos       ctf_tid_key.set (of, htab);
2537d62b00eSchristos     }
2547d62b00eSchristos 
2557d62b00eSchristos   struct ctf_tid_and_type **slot, ids;
2567d62b00eSchristos   ids.tid = tid;
2577d62b00eSchristos   ids.type = typ;
2587d62b00eSchristos   slot = (struct ctf_tid_and_type **) htab_find_slot (htab, &ids, INSERT);
259*6881a400Schristos   if (*slot == nullptr)
2607d62b00eSchristos     *slot = XOBNEW (&of->objfile_obstack, struct ctf_tid_and_type);
2617d62b00eSchristos   **slot = ids;
2627d62b00eSchristos   return typ;
2637d62b00eSchristos }
2647d62b00eSchristos 
2657d62b00eSchristos /* Look up the type for TID in tid_and_type hash, return NULL if hash is
2667d62b00eSchristos    empty or TID does not have a saved type.  */
2677d62b00eSchristos 
2687d62b00eSchristos static struct type *
2697d62b00eSchristos get_tid_type (struct objfile *of, ctf_id_t tid)
2707d62b00eSchristos {
2717d62b00eSchristos   struct ctf_tid_and_type *slot, ids;
2727d62b00eSchristos   htab_t htab;
2737d62b00eSchristos 
274*6881a400Schristos   htab = ctf_tid_key.get (of);
2757d62b00eSchristos   if (htab == NULL)
276*6881a400Schristos     return nullptr;
2777d62b00eSchristos 
2787d62b00eSchristos   ids.tid = tid;
279*6881a400Schristos   ids.type = nullptr;
2807d62b00eSchristos   slot = (struct ctf_tid_and_type *) htab_find (htab, &ids);
2817d62b00eSchristos   if (slot)
2827d62b00eSchristos     return slot->type;
2837d62b00eSchristos   else
284*6881a400Schristos     return nullptr;
285*6881a400Schristos }
286*6881a400Schristos 
287*6881a400Schristos /* Fetch the type for TID in CCP OF's tid_and_type hash, add the type to
288*6881a400Schristos  *    context CCP if hash is empty or TID does not have a saved type.  */
289*6881a400Schristos 
290*6881a400Schristos static struct type *
291*6881a400Schristos fetch_tid_type (struct ctf_context *ccp, ctf_id_t tid)
292*6881a400Schristos {
293*6881a400Schristos   struct objfile *of = ccp->of;
294*6881a400Schristos   struct type *typ;
295*6881a400Schristos 
296*6881a400Schristos   typ = get_tid_type (of, tid);
297*6881a400Schristos   if (typ == nullptr)
298*6881a400Schristos     {
299*6881a400Schristos       ctf_add_type_cb (tid, ccp);
300*6881a400Schristos       typ = get_tid_type (of, tid);
301*6881a400Schristos     }
302*6881a400Schristos 
303*6881a400Schristos   return typ;
3047d62b00eSchristos }
3057d62b00eSchristos 
3067d62b00eSchristos /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM.  */
3077d62b00eSchristos 
3087d62b00eSchristos static int
309*6881a400Schristos get_bitsize (ctf_dict_t *fp, ctf_id_t tid, uint32_t kind)
3107d62b00eSchristos {
3117d62b00eSchristos   ctf_encoding_t cet;
3127d62b00eSchristos 
3137d62b00eSchristos   if ((kind == CTF_K_INTEGER || kind == CTF_K_ENUM
3147d62b00eSchristos       || kind == CTF_K_FLOAT)
3157d62b00eSchristos       && ctf_type_reference (fp, tid) != CTF_ERR
3167d62b00eSchristos       && ctf_type_encoding (fp, tid, &cet) != CTF_ERR)
3177d62b00eSchristos     return cet.cte_bits;
3187d62b00eSchristos 
3197d62b00eSchristos   return 0;
3207d62b00eSchristos }
3217d62b00eSchristos 
3227d62b00eSchristos /* Set SYM's address, with NAME, from its minimal symbol entry.  */
3237d62b00eSchristos 
3247d62b00eSchristos static void
3257d62b00eSchristos set_symbol_address (struct objfile *of, struct symbol *sym, const char *name)
3267d62b00eSchristos {
3277d62b00eSchristos   struct bound_minimal_symbol msym;
3287d62b00eSchristos 
329*6881a400Schristos   msym = lookup_minimal_symbol (name, nullptr, of);
3307d62b00eSchristos   if (msym.minsym != NULL)
3317d62b00eSchristos     {
332*6881a400Schristos       sym->set_value_address (msym.value_address ());
333*6881a400Schristos       sym->set_aclass_index (LOC_STATIC);
334*6881a400Schristos       sym->set_section_index (msym.minsym->section_index ());
3357d62b00eSchristos     }
3367d62b00eSchristos }
3377d62b00eSchristos 
3387d62b00eSchristos /* Create the vector of fields, and attach it to TYPE.  */
3397d62b00eSchristos 
3407d62b00eSchristos static void
3417d62b00eSchristos attach_fields_to_type (struct ctf_field_info *fip, struct type *type)
3427d62b00eSchristos {
3437d62b00eSchristos   int nfields = fip->fields.size ();
3447d62b00eSchristos 
3457d62b00eSchristos   if (nfields == 0)
3467d62b00eSchristos     return;
3477d62b00eSchristos 
3487d62b00eSchristos   /* Record the field count, allocate space for the array of fields.  */
3497d62b00eSchristos   type->set_num_fields (nfields);
3507d62b00eSchristos   type->set_fields
3517d62b00eSchristos     ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
3527d62b00eSchristos 
3537d62b00eSchristos   /* Copy the saved-up fields into the field vector.  */
3547d62b00eSchristos   for (int i = 0; i < nfields; ++i)
3557d62b00eSchristos     {
3567d62b00eSchristos       struct ctf_nextfield &field = fip->fields[i];
3577d62b00eSchristos       type->field (i) = field.field;
3587d62b00eSchristos     }
3597d62b00eSchristos }
3607d62b00eSchristos 
3617d62b00eSchristos /* Allocate a floating-point type of size BITS and name NAME.  Pass NAME_HINT
3627d62b00eSchristos    (which may be different from NAME) to the architecture back-end to allow
3637d62b00eSchristos    it to guess the correct format if necessary.  */
3647d62b00eSchristos 
3657d62b00eSchristos static struct type *
3667d62b00eSchristos ctf_init_float_type (struct objfile *objfile,
3677d62b00eSchristos 		     int bits,
3687d62b00eSchristos 		     const char *name,
3697d62b00eSchristos 		     const char *name_hint)
3707d62b00eSchristos {
3717d62b00eSchristos   struct gdbarch *gdbarch = objfile->arch ();
3727d62b00eSchristos   const struct floatformat **format;
3737d62b00eSchristos   struct type *type;
3747d62b00eSchristos 
3757d62b00eSchristos   format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
376*6881a400Schristos   if (format != nullptr)
3777d62b00eSchristos     type = init_float_type (objfile, bits, name, format);
3787d62b00eSchristos   else
3797d62b00eSchristos     type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
3807d62b00eSchristos 
3817d62b00eSchristos   return type;
3827d62b00eSchristos }
3837d62b00eSchristos 
3847d62b00eSchristos /* Callback to add member NAME to a struct/union type. TID is the type
3857d62b00eSchristos    of struct/union member, OFFSET is the offset of member in bits,
3867d62b00eSchristos    and ARG contains the ctf_field_info.  */
3877d62b00eSchristos 
3887d62b00eSchristos static int
3897d62b00eSchristos ctf_add_member_cb (const char *name,
3907d62b00eSchristos 		   ctf_id_t tid,
3917d62b00eSchristos 		   unsigned long offset,
3927d62b00eSchristos 		   void *arg)
3937d62b00eSchristos {
3947d62b00eSchristos   struct ctf_field_info *fip = (struct ctf_field_info *) arg;
3957d62b00eSchristos   struct ctf_context *ccp = fip->cur_context;
3967d62b00eSchristos   struct ctf_nextfield new_field;
3977d62b00eSchristos   struct field *fp;
3987d62b00eSchristos   struct type *t;
3997d62b00eSchristos   uint32_t kind;
4007d62b00eSchristos 
4017d62b00eSchristos   fp = &new_field.field;
402*6881a400Schristos   fp->set_name (name);
4037d62b00eSchristos 
4047d62b00eSchristos   kind = ctf_type_kind (ccp->fp, tid);
405*6881a400Schristos   t = fetch_tid_type (ccp, tid);
406*6881a400Schristos   if (t == nullptr)
4077d62b00eSchristos     {
4087d62b00eSchristos       t = read_type_record (ccp, tid);
409*6881a400Schristos       if (t == nullptr)
4107d62b00eSchristos 	{
4117d62b00eSchristos 	  complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name, tid);
4127d62b00eSchristos 	  t = objfile_type (ccp->of)->builtin_error;
4137d62b00eSchristos 	  set_tid_type (ccp->of, tid, t);
4147d62b00eSchristos 	}
4157d62b00eSchristos     }
4167d62b00eSchristos 
4177d62b00eSchristos   if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
4187d62b00eSchristos     process_struct_members (ccp, tid, t);
4197d62b00eSchristos 
4207d62b00eSchristos   fp->set_type (t);
421*6881a400Schristos   fp->set_loc_bitpos (offset / TARGET_CHAR_BIT);
4227d62b00eSchristos   FIELD_BITSIZE (*fp) = get_bitsize (ccp->fp, tid, kind);
4237d62b00eSchristos 
4247d62b00eSchristos   fip->fields.emplace_back (new_field);
4257d62b00eSchristos 
4267d62b00eSchristos   return 0;
4277d62b00eSchristos }
4287d62b00eSchristos 
4297d62b00eSchristos /* Callback to add member NAME of EVAL to an enumeration type.
4307d62b00eSchristos    ARG contains the ctf_field_info.  */
4317d62b00eSchristos 
4327d62b00eSchristos static int
4337d62b00eSchristos ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)
4347d62b00eSchristos {
4357d62b00eSchristos   struct ctf_field_info *fip = (struct ctf_field_info *) arg;
4367d62b00eSchristos   struct ctf_nextfield new_field;
4377d62b00eSchristos   struct field *fp;
4387d62b00eSchristos   struct ctf_context *ccp = fip->cur_context;
4397d62b00eSchristos 
4407d62b00eSchristos   fp = &new_field.field;
441*6881a400Schristos   fp->set_name (name);
442*6881a400Schristos   fp->set_type (nullptr);
443*6881a400Schristos   fp->set_loc_enumval (enum_value);
4447d62b00eSchristos   FIELD_BITSIZE (*fp) = 0;
4457d62b00eSchristos 
446*6881a400Schristos   if (name != nullptr)
4477d62b00eSchristos     {
4487d62b00eSchristos       struct symbol *sym = new (&ccp->of->objfile_obstack) symbol;
4497d62b00eSchristos       OBJSTAT (ccp->of, n_syms++);
4507d62b00eSchristos 
4517d62b00eSchristos       sym->set_language (language_c, &ccp->of->objfile_obstack);
4527d62b00eSchristos       sym->compute_and_set_names (name, false, ccp->of->per_bfd);
453*6881a400Schristos       sym->set_aclass_index (LOC_CONST);
454*6881a400Schristos       sym->set_domain (VAR_DOMAIN);
455*6881a400Schristos       sym->set_type (fip->ptype);
4567d62b00eSchristos       add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
4577d62b00eSchristos     }
4587d62b00eSchristos 
4597d62b00eSchristos   fip->fields.emplace_back (new_field);
4607d62b00eSchristos 
4617d62b00eSchristos   return 0;
4627d62b00eSchristos }
4637d62b00eSchristos 
4647d62b00eSchristos /* Add a new symbol entry, with its name from TID, its access index and
4657d62b00eSchristos    domain from TID's kind, and its type from TYPE.  */
4667d62b00eSchristos 
4677d62b00eSchristos static struct symbol *
4687d62b00eSchristos new_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)
4697d62b00eSchristos {
4707d62b00eSchristos   struct objfile *objfile = ccp->of;
471*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
472*6881a400Schristos   struct symbol *sym = nullptr;
4737d62b00eSchristos 
474*6881a400Schristos   const char *name = ctf_type_name_raw (fp, tid);
475*6881a400Schristos   if (name != nullptr)
4767d62b00eSchristos     {
4777d62b00eSchristos       sym = new (&objfile->objfile_obstack) symbol;
4787d62b00eSchristos       OBJSTAT (objfile, n_syms++);
4797d62b00eSchristos 
4807d62b00eSchristos       sym->set_language (language_c, &objfile->objfile_obstack);
481*6881a400Schristos       sym->compute_and_set_names (name, false, objfile->per_bfd);
482*6881a400Schristos       sym->set_domain (VAR_DOMAIN);
483*6881a400Schristos       sym->set_aclass_index (LOC_OPTIMIZED_OUT);
4847d62b00eSchristos 
485*6881a400Schristos       if (type != nullptr)
486*6881a400Schristos 	sym->set_type (type);
4877d62b00eSchristos 
4887d62b00eSchristos       uint32_t kind = ctf_type_kind (fp, tid);
4897d62b00eSchristos       switch (kind)
4907d62b00eSchristos 	{
4917d62b00eSchristos 	  case CTF_K_STRUCT:
4927d62b00eSchristos 	  case CTF_K_UNION:
4937d62b00eSchristos 	  case CTF_K_ENUM:
494*6881a400Schristos 	    sym->set_aclass_index (LOC_TYPEDEF);
495*6881a400Schristos 	    sym->set_domain (STRUCT_DOMAIN);
4967d62b00eSchristos 	    break;
4977d62b00eSchristos 	  case CTF_K_FUNCTION:
498*6881a400Schristos 	    sym->set_aclass_index (LOC_STATIC);
499*6881a400Schristos 	    set_symbol_address (objfile, sym, sym->linkage_name ());
5007d62b00eSchristos 	    break;
5017d62b00eSchristos 	  case CTF_K_CONST:
502*6881a400Schristos 	    if (sym->type ()->code () == TYPE_CODE_VOID)
503*6881a400Schristos 	      sym->set_type (objfile_type (objfile)->builtin_int);
5047d62b00eSchristos 	    break;
5057d62b00eSchristos 	  case CTF_K_TYPEDEF:
5067d62b00eSchristos 	  case CTF_K_INTEGER:
5077d62b00eSchristos 	  case CTF_K_FLOAT:
508*6881a400Schristos 	    sym->set_aclass_index (LOC_TYPEDEF);
509*6881a400Schristos 	    sym->set_domain (VAR_DOMAIN);
5107d62b00eSchristos 	    break;
5117d62b00eSchristos 	  case CTF_K_POINTER:
5127d62b00eSchristos 	    break;
5137d62b00eSchristos 	  case CTF_K_VOLATILE:
5147d62b00eSchristos 	  case CTF_K_RESTRICT:
5157d62b00eSchristos 	    break;
5167d62b00eSchristos 	  case CTF_K_SLICE:
5177d62b00eSchristos 	  case CTF_K_ARRAY:
5187d62b00eSchristos 	  case CTF_K_UNKNOWN:
5197d62b00eSchristos 	    break;
5207d62b00eSchristos 	}
5217d62b00eSchristos 
522*6881a400Schristos       add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
5237d62b00eSchristos     }
5247d62b00eSchristos 
5257d62b00eSchristos   return sym;
5267d62b00eSchristos }
5277d62b00eSchristos 
5287d62b00eSchristos /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
5297d62b00eSchristos    and create the symbol for it.  */
5307d62b00eSchristos 
5317d62b00eSchristos static struct type *
5327d62b00eSchristos read_base_type (struct ctf_context *ccp, ctf_id_t tid)
5337d62b00eSchristos {
5347d62b00eSchristos   struct objfile *of = ccp->of;
535*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
5367d62b00eSchristos   ctf_encoding_t cet;
537*6881a400Schristos   struct type *type = nullptr;
538*6881a400Schristos   const char *name;
5397d62b00eSchristos   uint32_t kind;
5407d62b00eSchristos 
5417d62b00eSchristos   if (ctf_type_encoding (fp, tid, &cet))
5427d62b00eSchristos     {
5437d62b00eSchristos       complaint (_("ctf_type_encoding read_base_type failed - %s"),
5447d62b00eSchristos 		 ctf_errmsg (ctf_errno (fp)));
545*6881a400Schristos       return nullptr;
5467d62b00eSchristos     }
5477d62b00eSchristos 
548*6881a400Schristos   name = ctf_type_name_raw (fp, tid);
549*6881a400Schristos   if (name == nullptr || strlen (name) == 0)
5507d62b00eSchristos     {
5517d62b00eSchristos       name = ctf_type_aname (fp, tid);
552*6881a400Schristos       if (name == nullptr)
5537d62b00eSchristos 	complaint (_("ctf_type_aname read_base_type failed - %s"),
5547d62b00eSchristos 		   ctf_errmsg (ctf_errno (fp)));
5557d62b00eSchristos     }
5567d62b00eSchristos 
5577d62b00eSchristos   kind = ctf_type_kind (fp, tid);
5587d62b00eSchristos   if (kind == CTF_K_INTEGER)
5597d62b00eSchristos     {
5607d62b00eSchristos       uint32_t issigned, ischar, isbool;
5617d62b00eSchristos       struct gdbarch *gdbarch = of->arch ();
5627d62b00eSchristos 
5637d62b00eSchristos       issigned = cet.cte_format & CTF_INT_SIGNED;
5647d62b00eSchristos       ischar = cet.cte_format & CTF_INT_CHAR;
5657d62b00eSchristos       isbool = cet.cte_format & CTF_INT_BOOL;
5667d62b00eSchristos       if (ischar)
5677d62b00eSchristos 	type = init_character_type (of, TARGET_CHAR_BIT, !issigned, name);
5687d62b00eSchristos       else if (isbool)
5697d62b00eSchristos 	type = init_boolean_type (of, gdbarch_int_bit (gdbarch),
5707d62b00eSchristos 				  !issigned, name);
5717d62b00eSchristos       else
5727d62b00eSchristos 	{
5737d62b00eSchristos 	  int bits;
5747d62b00eSchristos 	  if (cet.cte_bits && ((cet.cte_bits % TARGET_CHAR_BIT) == 0))
5757d62b00eSchristos 	    bits = cet.cte_bits;
5767d62b00eSchristos 	  else
5777d62b00eSchristos 	    bits = gdbarch_int_bit (gdbarch);
5787d62b00eSchristos 	  type = init_integer_type (of, bits, !issigned, name);
5797d62b00eSchristos 	}
5807d62b00eSchristos     }
5817d62b00eSchristos   else if (kind == CTF_K_FLOAT)
5827d62b00eSchristos     {
5837d62b00eSchristos       uint32_t isflt;
5847d62b00eSchristos       isflt = !((cet.cte_format & CTF_FP_IMAGRY) == CTF_FP_IMAGRY
5857d62b00eSchristos 		 || (cet.cte_format & CTF_FP_DIMAGRY) == CTF_FP_DIMAGRY
5867d62b00eSchristos 		 || (cet.cte_format & CTF_FP_LDIMAGRY) == CTF_FP_LDIMAGRY);
5877d62b00eSchristos       if (isflt)
5887d62b00eSchristos 	type = ctf_init_float_type (of, cet.cte_bits, name, name);
5897d62b00eSchristos       else
5907d62b00eSchristos 	{
5917d62b00eSchristos 	  struct type *t
5927d62b00eSchristos 	    = ctf_init_float_type (of, cet.cte_bits / 2, NULL, name);
5937d62b00eSchristos 	  type = init_complex_type (name, t);
5947d62b00eSchristos 	}
5957d62b00eSchristos     }
5967d62b00eSchristos   else
5977d62b00eSchristos     {
5987d62b00eSchristos       complaint (_("read_base_type: unsupported base kind (%d)"), kind);
5997d62b00eSchristos       type = init_type (of, TYPE_CODE_ERROR, cet.cte_bits, name);
6007d62b00eSchristos     }
6017d62b00eSchristos 
602*6881a400Schristos   if (name != nullptr && strcmp (name, "char") == 0)
603*6881a400Schristos     type->set_has_no_signedness (true);
6047d62b00eSchristos 
6057d62b00eSchristos   return set_tid_type (of, tid, type);
6067d62b00eSchristos }
6077d62b00eSchristos 
6087d62b00eSchristos static void
6097d62b00eSchristos process_base_type (struct ctf_context *ccp, ctf_id_t tid)
6107d62b00eSchristos {
6117d62b00eSchristos   struct type *type;
6127d62b00eSchristos 
6137d62b00eSchristos   type = read_base_type (ccp, tid);
6147d62b00eSchristos   new_symbol (ccp, type, tid);
6157d62b00eSchristos }
6167d62b00eSchristos 
6177d62b00eSchristos /* Start a structure or union scope (definition) with TID to create a type
6187d62b00eSchristos    for the structure or union.
6197d62b00eSchristos 
6207d62b00eSchristos    Fill in the type's name and general properties. The members will not be
6217d62b00eSchristos    processed, nor a symbol table entry be done until process_structure_type
6227d62b00eSchristos    (assuming the type has a name).  */
6237d62b00eSchristos 
6247d62b00eSchristos static struct type *
6257d62b00eSchristos read_structure_type (struct ctf_context *ccp, ctf_id_t tid)
6267d62b00eSchristos {
6277d62b00eSchristos   struct objfile *of = ccp->of;
628*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
6297d62b00eSchristos   struct type *type;
6307d62b00eSchristos   uint32_t kind;
6317d62b00eSchristos 
6327d62b00eSchristos   type = alloc_type (of);
6337d62b00eSchristos 
634*6881a400Schristos   const char *name = ctf_type_name_raw (fp, tid);
635*6881a400Schristos   if (name != nullptr && strlen (name) != 0)
636*6881a400Schristos     type->set_name (name);
6377d62b00eSchristos 
6387d62b00eSchristos   kind = ctf_type_kind (fp, tid);
6397d62b00eSchristos   if (kind == CTF_K_UNION)
6407d62b00eSchristos     type->set_code (TYPE_CODE_UNION);
6417d62b00eSchristos   else
6427d62b00eSchristos     type->set_code (TYPE_CODE_STRUCT);
6437d62b00eSchristos 
644*6881a400Schristos   type->set_length (ctf_type_size (fp, tid));
6457d62b00eSchristos   set_type_align (type, ctf_type_align (fp, tid));
6467d62b00eSchristos 
6477d62b00eSchristos   return set_tid_type (ccp->of, tid, type);
6487d62b00eSchristos }
6497d62b00eSchristos 
6507d62b00eSchristos /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
6517d62b00eSchristos    and create the symbol for it.  */
6527d62b00eSchristos 
6537d62b00eSchristos static void
6547d62b00eSchristos process_struct_members (struct ctf_context *ccp,
6557d62b00eSchristos 			ctf_id_t tid,
6567d62b00eSchristos 			struct type *type)
6577d62b00eSchristos {
6587d62b00eSchristos   struct ctf_field_info fi;
6597d62b00eSchristos 
6607d62b00eSchristos   fi.cur_context = ccp;
6617d62b00eSchristos   if (ctf_member_iter (ccp->fp, tid, ctf_add_member_cb, &fi) == CTF_ERR)
6627d62b00eSchristos     complaint (_("ctf_member_iter process_struct_members failed - %s"),
6637d62b00eSchristos 	       ctf_errmsg (ctf_errno (ccp->fp)));
6647d62b00eSchristos 
6657d62b00eSchristos   /* Attach fields to the type.  */
6667d62b00eSchristos   attach_fields_to_type (&fi, type);
6677d62b00eSchristos 
6687d62b00eSchristos   new_symbol (ccp, type, tid);
6697d62b00eSchristos }
6707d62b00eSchristos 
6717d62b00eSchristos static void
6727d62b00eSchristos process_structure_type (struct ctf_context *ccp, ctf_id_t tid)
6737d62b00eSchristos {
6747d62b00eSchristos   struct type *type;
6757d62b00eSchristos 
6767d62b00eSchristos   type = read_structure_type (ccp, tid);
6777d62b00eSchristos   process_struct_members (ccp, tid, type);
6787d62b00eSchristos }
6797d62b00eSchristos 
6807d62b00eSchristos /* Create a function type for TID and set its return type.  */
6817d62b00eSchristos 
6827d62b00eSchristos static struct type *
6837d62b00eSchristos read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
6847d62b00eSchristos {
6857d62b00eSchristos   struct objfile *of = ccp->of;
686*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
687*6881a400Schristos   struct type *type, *rettype, *atype;
6887d62b00eSchristos   ctf_funcinfo_t cfi;
689*6881a400Schristos   uint32_t argc;
6907d62b00eSchristos 
6917d62b00eSchristos   type = alloc_type (of);
6927d62b00eSchristos 
6937d62b00eSchristos   type->set_code (TYPE_CODE_FUNC);
694*6881a400Schristos   if (ctf_func_type_info (fp, tid, &cfi) < 0)
695*6881a400Schristos     {
696*6881a400Schristos       const char *fname = ctf_type_name_raw (fp, tid);
697*6881a400Schristos       error (_("Error getting function type info: %s"),
698*6881a400Schristos 	     fname == nullptr ? "noname" : fname);
699*6881a400Schristos     }
700*6881a400Schristos   rettype = fetch_tid_type (ccp, cfi.ctc_return);
701*6881a400Schristos   type->set_target_type (rettype);
7027d62b00eSchristos   set_type_align (type, ctf_type_align (fp, tid));
7037d62b00eSchristos 
704*6881a400Schristos   /* Set up function's arguments.  */
705*6881a400Schristos   argc = cfi.ctc_argc;
706*6881a400Schristos   type->set_num_fields (argc);
707*6881a400Schristos   if ((cfi.ctc_flags & CTF_FUNC_VARARG) != 0)
708*6881a400Schristos     type->set_has_varargs (true);
709*6881a400Schristos 
710*6881a400Schristos   if (argc != 0)
711*6881a400Schristos     {
712*6881a400Schristos       std::vector<ctf_id_t> argv (argc);
713*6881a400Schristos       if (ctf_func_type_args (fp, tid, argc, argv.data ()) == CTF_ERR)
714*6881a400Schristos 	return nullptr;
715*6881a400Schristos 
716*6881a400Schristos       type->set_fields
717*6881a400Schristos 	((struct field *) TYPE_ZALLOC (type, argc * sizeof (struct field)));
718*6881a400Schristos       struct type *void_type = objfile_type (of)->builtin_void;
719*6881a400Schristos       /* If failed to find the argument type, fill it with void_type.  */
720*6881a400Schristos       for (int iparam = 0; iparam < argc; iparam++)
721*6881a400Schristos 	{
722*6881a400Schristos 	  atype = fetch_tid_type (ccp, argv[iparam]);
723*6881a400Schristos 	  if (atype != nullptr)
724*6881a400Schristos 	    type->field (iparam).set_type (atype);
725*6881a400Schristos 	  else
726*6881a400Schristos 	    type->field (iparam).set_type (void_type);
727*6881a400Schristos 	}
728*6881a400Schristos     }
729*6881a400Schristos 
7307d62b00eSchristos   return set_tid_type (of, tid, type);
7317d62b00eSchristos }
7327d62b00eSchristos 
7337d62b00eSchristos /* Given a TID of CTF_K_ENUM, process all the members of the
7347d62b00eSchristos    enumeration, and create the symbol for the enumeration type.  */
7357d62b00eSchristos 
7367d62b00eSchristos static struct type *
7377d62b00eSchristos read_enum_type (struct ctf_context *ccp, ctf_id_t tid)
7387d62b00eSchristos {
7397d62b00eSchristos   struct objfile *of = ccp->of;
740*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
741*6881a400Schristos   struct type *type;
7427d62b00eSchristos 
7437d62b00eSchristos   type = alloc_type (of);
7447d62b00eSchristos 
745*6881a400Schristos   const char *name = ctf_type_name_raw (fp, tid);
746*6881a400Schristos   if (name != nullptr && strlen (name) != 0)
747*6881a400Schristos     type->set_name (name);
7487d62b00eSchristos 
7497d62b00eSchristos   type->set_code (TYPE_CODE_ENUM);
750*6881a400Schristos   type->set_length (ctf_type_size (fp, tid));
751*6881a400Schristos   /* Set the underlying type based on its ctf_type_size bits.  */
752*6881a400Schristos   type->set_target_type (objfile_int_type (of, type->length (), false));
7537d62b00eSchristos   set_type_align (type, ctf_type_align (fp, tid));
7547d62b00eSchristos 
7557d62b00eSchristos   return set_tid_type (of, tid, type);
7567d62b00eSchristos }
7577d62b00eSchristos 
7587d62b00eSchristos static void
7597d62b00eSchristos process_enum_type (struct ctf_context *ccp, ctf_id_t tid)
7607d62b00eSchristos {
7617d62b00eSchristos   struct type *type;
7627d62b00eSchristos   struct ctf_field_info fi;
7637d62b00eSchristos 
7647d62b00eSchristos   type = read_enum_type (ccp, tid);
7657d62b00eSchristos 
7667d62b00eSchristos   fi.cur_context = ccp;
7677d62b00eSchristos   fi.ptype = type;
7687d62b00eSchristos   if (ctf_enum_iter (ccp->fp, tid, ctf_add_enum_member_cb, &fi) == CTF_ERR)
7697d62b00eSchristos     complaint (_("ctf_enum_iter process_enum_type failed - %s"),
7707d62b00eSchristos 	       ctf_errmsg (ctf_errno (ccp->fp)));
7717d62b00eSchristos 
7727d62b00eSchristos   /* Attach fields to the type.  */
7737d62b00eSchristos   attach_fields_to_type (&fi, type);
7747d62b00eSchristos 
7757d62b00eSchristos   new_symbol (ccp, type, tid);
7767d62b00eSchristos }
7777d62b00eSchristos 
7787d62b00eSchristos /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID.  */
7797d62b00eSchristos 
7807d62b00eSchristos static struct type *
7817d62b00eSchristos add_array_cv_type (struct ctf_context *ccp,
7827d62b00eSchristos 		   ctf_id_t tid,
7837d62b00eSchristos 		   struct type *base_type,
7847d62b00eSchristos 		   int cnst,
7857d62b00eSchristos 		   int voltl)
7867d62b00eSchristos {
7877d62b00eSchristos   struct type *el_type, *inner_array;
7887d62b00eSchristos 
7897d62b00eSchristos   base_type = copy_type (base_type);
7907d62b00eSchristos   inner_array = base_type;
7917d62b00eSchristos 
792*6881a400Schristos   while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
7937d62b00eSchristos     {
794*6881a400Schristos       inner_array->set_target_type (copy_type (inner_array->target_type ()));
795*6881a400Schristos       inner_array = inner_array->target_type ();
7967d62b00eSchristos     }
7977d62b00eSchristos 
798*6881a400Schristos   el_type = inner_array->target_type ();
7997d62b00eSchristos   cnst |= TYPE_CONST (el_type);
8007d62b00eSchristos   voltl |= TYPE_VOLATILE (el_type);
801*6881a400Schristos   inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, nullptr));
8027d62b00eSchristos 
8037d62b00eSchristos   return set_tid_type (ccp->of, tid, base_type);
8047d62b00eSchristos }
8057d62b00eSchristos 
8067d62b00eSchristos /* Read all information from a TID of CTF_K_ARRAY.  */
8077d62b00eSchristos 
8087d62b00eSchristos static struct type *
8097d62b00eSchristos read_array_type (struct ctf_context *ccp, ctf_id_t tid)
8107d62b00eSchristos {
8117d62b00eSchristos   struct objfile *objfile = ccp->of;
812*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
8137d62b00eSchristos   struct type *element_type, *range_type, *idx_type;
8147d62b00eSchristos   struct type *type;
8157d62b00eSchristos   ctf_arinfo_t ar;
8167d62b00eSchristos 
8177d62b00eSchristos   if (ctf_array_info (fp, tid, &ar) == CTF_ERR)
8187d62b00eSchristos     {
8197d62b00eSchristos       complaint (_("ctf_array_info read_array_type failed - %s"),
8207d62b00eSchristos 		 ctf_errmsg (ctf_errno (fp)));
821*6881a400Schristos       return nullptr;
8227d62b00eSchristos     }
8237d62b00eSchristos 
824*6881a400Schristos   element_type = fetch_tid_type (ccp, ar.ctr_contents);
825*6881a400Schristos   if (element_type == nullptr)
826*6881a400Schristos     return nullptr;
8277d62b00eSchristos 
828*6881a400Schristos   idx_type = fetch_tid_type (ccp, ar.ctr_index);
829*6881a400Schristos   if (idx_type == nullptr)
8307d62b00eSchristos     idx_type = objfile_type (objfile)->builtin_int;
8317d62b00eSchristos 
8327d62b00eSchristos   range_type = create_static_range_type (NULL, idx_type, 0, ar.ctr_nelems - 1);
8337d62b00eSchristos   type = create_array_type (NULL, element_type, range_type);
8347d62b00eSchristos   if (ar.ctr_nelems <= 1)	/* Check if undefined upper bound.  */
8357d62b00eSchristos     {
8367d62b00eSchristos       range_type->bounds ()->high.set_undefined ();
837*6881a400Schristos       type->set_length (0);
838*6881a400Schristos       type->set_target_is_stub (true);
8397d62b00eSchristos     }
8407d62b00eSchristos   else
841*6881a400Schristos     type->set_length (ctf_type_size (fp, tid));
8427d62b00eSchristos 
8437d62b00eSchristos   set_type_align (type, ctf_type_align (fp, tid));
8447d62b00eSchristos 
8457d62b00eSchristos   return set_tid_type (objfile, tid, type);
8467d62b00eSchristos }
8477d62b00eSchristos 
8487d62b00eSchristos /* Read TID of kind CTF_K_CONST with base type BTID.  */
8497d62b00eSchristos 
8507d62b00eSchristos static struct type *
8517d62b00eSchristos read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
8527d62b00eSchristos {
8537d62b00eSchristos   struct objfile *objfile = ccp->of;
8547d62b00eSchristos   struct type *base_type, *cv_type;
8557d62b00eSchristos 
856*6881a400Schristos   base_type = fetch_tid_type (ccp, btid);
857*6881a400Schristos   if (base_type == nullptr)
8587d62b00eSchristos     {
8597d62b00eSchristos       base_type = read_type_record (ccp, btid);
860*6881a400Schristos       if (base_type == nullptr)
8617d62b00eSchristos 	{
8627d62b00eSchristos 	  complaint (_("read_const_type: NULL base type (%ld)"), btid);
8637d62b00eSchristos 	  base_type = objfile_type (objfile)->builtin_error;
8647d62b00eSchristos 	}
8657d62b00eSchristos     }
8667d62b00eSchristos   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8677d62b00eSchristos 
8687d62b00eSchristos   return set_tid_type (objfile, tid, cv_type);
8697d62b00eSchristos }
8707d62b00eSchristos 
8717d62b00eSchristos /* Read TID of kind CTF_K_VOLATILE with base type BTID.  */
8727d62b00eSchristos 
8737d62b00eSchristos static struct type *
8747d62b00eSchristos read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
8757d62b00eSchristos {
8767d62b00eSchristos   struct objfile *objfile = ccp->of;
877*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
8787d62b00eSchristos   struct type *base_type, *cv_type;
8797d62b00eSchristos 
880*6881a400Schristos   base_type = fetch_tid_type (ccp, btid);
881*6881a400Schristos   if (base_type == nullptr)
8827d62b00eSchristos     {
8837d62b00eSchristos       base_type = read_type_record (ccp, btid);
884*6881a400Schristos       if (base_type == nullptr)
8857d62b00eSchristos 	{
8867d62b00eSchristos 	  complaint (_("read_volatile_type: NULL base type (%ld)"), btid);
8877d62b00eSchristos 	  base_type = objfile_type (objfile)->builtin_error;
8887d62b00eSchristos 	}
8897d62b00eSchristos     }
8907d62b00eSchristos 
8917d62b00eSchristos   if (ctf_type_kind (fp, btid) == CTF_K_ARRAY)
8927d62b00eSchristos     return add_array_cv_type (ccp, tid, base_type, 0, 1);
8937d62b00eSchristos   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8947d62b00eSchristos 
8957d62b00eSchristos   return set_tid_type (objfile, tid, cv_type);
8967d62b00eSchristos }
8977d62b00eSchristos 
8987d62b00eSchristos /* Read TID of kind CTF_K_RESTRICT with base type BTID.  */
8997d62b00eSchristos 
9007d62b00eSchristos static struct type *
9017d62b00eSchristos read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
9027d62b00eSchristos {
9037d62b00eSchristos   struct objfile *objfile = ccp->of;
9047d62b00eSchristos   struct type *base_type, *cv_type;
9057d62b00eSchristos 
906*6881a400Schristos   base_type = fetch_tid_type (ccp, btid);
907*6881a400Schristos   if (base_type == nullptr)
9087d62b00eSchristos     {
9097d62b00eSchristos       base_type = read_type_record (ccp, btid);
910*6881a400Schristos       if (base_type == nullptr)
9117d62b00eSchristos 	{
9127d62b00eSchristos 	  complaint (_("read_restrict_type: NULL base type (%ld)"), btid);
9137d62b00eSchristos 	  base_type = objfile_type (objfile)->builtin_error;
9147d62b00eSchristos 	}
9157d62b00eSchristos     }
9167d62b00eSchristos   cv_type = make_restrict_type (base_type);
9177d62b00eSchristos 
9187d62b00eSchristos   return set_tid_type (objfile, tid, cv_type);
9197d62b00eSchristos }
9207d62b00eSchristos 
9217d62b00eSchristos /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID.  */
9227d62b00eSchristos 
9237d62b00eSchristos static struct type *
9247d62b00eSchristos read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
9257d62b00eSchristos 		   ctf_id_t btid, const char *name)
9267d62b00eSchristos {
9277d62b00eSchristos   struct objfile *objfile = ccp->of;
9287d62b00eSchristos   struct type *this_type, *target_type;
9297d62b00eSchristos 
9307d62b00eSchristos   char *aname = obstack_strdup (&objfile->objfile_obstack, name);
9317d62b00eSchristos   this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, aname);
9327d62b00eSchristos   set_tid_type (objfile, tid, this_type);
933*6881a400Schristos   target_type = fetch_tid_type (ccp, btid);
9347d62b00eSchristos   if (target_type != this_type)
935*6881a400Schristos     this_type->set_target_type (target_type);
9367d62b00eSchristos   else
937*6881a400Schristos     this_type->set_target_type (nullptr);
938*6881a400Schristos 
939*6881a400Schristos   this_type->set_target_is_stub (this_type->target_type () != nullptr);
9407d62b00eSchristos 
9417d62b00eSchristos   return set_tid_type (objfile, tid, this_type);
9427d62b00eSchristos }
9437d62b00eSchristos 
9447d62b00eSchristos /* Read TID of kind CTF_K_POINTER with base type BTID.  */
9457d62b00eSchristos 
9467d62b00eSchristos static struct type *
9477d62b00eSchristos read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
9487d62b00eSchristos {
9497d62b00eSchristos   struct objfile *of = ccp->of;
9507d62b00eSchristos   struct type *target_type, *type;
9517d62b00eSchristos 
952*6881a400Schristos   target_type = fetch_tid_type (ccp, btid);
953*6881a400Schristos   if (target_type == nullptr)
9547d62b00eSchristos     {
9557d62b00eSchristos       target_type = read_type_record (ccp, btid);
956*6881a400Schristos       if (target_type == nullptr)
9577d62b00eSchristos 	{
9587d62b00eSchristos 	  complaint (_("read_pointer_type: NULL target type (%ld)"), btid);
9597d62b00eSchristos 	  target_type = objfile_type (ccp->of)->builtin_error;
9607d62b00eSchristos 	}
9617d62b00eSchristos     }
9627d62b00eSchristos 
9637d62b00eSchristos   type = lookup_pointer_type (target_type);
9647d62b00eSchristos   set_type_align (type, ctf_type_align (ccp->fp, tid));
9657d62b00eSchristos 
9667d62b00eSchristos   return set_tid_type (of, tid, type);
9677d62b00eSchristos }
9687d62b00eSchristos 
969*6881a400Schristos /* Read information from a TID of CTF_K_FORWARD.  */
970*6881a400Schristos 
971*6881a400Schristos static struct type *
972*6881a400Schristos read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
973*6881a400Schristos {
974*6881a400Schristos   struct objfile *of = ccp->of;
975*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
976*6881a400Schristos   struct type *type;
977*6881a400Schristos   uint32_t kind;
978*6881a400Schristos 
979*6881a400Schristos   type = alloc_type (of);
980*6881a400Schristos 
981*6881a400Schristos   const char *name = ctf_type_name_raw (fp, tid);
982*6881a400Schristos   if (name != nullptr && strlen (name) != 0)
983*6881a400Schristos     type->set_name (name);
984*6881a400Schristos 
985*6881a400Schristos   kind = ctf_type_kind_forwarded (fp, tid);
986*6881a400Schristos   if (kind == CTF_K_UNION)
987*6881a400Schristos     type->set_code (TYPE_CODE_UNION);
988*6881a400Schristos   else
989*6881a400Schristos     type->set_code (TYPE_CODE_STRUCT);
990*6881a400Schristos 
991*6881a400Schristos   type->set_length (0);
992*6881a400Schristos   type->set_is_stub (true);
993*6881a400Schristos 
994*6881a400Schristos   return set_tid_type (of, tid, type);
995*6881a400Schristos }
996*6881a400Schristos 
9977d62b00eSchristos /* Read information associated with type TID.  */
9987d62b00eSchristos 
9997d62b00eSchristos static struct type *
10007d62b00eSchristos read_type_record (struct ctf_context *ccp, ctf_id_t tid)
10017d62b00eSchristos {
1002*6881a400Schristos   ctf_dict_t *fp = ccp->fp;
10037d62b00eSchristos   uint32_t kind;
1004*6881a400Schristos   struct type *type = nullptr;
10057d62b00eSchristos   ctf_id_t btid;
10067d62b00eSchristos 
10077d62b00eSchristos   kind = ctf_type_kind (fp, tid);
10087d62b00eSchristos   switch (kind)
10097d62b00eSchristos     {
10107d62b00eSchristos       case CTF_K_STRUCT:
10117d62b00eSchristos       case CTF_K_UNION:
10127d62b00eSchristos 	type = read_structure_type (ccp, tid);
10137d62b00eSchristos 	break;
10147d62b00eSchristos       case CTF_K_ENUM:
10157d62b00eSchristos 	type = read_enum_type (ccp, tid);
10167d62b00eSchristos 	break;
10177d62b00eSchristos       case CTF_K_FUNCTION:
10187d62b00eSchristos 	type = read_func_kind_type (ccp, tid);
10197d62b00eSchristos 	break;
10207d62b00eSchristos       case CTF_K_CONST:
10217d62b00eSchristos 	btid = ctf_type_reference (fp, tid);
10227d62b00eSchristos 	type = read_const_type (ccp, tid, btid);
10237d62b00eSchristos 	break;
10247d62b00eSchristos       case CTF_K_TYPEDEF:
10257d62b00eSchristos 	{
1026*6881a400Schristos 	  const char *name = ctf_type_name_raw (fp, tid);
10277d62b00eSchristos 	  btid = ctf_type_reference (fp, tid);
1028*6881a400Schristos 	  type = read_typedef_type (ccp, tid, btid, name);
10297d62b00eSchristos 	}
10307d62b00eSchristos 	break;
10317d62b00eSchristos       case CTF_K_VOLATILE:
10327d62b00eSchristos 	btid = ctf_type_reference (fp, tid);
10337d62b00eSchristos 	type = read_volatile_type (ccp, tid, btid);
10347d62b00eSchristos 	break;
10357d62b00eSchristos       case CTF_K_RESTRICT:
10367d62b00eSchristos 	btid = ctf_type_reference (fp, tid);
10377d62b00eSchristos 	type = read_restrict_type (ccp, tid, btid);
10387d62b00eSchristos 	break;
10397d62b00eSchristos       case CTF_K_POINTER:
10407d62b00eSchristos 	btid = ctf_type_reference (fp, tid);
10417d62b00eSchristos 	type = read_pointer_type (ccp, tid, btid);
10427d62b00eSchristos 	break;
10437d62b00eSchristos       case CTF_K_INTEGER:
10447d62b00eSchristos       case CTF_K_FLOAT:
10457d62b00eSchristos 	type = read_base_type (ccp, tid);
10467d62b00eSchristos 	break;
10477d62b00eSchristos       case CTF_K_ARRAY:
10487d62b00eSchristos 	type = read_array_type (ccp, tid);
10497d62b00eSchristos 	break;
1050*6881a400Schristos       case CTF_K_FORWARD:
1051*6881a400Schristos 	type = read_forward_type (ccp, tid);
1052*6881a400Schristos 	break;
10537d62b00eSchristos       case CTF_K_UNKNOWN:
10547d62b00eSchristos 	break;
10557d62b00eSchristos       default:
10567d62b00eSchristos 	break;
10577d62b00eSchristos     }
10587d62b00eSchristos 
10597d62b00eSchristos   return type;
10607d62b00eSchristos }
10617d62b00eSchristos 
10627d62b00eSchristos /* Callback to add type TID to the symbol table.  */
10637d62b00eSchristos 
10647d62b00eSchristos static int
10657d62b00eSchristos ctf_add_type_cb (ctf_id_t tid, void *arg)
10667d62b00eSchristos {
10677d62b00eSchristos   struct ctf_context *ccp = (struct ctf_context *) arg;
10687d62b00eSchristos   struct type *type;
10697d62b00eSchristos   uint32_t kind;
10707d62b00eSchristos 
10717d62b00eSchristos   /* Check if tid's type has already been defined.  */
10727d62b00eSchristos   type = get_tid_type (ccp->of, tid);
1073*6881a400Schristos   if (type != nullptr)
10747d62b00eSchristos     return 0;
10757d62b00eSchristos 
10767d62b00eSchristos   ctf_id_t btid = ctf_type_reference (ccp->fp, tid);
10777d62b00eSchristos   kind = ctf_type_kind (ccp->fp, tid);
10787d62b00eSchristos   switch (kind)
10797d62b00eSchristos     {
10807d62b00eSchristos       case CTF_K_STRUCT:
10817d62b00eSchristos       case CTF_K_UNION:
10827d62b00eSchristos 	process_structure_type (ccp, tid);
10837d62b00eSchristos 	break;
10847d62b00eSchristos       case CTF_K_ENUM:
10857d62b00eSchristos 	process_enum_type (ccp, tid);
10867d62b00eSchristos 	break;
10877d62b00eSchristos       case CTF_K_FUNCTION:
10887d62b00eSchristos 	type = read_func_kind_type (ccp, tid);
10897d62b00eSchristos 	new_symbol (ccp, type, tid);
10907d62b00eSchristos 	break;
10917d62b00eSchristos       case CTF_K_INTEGER:
10927d62b00eSchristos       case CTF_K_FLOAT:
10937d62b00eSchristos 	process_base_type (ccp, tid);
10947d62b00eSchristos 	break;
10957d62b00eSchristos       case CTF_K_TYPEDEF:
10967d62b00eSchristos 	new_symbol (ccp, read_type_record (ccp, tid), tid);
10977d62b00eSchristos 	break;
10987d62b00eSchristos       case CTF_K_CONST:
10997d62b00eSchristos 	type = read_const_type (ccp, tid, btid);
11007d62b00eSchristos 	new_symbol (ccp, type, tid);
11017d62b00eSchristos 	break;
11027d62b00eSchristos       case CTF_K_VOLATILE:
11037d62b00eSchristos 	type = read_volatile_type (ccp, tid, btid);
11047d62b00eSchristos 	new_symbol (ccp, type, tid);
11057d62b00eSchristos 	break;
11067d62b00eSchristos       case CTF_K_RESTRICT:
11077d62b00eSchristos 	type = read_restrict_type (ccp, tid, btid);
11087d62b00eSchristos 	new_symbol (ccp, type, tid);
11097d62b00eSchristos 	break;
11107d62b00eSchristos       case CTF_K_POINTER:
11117d62b00eSchristos 	type = read_pointer_type (ccp, tid, btid);
11127d62b00eSchristos 	new_symbol (ccp, type, tid);
11137d62b00eSchristos 	break;
11147d62b00eSchristos       case CTF_K_ARRAY:
11157d62b00eSchristos 	type = read_array_type (ccp, tid);
11167d62b00eSchristos 	new_symbol (ccp, type, tid);
11177d62b00eSchristos 	break;
11187d62b00eSchristos       case CTF_K_UNKNOWN:
11197d62b00eSchristos 	break;
11207d62b00eSchristos       default:
11217d62b00eSchristos 	break;
11227d62b00eSchristos     }
11237d62b00eSchristos 
11247d62b00eSchristos   return 0;
11257d62b00eSchristos }
11267d62b00eSchristos 
11277d62b00eSchristos /* Callback to add variable NAME with TID to the symbol table.  */
11287d62b00eSchristos 
11297d62b00eSchristos static int
11307d62b00eSchristos ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
11317d62b00eSchristos {
11327d62b00eSchristos   struct ctf_context *ccp = (struct ctf_context *) arg;
1133*6881a400Schristos   struct symbol *sym = nullptr;
11347d62b00eSchristos   struct type *type;
11357d62b00eSchristos   uint32_t kind;
11367d62b00eSchristos 
11377d62b00eSchristos   type = get_tid_type (ccp->of, id);
11387d62b00eSchristos 
11397d62b00eSchristos   kind = ctf_type_kind (ccp->fp, id);
11407d62b00eSchristos   switch (kind)
11417d62b00eSchristos     {
11427d62b00eSchristos       case CTF_K_FUNCTION:
1143*6881a400Schristos 	if (name != nullptr && strcmp (name, "main") == 0)
11447d62b00eSchristos 	  set_objfile_main_name (ccp->of, name, language_c);
11457d62b00eSchristos 	break;
11467d62b00eSchristos       case CTF_K_INTEGER:
11477d62b00eSchristos       case CTF_K_FLOAT:
11487d62b00eSchristos       case CTF_K_VOLATILE:
11497d62b00eSchristos       case CTF_K_RESTRICT:
11507d62b00eSchristos       case CTF_K_TYPEDEF:
11517d62b00eSchristos       case CTF_K_CONST:
11527d62b00eSchristos       case CTF_K_POINTER:
11537d62b00eSchristos       case CTF_K_ARRAY:
1154*6881a400Schristos 	if (type != nullptr)
11557d62b00eSchristos 	  {
11567d62b00eSchristos 	    sym = new_symbol (ccp, type, id);
1157*6881a400Schristos 	    if (sym != nullptr)
11587d62b00eSchristos 	      sym->compute_and_set_names (name, false, ccp->of->per_bfd);
11597d62b00eSchristos 	  }
11607d62b00eSchristos 	break;
11617d62b00eSchristos       case CTF_K_STRUCT:
11627d62b00eSchristos       case CTF_K_UNION:
11637d62b00eSchristos       case CTF_K_ENUM:
1164*6881a400Schristos 	if (type == nullptr)
11657d62b00eSchristos 	  {
11667d62b00eSchristos 	    complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
11677d62b00eSchristos 	    type = objfile_type (ccp->of)->builtin_error;
11687d62b00eSchristos 	  }
11697d62b00eSchristos 	sym = new (&ccp->of->objfile_obstack) symbol;
11707d62b00eSchristos 	OBJSTAT (ccp->of, n_syms++);
1171*6881a400Schristos 	sym->set_type (type);
1172*6881a400Schristos 	sym->set_domain (VAR_DOMAIN);
1173*6881a400Schristos 	sym->set_aclass_index (LOC_OPTIMIZED_OUT);
11747d62b00eSchristos 	sym->compute_and_set_names (name, false, ccp->of->per_bfd);
1175*6881a400Schristos 	add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
11767d62b00eSchristos 	break;
11777d62b00eSchristos       default:
11787d62b00eSchristos 	complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind);
11797d62b00eSchristos 	break;
11807d62b00eSchristos     }
11817d62b00eSchristos 
1182*6881a400Schristos   if (sym != nullptr)
11837d62b00eSchristos     set_symbol_address (ccp->of, sym, name);
11847d62b00eSchristos 
11857d62b00eSchristos   return 0;
11867d62b00eSchristos }
11877d62b00eSchristos 
1188*6881a400Schristos /* Add entries in either data objects or function info section, controlled
1189*6881a400Schristos    by FUNCTIONS.  */
11907d62b00eSchristos 
1191*6881a400Schristos static void
1192*6881a400Schristos add_stt_entries (struct ctf_context *ccp, int functions)
11937d62b00eSchristos {
1194*6881a400Schristos   ctf_next_t *i = nullptr;
1195*6881a400Schristos   const char *tname;
1196*6881a400Schristos   ctf_id_t tid;
1197*6881a400Schristos   struct symbol *sym = nullptr;
11987d62b00eSchristos   struct type *type;
11997d62b00eSchristos 
1200*6881a400Schristos   while ((tid = ctf_symbol_next (ccp->fp, &i, &tname, functions)) != CTF_ERR)
1201*6881a400Schristos     {
12027d62b00eSchristos       type = get_tid_type (ccp->of, tid);
1203*6881a400Schristos       if (type == nullptr)
1204*6881a400Schristos 	continue;
1205*6881a400Schristos       sym = new (&ccp->of->objfile_obstack) symbol;
1206*6881a400Schristos       OBJSTAT (ccp->of, n_syms++);
1207*6881a400Schristos       sym->set_type (type);
1208*6881a400Schristos       sym->set_domain (VAR_DOMAIN);
1209*6881a400Schristos       sym->set_aclass_index (LOC_STATIC);
1210*6881a400Schristos       sym->compute_and_set_names (tname, false, ccp->of->per_bfd);
1211*6881a400Schristos       add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
1212*6881a400Schristos       set_symbol_address (ccp->of, sym, tname);
1213*6881a400Schristos     }
12147d62b00eSchristos }
12157d62b00eSchristos 
1216*6881a400Schristos /* Add entries in data objects section.  */
12177d62b00eSchristos 
1218*6881a400Schristos static void
1219*6881a400Schristos add_stt_obj (struct ctf_context *ccp)
12207d62b00eSchristos {
1221*6881a400Schristos   add_stt_entries (ccp, 0);
12227d62b00eSchristos }
12237d62b00eSchristos 
1224*6881a400Schristos /* Add entries in function info section.  */
12257d62b00eSchristos 
1226*6881a400Schristos static void
1227*6881a400Schristos add_stt_func (struct ctf_context *ccp)
1228*6881a400Schristos {
1229*6881a400Schristos   add_stt_entries (ccp, 1);
12307d62b00eSchristos }
12317d62b00eSchristos 
12327d62b00eSchristos /* Get text segment base for OBJFILE, TSIZE contains the segment size.  */
12337d62b00eSchristos 
12347d62b00eSchristos static CORE_ADDR
12357d62b00eSchristos get_objfile_text_range (struct objfile *of, int *tsize)
12367d62b00eSchristos {
1237*6881a400Schristos   bfd *abfd = of->obfd.get ();
12387d62b00eSchristos   const asection *codes;
12397d62b00eSchristos 
12407d62b00eSchristos   codes = bfd_get_section_by_name (abfd, ".text");
12417d62b00eSchristos   *tsize = codes ? bfd_section_size (codes) : 0;
12427d62b00eSchristos   return of->text_section_offset ();
12437d62b00eSchristos }
12447d62b00eSchristos 
12457d62b00eSchristos /* Start a symtab for OBJFILE in CTF format.  */
12467d62b00eSchristos 
12477d62b00eSchristos static void
1248*6881a400Schristos ctf_start_compunit_symtab (ctf_psymtab *pst,
12497d62b00eSchristos 			   struct objfile *of, CORE_ADDR text_offset)
12507d62b00eSchristos {
12517d62b00eSchristos   struct ctf_context *ccp;
12527d62b00eSchristos 
1253*6881a400Schristos   ccp = &pst->context;
12547d62b00eSchristos   ccp->builder = new buildsym_compunit
1255*6881a400Schristos 		       (of, pst->filename, nullptr,
12567d62b00eSchristos 		       language_c, text_offset);
12577d62b00eSchristos   ccp->builder->record_debugformat ("ctf");
12587d62b00eSchristos }
12597d62b00eSchristos 
12607d62b00eSchristos /* Finish reading symbol/type definitions in CTF format.
12617d62b00eSchristos    END_ADDR is the end address of the file's text.  SECTION is
12627d62b00eSchristos    the .text section number.  */
12637d62b00eSchristos 
12647d62b00eSchristos static struct compunit_symtab *
1265*6881a400Schristos ctf_end_compunit_symtab (ctf_psymtab *pst,
12667d62b00eSchristos 			 CORE_ADDR end_addr, int section)
12677d62b00eSchristos {
12687d62b00eSchristos   struct ctf_context *ccp;
12697d62b00eSchristos 
1270*6881a400Schristos   ccp = &pst->context;
12717d62b00eSchristos   struct compunit_symtab *result
1272*6881a400Schristos     = ccp->builder->end_compunit_symtab (end_addr, section);
12737d62b00eSchristos   delete ccp->builder;
1274*6881a400Schristos   ccp->builder = nullptr;
12757d62b00eSchristos   return result;
12767d62b00eSchristos }
12777d62b00eSchristos 
1278*6881a400Schristos /* Add all members of an enum with type TID to partial symbol table.  */
1279*6881a400Schristos 
1280*6881a400Schristos static void
1281*6881a400Schristos ctf_psymtab_add_enums (struct ctf_context *ccp, ctf_id_t tid)
1282*6881a400Schristos {
1283*6881a400Schristos   int val;
1284*6881a400Schristos   const char *ename;
1285*6881a400Schristos   ctf_next_t *i = nullptr;
1286*6881a400Schristos 
1287*6881a400Schristos   while ((ename = ctf_enum_next (ccp->fp, tid, &i, &val)) != nullptr)
1288*6881a400Schristos     {
1289*6881a400Schristos       ccp->pst->add_psymbol (ename, true,
1290*6881a400Schristos 			     VAR_DOMAIN, LOC_CONST, -1,
1291*6881a400Schristos 			     psymbol_placement::GLOBAL,
1292*6881a400Schristos 			     0, language_c, ccp->partial_symtabs, ccp->of);
1293*6881a400Schristos     }
1294*6881a400Schristos   if (ctf_errno (ccp->fp) != ECTF_NEXT_END)
1295*6881a400Schristos     complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
1296*6881a400Schristos 	       ctf_errmsg (ctf_errno (ccp->fp)));
1297*6881a400Schristos }
1298*6881a400Schristos 
1299*6881a400Schristos /* Add entries in either data objects or function info section, controlled
1300*6881a400Schristos    by FUNCTIONS, to psymtab.  */
1301*6881a400Schristos 
1302*6881a400Schristos static void
1303*6881a400Schristos ctf_psymtab_add_stt_entries (ctf_dict_t *cfp, ctf_psymtab *pst,
1304*6881a400Schristos 			     struct objfile *of, int functions)
1305*6881a400Schristos {
1306*6881a400Schristos   ctf_next_t *i = nullptr;
1307*6881a400Schristos   ctf_id_t tid;
1308*6881a400Schristos   const char *tname;
1309*6881a400Schristos 
1310*6881a400Schristos   while ((tid = ctf_symbol_next (cfp, &i, &tname, functions)) != CTF_ERR)
1311*6881a400Schristos     {
1312*6881a400Schristos       uint32_t kind = ctf_type_kind (cfp, tid);
1313*6881a400Schristos       address_class aclass;
1314*6881a400Schristos       domain_enum tdomain;
1315*6881a400Schristos       switch (kind)
1316*6881a400Schristos 	{
1317*6881a400Schristos 	  case CTF_K_STRUCT:
1318*6881a400Schristos 	  case CTF_K_UNION:
1319*6881a400Schristos 	  case CTF_K_ENUM:
1320*6881a400Schristos 	    tdomain = STRUCT_DOMAIN;
1321*6881a400Schristos 	    break;
1322*6881a400Schristos 	  default:
1323*6881a400Schristos 	    tdomain = VAR_DOMAIN;
1324*6881a400Schristos 	    break;
1325*6881a400Schristos 	}
1326*6881a400Schristos 
1327*6881a400Schristos       if (kind == CTF_K_FUNCTION)
1328*6881a400Schristos 	aclass = LOC_STATIC;
1329*6881a400Schristos       else if (kind == CTF_K_CONST)
1330*6881a400Schristos 	aclass = LOC_CONST;
1331*6881a400Schristos       else
1332*6881a400Schristos 	aclass = LOC_TYPEDEF;
1333*6881a400Schristos 
1334*6881a400Schristos       pst->add_psymbol (tname, true,
1335*6881a400Schristos 			tdomain, aclass, -1,
1336*6881a400Schristos 			psymbol_placement::GLOBAL,
1337*6881a400Schristos 			0, language_c, pst->context.partial_symtabs, of);
1338*6881a400Schristos     }
1339*6881a400Schristos }
1340*6881a400Schristos 
1341*6881a400Schristos /* Add entries in data objects section to psymtab.  */
1342*6881a400Schristos 
1343*6881a400Schristos static void
1344*6881a400Schristos ctf_psymtab_add_stt_obj (ctf_dict_t *cfp, ctf_psymtab *pst,
1345*6881a400Schristos 			 struct objfile *of)
1346*6881a400Schristos {
1347*6881a400Schristos   ctf_psymtab_add_stt_entries (cfp, pst, of, 0);
1348*6881a400Schristos }
1349*6881a400Schristos 
1350*6881a400Schristos /* Add entries in function info section to psymtab.  */
1351*6881a400Schristos 
1352*6881a400Schristos static void
1353*6881a400Schristos ctf_psymtab_add_stt_func (ctf_dict_t *cfp, ctf_psymtab *pst,
1354*6881a400Schristos 			  struct objfile *of)
1355*6881a400Schristos {
1356*6881a400Schristos   ctf_psymtab_add_stt_entries (cfp, pst, of, 1);
1357*6881a400Schristos }
1358*6881a400Schristos 
13597d62b00eSchristos /* Read in full symbols for PST, and anything it depends on.  */
13607d62b00eSchristos 
13617d62b00eSchristos void
13627d62b00eSchristos ctf_psymtab::expand_psymtab (struct objfile *objfile)
13637d62b00eSchristos {
13647d62b00eSchristos   struct ctf_context *ccp;
13657d62b00eSchristos 
13667d62b00eSchristos   gdb_assert (!readin);
13677d62b00eSchristos 
1368*6881a400Schristos   ccp = &context;
13697d62b00eSchristos 
13707d62b00eSchristos   /* Iterate over entries in data types section.  */
13717d62b00eSchristos   if (ctf_type_iter (ccp->fp, ctf_add_type_cb, ccp) == CTF_ERR)
13727d62b00eSchristos     complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
13737d62b00eSchristos 	       ctf_errmsg (ctf_errno (ccp->fp)));
13747d62b00eSchristos 
13757d62b00eSchristos 
13767d62b00eSchristos   /* Iterate over entries in variable info section.  */
13777d62b00eSchristos   if (ctf_variable_iter (ccp->fp, ctf_add_var_cb, ccp) == CTF_ERR)
13787d62b00eSchristos     complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
13797d62b00eSchristos 	       ctf_errmsg (ctf_errno (ccp->fp)));
13807d62b00eSchristos 
13817d62b00eSchristos   /* Add entries in data objects and function info sections.  */
1382*6881a400Schristos   add_stt_obj (ccp);
1383*6881a400Schristos   add_stt_func (ccp);
13847d62b00eSchristos 
13857d62b00eSchristos   readin = true;
13867d62b00eSchristos }
13877d62b00eSchristos 
13887d62b00eSchristos /* Expand partial symbol table PST into a full symbol table.
13897d62b00eSchristos    PST is not NULL.  */
13907d62b00eSchristos 
13917d62b00eSchristos void
13927d62b00eSchristos ctf_psymtab::read_symtab (struct objfile *objfile)
13937d62b00eSchristos {
13947d62b00eSchristos   if (readin)
13957d62b00eSchristos     warning (_("bug: psymtab for %s is already read in."), filename);
13967d62b00eSchristos   else
13977d62b00eSchristos     {
13987d62b00eSchristos       if (info_verbose)
13997d62b00eSchristos 	{
1400*6881a400Schristos 	  gdb_printf (_("Reading in CTF data for %s..."), filename);
14017d62b00eSchristos 	  gdb_flush (gdb_stdout);
14027d62b00eSchristos 	}
14037d62b00eSchristos 
14047d62b00eSchristos       /* Start a symtab.  */
14057d62b00eSchristos       CORE_ADDR offset;        /* Start of text segment.  */
14067d62b00eSchristos       int tsize;
14077d62b00eSchristos 
14087d62b00eSchristos       offset = get_objfile_text_range (objfile, &tsize);
1409*6881a400Schristos       ctf_start_compunit_symtab (this, objfile, offset);
14107d62b00eSchristos       expand_psymtab (objfile);
14117d62b00eSchristos 
14127d62b00eSchristos       set_text_low (offset);
14137d62b00eSchristos       set_text_high (offset + tsize);
1414*6881a400Schristos       compunit_symtab = ctf_end_compunit_symtab (this, offset + tsize,
14157d62b00eSchristos 						 SECT_OFF_TEXT (objfile));
14167d62b00eSchristos 
14177d62b00eSchristos       /* Finish up the debug error message.  */
14187d62b00eSchristos       if (info_verbose)
1419*6881a400Schristos 	gdb_printf (_("done.\n"));
14207d62b00eSchristos     }
14217d62b00eSchristos }
14227d62b00eSchristos 
14237d62b00eSchristos /* Allocate a new partial_symtab NAME.
14247d62b00eSchristos 
14257d62b00eSchristos    Each source file that has not been fully read in is represented by
14267d62b00eSchristos    a partial_symtab.  This contains the information on where in the
14277d62b00eSchristos    executable the debugging symbols for a specific file are, and a
14287d62b00eSchristos    list of names of global symbols which are located in this file.
14297d62b00eSchristos    They are all chained on partial symtab lists.
14307d62b00eSchristos 
14317d62b00eSchristos    Even after the source file has been read into a symtab, the
14327d62b00eSchristos    partial_symtab remains around.  They are allocated on an obstack,
14337d62b00eSchristos    objfile_obstack.  */
14347d62b00eSchristos 
14357d62b00eSchristos static ctf_psymtab *
14367d62b00eSchristos create_partial_symtab (const char *name,
1437*6881a400Schristos 		       ctf_archive_t *arc,
1438*6881a400Schristos 		       ctf_dict_t *cfp,
1439*6881a400Schristos 		       psymtab_storage *partial_symtabs,
14407d62b00eSchristos 		       struct objfile *objfile)
14417d62b00eSchristos {
14427d62b00eSchristos   ctf_psymtab *pst;
14437d62b00eSchristos 
1444*6881a400Schristos   pst = new ctf_psymtab (name, partial_symtabs, objfile->per_bfd, 0);
14457d62b00eSchristos 
1446*6881a400Schristos   pst->context.arc = arc;
1447*6881a400Schristos   pst->context.fp = cfp;
1448*6881a400Schristos   pst->context.of = objfile;
1449*6881a400Schristos   pst->context.partial_symtabs = partial_symtabs;
1450*6881a400Schristos   pst->context.pst = pst;
1451*6881a400Schristos   pst->context.builder = nullptr;
14527d62b00eSchristos 
14537d62b00eSchristos   return pst;
14547d62b00eSchristos }
14557d62b00eSchristos 
14567d62b00eSchristos /* Callback to add type TID to partial symbol table.  */
14577d62b00eSchristos 
14587d62b00eSchristos static int
14597d62b00eSchristos ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
14607d62b00eSchristos {
14617d62b00eSchristos   struct ctf_context *ccp;
14627d62b00eSchristos   uint32_t kind;
14637d62b00eSchristos   short section = -1;
14647d62b00eSchristos 
14657d62b00eSchristos   ccp = (struct ctf_context *) arg;
14667d62b00eSchristos 
14677d62b00eSchristos   domain_enum domain = UNDEF_DOMAIN;
14687d62b00eSchristos   enum address_class aclass = LOC_UNDEF;
14697d62b00eSchristos   kind = ctf_type_kind (ccp->fp, tid);
14707d62b00eSchristos   switch (kind)
14717d62b00eSchristos     {
1472*6881a400Schristos       case CTF_K_ENUM:
1473*6881a400Schristos 	ctf_psymtab_add_enums (ccp, tid);
1474*6881a400Schristos 	/* FALL THROUGH */
14757d62b00eSchristos       case CTF_K_STRUCT:
14767d62b00eSchristos       case CTF_K_UNION:
14777d62b00eSchristos 	domain = STRUCT_DOMAIN;
14787d62b00eSchristos 	aclass = LOC_TYPEDEF;
14797d62b00eSchristos 	break;
14807d62b00eSchristos       case CTF_K_FUNCTION:
14817d62b00eSchristos       case CTF_K_FORWARD:
14827d62b00eSchristos 	domain = VAR_DOMAIN;
14837d62b00eSchristos 	aclass = LOC_STATIC;
14847d62b00eSchristos 	section = SECT_OFF_TEXT (ccp->of);
14857d62b00eSchristos 	break;
14867d62b00eSchristos       case CTF_K_CONST:
14877d62b00eSchristos 	domain = VAR_DOMAIN;
14887d62b00eSchristos 	aclass = LOC_STATIC;
14897d62b00eSchristos 	break;
14907d62b00eSchristos       case CTF_K_TYPEDEF:
14917d62b00eSchristos       case CTF_K_POINTER:
14927d62b00eSchristos       case CTF_K_VOLATILE:
14937d62b00eSchristos       case CTF_K_RESTRICT:
14947d62b00eSchristos 	domain = VAR_DOMAIN;
14957d62b00eSchristos 	aclass = LOC_TYPEDEF;
14967d62b00eSchristos 	break;
14977d62b00eSchristos       case CTF_K_INTEGER:
14987d62b00eSchristos       case CTF_K_FLOAT:
14997d62b00eSchristos 	domain = VAR_DOMAIN;
15007d62b00eSchristos 	aclass = LOC_TYPEDEF;
15017d62b00eSchristos 	break;
15027d62b00eSchristos       case CTF_K_ARRAY:
15037d62b00eSchristos       case CTF_K_UNKNOWN:
15047d62b00eSchristos 	return 0;
15057d62b00eSchristos     }
15067d62b00eSchristos 
1507*6881a400Schristos   const char *name = ctf_type_name_raw (ccp->fp, tid);
1508*6881a400Schristos   if (name == nullptr || strlen (name) == 0)
1509*6881a400Schristos     return 0;
1510*6881a400Schristos 
1511*6881a400Schristos   ccp->pst->add_psymbol (name, false,
15127d62b00eSchristos 			 domain, aclass, section,
1513*6881a400Schristos 			 psymbol_placement::STATIC,
1514*6881a400Schristos 			 0, language_c, ccp->partial_symtabs, ccp->of);
15157d62b00eSchristos 
15167d62b00eSchristos   return 0;
15177d62b00eSchristos }
15187d62b00eSchristos 
15197d62b00eSchristos /* Callback to add variable NAME with ID to partial symbol table.  */
15207d62b00eSchristos 
15217d62b00eSchristos static int
15227d62b00eSchristos ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
15237d62b00eSchristos {
15247d62b00eSchristos   struct ctf_context *ccp = (struct ctf_context *) arg;
15257d62b00eSchristos 
1526*6881a400Schristos   ccp->pst->add_psymbol (name, true,
15277d62b00eSchristos 			 VAR_DOMAIN, LOC_STATIC, -1,
15287d62b00eSchristos 			 psymbol_placement::GLOBAL,
1529*6881a400Schristos 			 0, language_c, ccp->partial_symtabs, ccp->of);
15307d62b00eSchristos   return 0;
15317d62b00eSchristos }
15327d62b00eSchristos 
15337d62b00eSchristos /* Setup partial_symtab's describing each source file for which
15347d62b00eSchristos    debugging information is available.  */
15357d62b00eSchristos 
15367d62b00eSchristos static void
1537*6881a400Schristos scan_partial_symbols (ctf_dict_t *cfp, psymtab_storage *partial_symtabs,
1538*6881a400Schristos 		      struct ctf_per_tu_data *tup, const char *fname)
15397d62b00eSchristos {
1540*6881a400Schristos   struct objfile *of = tup->of;
1541*6881a400Schristos   bool isparent = false;
15427d62b00eSchristos 
1543*6881a400Schristos   if (strcmp (fname, ".ctf") == 0)
1544*6881a400Schristos     {
1545*6881a400Schristos       fname = bfd_get_filename (of->obfd.get ());
1546*6881a400Schristos       isparent = true;
1547*6881a400Schristos     }
15487d62b00eSchristos 
1549*6881a400Schristos   ctf_psymtab *pst = create_partial_symtab (fname, tup->arc, cfp,
1550*6881a400Schristos 					    partial_symtabs, of);
1551*6881a400Schristos 
1552*6881a400Schristos   struct ctf_context *ccx = &pst->context;
1553*6881a400Schristos   if (isparent == false)
1554*6881a400Schristos     ccx->pst = pst;
1555*6881a400Schristos 
1556*6881a400Schristos   if (ctf_type_iter (cfp, ctf_psymtab_type_cb, ccx) == CTF_ERR)
15577d62b00eSchristos     complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
15587d62b00eSchristos 	       ctf_errmsg (ctf_errno (cfp)));
15597d62b00eSchristos 
1560*6881a400Schristos   if (ctf_variable_iter (cfp, ctf_psymtab_var_cb, ccx) == CTF_ERR)
15617d62b00eSchristos     complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
15627d62b00eSchristos 	       ctf_errmsg (ctf_errno (cfp)));
15637d62b00eSchristos 
15647d62b00eSchristos   /* Scan CTF object and function sections which correspond to each
15657d62b00eSchristos      STT_FUNC or STT_OBJECT entry in the symbol table,
15667d62b00eSchristos      pick up what init_symtab has done.  */
1567*6881a400Schristos   ctf_psymtab_add_stt_obj (cfp, pst, of);
1568*6881a400Schristos   ctf_psymtab_add_stt_func (cfp, pst, of);
1569*6881a400Schristos 
1570*6881a400Schristos   pst->end ();
15717d62b00eSchristos }
15727d62b00eSchristos 
1573*6881a400Schristos /* Callback to build the psymtab for archive member NAME.  */
15747d62b00eSchristos 
1575*6881a400Schristos static int
1576*6881a400Schristos build_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
1577*6881a400Schristos {
1578*6881a400Schristos   struct ctf_per_tu_data *tup = (struct ctf_per_tu_data *) arg;
1579*6881a400Schristos   ctf_dict_t *parent = tup->fp;
1580*6881a400Schristos 
1581*6881a400Schristos   if (strcmp (name, ".ctf") != 0)
1582*6881a400Schristos     ctf_import (ctf, parent);
1583*6881a400Schristos 
1584*6881a400Schristos   if (info_verbose)
1585*6881a400Schristos     {
1586*6881a400Schristos       gdb_printf (_("Scanning archive member %s..."), name);
1587*6881a400Schristos       gdb_flush (gdb_stdout);
15887d62b00eSchristos     }
15897d62b00eSchristos 
1590*6881a400Schristos   psymtab_storage *pss = tup->psf->get_partial_symtabs ().get ();
1591*6881a400Schristos   scan_partial_symbols (ctf, pss, tup, name);
1592*6881a400Schristos 
1593*6881a400Schristos   return 0;
15947d62b00eSchristos }
15957d62b00eSchristos 
15967d62b00eSchristos /* Read CTF debugging information from a BFD section.  This is
15977d62b00eSchristos    called from elfread.c.  It does a quick pass through the
15987d62b00eSchristos    .ctf section to set up the partial symbol table.  */
15997d62b00eSchristos 
16007d62b00eSchristos void
16017d62b00eSchristos elfctf_build_psymtabs (struct objfile *of)
16027d62b00eSchristos {
1603*6881a400Schristos   struct ctf_per_tu_data pcu;
1604*6881a400Schristos   bfd *abfd = of->obfd.get ();
16057d62b00eSchristos   int err;
16067d62b00eSchristos 
16077d62b00eSchristos   ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
1608*6881a400Schristos   if (arc == nullptr)
16097d62b00eSchristos     error (_("ctf_bfdopen failed on %s - %s"),
16107d62b00eSchristos 	   bfd_get_filename (abfd), ctf_errmsg (err));
16117d62b00eSchristos 
1612*6881a400Schristos   ctf_dict_t *fp = ctf_dict_open (arc, NULL, &err);
1613*6881a400Schristos   if (fp == nullptr)
1614*6881a400Schristos     error (_("ctf_dict_open failed on %s - %s"),
16157d62b00eSchristos 	   bfd_get_filename (abfd), ctf_errmsg (err));
1616*6881a400Schristos   ctf_dict_key.emplace (of, fp);
16177d62b00eSchristos 
1618*6881a400Schristos   pcu.fp = fp;
1619*6881a400Schristos   pcu.of = of;
1620*6881a400Schristos   pcu.arc = arc;
1621*6881a400Schristos 
1622*6881a400Schristos   psymbol_functions *psf = new psymbol_functions ();
1623*6881a400Schristos   of->qf.emplace_front (psf);
1624*6881a400Schristos   pcu.psf = psf;
1625*6881a400Schristos 
1626*6881a400Schristos   if (ctf_archive_iter (arc, build_ctf_archive_member, &pcu) < 0)
1627*6881a400Schristos     error (_("ctf_archive_iter failed in input file %s: - %s"),
1628*6881a400Schristos 	   bfd_get_filename (abfd), ctf_errmsg (err));
16297d62b00eSchristos }
16307d62b00eSchristos 
16317d62b00eSchristos #else
16327d62b00eSchristos 
16337d62b00eSchristos void
16347d62b00eSchristos elfctf_build_psymtabs (struct objfile *of)
16357d62b00eSchristos {
16367d62b00eSchristos   /* Nothing to do if CTF is disabled.  */
16377d62b00eSchristos }
16387d62b00eSchristos 
16397d62b00eSchristos #endif /* ENABLE_LIBCTF */
1640