Home | History | Annotate | Line # | Download | only in gdb
ctfread.c revision 1.1.1.2
      1      1.1  christos /* Compact ANSI-C Type Format (CTF) support in GDB.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 2019-2023 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos /* This file format can be used to compactly represent the information needed
     21      1.1  christos    by a debugger to interpret the ANSI-C types used by a given program.
     22      1.1  christos    Traditionally, this kind of information is generated by the compiler when
     23      1.1  christos    invoked with the -g flag and is stored in "stabs" strings or in the more
     24      1.1  christos    modern DWARF format. A new -gtLEVEL option has been added in gcc to generate
     25      1.1  christos    such information. CTF provides a representation of only the information
     26      1.1  christos    that is relevant to debugging a complex, optimized C program such as the
     27      1.1  christos    operating system kernel in a form that is significantly more compact than
     28      1.1  christos    the equivalent stabs or DWARF representation.  The format is data-model
     29      1.1  christos    independent, so consumers do not need different code depending on whether
     30      1.1  christos    they are 32-bit or 64-bit programs.  CTF assumes that a standard ELF symbol
     31      1.1  christos    table is available for use in the debugger, and uses the structure and data
     32      1.1  christos    of the symbol table to avoid storing redundant information.  The CTF data
     33      1.1  christos    may be compressed on disk or in memory, indicated by a bit in the header.
     34      1.1  christos    CTF may be interpreted in a raw disk file, or it may be stored in an ELF
     35      1.1  christos    section, typically named .ctf.  Data structures are aligned so that a raw
     36      1.1  christos    CTF file or CTF ELF section may be manipulated using mmap(2).
     37      1.1  christos 
     38      1.1  christos    The CTF file or section itself has the following structure:
     39      1.1  christos 
     40      1.1  christos    +--------+--------+---------+----------+----------+-------+--------+
     41      1.1  christos    |  file  |  type  |  data   | function | variable | data  | string |
     42      1.1  christos    | header | labels | objects |   info   |   info   | types | table  |
     43      1.1  christos    +--------+--------+---------+----------+----------+-------+--------+
     44      1.1  christos 
     45      1.1  christos    The file header stores a magic number and version information, encoding
     46      1.1  christos    flags, and the byte offset of each of the sections relative to the end of the
     47      1.1  christos    header itself.  If the CTF data has been uniquified against another set of
     48      1.1  christos    CTF data, a reference to that data also appears in the header.  This
     49      1.1  christos    reference is the name of the label corresponding to the types uniquified
     50      1.1  christos    against.
     51      1.1  christos 
     52      1.1  christos    Following the header is a list of labels, used to group the types included in
     53      1.1  christos    the data types section.  Each label is accompanied by a type ID i.  A given
     54      1.1  christos    label refers to the group of types whose IDs are in the range [0, i].
     55      1.1  christos 
     56      1.1  christos    Data object and function records are stored in the same order as they appear
     57      1.1  christos    in the corresponding symbol table, except that symbols marked SHN_UNDEF are
     58      1.1  christos    not stored and symbols that have no type data are padded out with zeroes.
     59      1.1  christos    For each data object, the type ID (a small integer) is recorded.  For each
     60      1.1  christos    function, the type ID of the return type and argument types is recorded.
     61      1.1  christos 
     62      1.1  christos    Variable records (as distinct from data objects) provide a modicum of support
     63      1.1  christos    for non-ELF systems, mapping a variable name to a CTF type ID.  The variable
     64      1.1  christos    names are sorted into ASCIIbetical order, permitting binary searching.
     65      1.1  christos 
     66      1.1  christos    The data types section is a list of variable size records that represent each
     67      1.1  christos    type, in order by their ID.  The types themselves form a directed graph,
     68      1.1  christos    where each node may contain one or more outgoing edges to other type nodes,
     69      1.1  christos    denoted by their ID.
     70      1.1  christos 
     71      1.1  christos    Strings are recorded as a string table ID (0 or 1) and a byte offset into the
     72      1.1  christos    string table.  String table 0 is the internal CTF string table.  String table
     73      1.1  christos    1 is the external string table, which is the string table associated with the
     74      1.1  christos    ELF symbol table for this object.  CTF does not record any strings that are
     75      1.1  christos    already in the symbol table, and the CTF string table does not contain any
     76      1.1  christos    duplicated strings.  */
     77      1.1  christos 
     78      1.1  christos #include "defs.h"
     79      1.1  christos #include "buildsym.h"
     80      1.1  christos #include "complaints.h"
     81      1.1  christos #include "block.h"
     82      1.1  christos #include "ctfread.h"
     83      1.1  christos #include "psympriv.h"
     84      1.1  christos 
     85      1.1  christos #if ENABLE_LIBCTF
     86      1.1  christos 
     87      1.1  christos #include "ctf.h"
     88      1.1  christos #include "ctf-api.h"
     89      1.1  christos 
     90  1.1.1.2  christos static const registry<objfile>::key<htab, htab_deleter> ctf_tid_key;
     91      1.1  christos 
     92      1.1  christos struct ctf_fp_info
     93      1.1  christos {
     94  1.1.1.2  christos   explicit ctf_fp_info (ctf_dict_t *cfp) : fp (cfp) {}
     95      1.1  christos   ~ctf_fp_info ();
     96  1.1.1.2  christos   ctf_dict_t *fp;
     97      1.1  christos };
     98      1.1  christos 
     99  1.1.1.2  christos /* Cleanup function for the ctf_dict_key data.  */
    100      1.1  christos ctf_fp_info::~ctf_fp_info ()
    101      1.1  christos {
    102  1.1.1.2  christos   if (fp == nullptr)
    103      1.1  christos     return;
    104      1.1  christos 
    105      1.1  christos   ctf_archive_t *arc = ctf_get_arc (fp);
    106  1.1.1.2  christos   ctf_dict_close (fp);
    107      1.1  christos   ctf_close (arc);
    108      1.1  christos }
    109      1.1  christos 
    110  1.1.1.2  christos static const registry<objfile>::key<ctf_fp_info> ctf_dict_key;
    111      1.1  christos 
    112      1.1  christos /* A CTF context consists of a file pointer and an objfile pointer.  */
    113      1.1  christos 
    114      1.1  christos struct ctf_context
    115      1.1  christos {
    116  1.1.1.2  christos   ctf_dict_t *fp;
    117      1.1  christos   struct objfile *of;
    118  1.1.1.2  christos   psymtab_storage *partial_symtabs;
    119  1.1.1.2  christos   partial_symtab *pst;
    120  1.1.1.2  christos   ctf_archive_t *arc;
    121      1.1  christos   struct buildsym_compunit *builder;
    122      1.1  christos };
    123      1.1  christos 
    124      1.1  christos /* A partial symtab, specialized for this module.  */
    125      1.1  christos struct ctf_psymtab : public standard_psymtab
    126      1.1  christos {
    127  1.1.1.2  christos   ctf_psymtab (const char *filename,
    128  1.1.1.2  christos 	       psymtab_storage *partial_symtabs,
    129  1.1.1.2  christos 	       objfile_per_bfd_storage *objfile_per_bfd,
    130  1.1.1.2  christos 	       CORE_ADDR addr)
    131  1.1.1.2  christos     : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
    132      1.1  christos   {
    133      1.1  christos   }
    134      1.1  christos 
    135      1.1  christos   void read_symtab (struct objfile *) override;
    136      1.1  christos   void expand_psymtab (struct objfile *) override;
    137      1.1  christos 
    138  1.1.1.2  christos   struct ctf_context context;
    139      1.1  christos };
    140      1.1  christos 
    141      1.1  christos /* The routines that read and process fields/members of a C struct, union,
    142      1.1  christos    or enumeration, pass lists of data member fields in an instance of a
    143      1.1  christos    ctf_field_info structure. It is derived from dwarf2read.c.  */
    144      1.1  christos 
    145      1.1  christos struct ctf_nextfield
    146      1.1  christos {
    147      1.1  christos   struct field field {};
    148      1.1  christos };
    149      1.1  christos 
    150      1.1  christos struct ctf_field_info
    151      1.1  christos {
    152      1.1  christos   /* List of data member fields.  */
    153      1.1  christos   std::vector<struct ctf_nextfield> fields;
    154      1.1  christos 
    155      1.1  christos   /* Context.  */
    156      1.1  christos   struct ctf_context *cur_context;
    157      1.1  christos 
    158      1.1  christos   /* Parent type.  */
    159      1.1  christos   struct type *ptype;
    160      1.1  christos 
    161      1.1  christos   /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head
    162      1.1  christos      of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
    163      1.1  christos   std::vector<struct decl_field> typedef_field_list;
    164      1.1  christos 
    165      1.1  christos   /* Nested types defined by this struct and the number of elements in
    166      1.1  christos      this list.  */
    167      1.1  christos   std::vector<struct decl_field> nested_types_list;
    168      1.1  christos };
    169      1.1  christos 
    170  1.1.1.2  christos /* Data held for a translation unit.  */
    171  1.1.1.2  christos 
    172  1.1.1.2  christos struct ctf_per_tu_data
    173  1.1.1.2  christos {
    174  1.1.1.2  christos   ctf_dict_t *fp;
    175  1.1.1.2  christos   struct objfile *of;
    176  1.1.1.2  christos   ctf_archive_t *arc;
    177  1.1.1.2  christos   psymtab_storage *pss;
    178  1.1.1.2  christos   psymbol_functions *psf;
    179  1.1.1.2  christos };
    180      1.1  christos 
    181      1.1  christos /* Local function prototypes */
    182      1.1  christos 
    183      1.1  christos static int ctf_add_type_cb (ctf_id_t tid, void *arg);
    184      1.1  christos 
    185      1.1  christos static struct type *read_array_type (struct ctf_context *cp, ctf_id_t tid);
    186      1.1  christos 
    187      1.1  christos static struct type *read_pointer_type (struct ctf_context *cp, ctf_id_t tid,
    188      1.1  christos 				       ctf_id_t btid);
    189      1.1  christos 
    190      1.1  christos static struct type *read_structure_type (struct ctf_context *cp, ctf_id_t tid);
    191      1.1  christos 
    192      1.1  christos static struct type *read_enum_type (struct ctf_context *cp, ctf_id_t tid);
    193      1.1  christos 
    194      1.1  christos static struct type *read_typedef_type (struct ctf_context *cp, ctf_id_t tid,
    195      1.1  christos 				       ctf_id_t btid, const char *name);
    196      1.1  christos 
    197      1.1  christos static struct type *read_type_record (struct ctf_context *cp, ctf_id_t tid);
    198      1.1  christos 
    199      1.1  christos static void process_structure_type (struct ctf_context *cp, ctf_id_t tid);
    200      1.1  christos 
    201      1.1  christos static void process_struct_members (struct ctf_context *cp, ctf_id_t tid,
    202      1.1  christos 				    struct type *type);
    203      1.1  christos 
    204  1.1.1.2  christos static struct type *read_forward_type (struct ctf_context *cp, ctf_id_t tid);
    205  1.1.1.2  christos 
    206      1.1  christos static struct symbol *new_symbol (struct ctf_context *cp, struct type *type,
    207      1.1  christos 				  ctf_id_t tid);
    208      1.1  christos 
    209      1.1  christos struct ctf_tid_and_type
    210      1.1  christos {
    211      1.1  christos   ctf_id_t tid;
    212      1.1  christos   struct type *type;
    213      1.1  christos };
    214      1.1  christos 
    215      1.1  christos /* Hash function for a ctf_tid_and_type.  */
    216      1.1  christos 
    217      1.1  christos static hashval_t
    218      1.1  christos tid_and_type_hash (const void *item)
    219      1.1  christos {
    220      1.1  christos   const struct ctf_tid_and_type *ids
    221      1.1  christos     = (const struct ctf_tid_and_type *) item;
    222      1.1  christos 
    223      1.1  christos   return ids->tid;
    224      1.1  christos }
    225      1.1  christos 
    226      1.1  christos /* Equality function for a ctf_tid_and_type.  */
    227      1.1  christos 
    228      1.1  christos static int
    229      1.1  christos tid_and_type_eq (const void *item_lhs, const void *item_rhs)
    230      1.1  christos {
    231      1.1  christos   const struct ctf_tid_and_type *ids_lhs
    232      1.1  christos     = (const struct ctf_tid_and_type *) item_lhs;
    233      1.1  christos   const struct ctf_tid_and_type *ids_rhs
    234      1.1  christos     = (const struct ctf_tid_and_type *) item_rhs;
    235      1.1  christos 
    236      1.1  christos   return ids_lhs->tid == ids_rhs->tid;
    237      1.1  christos }
    238      1.1  christos 
    239      1.1  christos /* Set the type associated with TID to TYP.  */
    240      1.1  christos 
    241      1.1  christos static struct type *
    242      1.1  christos set_tid_type (struct objfile *of, ctf_id_t tid, struct type *typ)
    243      1.1  christos {
    244      1.1  christos   htab_t htab;
    245      1.1  christos 
    246  1.1.1.2  christos   htab = ctf_tid_key.get (of);
    247      1.1  christos   if (htab == NULL)
    248      1.1  christos     {
    249      1.1  christos       htab = htab_create_alloc (1, tid_and_type_hash,
    250      1.1  christos 				tid_and_type_eq,
    251      1.1  christos 				NULL, xcalloc, xfree);
    252      1.1  christos       ctf_tid_key.set (of, htab);
    253      1.1  christos     }
    254      1.1  christos 
    255      1.1  christos   struct ctf_tid_and_type **slot, ids;
    256      1.1  christos   ids.tid = tid;
    257      1.1  christos   ids.type = typ;
    258      1.1  christos   slot = (struct ctf_tid_and_type **) htab_find_slot (htab, &ids, INSERT);
    259  1.1.1.2  christos   if (*slot == nullptr)
    260  1.1.1.2  christos     *slot = XOBNEW (&of->objfile_obstack, struct ctf_tid_and_type);
    261      1.1  christos   **slot = ids;
    262      1.1  christos   return typ;
    263      1.1  christos }
    264      1.1  christos 
    265      1.1  christos /* Look up the type for TID in tid_and_type hash, return NULL if hash is
    266      1.1  christos    empty or TID does not have a saved type.  */
    267      1.1  christos 
    268      1.1  christos static struct type *
    269      1.1  christos get_tid_type (struct objfile *of, ctf_id_t tid)
    270      1.1  christos {
    271      1.1  christos   struct ctf_tid_and_type *slot, ids;
    272      1.1  christos   htab_t htab;
    273      1.1  christos 
    274  1.1.1.2  christos   htab = ctf_tid_key.get (of);
    275      1.1  christos   if (htab == NULL)
    276  1.1.1.2  christos     return nullptr;
    277      1.1  christos 
    278      1.1  christos   ids.tid = tid;
    279  1.1.1.2  christos   ids.type = nullptr;
    280      1.1  christos   slot = (struct ctf_tid_and_type *) htab_find (htab, &ids);
    281      1.1  christos   if (slot)
    282      1.1  christos     return slot->type;
    283      1.1  christos   else
    284  1.1.1.2  christos     return nullptr;
    285  1.1.1.2  christos }
    286  1.1.1.2  christos 
    287  1.1.1.2  christos /* Fetch the type for TID in CCP OF's tid_and_type hash, add the type to
    288  1.1.1.2  christos  *    context CCP if hash is empty or TID does not have a saved type.  */
    289  1.1.1.2  christos 
    290  1.1.1.2  christos static struct type *
    291  1.1.1.2  christos fetch_tid_type (struct ctf_context *ccp, ctf_id_t tid)
    292  1.1.1.2  christos {
    293  1.1.1.2  christos   struct objfile *of = ccp->of;
    294  1.1.1.2  christos   struct type *typ;
    295  1.1.1.2  christos 
    296  1.1.1.2  christos   typ = get_tid_type (of, tid);
    297  1.1.1.2  christos   if (typ == nullptr)
    298  1.1.1.2  christos     {
    299  1.1.1.2  christos       ctf_add_type_cb (tid, ccp);
    300  1.1.1.2  christos       typ = get_tid_type (of, tid);
    301  1.1.1.2  christos     }
    302  1.1.1.2  christos 
    303  1.1.1.2  christos   return typ;
    304      1.1  christos }
    305      1.1  christos 
    306      1.1  christos /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM.  */
    307      1.1  christos 
    308      1.1  christos static int
    309  1.1.1.2  christos get_bitsize (ctf_dict_t *fp, ctf_id_t tid, uint32_t kind)
    310      1.1  christos {
    311      1.1  christos   ctf_encoding_t cet;
    312      1.1  christos 
    313      1.1  christos   if ((kind == CTF_K_INTEGER || kind == CTF_K_ENUM
    314      1.1  christos       || kind == CTF_K_FLOAT)
    315      1.1  christos       && ctf_type_reference (fp, tid) != CTF_ERR
    316      1.1  christos       && ctf_type_encoding (fp, tid, &cet) != CTF_ERR)
    317      1.1  christos     return cet.cte_bits;
    318      1.1  christos 
    319      1.1  christos   return 0;
    320      1.1  christos }
    321      1.1  christos 
    322      1.1  christos /* Set SYM's address, with NAME, from its minimal symbol entry.  */
    323      1.1  christos 
    324      1.1  christos static void
    325      1.1  christos set_symbol_address (struct objfile *of, struct symbol *sym, const char *name)
    326      1.1  christos {
    327      1.1  christos   struct bound_minimal_symbol msym;
    328      1.1  christos 
    329  1.1.1.2  christos   msym = lookup_minimal_symbol (name, nullptr, of);
    330      1.1  christos   if (msym.minsym != NULL)
    331      1.1  christos     {
    332  1.1.1.2  christos       sym->set_value_address (msym.value_address ());
    333  1.1.1.2  christos       sym->set_aclass_index (LOC_STATIC);
    334  1.1.1.2  christos       sym->set_section_index (msym.minsym->section_index ());
    335      1.1  christos     }
    336      1.1  christos }
    337      1.1  christos 
    338      1.1  christos /* Create the vector of fields, and attach it to TYPE.  */
    339      1.1  christos 
    340      1.1  christos static void
    341      1.1  christos attach_fields_to_type (struct ctf_field_info *fip, struct type *type)
    342      1.1  christos {
    343      1.1  christos   int nfields = fip->fields.size ();
    344      1.1  christos 
    345      1.1  christos   if (nfields == 0)
    346      1.1  christos     return;
    347      1.1  christos 
    348      1.1  christos   /* Record the field count, allocate space for the array of fields.  */
    349      1.1  christos   type->set_num_fields (nfields);
    350      1.1  christos   type->set_fields
    351      1.1  christos     ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
    352      1.1  christos 
    353      1.1  christos   /* Copy the saved-up fields into the field vector.  */
    354      1.1  christos   for (int i = 0; i < nfields; ++i)
    355      1.1  christos     {
    356      1.1  christos       struct ctf_nextfield &field = fip->fields[i];
    357      1.1  christos       type->field (i) = field.field;
    358      1.1  christos     }
    359      1.1  christos }
    360      1.1  christos 
    361      1.1  christos /* Allocate a floating-point type of size BITS and name NAME.  Pass NAME_HINT
    362      1.1  christos    (which may be different from NAME) to the architecture back-end to allow
    363      1.1  christos    it to guess the correct format if necessary.  */
    364      1.1  christos 
    365      1.1  christos static struct type *
    366      1.1  christos ctf_init_float_type (struct objfile *objfile,
    367      1.1  christos 		     int bits,
    368      1.1  christos 		     const char *name,
    369      1.1  christos 		     const char *name_hint)
    370      1.1  christos {
    371      1.1  christos   struct gdbarch *gdbarch = objfile->arch ();
    372      1.1  christos   const struct floatformat **format;
    373      1.1  christos   struct type *type;
    374      1.1  christos 
    375      1.1  christos   format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
    376  1.1.1.2  christos   if (format != nullptr)
    377      1.1  christos     type = init_float_type (objfile, bits, name, format);
    378      1.1  christos   else
    379      1.1  christos     type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
    380      1.1  christos 
    381      1.1  christos   return type;
    382      1.1  christos }
    383      1.1  christos 
    384      1.1  christos /* Callback to add member NAME to a struct/union type. TID is the type
    385      1.1  christos    of struct/union member, OFFSET is the offset of member in bits,
    386      1.1  christos    and ARG contains the ctf_field_info.  */
    387      1.1  christos 
    388      1.1  christos static int
    389      1.1  christos ctf_add_member_cb (const char *name,
    390      1.1  christos 		   ctf_id_t tid,
    391      1.1  christos 		   unsigned long offset,
    392      1.1  christos 		   void *arg)
    393      1.1  christos {
    394      1.1  christos   struct ctf_field_info *fip = (struct ctf_field_info *) arg;
    395      1.1  christos   struct ctf_context *ccp = fip->cur_context;
    396      1.1  christos   struct ctf_nextfield new_field;
    397      1.1  christos   struct field *fp;
    398      1.1  christos   struct type *t;
    399      1.1  christos   uint32_t kind;
    400      1.1  christos 
    401      1.1  christos   fp = &new_field.field;
    402  1.1.1.2  christos   fp->set_name (name);
    403      1.1  christos 
    404      1.1  christos   kind = ctf_type_kind (ccp->fp, tid);
    405  1.1.1.2  christos   t = fetch_tid_type (ccp, tid);
    406  1.1.1.2  christos   if (t == nullptr)
    407      1.1  christos     {
    408      1.1  christos       t = read_type_record (ccp, tid);
    409  1.1.1.2  christos       if (t == nullptr)
    410      1.1  christos 	{
    411      1.1  christos 	  complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name, tid);
    412      1.1  christos 	  t = objfile_type (ccp->of)->builtin_error;
    413      1.1  christos 	  set_tid_type (ccp->of, tid, t);
    414      1.1  christos 	}
    415      1.1  christos     }
    416      1.1  christos 
    417      1.1  christos   if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
    418      1.1  christos     process_struct_members (ccp, tid, t);
    419      1.1  christos 
    420      1.1  christos   fp->set_type (t);
    421  1.1.1.2  christos   fp->set_loc_bitpos (offset / TARGET_CHAR_BIT);
    422      1.1  christos   FIELD_BITSIZE (*fp) = get_bitsize (ccp->fp, tid, kind);
    423      1.1  christos 
    424      1.1  christos   fip->fields.emplace_back (new_field);
    425      1.1  christos 
    426      1.1  christos   return 0;
    427      1.1  christos }
    428      1.1  christos 
    429      1.1  christos /* Callback to add member NAME of EVAL to an enumeration type.
    430      1.1  christos    ARG contains the ctf_field_info.  */
    431      1.1  christos 
    432      1.1  christos static int
    433      1.1  christos ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)
    434      1.1  christos {
    435      1.1  christos   struct ctf_field_info *fip = (struct ctf_field_info *) arg;
    436      1.1  christos   struct ctf_nextfield new_field;
    437      1.1  christos   struct field *fp;
    438      1.1  christos   struct ctf_context *ccp = fip->cur_context;
    439      1.1  christos 
    440      1.1  christos   fp = &new_field.field;
    441  1.1.1.2  christos   fp->set_name (name);
    442  1.1.1.2  christos   fp->set_type (nullptr);
    443  1.1.1.2  christos   fp->set_loc_enumval (enum_value);
    444      1.1  christos   FIELD_BITSIZE (*fp) = 0;
    445      1.1  christos 
    446  1.1.1.2  christos   if (name != nullptr)
    447      1.1  christos     {
    448      1.1  christos       struct symbol *sym = new (&ccp->of->objfile_obstack) symbol;
    449      1.1  christos       OBJSTAT (ccp->of, n_syms++);
    450      1.1  christos 
    451      1.1  christos       sym->set_language (language_c, &ccp->of->objfile_obstack);
    452      1.1  christos       sym->compute_and_set_names (name, false, ccp->of->per_bfd);
    453  1.1.1.2  christos       sym->set_aclass_index (LOC_CONST);
    454  1.1.1.2  christos       sym->set_domain (VAR_DOMAIN);
    455  1.1.1.2  christos       sym->set_type (fip->ptype);
    456      1.1  christos       add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
    457      1.1  christos     }
    458      1.1  christos 
    459      1.1  christos   fip->fields.emplace_back (new_field);
    460      1.1  christos 
    461      1.1  christos   return 0;
    462      1.1  christos }
    463      1.1  christos 
    464      1.1  christos /* Add a new symbol entry, with its name from TID, its access index and
    465      1.1  christos    domain from TID's kind, and its type from TYPE.  */
    466      1.1  christos 
    467      1.1  christos static struct symbol *
    468      1.1  christos new_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)
    469      1.1  christos {
    470      1.1  christos   struct objfile *objfile = ccp->of;
    471  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    472  1.1.1.2  christos   struct symbol *sym = nullptr;
    473      1.1  christos 
    474  1.1.1.2  christos   const char *name = ctf_type_name_raw (fp, tid);
    475  1.1.1.2  christos   if (name != nullptr)
    476      1.1  christos     {
    477      1.1  christos       sym = new (&objfile->objfile_obstack) symbol;
    478      1.1  christos       OBJSTAT (objfile, n_syms++);
    479      1.1  christos 
    480      1.1  christos       sym->set_language (language_c, &objfile->objfile_obstack);
    481  1.1.1.2  christos       sym->compute_and_set_names (name, false, objfile->per_bfd);
    482  1.1.1.2  christos       sym->set_domain (VAR_DOMAIN);
    483  1.1.1.2  christos       sym->set_aclass_index (LOC_OPTIMIZED_OUT);
    484      1.1  christos 
    485  1.1.1.2  christos       if (type != nullptr)
    486  1.1.1.2  christos 	sym->set_type (type);
    487      1.1  christos 
    488      1.1  christos       uint32_t kind = ctf_type_kind (fp, tid);
    489      1.1  christos       switch (kind)
    490      1.1  christos 	{
    491      1.1  christos 	  case CTF_K_STRUCT:
    492      1.1  christos 	  case CTF_K_UNION:
    493      1.1  christos 	  case CTF_K_ENUM:
    494  1.1.1.2  christos 	    sym->set_aclass_index (LOC_TYPEDEF);
    495  1.1.1.2  christos 	    sym->set_domain (STRUCT_DOMAIN);
    496      1.1  christos 	    break;
    497      1.1  christos 	  case CTF_K_FUNCTION:
    498  1.1.1.2  christos 	    sym->set_aclass_index (LOC_STATIC);
    499  1.1.1.2  christos 	    set_symbol_address (objfile, sym, sym->linkage_name ());
    500      1.1  christos 	    break;
    501      1.1  christos 	  case CTF_K_CONST:
    502  1.1.1.2  christos 	    if (sym->type ()->code () == TYPE_CODE_VOID)
    503  1.1.1.2  christos 	      sym->set_type (objfile_type (objfile)->builtin_int);
    504      1.1  christos 	    break;
    505      1.1  christos 	  case CTF_K_TYPEDEF:
    506      1.1  christos 	  case CTF_K_INTEGER:
    507      1.1  christos 	  case CTF_K_FLOAT:
    508  1.1.1.2  christos 	    sym->set_aclass_index (LOC_TYPEDEF);
    509  1.1.1.2  christos 	    sym->set_domain (VAR_DOMAIN);
    510      1.1  christos 	    break;
    511      1.1  christos 	  case CTF_K_POINTER:
    512      1.1  christos 	    break;
    513      1.1  christos 	  case CTF_K_VOLATILE:
    514      1.1  christos 	  case CTF_K_RESTRICT:
    515      1.1  christos 	    break;
    516      1.1  christos 	  case CTF_K_SLICE:
    517      1.1  christos 	  case CTF_K_ARRAY:
    518      1.1  christos 	  case CTF_K_UNKNOWN:
    519      1.1  christos 	    break;
    520      1.1  christos 	}
    521      1.1  christos 
    522  1.1.1.2  christos       add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
    523      1.1  christos     }
    524      1.1  christos 
    525      1.1  christos   return sym;
    526      1.1  christos }
    527      1.1  christos 
    528      1.1  christos /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
    529      1.1  christos    and create the symbol for it.  */
    530      1.1  christos 
    531      1.1  christos static struct type *
    532      1.1  christos read_base_type (struct ctf_context *ccp, ctf_id_t tid)
    533      1.1  christos {
    534      1.1  christos   struct objfile *of = ccp->of;
    535  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    536      1.1  christos   ctf_encoding_t cet;
    537  1.1.1.2  christos   struct type *type = nullptr;
    538  1.1.1.2  christos   const char *name;
    539      1.1  christos   uint32_t kind;
    540      1.1  christos 
    541      1.1  christos   if (ctf_type_encoding (fp, tid, &cet))
    542      1.1  christos     {
    543      1.1  christos       complaint (_("ctf_type_encoding read_base_type failed - %s"),
    544      1.1  christos 		 ctf_errmsg (ctf_errno (fp)));
    545  1.1.1.2  christos       return nullptr;
    546      1.1  christos     }
    547      1.1  christos 
    548  1.1.1.2  christos   name = ctf_type_name_raw (fp, tid);
    549  1.1.1.2  christos   if (name == nullptr || strlen (name) == 0)
    550      1.1  christos     {
    551      1.1  christos       name = ctf_type_aname (fp, tid);
    552  1.1.1.2  christos       if (name == nullptr)
    553      1.1  christos 	complaint (_("ctf_type_aname read_base_type failed - %s"),
    554      1.1  christos 		   ctf_errmsg (ctf_errno (fp)));
    555      1.1  christos     }
    556      1.1  christos 
    557      1.1  christos   kind = ctf_type_kind (fp, tid);
    558      1.1  christos   if (kind == CTF_K_INTEGER)
    559      1.1  christos     {
    560      1.1  christos       uint32_t issigned, ischar, isbool;
    561      1.1  christos       struct gdbarch *gdbarch = of->arch ();
    562      1.1  christos 
    563      1.1  christos       issigned = cet.cte_format & CTF_INT_SIGNED;
    564      1.1  christos       ischar = cet.cte_format & CTF_INT_CHAR;
    565      1.1  christos       isbool = cet.cte_format & CTF_INT_BOOL;
    566      1.1  christos       if (ischar)
    567      1.1  christos 	type = init_character_type (of, TARGET_CHAR_BIT, !issigned, name);
    568      1.1  christos       else if (isbool)
    569      1.1  christos 	type = init_boolean_type (of, gdbarch_int_bit (gdbarch),
    570      1.1  christos 				  !issigned, name);
    571      1.1  christos       else
    572      1.1  christos 	{
    573      1.1  christos 	  int bits;
    574      1.1  christos 	  if (cet.cte_bits && ((cet.cte_bits % TARGET_CHAR_BIT) == 0))
    575      1.1  christos 	    bits = cet.cte_bits;
    576      1.1  christos 	  else
    577      1.1  christos 	    bits = gdbarch_int_bit (gdbarch);
    578      1.1  christos 	  type = init_integer_type (of, bits, !issigned, name);
    579      1.1  christos 	}
    580      1.1  christos     }
    581      1.1  christos   else if (kind == CTF_K_FLOAT)
    582      1.1  christos     {
    583      1.1  christos       uint32_t isflt;
    584      1.1  christos       isflt = !((cet.cte_format & CTF_FP_IMAGRY) == CTF_FP_IMAGRY
    585      1.1  christos 		 || (cet.cte_format & CTF_FP_DIMAGRY) == CTF_FP_DIMAGRY
    586      1.1  christos 		 || (cet.cte_format & CTF_FP_LDIMAGRY) == CTF_FP_LDIMAGRY);
    587      1.1  christos       if (isflt)
    588      1.1  christos 	type = ctf_init_float_type (of, cet.cte_bits, name, name);
    589      1.1  christos       else
    590      1.1  christos 	{
    591      1.1  christos 	  struct type *t
    592      1.1  christos 	    = ctf_init_float_type (of, cet.cte_bits / 2, NULL, name);
    593      1.1  christos 	  type = init_complex_type (name, t);
    594      1.1  christos 	}
    595      1.1  christos     }
    596      1.1  christos   else
    597      1.1  christos     {
    598      1.1  christos       complaint (_("read_base_type: unsupported base kind (%d)"), kind);
    599      1.1  christos       type = init_type (of, TYPE_CODE_ERROR, cet.cte_bits, name);
    600      1.1  christos     }
    601      1.1  christos 
    602  1.1.1.2  christos   if (name != nullptr && strcmp (name, "char") == 0)
    603  1.1.1.2  christos     type->set_has_no_signedness (true);
    604      1.1  christos 
    605      1.1  christos   return set_tid_type (of, tid, type);
    606      1.1  christos }
    607      1.1  christos 
    608      1.1  christos static void
    609      1.1  christos process_base_type (struct ctf_context *ccp, ctf_id_t tid)
    610      1.1  christos {
    611      1.1  christos   struct type *type;
    612      1.1  christos 
    613      1.1  christos   type = read_base_type (ccp, tid);
    614      1.1  christos   new_symbol (ccp, type, tid);
    615      1.1  christos }
    616      1.1  christos 
    617      1.1  christos /* Start a structure or union scope (definition) with TID to create a type
    618      1.1  christos    for the structure or union.
    619      1.1  christos 
    620      1.1  christos    Fill in the type's name and general properties. The members will not be
    621      1.1  christos    processed, nor a symbol table entry be done until process_structure_type
    622      1.1  christos    (assuming the type has a name).  */
    623      1.1  christos 
    624      1.1  christos static struct type *
    625      1.1  christos read_structure_type (struct ctf_context *ccp, ctf_id_t tid)
    626      1.1  christos {
    627      1.1  christos   struct objfile *of = ccp->of;
    628  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    629      1.1  christos   struct type *type;
    630      1.1  christos   uint32_t kind;
    631      1.1  christos 
    632      1.1  christos   type = alloc_type (of);
    633      1.1  christos 
    634  1.1.1.2  christos   const char *name = ctf_type_name_raw (fp, tid);
    635  1.1.1.2  christos   if (name != nullptr && strlen (name) != 0)
    636  1.1.1.2  christos     type->set_name (name);
    637      1.1  christos 
    638      1.1  christos   kind = ctf_type_kind (fp, tid);
    639      1.1  christos   if (kind == CTF_K_UNION)
    640      1.1  christos     type->set_code (TYPE_CODE_UNION);
    641      1.1  christos   else
    642      1.1  christos     type->set_code (TYPE_CODE_STRUCT);
    643      1.1  christos 
    644  1.1.1.2  christos   type->set_length (ctf_type_size (fp, tid));
    645      1.1  christos   set_type_align (type, ctf_type_align (fp, tid));
    646      1.1  christos 
    647      1.1  christos   return set_tid_type (ccp->of, tid, type);
    648      1.1  christos }
    649      1.1  christos 
    650      1.1  christos /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
    651      1.1  christos    and create the symbol for it.  */
    652      1.1  christos 
    653      1.1  christos static void
    654      1.1  christos process_struct_members (struct ctf_context *ccp,
    655      1.1  christos 			ctf_id_t tid,
    656      1.1  christos 			struct type *type)
    657      1.1  christos {
    658      1.1  christos   struct ctf_field_info fi;
    659      1.1  christos 
    660      1.1  christos   fi.cur_context = ccp;
    661      1.1  christos   if (ctf_member_iter (ccp->fp, tid, ctf_add_member_cb, &fi) == CTF_ERR)
    662      1.1  christos     complaint (_("ctf_member_iter process_struct_members failed - %s"),
    663      1.1  christos 	       ctf_errmsg (ctf_errno (ccp->fp)));
    664      1.1  christos 
    665      1.1  christos   /* Attach fields to the type.  */
    666      1.1  christos   attach_fields_to_type (&fi, type);
    667      1.1  christos 
    668      1.1  christos   new_symbol (ccp, type, tid);
    669      1.1  christos }
    670      1.1  christos 
    671      1.1  christos static void
    672      1.1  christos process_structure_type (struct ctf_context *ccp, ctf_id_t tid)
    673      1.1  christos {
    674      1.1  christos   struct type *type;
    675      1.1  christos 
    676      1.1  christos   type = read_structure_type (ccp, tid);
    677      1.1  christos   process_struct_members (ccp, tid, type);
    678      1.1  christos }
    679      1.1  christos 
    680      1.1  christos /* Create a function type for TID and set its return type.  */
    681      1.1  christos 
    682      1.1  christos static struct type *
    683      1.1  christos read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
    684      1.1  christos {
    685      1.1  christos   struct objfile *of = ccp->of;
    686  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    687  1.1.1.2  christos   struct type *type, *rettype, *atype;
    688      1.1  christos   ctf_funcinfo_t cfi;
    689  1.1.1.2  christos   uint32_t argc;
    690      1.1  christos 
    691      1.1  christos   type = alloc_type (of);
    692      1.1  christos 
    693      1.1  christos   type->set_code (TYPE_CODE_FUNC);
    694  1.1.1.2  christos   if (ctf_func_type_info (fp, tid, &cfi) < 0)
    695  1.1.1.2  christos     {
    696  1.1.1.2  christos       const char *fname = ctf_type_name_raw (fp, tid);
    697  1.1.1.2  christos       error (_("Error getting function type info: %s"),
    698  1.1.1.2  christos 	     fname == nullptr ? "noname" : fname);
    699  1.1.1.2  christos     }
    700  1.1.1.2  christos   rettype = fetch_tid_type (ccp, cfi.ctc_return);
    701  1.1.1.2  christos   type->set_target_type (rettype);
    702      1.1  christos   set_type_align (type, ctf_type_align (fp, tid));
    703      1.1  christos 
    704  1.1.1.2  christos   /* Set up function's arguments.  */
    705  1.1.1.2  christos   argc = cfi.ctc_argc;
    706  1.1.1.2  christos   type->set_num_fields (argc);
    707  1.1.1.2  christos   if ((cfi.ctc_flags & CTF_FUNC_VARARG) != 0)
    708  1.1.1.2  christos     type->set_has_varargs (true);
    709  1.1.1.2  christos 
    710  1.1.1.2  christos   if (argc != 0)
    711  1.1.1.2  christos     {
    712  1.1.1.2  christos       std::vector<ctf_id_t> argv (argc);
    713  1.1.1.2  christos       if (ctf_func_type_args (fp, tid, argc, argv.data ()) == CTF_ERR)
    714  1.1.1.2  christos 	return nullptr;
    715  1.1.1.2  christos 
    716  1.1.1.2  christos       type->set_fields
    717  1.1.1.2  christos 	((struct field *) TYPE_ZALLOC (type, argc * sizeof (struct field)));
    718  1.1.1.2  christos       struct type *void_type = objfile_type (of)->builtin_void;
    719  1.1.1.2  christos       /* If failed to find the argument type, fill it with void_type.  */
    720  1.1.1.2  christos       for (int iparam = 0; iparam < argc; iparam++)
    721  1.1.1.2  christos 	{
    722  1.1.1.2  christos 	  atype = fetch_tid_type (ccp, argv[iparam]);
    723  1.1.1.2  christos 	  if (atype != nullptr)
    724  1.1.1.2  christos 	    type->field (iparam).set_type (atype);
    725  1.1.1.2  christos 	  else
    726  1.1.1.2  christos 	    type->field (iparam).set_type (void_type);
    727  1.1.1.2  christos 	}
    728  1.1.1.2  christos     }
    729  1.1.1.2  christos 
    730      1.1  christos   return set_tid_type (of, tid, type);
    731      1.1  christos }
    732      1.1  christos 
    733      1.1  christos /* Given a TID of CTF_K_ENUM, process all the members of the
    734      1.1  christos    enumeration, and create the symbol for the enumeration type.  */
    735      1.1  christos 
    736      1.1  christos static struct type *
    737      1.1  christos read_enum_type (struct ctf_context *ccp, ctf_id_t tid)
    738      1.1  christos {
    739      1.1  christos   struct objfile *of = ccp->of;
    740  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    741  1.1.1.2  christos   struct type *type;
    742      1.1  christos 
    743      1.1  christos   type = alloc_type (of);
    744      1.1  christos 
    745  1.1.1.2  christos   const char *name = ctf_type_name_raw (fp, tid);
    746  1.1.1.2  christos   if (name != nullptr && strlen (name) != 0)
    747  1.1.1.2  christos     type->set_name (name);
    748      1.1  christos 
    749      1.1  christos   type->set_code (TYPE_CODE_ENUM);
    750  1.1.1.2  christos   type->set_length (ctf_type_size (fp, tid));
    751  1.1.1.2  christos   /* Set the underlying type based on its ctf_type_size bits.  */
    752  1.1.1.2  christos   type->set_target_type (objfile_int_type (of, type->length (), false));
    753      1.1  christos   set_type_align (type, ctf_type_align (fp, tid));
    754      1.1  christos 
    755      1.1  christos   return set_tid_type (of, tid, type);
    756      1.1  christos }
    757      1.1  christos 
    758      1.1  christos static void
    759      1.1  christos process_enum_type (struct ctf_context *ccp, ctf_id_t tid)
    760      1.1  christos {
    761      1.1  christos   struct type *type;
    762      1.1  christos   struct ctf_field_info fi;
    763      1.1  christos 
    764      1.1  christos   type = read_enum_type (ccp, tid);
    765      1.1  christos 
    766      1.1  christos   fi.cur_context = ccp;
    767      1.1  christos   fi.ptype = type;
    768      1.1  christos   if (ctf_enum_iter (ccp->fp, tid, ctf_add_enum_member_cb, &fi) == CTF_ERR)
    769      1.1  christos     complaint (_("ctf_enum_iter process_enum_type failed - %s"),
    770      1.1  christos 	       ctf_errmsg (ctf_errno (ccp->fp)));
    771      1.1  christos 
    772      1.1  christos   /* Attach fields to the type.  */
    773      1.1  christos   attach_fields_to_type (&fi, type);
    774      1.1  christos 
    775      1.1  christos   new_symbol (ccp, type, tid);
    776      1.1  christos }
    777      1.1  christos 
    778      1.1  christos /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID.  */
    779      1.1  christos 
    780      1.1  christos static struct type *
    781      1.1  christos add_array_cv_type (struct ctf_context *ccp,
    782      1.1  christos 		   ctf_id_t tid,
    783      1.1  christos 		   struct type *base_type,
    784      1.1  christos 		   int cnst,
    785      1.1  christos 		   int voltl)
    786      1.1  christos {
    787      1.1  christos   struct type *el_type, *inner_array;
    788      1.1  christos 
    789      1.1  christos   base_type = copy_type (base_type);
    790      1.1  christos   inner_array = base_type;
    791      1.1  christos 
    792  1.1.1.2  christos   while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
    793      1.1  christos     {
    794  1.1.1.2  christos       inner_array->set_target_type (copy_type (inner_array->target_type ()));
    795  1.1.1.2  christos       inner_array = inner_array->target_type ();
    796      1.1  christos     }
    797      1.1  christos 
    798  1.1.1.2  christos   el_type = inner_array->target_type ();
    799      1.1  christos   cnst |= TYPE_CONST (el_type);
    800      1.1  christos   voltl |= TYPE_VOLATILE (el_type);
    801  1.1.1.2  christos   inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, nullptr));
    802      1.1  christos 
    803      1.1  christos   return set_tid_type (ccp->of, tid, base_type);
    804      1.1  christos }
    805      1.1  christos 
    806      1.1  christos /* Read all information from a TID of CTF_K_ARRAY.  */
    807      1.1  christos 
    808      1.1  christos static struct type *
    809      1.1  christos read_array_type (struct ctf_context *ccp, ctf_id_t tid)
    810      1.1  christos {
    811      1.1  christos   struct objfile *objfile = ccp->of;
    812  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    813      1.1  christos   struct type *element_type, *range_type, *idx_type;
    814      1.1  christos   struct type *type;
    815      1.1  christos   ctf_arinfo_t ar;
    816      1.1  christos 
    817      1.1  christos   if (ctf_array_info (fp, tid, &ar) == CTF_ERR)
    818      1.1  christos     {
    819      1.1  christos       complaint (_("ctf_array_info read_array_type failed - %s"),
    820      1.1  christos 		 ctf_errmsg (ctf_errno (fp)));
    821  1.1.1.2  christos       return nullptr;
    822      1.1  christos     }
    823      1.1  christos 
    824  1.1.1.2  christos   element_type = fetch_tid_type (ccp, ar.ctr_contents);
    825  1.1.1.2  christos   if (element_type == nullptr)
    826  1.1.1.2  christos     return nullptr;
    827      1.1  christos 
    828  1.1.1.2  christos   idx_type = fetch_tid_type (ccp, ar.ctr_index);
    829  1.1.1.2  christos   if (idx_type == nullptr)
    830      1.1  christos     idx_type = objfile_type (objfile)->builtin_int;
    831      1.1  christos 
    832      1.1  christos   range_type = create_static_range_type (NULL, idx_type, 0, ar.ctr_nelems - 1);
    833      1.1  christos   type = create_array_type (NULL, element_type, range_type);
    834      1.1  christos   if (ar.ctr_nelems <= 1)	/* Check if undefined upper bound.  */
    835      1.1  christos     {
    836      1.1  christos       range_type->bounds ()->high.set_undefined ();
    837  1.1.1.2  christos       type->set_length (0);
    838  1.1.1.2  christos       type->set_target_is_stub (true);
    839      1.1  christos     }
    840      1.1  christos   else
    841  1.1.1.2  christos     type->set_length (ctf_type_size (fp, tid));
    842      1.1  christos 
    843      1.1  christos   set_type_align (type, ctf_type_align (fp, tid));
    844      1.1  christos 
    845      1.1  christos   return set_tid_type (objfile, tid, type);
    846      1.1  christos }
    847      1.1  christos 
    848      1.1  christos /* Read TID of kind CTF_K_CONST with base type BTID.  */
    849      1.1  christos 
    850      1.1  christos static struct type *
    851      1.1  christos read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
    852      1.1  christos {
    853      1.1  christos   struct objfile *objfile = ccp->of;
    854      1.1  christos   struct type *base_type, *cv_type;
    855      1.1  christos 
    856  1.1.1.2  christos   base_type = fetch_tid_type (ccp, btid);
    857  1.1.1.2  christos   if (base_type == nullptr)
    858      1.1  christos     {
    859      1.1  christos       base_type = read_type_record (ccp, btid);
    860  1.1.1.2  christos       if (base_type == nullptr)
    861      1.1  christos 	{
    862      1.1  christos 	  complaint (_("read_const_type: NULL base type (%ld)"), btid);
    863      1.1  christos 	  base_type = objfile_type (objfile)->builtin_error;
    864      1.1  christos 	}
    865      1.1  christos     }
    866      1.1  christos   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
    867      1.1  christos 
    868      1.1  christos   return set_tid_type (objfile, tid, cv_type);
    869      1.1  christos }
    870      1.1  christos 
    871      1.1  christos /* Read TID of kind CTF_K_VOLATILE with base type BTID.  */
    872      1.1  christos 
    873      1.1  christos static struct type *
    874      1.1  christos read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
    875      1.1  christos {
    876      1.1  christos   struct objfile *objfile = ccp->of;
    877  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    878      1.1  christos   struct type *base_type, *cv_type;
    879      1.1  christos 
    880  1.1.1.2  christos   base_type = fetch_tid_type (ccp, btid);
    881  1.1.1.2  christos   if (base_type == nullptr)
    882      1.1  christos     {
    883      1.1  christos       base_type = read_type_record (ccp, btid);
    884  1.1.1.2  christos       if (base_type == nullptr)
    885      1.1  christos 	{
    886      1.1  christos 	  complaint (_("read_volatile_type: NULL base type (%ld)"), btid);
    887      1.1  christos 	  base_type = objfile_type (objfile)->builtin_error;
    888      1.1  christos 	}
    889      1.1  christos     }
    890      1.1  christos 
    891      1.1  christos   if (ctf_type_kind (fp, btid) == CTF_K_ARRAY)
    892      1.1  christos     return add_array_cv_type (ccp, tid, base_type, 0, 1);
    893      1.1  christos   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
    894      1.1  christos 
    895      1.1  christos   return set_tid_type (objfile, tid, cv_type);
    896      1.1  christos }
    897      1.1  christos 
    898      1.1  christos /* Read TID of kind CTF_K_RESTRICT with base type BTID.  */
    899      1.1  christos 
    900      1.1  christos static struct type *
    901      1.1  christos read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
    902      1.1  christos {
    903      1.1  christos   struct objfile *objfile = ccp->of;
    904      1.1  christos   struct type *base_type, *cv_type;
    905      1.1  christos 
    906  1.1.1.2  christos   base_type = fetch_tid_type (ccp, btid);
    907  1.1.1.2  christos   if (base_type == nullptr)
    908      1.1  christos     {
    909      1.1  christos       base_type = read_type_record (ccp, btid);
    910  1.1.1.2  christos       if (base_type == nullptr)
    911      1.1  christos 	{
    912      1.1  christos 	  complaint (_("read_restrict_type: NULL base type (%ld)"), btid);
    913      1.1  christos 	  base_type = objfile_type (objfile)->builtin_error;
    914      1.1  christos 	}
    915      1.1  christos     }
    916      1.1  christos   cv_type = make_restrict_type (base_type);
    917      1.1  christos 
    918      1.1  christos   return set_tid_type (objfile, tid, cv_type);
    919      1.1  christos }
    920      1.1  christos 
    921      1.1  christos /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID.  */
    922      1.1  christos 
    923      1.1  christos static struct type *
    924      1.1  christos read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
    925      1.1  christos 		   ctf_id_t btid, const char *name)
    926      1.1  christos {
    927      1.1  christos   struct objfile *objfile = ccp->of;
    928      1.1  christos   struct type *this_type, *target_type;
    929      1.1  christos 
    930      1.1  christos   char *aname = obstack_strdup (&objfile->objfile_obstack, name);
    931      1.1  christos   this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, aname);
    932      1.1  christos   set_tid_type (objfile, tid, this_type);
    933  1.1.1.2  christos   target_type = fetch_tid_type (ccp, btid);
    934      1.1  christos   if (target_type != this_type)
    935  1.1.1.2  christos     this_type->set_target_type (target_type);
    936      1.1  christos   else
    937  1.1.1.2  christos     this_type->set_target_type (nullptr);
    938  1.1.1.2  christos 
    939  1.1.1.2  christos   this_type->set_target_is_stub (this_type->target_type () != nullptr);
    940      1.1  christos 
    941      1.1  christos   return set_tid_type (objfile, tid, this_type);
    942      1.1  christos }
    943      1.1  christos 
    944      1.1  christos /* Read TID of kind CTF_K_POINTER with base type BTID.  */
    945      1.1  christos 
    946      1.1  christos static struct type *
    947      1.1  christos read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
    948      1.1  christos {
    949      1.1  christos   struct objfile *of = ccp->of;
    950      1.1  christos   struct type *target_type, *type;
    951      1.1  christos 
    952  1.1.1.2  christos   target_type = fetch_tid_type (ccp, btid);
    953  1.1.1.2  christos   if (target_type == nullptr)
    954      1.1  christos     {
    955      1.1  christos       target_type = read_type_record (ccp, btid);
    956  1.1.1.2  christos       if (target_type == nullptr)
    957      1.1  christos 	{
    958      1.1  christos 	  complaint (_("read_pointer_type: NULL target type (%ld)"), btid);
    959      1.1  christos 	  target_type = objfile_type (ccp->of)->builtin_error;
    960      1.1  christos 	}
    961      1.1  christos     }
    962      1.1  christos 
    963      1.1  christos   type = lookup_pointer_type (target_type);
    964      1.1  christos   set_type_align (type, ctf_type_align (ccp->fp, tid));
    965      1.1  christos 
    966      1.1  christos   return set_tid_type (of, tid, type);
    967      1.1  christos }
    968      1.1  christos 
    969  1.1.1.2  christos /* Read information from a TID of CTF_K_FORWARD.  */
    970  1.1.1.2  christos 
    971  1.1.1.2  christos static struct type *
    972  1.1.1.2  christos read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
    973  1.1.1.2  christos {
    974  1.1.1.2  christos   struct objfile *of = ccp->of;
    975  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
    976  1.1.1.2  christos   struct type *type;
    977  1.1.1.2  christos   uint32_t kind;
    978  1.1.1.2  christos 
    979  1.1.1.2  christos   type = alloc_type (of);
    980  1.1.1.2  christos 
    981  1.1.1.2  christos   const char *name = ctf_type_name_raw (fp, tid);
    982  1.1.1.2  christos   if (name != nullptr && strlen (name) != 0)
    983  1.1.1.2  christos     type->set_name (name);
    984  1.1.1.2  christos 
    985  1.1.1.2  christos   kind = ctf_type_kind_forwarded (fp, tid);
    986  1.1.1.2  christos   if (kind == CTF_K_UNION)
    987  1.1.1.2  christos     type->set_code (TYPE_CODE_UNION);
    988  1.1.1.2  christos   else
    989  1.1.1.2  christos     type->set_code (TYPE_CODE_STRUCT);
    990  1.1.1.2  christos 
    991  1.1.1.2  christos   type->set_length (0);
    992  1.1.1.2  christos   type->set_is_stub (true);
    993  1.1.1.2  christos 
    994  1.1.1.2  christos   return set_tid_type (of, tid, type);
    995  1.1.1.2  christos }
    996  1.1.1.2  christos 
    997      1.1  christos /* Read information associated with type TID.  */
    998      1.1  christos 
    999      1.1  christos static struct type *
   1000      1.1  christos read_type_record (struct ctf_context *ccp, ctf_id_t tid)
   1001      1.1  christos {
   1002  1.1.1.2  christos   ctf_dict_t *fp = ccp->fp;
   1003      1.1  christos   uint32_t kind;
   1004  1.1.1.2  christos   struct type *type = nullptr;
   1005      1.1  christos   ctf_id_t btid;
   1006      1.1  christos 
   1007      1.1  christos   kind = ctf_type_kind (fp, tid);
   1008      1.1  christos   switch (kind)
   1009      1.1  christos     {
   1010      1.1  christos       case CTF_K_STRUCT:
   1011      1.1  christos       case CTF_K_UNION:
   1012      1.1  christos 	type = read_structure_type (ccp, tid);
   1013      1.1  christos 	break;
   1014      1.1  christos       case CTF_K_ENUM:
   1015      1.1  christos 	type = read_enum_type (ccp, tid);
   1016      1.1  christos 	break;
   1017      1.1  christos       case CTF_K_FUNCTION:
   1018      1.1  christos 	type = read_func_kind_type (ccp, tid);
   1019      1.1  christos 	break;
   1020      1.1  christos       case CTF_K_CONST:
   1021      1.1  christos 	btid = ctf_type_reference (fp, tid);
   1022      1.1  christos 	type = read_const_type (ccp, tid, btid);
   1023      1.1  christos 	break;
   1024      1.1  christos       case CTF_K_TYPEDEF:
   1025      1.1  christos 	{
   1026  1.1.1.2  christos 	  const char *name = ctf_type_name_raw (fp, tid);
   1027      1.1  christos 	  btid = ctf_type_reference (fp, tid);
   1028  1.1.1.2  christos 	  type = read_typedef_type (ccp, tid, btid, name);
   1029      1.1  christos 	}
   1030      1.1  christos 	break;
   1031      1.1  christos       case CTF_K_VOLATILE:
   1032      1.1  christos 	btid = ctf_type_reference (fp, tid);
   1033      1.1  christos 	type = read_volatile_type (ccp, tid, btid);
   1034      1.1  christos 	break;
   1035      1.1  christos       case CTF_K_RESTRICT:
   1036      1.1  christos 	btid = ctf_type_reference (fp, tid);
   1037      1.1  christos 	type = read_restrict_type (ccp, tid, btid);
   1038      1.1  christos 	break;
   1039      1.1  christos       case CTF_K_POINTER:
   1040      1.1  christos 	btid = ctf_type_reference (fp, tid);
   1041      1.1  christos 	type = read_pointer_type (ccp, tid, btid);
   1042      1.1  christos 	break;
   1043      1.1  christos       case CTF_K_INTEGER:
   1044      1.1  christos       case CTF_K_FLOAT:
   1045      1.1  christos 	type = read_base_type (ccp, tid);
   1046      1.1  christos 	break;
   1047      1.1  christos       case CTF_K_ARRAY:
   1048      1.1  christos 	type = read_array_type (ccp, tid);
   1049      1.1  christos 	break;
   1050  1.1.1.2  christos       case CTF_K_FORWARD:
   1051  1.1.1.2  christos 	type = read_forward_type (ccp, tid);
   1052  1.1.1.2  christos 	break;
   1053      1.1  christos       case CTF_K_UNKNOWN:
   1054      1.1  christos 	break;
   1055      1.1  christos       default:
   1056      1.1  christos 	break;
   1057      1.1  christos     }
   1058      1.1  christos 
   1059      1.1  christos   return type;
   1060      1.1  christos }
   1061      1.1  christos 
   1062      1.1  christos /* Callback to add type TID to the symbol table.  */
   1063      1.1  christos 
   1064      1.1  christos static int
   1065      1.1  christos ctf_add_type_cb (ctf_id_t tid, void *arg)
   1066      1.1  christos {
   1067      1.1  christos   struct ctf_context *ccp = (struct ctf_context *) arg;
   1068      1.1  christos   struct type *type;
   1069      1.1  christos   uint32_t kind;
   1070      1.1  christos 
   1071      1.1  christos   /* Check if tid's type has already been defined.  */
   1072      1.1  christos   type = get_tid_type (ccp->of, tid);
   1073  1.1.1.2  christos   if (type != nullptr)
   1074      1.1  christos     return 0;
   1075      1.1  christos 
   1076      1.1  christos   ctf_id_t btid = ctf_type_reference (ccp->fp, tid);
   1077      1.1  christos   kind = ctf_type_kind (ccp->fp, tid);
   1078      1.1  christos   switch (kind)
   1079      1.1  christos     {
   1080      1.1  christos       case CTF_K_STRUCT:
   1081      1.1  christos       case CTF_K_UNION:
   1082      1.1  christos 	process_structure_type (ccp, tid);
   1083      1.1  christos 	break;
   1084      1.1  christos       case CTF_K_ENUM:
   1085      1.1  christos 	process_enum_type (ccp, tid);
   1086      1.1  christos 	break;
   1087      1.1  christos       case CTF_K_FUNCTION:
   1088      1.1  christos 	type = read_func_kind_type (ccp, tid);
   1089      1.1  christos 	new_symbol (ccp, type, tid);
   1090      1.1  christos 	break;
   1091      1.1  christos       case CTF_K_INTEGER:
   1092      1.1  christos       case CTF_K_FLOAT:
   1093      1.1  christos 	process_base_type (ccp, tid);
   1094      1.1  christos 	break;
   1095      1.1  christos       case CTF_K_TYPEDEF:
   1096      1.1  christos 	new_symbol (ccp, read_type_record (ccp, tid), tid);
   1097      1.1  christos 	break;
   1098      1.1  christos       case CTF_K_CONST:
   1099      1.1  christos 	type = read_const_type (ccp, tid, btid);
   1100      1.1  christos 	new_symbol (ccp, type, tid);
   1101      1.1  christos 	break;
   1102      1.1  christos       case CTF_K_VOLATILE:
   1103      1.1  christos 	type = read_volatile_type (ccp, tid, btid);
   1104      1.1  christos 	new_symbol (ccp, type, tid);
   1105      1.1  christos 	break;
   1106      1.1  christos       case CTF_K_RESTRICT:
   1107      1.1  christos 	type = read_restrict_type (ccp, tid, btid);
   1108      1.1  christos 	new_symbol (ccp, type, tid);
   1109      1.1  christos 	break;
   1110      1.1  christos       case CTF_K_POINTER:
   1111      1.1  christos 	type = read_pointer_type (ccp, tid, btid);
   1112      1.1  christos 	new_symbol (ccp, type, tid);
   1113      1.1  christos 	break;
   1114      1.1  christos       case CTF_K_ARRAY:
   1115      1.1  christos 	type = read_array_type (ccp, tid);
   1116      1.1  christos 	new_symbol (ccp, type, tid);
   1117      1.1  christos 	break;
   1118      1.1  christos       case CTF_K_UNKNOWN:
   1119      1.1  christos 	break;
   1120      1.1  christos       default:
   1121      1.1  christos 	break;
   1122      1.1  christos     }
   1123      1.1  christos 
   1124      1.1  christos   return 0;
   1125      1.1  christos }
   1126      1.1  christos 
   1127      1.1  christos /* Callback to add variable NAME with TID to the symbol table.  */
   1128      1.1  christos 
   1129      1.1  christos static int
   1130      1.1  christos ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
   1131      1.1  christos {
   1132      1.1  christos   struct ctf_context *ccp = (struct ctf_context *) arg;
   1133  1.1.1.2  christos   struct symbol *sym = nullptr;
   1134      1.1  christos   struct type *type;
   1135      1.1  christos   uint32_t kind;
   1136      1.1  christos 
   1137      1.1  christos   type = get_tid_type (ccp->of, id);
   1138      1.1  christos 
   1139      1.1  christos   kind = ctf_type_kind (ccp->fp, id);
   1140      1.1  christos   switch (kind)
   1141      1.1  christos     {
   1142      1.1  christos       case CTF_K_FUNCTION:
   1143  1.1.1.2  christos 	if (name != nullptr && strcmp (name, "main") == 0)
   1144      1.1  christos 	  set_objfile_main_name (ccp->of, name, language_c);
   1145      1.1  christos 	break;
   1146      1.1  christos       case CTF_K_INTEGER:
   1147      1.1  christos       case CTF_K_FLOAT:
   1148      1.1  christos       case CTF_K_VOLATILE:
   1149      1.1  christos       case CTF_K_RESTRICT:
   1150      1.1  christos       case CTF_K_TYPEDEF:
   1151      1.1  christos       case CTF_K_CONST:
   1152      1.1  christos       case CTF_K_POINTER:
   1153      1.1  christos       case CTF_K_ARRAY:
   1154  1.1.1.2  christos 	if (type != nullptr)
   1155      1.1  christos 	  {
   1156      1.1  christos 	    sym = new_symbol (ccp, type, id);
   1157  1.1.1.2  christos 	    if (sym != nullptr)
   1158  1.1.1.2  christos 	      sym->compute_and_set_names (name, false, ccp->of->per_bfd);
   1159      1.1  christos 	  }
   1160      1.1  christos 	break;
   1161      1.1  christos       case CTF_K_STRUCT:
   1162      1.1  christos       case CTF_K_UNION:
   1163      1.1  christos       case CTF_K_ENUM:
   1164  1.1.1.2  christos 	if (type == nullptr)
   1165  1.1.1.2  christos 	  {
   1166  1.1.1.2  christos 	    complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
   1167  1.1.1.2  christos 	    type = objfile_type (ccp->of)->builtin_error;
   1168  1.1.1.2  christos 	  }
   1169      1.1  christos 	sym = new (&ccp->of->objfile_obstack) symbol;
   1170      1.1  christos 	OBJSTAT (ccp->of, n_syms++);
   1171  1.1.1.2  christos 	sym->set_type (type);
   1172  1.1.1.2  christos 	sym->set_domain (VAR_DOMAIN);
   1173  1.1.1.2  christos 	sym->set_aclass_index (LOC_OPTIMIZED_OUT);
   1174      1.1  christos 	sym->compute_and_set_names (name, false, ccp->of->per_bfd);
   1175  1.1.1.2  christos 	add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
   1176      1.1  christos 	break;
   1177      1.1  christos       default:
   1178      1.1  christos 	complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind);
   1179      1.1  christos 	break;
   1180      1.1  christos     }
   1181      1.1  christos 
   1182  1.1.1.2  christos   if (sym != nullptr)
   1183      1.1  christos     set_symbol_address (ccp->of, sym, name);
   1184      1.1  christos 
   1185      1.1  christos   return 0;
   1186      1.1  christos }
   1187      1.1  christos 
   1188  1.1.1.2  christos /* Add entries in either data objects or function info section, controlled
   1189  1.1.1.2  christos    by FUNCTIONS.  */
   1190      1.1  christos 
   1191  1.1.1.2  christos static void
   1192  1.1.1.2  christos add_stt_entries (struct ctf_context *ccp, int functions)
   1193      1.1  christos {
   1194  1.1.1.2  christos   ctf_next_t *i = nullptr;
   1195  1.1.1.2  christos   const char *tname;
   1196      1.1  christos   ctf_id_t tid;
   1197  1.1.1.2  christos   struct symbol *sym = nullptr;
   1198  1.1.1.2  christos   struct type *type;
   1199      1.1  christos 
   1200  1.1.1.2  christos   while ((tid = ctf_symbol_next (ccp->fp, &i, &tname, functions)) != CTF_ERR)
   1201  1.1.1.2  christos     {
   1202  1.1.1.2  christos       type = get_tid_type (ccp->of, tid);
   1203  1.1.1.2  christos       if (type == nullptr)
   1204  1.1.1.2  christos 	continue;
   1205  1.1.1.2  christos       sym = new (&ccp->of->objfile_obstack) symbol;
   1206  1.1.1.2  christos       OBJSTAT (ccp->of, n_syms++);
   1207  1.1.1.2  christos       sym->set_type (type);
   1208  1.1.1.2  christos       sym->set_domain (VAR_DOMAIN);
   1209  1.1.1.2  christos       sym->set_aclass_index (LOC_STATIC);
   1210  1.1.1.2  christos       sym->compute_and_set_names (tname, false, ccp->of->per_bfd);
   1211  1.1.1.2  christos       add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
   1212  1.1.1.2  christos       set_symbol_address (ccp->of, sym, tname);
   1213  1.1.1.2  christos     }
   1214      1.1  christos }
   1215      1.1  christos 
   1216  1.1.1.2  christos /* Add entries in data objects section.  */
   1217      1.1  christos 
   1218  1.1.1.2  christos static void
   1219  1.1.1.2  christos add_stt_obj (struct ctf_context *ccp)
   1220      1.1  christos {
   1221  1.1.1.2  christos   add_stt_entries (ccp, 0);
   1222  1.1.1.2  christos }
   1223      1.1  christos 
   1224  1.1.1.2  christos /* Add entries in function info section.  */
   1225      1.1  christos 
   1226  1.1.1.2  christos static void
   1227  1.1.1.2  christos add_stt_func (struct ctf_context *ccp)
   1228  1.1.1.2  christos {
   1229  1.1.1.2  christos   add_stt_entries (ccp, 1);
   1230      1.1  christos }
   1231      1.1  christos 
   1232      1.1  christos /* Get text segment base for OBJFILE, TSIZE contains the segment size.  */
   1233      1.1  christos 
   1234      1.1  christos static CORE_ADDR
   1235      1.1  christos get_objfile_text_range (struct objfile *of, int *tsize)
   1236      1.1  christos {
   1237  1.1.1.2  christos   bfd *abfd = of->obfd.get ();
   1238      1.1  christos   const asection *codes;
   1239      1.1  christos 
   1240      1.1  christos   codes = bfd_get_section_by_name (abfd, ".text");
   1241      1.1  christos   *tsize = codes ? bfd_section_size (codes) : 0;
   1242      1.1  christos   return of->text_section_offset ();
   1243      1.1  christos }
   1244      1.1  christos 
   1245      1.1  christos /* Start a symtab for OBJFILE in CTF format.  */
   1246      1.1  christos 
   1247      1.1  christos static void
   1248  1.1.1.2  christos ctf_start_compunit_symtab (ctf_psymtab *pst,
   1249  1.1.1.2  christos 			   struct objfile *of, CORE_ADDR text_offset)
   1250      1.1  christos {
   1251      1.1  christos   struct ctf_context *ccp;
   1252      1.1  christos 
   1253  1.1.1.2  christos   ccp = &pst->context;
   1254      1.1  christos   ccp->builder = new buildsym_compunit
   1255  1.1.1.2  christos 		       (of, pst->filename, nullptr,
   1256      1.1  christos 		       language_c, text_offset);
   1257      1.1  christos   ccp->builder->record_debugformat ("ctf");
   1258      1.1  christos }
   1259      1.1  christos 
   1260      1.1  christos /* Finish reading symbol/type definitions in CTF format.
   1261      1.1  christos    END_ADDR is the end address of the file's text.  SECTION is
   1262      1.1  christos    the .text section number.  */
   1263      1.1  christos 
   1264      1.1  christos static struct compunit_symtab *
   1265  1.1.1.2  christos ctf_end_compunit_symtab (ctf_psymtab *pst,
   1266  1.1.1.2  christos 			 CORE_ADDR end_addr, int section)
   1267      1.1  christos {
   1268      1.1  christos   struct ctf_context *ccp;
   1269      1.1  christos 
   1270  1.1.1.2  christos   ccp = &pst->context;
   1271      1.1  christos   struct compunit_symtab *result
   1272  1.1.1.2  christos     = ccp->builder->end_compunit_symtab (end_addr, section);
   1273      1.1  christos   delete ccp->builder;
   1274  1.1.1.2  christos   ccp->builder = nullptr;
   1275      1.1  christos   return result;
   1276      1.1  christos }
   1277      1.1  christos 
   1278  1.1.1.2  christos /* Add all members of an enum with type TID to partial symbol table.  */
   1279  1.1.1.2  christos 
   1280  1.1.1.2  christos static void
   1281  1.1.1.2  christos ctf_psymtab_add_enums (struct ctf_context *ccp, ctf_id_t tid)
   1282  1.1.1.2  christos {
   1283  1.1.1.2  christos   int val;
   1284  1.1.1.2  christos   const char *ename;
   1285  1.1.1.2  christos   ctf_next_t *i = nullptr;
   1286  1.1.1.2  christos 
   1287  1.1.1.2  christos   while ((ename = ctf_enum_next (ccp->fp, tid, &i, &val)) != nullptr)
   1288  1.1.1.2  christos     {
   1289  1.1.1.2  christos       ccp->pst->add_psymbol (ename, true,
   1290  1.1.1.2  christos 			     VAR_DOMAIN, LOC_CONST, -1,
   1291  1.1.1.2  christos 			     psymbol_placement::GLOBAL,
   1292  1.1.1.2  christos 			     0, language_c, ccp->partial_symtabs, ccp->of);
   1293  1.1.1.2  christos     }
   1294  1.1.1.2  christos   if (ctf_errno (ccp->fp) != ECTF_NEXT_END)
   1295  1.1.1.2  christos     complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
   1296  1.1.1.2  christos 	       ctf_errmsg (ctf_errno (ccp->fp)));
   1297  1.1.1.2  christos }
   1298  1.1.1.2  christos 
   1299  1.1.1.2  christos /* Add entries in either data objects or function info section, controlled
   1300  1.1.1.2  christos    by FUNCTIONS, to psymtab.  */
   1301  1.1.1.2  christos 
   1302  1.1.1.2  christos static void
   1303  1.1.1.2  christos ctf_psymtab_add_stt_entries (ctf_dict_t *cfp, ctf_psymtab *pst,
   1304  1.1.1.2  christos 			     struct objfile *of, int functions)
   1305  1.1.1.2  christos {
   1306  1.1.1.2  christos   ctf_next_t *i = nullptr;
   1307  1.1.1.2  christos   ctf_id_t tid;
   1308  1.1.1.2  christos   const char *tname;
   1309  1.1.1.2  christos 
   1310  1.1.1.2  christos   while ((tid = ctf_symbol_next (cfp, &i, &tname, functions)) != CTF_ERR)
   1311  1.1.1.2  christos     {
   1312  1.1.1.2  christos       uint32_t kind = ctf_type_kind (cfp, tid);
   1313  1.1.1.2  christos       address_class aclass;
   1314  1.1.1.2  christos       domain_enum tdomain;
   1315  1.1.1.2  christos       switch (kind)
   1316  1.1.1.2  christos 	{
   1317  1.1.1.2  christos 	  case CTF_K_STRUCT:
   1318  1.1.1.2  christos 	  case CTF_K_UNION:
   1319  1.1.1.2  christos 	  case CTF_K_ENUM:
   1320  1.1.1.2  christos 	    tdomain = STRUCT_DOMAIN;
   1321  1.1.1.2  christos 	    break;
   1322  1.1.1.2  christos 	  default:
   1323  1.1.1.2  christos 	    tdomain = VAR_DOMAIN;
   1324  1.1.1.2  christos 	    break;
   1325  1.1.1.2  christos 	}
   1326  1.1.1.2  christos 
   1327  1.1.1.2  christos       if (kind == CTF_K_FUNCTION)
   1328  1.1.1.2  christos 	aclass = LOC_STATIC;
   1329  1.1.1.2  christos       else if (kind == CTF_K_CONST)
   1330  1.1.1.2  christos 	aclass = LOC_CONST;
   1331  1.1.1.2  christos       else
   1332  1.1.1.2  christos 	aclass = LOC_TYPEDEF;
   1333  1.1.1.2  christos 
   1334  1.1.1.2  christos       pst->add_psymbol (tname, true,
   1335  1.1.1.2  christos 			tdomain, aclass, -1,
   1336  1.1.1.2  christos 			psymbol_placement::GLOBAL,
   1337  1.1.1.2  christos 			0, language_c, pst->context.partial_symtabs, of);
   1338  1.1.1.2  christos     }
   1339  1.1.1.2  christos }
   1340  1.1.1.2  christos 
   1341  1.1.1.2  christos /* Add entries in data objects section to psymtab.  */
   1342  1.1.1.2  christos 
   1343  1.1.1.2  christos static void
   1344  1.1.1.2  christos ctf_psymtab_add_stt_obj (ctf_dict_t *cfp, ctf_psymtab *pst,
   1345  1.1.1.2  christos 			 struct objfile *of)
   1346  1.1.1.2  christos {
   1347  1.1.1.2  christos   ctf_psymtab_add_stt_entries (cfp, pst, of, 0);
   1348  1.1.1.2  christos }
   1349  1.1.1.2  christos 
   1350  1.1.1.2  christos /* Add entries in function info section to psymtab.  */
   1351  1.1.1.2  christos 
   1352  1.1.1.2  christos static void
   1353  1.1.1.2  christos ctf_psymtab_add_stt_func (ctf_dict_t *cfp, ctf_psymtab *pst,
   1354  1.1.1.2  christos 			  struct objfile *of)
   1355  1.1.1.2  christos {
   1356  1.1.1.2  christos   ctf_psymtab_add_stt_entries (cfp, pst, of, 1);
   1357  1.1.1.2  christos }
   1358  1.1.1.2  christos 
   1359      1.1  christos /* Read in full symbols for PST, and anything it depends on.  */
   1360      1.1  christos 
   1361      1.1  christos void
   1362      1.1  christos ctf_psymtab::expand_psymtab (struct objfile *objfile)
   1363      1.1  christos {
   1364      1.1  christos   struct ctf_context *ccp;
   1365      1.1  christos 
   1366      1.1  christos   gdb_assert (!readin);
   1367      1.1  christos 
   1368  1.1.1.2  christos   ccp = &context;
   1369      1.1  christos 
   1370      1.1  christos   /* Iterate over entries in data types section.  */
   1371      1.1  christos   if (ctf_type_iter (ccp->fp, ctf_add_type_cb, ccp) == CTF_ERR)
   1372      1.1  christos     complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
   1373      1.1  christos 	       ctf_errmsg (ctf_errno (ccp->fp)));
   1374      1.1  christos 
   1375      1.1  christos 
   1376      1.1  christos   /* Iterate over entries in variable info section.  */
   1377      1.1  christos   if (ctf_variable_iter (ccp->fp, ctf_add_var_cb, ccp) == CTF_ERR)
   1378      1.1  christos     complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
   1379      1.1  christos 	       ctf_errmsg (ctf_errno (ccp->fp)));
   1380      1.1  christos 
   1381      1.1  christos   /* Add entries in data objects and function info sections.  */
   1382  1.1.1.2  christos   add_stt_obj (ccp);
   1383  1.1.1.2  christos   add_stt_func (ccp);
   1384      1.1  christos 
   1385      1.1  christos   readin = true;
   1386      1.1  christos }
   1387      1.1  christos 
   1388      1.1  christos /* Expand partial symbol table PST into a full symbol table.
   1389      1.1  christos    PST is not NULL.  */
   1390      1.1  christos 
   1391      1.1  christos void
   1392      1.1  christos ctf_psymtab::read_symtab (struct objfile *objfile)
   1393      1.1  christos {
   1394      1.1  christos   if (readin)
   1395      1.1  christos     warning (_("bug: psymtab for %s is already read in."), filename);
   1396      1.1  christos   else
   1397      1.1  christos     {
   1398      1.1  christos       if (info_verbose)
   1399      1.1  christos 	{
   1400  1.1.1.2  christos 	  gdb_printf (_("Reading in CTF data for %s..."), filename);
   1401      1.1  christos 	  gdb_flush (gdb_stdout);
   1402      1.1  christos 	}
   1403      1.1  christos 
   1404      1.1  christos       /* Start a symtab.  */
   1405      1.1  christos       CORE_ADDR offset;        /* Start of text segment.  */
   1406      1.1  christos       int tsize;
   1407      1.1  christos 
   1408      1.1  christos       offset = get_objfile_text_range (objfile, &tsize);
   1409  1.1.1.2  christos       ctf_start_compunit_symtab (this, objfile, offset);
   1410      1.1  christos       expand_psymtab (objfile);
   1411      1.1  christos 
   1412      1.1  christos       set_text_low (offset);
   1413      1.1  christos       set_text_high (offset + tsize);
   1414  1.1.1.2  christos       compunit_symtab = ctf_end_compunit_symtab (this, offset + tsize,
   1415  1.1.1.2  christos 						 SECT_OFF_TEXT (objfile));
   1416      1.1  christos 
   1417      1.1  christos       /* Finish up the debug error message.  */
   1418      1.1  christos       if (info_verbose)
   1419  1.1.1.2  christos 	gdb_printf (_("done.\n"));
   1420      1.1  christos     }
   1421      1.1  christos }
   1422      1.1  christos 
   1423      1.1  christos /* Allocate a new partial_symtab NAME.
   1424      1.1  christos 
   1425      1.1  christos    Each source file that has not been fully read in is represented by
   1426      1.1  christos    a partial_symtab.  This contains the information on where in the
   1427      1.1  christos    executable the debugging symbols for a specific file are, and a
   1428      1.1  christos    list of names of global symbols which are located in this file.
   1429      1.1  christos    They are all chained on partial symtab lists.
   1430      1.1  christos 
   1431      1.1  christos    Even after the source file has been read into a symtab, the
   1432      1.1  christos    partial_symtab remains around.  They are allocated on an obstack,
   1433      1.1  christos    objfile_obstack.  */
   1434      1.1  christos 
   1435      1.1  christos static ctf_psymtab *
   1436      1.1  christos create_partial_symtab (const char *name,
   1437  1.1.1.2  christos 		       ctf_archive_t *arc,
   1438  1.1.1.2  christos 		       ctf_dict_t *cfp,
   1439  1.1.1.2  christos 		       psymtab_storage *partial_symtabs,
   1440      1.1  christos 		       struct objfile *objfile)
   1441      1.1  christos {
   1442      1.1  christos   ctf_psymtab *pst;
   1443      1.1  christos 
   1444  1.1.1.2  christos   pst = new ctf_psymtab (name, partial_symtabs, objfile->per_bfd, 0);
   1445      1.1  christos 
   1446  1.1.1.2  christos   pst->context.arc = arc;
   1447  1.1.1.2  christos   pst->context.fp = cfp;
   1448  1.1.1.2  christos   pst->context.of = objfile;
   1449  1.1.1.2  christos   pst->context.partial_symtabs = partial_symtabs;
   1450  1.1.1.2  christos   pst->context.pst = pst;
   1451  1.1.1.2  christos   pst->context.builder = nullptr;
   1452      1.1  christos 
   1453      1.1  christos   return pst;
   1454      1.1  christos }
   1455      1.1  christos 
   1456      1.1  christos /* Callback to add type TID to partial symbol table.  */
   1457      1.1  christos 
   1458      1.1  christos static int
   1459      1.1  christos ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
   1460      1.1  christos {
   1461      1.1  christos   struct ctf_context *ccp;
   1462      1.1  christos   uint32_t kind;
   1463      1.1  christos   short section = -1;
   1464      1.1  christos 
   1465      1.1  christos   ccp = (struct ctf_context *) arg;
   1466      1.1  christos 
   1467      1.1  christos   domain_enum domain = UNDEF_DOMAIN;
   1468      1.1  christos   enum address_class aclass = LOC_UNDEF;
   1469      1.1  christos   kind = ctf_type_kind (ccp->fp, tid);
   1470      1.1  christos   switch (kind)
   1471      1.1  christos     {
   1472  1.1.1.2  christos       case CTF_K_ENUM:
   1473  1.1.1.2  christos 	ctf_psymtab_add_enums (ccp, tid);
   1474  1.1.1.2  christos 	/* FALL THROUGH */
   1475      1.1  christos       case CTF_K_STRUCT:
   1476      1.1  christos       case CTF_K_UNION:
   1477      1.1  christos 	domain = STRUCT_DOMAIN;
   1478      1.1  christos 	aclass = LOC_TYPEDEF;
   1479      1.1  christos 	break;
   1480      1.1  christos       case CTF_K_FUNCTION:
   1481      1.1  christos       case CTF_K_FORWARD:
   1482      1.1  christos 	domain = VAR_DOMAIN;
   1483      1.1  christos 	aclass = LOC_STATIC;
   1484      1.1  christos 	section = SECT_OFF_TEXT (ccp->of);
   1485      1.1  christos 	break;
   1486      1.1  christos       case CTF_K_CONST:
   1487      1.1  christos 	domain = VAR_DOMAIN;
   1488      1.1  christos 	aclass = LOC_STATIC;
   1489      1.1  christos 	break;
   1490      1.1  christos       case CTF_K_TYPEDEF:
   1491      1.1  christos       case CTF_K_POINTER:
   1492      1.1  christos       case CTF_K_VOLATILE:
   1493      1.1  christos       case CTF_K_RESTRICT:
   1494      1.1  christos 	domain = VAR_DOMAIN;
   1495      1.1  christos 	aclass = LOC_TYPEDEF;
   1496      1.1  christos 	break;
   1497      1.1  christos       case CTF_K_INTEGER:
   1498      1.1  christos       case CTF_K_FLOAT:
   1499      1.1  christos 	domain = VAR_DOMAIN;
   1500      1.1  christos 	aclass = LOC_TYPEDEF;
   1501      1.1  christos 	break;
   1502      1.1  christos       case CTF_K_ARRAY:
   1503      1.1  christos       case CTF_K_UNKNOWN:
   1504      1.1  christos 	return 0;
   1505      1.1  christos     }
   1506      1.1  christos 
   1507  1.1.1.2  christos   const char *name = ctf_type_name_raw (ccp->fp, tid);
   1508  1.1.1.2  christos   if (name == nullptr || strlen (name) == 0)
   1509  1.1.1.2  christos     return 0;
   1510  1.1.1.2  christos 
   1511  1.1.1.2  christos   ccp->pst->add_psymbol (name, false,
   1512      1.1  christos 			 domain, aclass, section,
   1513  1.1.1.2  christos 			 psymbol_placement::STATIC,
   1514  1.1.1.2  christos 			 0, language_c, ccp->partial_symtabs, ccp->of);
   1515      1.1  christos 
   1516      1.1  christos   return 0;
   1517      1.1  christos }
   1518      1.1  christos 
   1519      1.1  christos /* Callback to add variable NAME with ID to partial symbol table.  */
   1520      1.1  christos 
   1521      1.1  christos static int
   1522      1.1  christos ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
   1523      1.1  christos {
   1524      1.1  christos   struct ctf_context *ccp = (struct ctf_context *) arg;
   1525      1.1  christos 
   1526  1.1.1.2  christos   ccp->pst->add_psymbol (name, true,
   1527  1.1.1.2  christos 			 VAR_DOMAIN, LOC_STATIC, -1,
   1528  1.1.1.2  christos 			 psymbol_placement::GLOBAL,
   1529  1.1.1.2  christos 			 0, language_c, ccp->partial_symtabs, ccp->of);
   1530      1.1  christos   return 0;
   1531      1.1  christos }
   1532      1.1  christos 
   1533      1.1  christos /* Setup partial_symtab's describing each source file for which
   1534      1.1  christos    debugging information is available.  */
   1535      1.1  christos 
   1536      1.1  christos static void
   1537  1.1.1.2  christos scan_partial_symbols (ctf_dict_t *cfp, psymtab_storage *partial_symtabs,
   1538  1.1.1.2  christos 		      struct ctf_per_tu_data *tup, const char *fname)
   1539      1.1  christos {
   1540  1.1.1.2  christos   struct objfile *of = tup->of;
   1541  1.1.1.2  christos   bool isparent = false;
   1542  1.1.1.2  christos 
   1543  1.1.1.2  christos   if (strcmp (fname, ".ctf") == 0)
   1544  1.1.1.2  christos     {
   1545  1.1.1.2  christos       fname = bfd_get_filename (of->obfd.get ());
   1546  1.1.1.2  christos       isparent = true;
   1547  1.1.1.2  christos     }
   1548      1.1  christos 
   1549  1.1.1.2  christos   ctf_psymtab *pst = create_partial_symtab (fname, tup->arc, cfp,
   1550  1.1.1.2  christos 					    partial_symtabs, of);
   1551      1.1  christos 
   1552  1.1.1.2  christos   struct ctf_context *ccx = &pst->context;
   1553  1.1.1.2  christos   if (isparent == false)
   1554  1.1.1.2  christos     ccx->pst = pst;
   1555  1.1.1.2  christos 
   1556  1.1.1.2  christos   if (ctf_type_iter (cfp, ctf_psymtab_type_cb, ccx) == CTF_ERR)
   1557      1.1  christos     complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
   1558      1.1  christos 	       ctf_errmsg (ctf_errno (cfp)));
   1559      1.1  christos 
   1560  1.1.1.2  christos   if (ctf_variable_iter (cfp, ctf_psymtab_var_cb, ccx) == CTF_ERR)
   1561      1.1  christos     complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
   1562      1.1  christos 	       ctf_errmsg (ctf_errno (cfp)));
   1563      1.1  christos 
   1564      1.1  christos   /* Scan CTF object and function sections which correspond to each
   1565      1.1  christos      STT_FUNC or STT_OBJECT entry in the symbol table,
   1566      1.1  christos      pick up what init_symtab has done.  */
   1567  1.1.1.2  christos   ctf_psymtab_add_stt_obj (cfp, pst, of);
   1568  1.1.1.2  christos   ctf_psymtab_add_stt_func (cfp, pst, of);
   1569      1.1  christos 
   1570  1.1.1.2  christos   pst->end ();
   1571  1.1.1.2  christos }
   1572  1.1.1.2  christos 
   1573  1.1.1.2  christos /* Callback to build the psymtab for archive member NAME.  */
   1574  1.1.1.2  christos 
   1575  1.1.1.2  christos static int
   1576  1.1.1.2  christos build_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
   1577  1.1.1.2  christos {
   1578  1.1.1.2  christos   struct ctf_per_tu_data *tup = (struct ctf_per_tu_data *) arg;
   1579  1.1.1.2  christos   ctf_dict_t *parent = tup->fp;
   1580  1.1.1.2  christos 
   1581  1.1.1.2  christos   if (strcmp (name, ".ctf") != 0)
   1582  1.1.1.2  christos     ctf_import (ctf, parent);
   1583      1.1  christos 
   1584  1.1.1.2  christos   if (info_verbose)
   1585  1.1.1.2  christos     {
   1586  1.1.1.2  christos       gdb_printf (_("Scanning archive member %s..."), name);
   1587  1.1.1.2  christos       gdb_flush (gdb_stdout);
   1588      1.1  christos     }
   1589      1.1  christos 
   1590  1.1.1.2  christos   psymtab_storage *pss = tup->psf->get_partial_symtabs ().get ();
   1591  1.1.1.2  christos   scan_partial_symbols (ctf, pss, tup, name);
   1592  1.1.1.2  christos 
   1593  1.1.1.2  christos   return 0;
   1594      1.1  christos }
   1595      1.1  christos 
   1596      1.1  christos /* Read CTF debugging information from a BFD section.  This is
   1597      1.1  christos    called from elfread.c.  It does a quick pass through the
   1598      1.1  christos    .ctf section to set up the partial symbol table.  */
   1599      1.1  christos 
   1600      1.1  christos void
   1601      1.1  christos elfctf_build_psymtabs (struct objfile *of)
   1602      1.1  christos {
   1603  1.1.1.2  christos   struct ctf_per_tu_data pcu;
   1604  1.1.1.2  christos   bfd *abfd = of->obfd.get ();
   1605      1.1  christos   int err;
   1606      1.1  christos 
   1607      1.1  christos   ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
   1608  1.1.1.2  christos   if (arc == nullptr)
   1609      1.1  christos     error (_("ctf_bfdopen failed on %s - %s"),
   1610      1.1  christos 	   bfd_get_filename (abfd), ctf_errmsg (err));
   1611      1.1  christos 
   1612  1.1.1.2  christos   ctf_dict_t *fp = ctf_dict_open (arc, NULL, &err);
   1613  1.1.1.2  christos   if (fp == nullptr)
   1614  1.1.1.2  christos     error (_("ctf_dict_open failed on %s - %s"),
   1615      1.1  christos 	   bfd_get_filename (abfd), ctf_errmsg (err));
   1616  1.1.1.2  christos   ctf_dict_key.emplace (of, fp);
   1617      1.1  christos 
   1618  1.1.1.2  christos   pcu.fp = fp;
   1619  1.1.1.2  christos   pcu.of = of;
   1620  1.1.1.2  christos   pcu.arc = arc;
   1621  1.1.1.2  christos 
   1622  1.1.1.2  christos   psymbol_functions *psf = new psymbol_functions ();
   1623  1.1.1.2  christos   of->qf.emplace_front (psf);
   1624  1.1.1.2  christos   pcu.psf = psf;
   1625  1.1.1.2  christos 
   1626  1.1.1.2  christos   if (ctf_archive_iter (arc, build_ctf_archive_member, &pcu) < 0)
   1627  1.1.1.2  christos     error (_("ctf_archive_iter failed in input file %s: - %s"),
   1628  1.1.1.2  christos 	   bfd_get_filename (abfd), ctf_errmsg (err));
   1629      1.1  christos }
   1630      1.1  christos 
   1631      1.1  christos #else
   1632      1.1  christos 
   1633      1.1  christos void
   1634      1.1  christos elfctf_build_psymtabs (struct objfile *of)
   1635      1.1  christos {
   1636      1.1  christos   /* Nothing to do if CTF is disabled.  */
   1637      1.1  christos }
   1638      1.1  christos 
   1639      1.1  christos #endif /* ENABLE_LIBCTF */
   1640