/* Compact ANSI-C Type Format (CTF) support in GDB.

   Copyright (C) 2019-2026 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* This file format can be used to compactly represent the information needed
   by a debugger to interpret the ANSI-C types used by a given program.
   Traditionally, this kind of information is generated by the compiler when
   invoked with the -g flag and is stored in "stabs" strings or in the more
   modern DWARF format. A new -gtLEVEL option has been added in gcc to generate
   such information. CTF provides a representation of only the information
   that is relevant to debugging a complex, optimized C program such as the
   operating system kernel in a form that is significantly more compact than
   the equivalent stabs or DWARF representation.  The format is data-model
   independent, so consumers do not need different code depending on whether
   they are 32-bit or 64-bit programs.  CTF assumes that a standard ELF symbol
   table is available for use in the debugger, and uses the structure and data
   of the symbol table to avoid storing redundant information.  The CTF data
   may be compressed on disk or in memory, indicated by a bit in the header.
   CTF may be interpreted in a raw disk file, or it may be stored in an ELF
   section, typically named .ctf.  Data structures are aligned so that a raw
   CTF file or CTF ELF section may be manipulated using mmap(2).

   The CTF file or section itself has the following structure:

   +--------+--------+---------+----------+----------+-------+--------+
   |  file  |  type  |  data   | function | variable | data  | string |
   | header | labels | objects |   info   |   info   | types | table  |
   +--------+--------+---------+----------+----------+-------+--------+

   The file header stores a magic number and version information, encoding
   flags, and the byte offset of each of the sections relative to the end of the
   header itself.  If the CTF data has been uniquified against another set of
   CTF data, a reference to that data also appears in the header.  This
   reference is the name of the label corresponding to the types uniquified
   against.

   Following the header is a list of labels, used to group the types included in
   the data types section.  Each label is accompanied by a type ID i.  A given
   label refers to the group of types whose IDs are in the range [0, i].

   Data object and function records are stored in the same order as they appear
   in the corresponding symbol table, except that symbols marked SHN_UNDEF are
   not stored and symbols that have no type data are padded out with zeroes.
   For each data object, the type ID (a small integer) is recorded.  For each
   function, the type ID of the return type and argument types is recorded.

   Variable records (as distinct from data objects) provide a modicum of support
   for non-ELF systems, mapping a variable name to a CTF type ID.  The variable
   names are sorted into ASCIIbetical order, permitting binary searching.

   The data types section is a list of variable size records that represent each
   type, in order by their ID.  The types themselves form a directed graph,
   where each node may contain one or more outgoing edges to other type nodes,
   denoted by their ID.

   Strings are recorded as a string table ID (0 or 1) and a byte offset into the
   string table.  String table 0 is the internal CTF string table.  String table
   1 is the external string table, which is the string table associated with the
   ELF symbol table for this object.  CTF does not record any strings that are
   already in the symbol table, and the CTF string table does not contain any
   duplicated strings.  */

#include "buildsym.h"
#include "complaints.h"
#include "block.h"
#include "ctfread.h"
#include "cli/cli-cmds.h"
#include "expanded-symbol.h"
#include "maint.h"
#include "objfiles.h"
#include "progspace.h"
#include "cli/cli-style.h"

/* When true, print debug messages related to CTF reading.  */
static bool debug_ctf = false;

/* Print a "ctf" debug statement.  */

#define ctf_debug_printf(fmt, ...) \
  debug_prefixed_printf_cond (debug_ctf, "ctf", fmt, ##__VA_ARGS__)

#define CTF_SCOPED_DEBUG_START_END(fmt, ...) \
  scoped_debug_start_end (debug_ctf, "ctf", fmt, ##__VA_ARGS__)

#if ENABLE_LIBCTF

#include "ctf.h"
#include "ctf-api.h"

/* Return a string representation of a CTF type kind.  */

static const char *
ctf_kind_str (uint32_t kind)
{
  switch (kind)
    {
    case CTF_K_UNKNOWN: return "UNKNOWN";
    case CTF_K_INTEGER: return "INTEGER";
    case CTF_K_FLOAT: return "FLOAT";
    case CTF_K_POINTER: return "POINTER";
    case CTF_K_ARRAY: return "ARRAY";
    case CTF_K_FUNCTION: return "FUNCTION";
    case CTF_K_STRUCT: return "STRUCT";
    case CTF_K_UNION: return "UNION";
    case CTF_K_ENUM: return "ENUM";
    case CTF_K_FORWARD: return "FORWARD";
    case CTF_K_TYPEDEF: return "TYPEDEF";
    case CTF_K_VOLATILE: return "VOLATILE";
    case CTF_K_CONST: return "CONST";
    case CTF_K_RESTRICT: return "RESTRICT";
    default: return "???";
    }
}

using ctf_type_map = gdb::unordered_map<ctf_id_t, struct type *>;

struct ctf_archive_closer
{
  void operator() (ctf_archive_t *arc) const noexcept
  {
    ctf_close (arc);
  }
};

using ctf_archive_up = std::unique_ptr<ctf_archive_t, ctf_archive_closer>;

struct ctf_dict_closer
{
  void operator() (ctf_dict_t *dict) const noexcept
  {
    ctf_dict_close (dict);
  }
};

using ctf_dict_up = std::unique_ptr<ctf_dict_t, ctf_dict_closer>;

struct ctf_per_objfile
{
  ctf_per_objfile (struct objfile *objfile, ctf_archive_up archive,
		   ctf_dict_up dict)
    : objfile (objfile),
      archive (std::move (archive)),
      parent_dict (std::move (dict))
  {}

  /* Backlink to objfile.  */
  struct objfile *objfile;

  /* The archive and parent dictionary.  */
  ctf_archive_up archive;
  ctf_dict_up parent_dict;
};

static const registry<objfile>::key<ctf_per_objfile> ctf_per_objfile_key;

/* Data passed around when creating symtabs.  */

struct ctf_context
{
  ctf_per_objfile *per_objfile;
  ctf_dict_t *dict;
  struct buildsym_compunit *builder;

  /* Map from IDs to types.  */
  ctf_type_map type_map;
};

/* The routines that read and process fields/members of a C struct, union,
   or enumeration, pass lists of data member fields in an instance of a
   ctf_field_info structure. It is derived from dwarf2read.c.  */

struct ctf_nextfield
{
  struct field field {};
};

struct ctf_field_info
{
  /* List of data member fields.  */
  std::vector<struct ctf_nextfield> fields;

  /* Context.  */
  struct ctf_context *cur_context;

  /* Parent type.  */
  struct type *ptype;

  /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head
     of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
  std::vector<struct decl_field> typedef_field_list;

  /* Nested types defined by this struct and the number of elements in
     this list.  */
  std::vector<struct decl_field> nested_types_list;
};

/* Local function prototypes */

static int ctf_add_type_cb (ctf_id_t tid, void *arg);

static struct type *read_array_type (struct ctf_context *cp, ctf_id_t tid);

static struct type *read_pointer_type (struct ctf_context *cp, ctf_id_t tid,
				       ctf_id_t btid);

static struct type *read_structure_type (struct ctf_context *cp, ctf_id_t tid);

static struct type *read_enum_type (struct ctf_context *cp, ctf_id_t tid);

static struct type *read_typedef_type (struct ctf_context *cp, ctf_id_t tid,
				       ctf_id_t btid, const char *name);

static struct type *read_type_record (struct ctf_context *cp, ctf_id_t tid);

static void process_structure_type (struct ctf_context *cp, ctf_id_t tid);

static void process_struct_members (struct ctf_context *cp, ctf_id_t tid,
				    struct type *type);

static struct type *read_forward_type (struct ctf_context *cp, ctf_id_t tid);

/* Set the type associated with TID to TYP.  */

static struct type *
set_tid_type (ctf_context *ccp, ctf_id_t tid, struct type *typ)
{
  ccp->type_map.emplace (tid, typ);
  return typ;
}

/* Look up the type for TID in OF's type map.  Return nullptr if TID
   does not have a saved type.  */

static struct type *
get_tid_type (ctf_context *ccp, ctf_id_t tid)
{
  auto iter = ccp->type_map.find (tid);
  if (iter == ccp->type_map.end ())
    return nullptr;
  return iter->second;
}

/* Fetch the type for TID in CCP OF's type map, add the type to
   context CCP if TID does not have a saved type.  */

static struct type *
fetch_tid_type (struct ctf_context *ccp, ctf_id_t tid)
{
  struct type *typ;

  typ = get_tid_type (ccp, tid);
  if (typ == nullptr)
    {
      ctf_add_type_cb (tid, ccp);
      typ = get_tid_type (ccp, tid);
    }

  return typ;
}

/* Return the size of storage in bits for INTEGER, FLOAT, or ENUM.  */

static int
get_bitsize (ctf_dict_t *dict, ctf_id_t tid, uint32_t kind)
{
  ctf_encoding_t cet;

  if ((kind == CTF_K_INTEGER || kind == CTF_K_ENUM
      || kind == CTF_K_FLOAT)
      && ctf_type_reference (dict, tid) != CTF_ERR
      && ctf_type_encoding (dict, tid, &cet) != CTF_ERR)
    return cet.cte_bits;

  return 0;
}

/* Set SYM's address, with NAME, from its minimal symbol entry.  */

static void
set_symbol_address (struct objfile *objfile, struct symbol *sym,
		    const char *name)
{
  bound_minimal_symbol msym
    = lookup_minimal_symbol (current_program_space, name, objfile);
  if (msym.minsym != NULL)
    {
      sym->set_value_address (msym.value_address ());
      sym->set_loc_class_index (LOC_STATIC);
      sym->set_section_index (msym.minsym->section_index ());
    }
}

/* Create the vector of fields, and attach it to TYPE.  */

static void
attach_fields_to_type (struct ctf_field_info *fip, struct type *type)
{
  int nfields = fip->fields.size ();

  if (nfields == 0)
    return;

  /* Record the field count, allocate space for the array of fields.  */
  type->alloc_fields (nfields);

  /* Copy the saved-up fields into the field vector.  */
  for (int i = 0; i < nfields; ++i)
    {
      struct ctf_nextfield &field = fip->fields[i];
      type->field (i) = field.field;
    }
}

/* Allocate a floating-point type of size BITS and name NAME.  Pass NAME_HINT
   (which may be different from NAME) to the architecture back-end to allow
   it to guess the correct format if necessary.  */

static struct type *
ctf_init_float_type (struct objfile *objfile,
		     int bits,
		     const char *name,
		     const char *name_hint)
{
  struct gdbarch *gdbarch = objfile->arch ();
  const struct floatformat **format;
  struct type *type;

  type_allocator alloc (objfile, language_c);
  format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
  if (format != nullptr)
    type = init_float_type (alloc, bits, name, format);
  else
    type = alloc.new_type (TYPE_CODE_ERROR, bits, name);

  return type;
}

/* Callback to add member NAME to a struct/union type. TID is the type
   of struct/union member, OFFSET is the offset of member in bits,
   and ARG contains the ctf_field_info.  */

static int
ctf_add_member_cb (const char *name,
		   ctf_id_t tid,
		   unsigned long offset,
		   void *arg)
{
  struct ctf_field_info *fip = (struct ctf_field_info *) arg;
  struct ctf_context *ccp = fip->cur_context;
  struct ctf_nextfield new_field;
  struct field *fp;
  struct type *t;
  uint32_t kind;

  fp = &new_field.field;
  fp->set_name (name);

  kind = ctf_type_kind (ccp->dict, tid);
  t = fetch_tid_type (ccp, tid);
  if (t == nullptr)
    {
      t = read_type_record (ccp, tid);
      if (t == nullptr)
	{
	  objfile *objfile = ccp->per_objfile->objfile;
	  complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name, tid);
	  t = builtin_type (objfile)->builtin_error;
	  set_tid_type (ccp, tid, t);
	}
    }

  if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
    process_struct_members (ccp, tid, t);

  fp->set_type (t);
  fp->set_loc_bitpos (offset / TARGET_CHAR_BIT);
  fp->set_bitsize (get_bitsize (ccp->dict, tid, kind));

  fip->fields.emplace_back (new_field);

  return 0;
}

/* Callback to add member NAME of EVAL to an enumeration type.
   ARG contains the ctf_field_info.  */

static int
ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)
{
  struct ctf_field_info *fip = (struct ctf_field_info *) arg;
  struct ctf_nextfield new_field;
  struct field *fp;
  struct ctf_context *ccp = fip->cur_context;

  fp = &new_field.field;
  fp->set_name (name);
  fp->set_type (nullptr);
  fp->set_loc_enumval (enum_value);
  fp->set_bitsize (0);

  if (name != nullptr && *name != '\0')
    {
      objfile *objfile = ccp->per_objfile->objfile;
      symbol *sym = objfile->new_symbol<symbol> ();

      sym->set_language (language_c, &objfile->objfile_obstack);
      sym->compute_and_set_names (name, false, objfile->per_bfd);
      sym->set_loc_class_index (LOC_CONST);
      sym->set_domain (VAR_DOMAIN);
      sym->set_type (fip->ptype);
      add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
    }

  fip->fields.emplace_back (new_field);

  return 0;
}

/* Add a new type symbol entry, with its name from TID, its access
   index and domain from TID's kind, and its type from TYPE.  */

static void
new_type_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)
{
  ctf_dict_t *dict = ccp->dict;

  const char *name = ctf_type_name_raw (dict, tid);
  if (name != nullptr && *name != '\0')
    {
      objfile *objfile = ccp->per_objfile->objfile;
      symbol *sym = objfile->new_symbol<symbol> ();

      sym->set_language (language_c, &objfile->objfile_obstack);
      sym->compute_and_set_names (name, false, objfile->per_bfd);
      sym->set_domain (TYPE_DOMAIN);
      sym->set_loc_class_index (LOC_TYPEDEF);

      if (type != nullptr)
	sym->set_type (type);

      uint32_t kind = ctf_type_kind (dict, tid);
      switch (kind)
	{
	  case CTF_K_STRUCT:
	  case CTF_K_UNION:
	  case CTF_K_ENUM:
	    sym->set_domain (STRUCT_DOMAIN);
	    break;
	}

      add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
    }
}

/* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
   and create the symbol for it.  */

static struct type *
read_base_type (struct ctf_context *ccp, ctf_id_t tid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  ctf_encoding_t cet;
  struct type *type = nullptr;
  const char *name;
  uint32_t kind;

  if (ctf_type_encoding (dict, tid, &cet))
    {
      complaint (_("ctf_type_encoding read_base_type failed - %s"),
		 ctf_errmsg (ctf_errno (dict)));
      return nullptr;
    }

  name = ctf_type_name_raw (dict, tid);
  if (name == nullptr || *name == '\0')
    {
      name = ctf_type_aname (dict, tid);
      if (name == nullptr || *name == '\0')
	complaint (_("ctf_type_aname read_base_type failed - %s"),
		   ctf_errmsg (ctf_errno (dict)));
    }

  type_allocator alloc (objfile, language_c);
  kind = ctf_type_kind (dict, tid);
  if (kind == CTF_K_INTEGER)
    {
      uint32_t issigned, ischar, isbool;
      struct gdbarch *gdbarch = objfile->arch ();

      issigned = cet.cte_format & CTF_INT_SIGNED;
      ischar = cet.cte_format & CTF_INT_CHAR;
      isbool = cet.cte_format & CTF_INT_BOOL;
      if (ischar)
	type = init_character_type (alloc, TARGET_CHAR_BIT, !issigned, name);
      else if (isbool)
	type = init_boolean_type (alloc, gdbarch_int_bit (gdbarch),
				  !issigned, name);
      else
	{
	  int bits;
	  if (cet.cte_bits && ((cet.cte_bits % TARGET_CHAR_BIT) == 0))
	    bits = cet.cte_bits;
	  else
	    bits = gdbarch_int_bit (gdbarch);
	  type = init_integer_type (alloc, bits, !issigned, name);
	}
    }
  else if (kind == CTF_K_FLOAT)
    {
      uint32_t isflt;
      isflt = !((cet.cte_format & CTF_FP_IMAGRY) == CTF_FP_IMAGRY
		 || (cet.cte_format & CTF_FP_DIMAGRY) == CTF_FP_DIMAGRY
		 || (cet.cte_format & CTF_FP_LDIMAGRY) == CTF_FP_LDIMAGRY);
      if (isflt)
	type = ctf_init_float_type (objfile, cet.cte_bits, name, name);
      else
	{
	  struct type *t
	    = ctf_init_float_type (objfile, cet.cte_bits / 2, NULL, name);
	  type = init_complex_type (name, t);
	}
    }
  else
    {
      complaint (_("read_base_type: unsupported base kind (%d)"), kind);
      type = alloc.new_type (TYPE_CODE_ERROR, cet.cte_bits, name);
    }

  if (name != nullptr && streq (name, "char"))
    type->set_has_no_signedness (true);

  return set_tid_type (ccp, tid, type);
}

static void
process_base_type (struct ctf_context *ccp, ctf_id_t tid)
{
  struct type *type;

  type = read_base_type (ccp, tid);
  new_type_symbol (ccp, type, tid);
}

/* Start a structure or union scope (definition) with TID to create a type
   for the structure or union.

   Fill in the type's name and general properties. The members will not be
   processed, nor a symbol table entry be done until process_structure_type
   (assuming the type has a name).  */

static struct type *
read_structure_type (struct ctf_context *ccp, ctf_id_t tid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  struct type *type;
  uint32_t kind;

  type = type_allocator (objfile, language_c).new_type ();

  const char *name = ctf_type_name_raw (dict, tid);
  if (name != nullptr && *name != '\0')
    type->set_name (name);

  kind = ctf_type_kind (dict, tid);
  if (kind == CTF_K_UNION)
    type->set_code (TYPE_CODE_UNION);
  else
    type->set_code (TYPE_CODE_STRUCT);

  type->set_length (ctf_type_size (dict, tid));

  if (ssize_t align = ctf_type_align (dict, tid);
      align >= 0)
    set_type_align (type, align);
  else
    complaint (_("ctf_type_align read_structure_type failed - %s"),
	       ctf_errmsg (ctf_errno (dict)));


  return set_tid_type (ccp, tid, type);
}

/* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
   and create the symbol for it.  */

static void
process_struct_members (struct ctf_context *ccp,
			ctf_id_t tid,
			struct type *type)
{
  struct ctf_field_info fi;

  fi.cur_context = ccp;
  if (ctf_member_iter (ccp->dict, tid, ctf_add_member_cb, &fi) == CTF_ERR)
    complaint (_("ctf_member_iter process_struct_members failed - %s"),
	       ctf_errmsg (ctf_errno (ccp->dict)));

  /* Attach fields to the type.  */
  attach_fields_to_type (&fi, type);

  new_type_symbol (ccp, type, tid);
}

static void
process_structure_type (struct ctf_context *ccp, ctf_id_t tid)
{
  struct type *type;

  type = read_structure_type (ccp, tid);
  process_struct_members (ccp, tid, type);
}

/* Create a function type for TID and set its return type.  */

static struct type *
read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  struct type *type, *rettype, *atype;
  ctf_funcinfo_t cfi;
  uint32_t argc;

  type = type_allocator (objfile, language_c).new_type ();

  type->set_code (TYPE_CODE_FUNC);
  if (ctf_func_type_info (dict, tid, &cfi) < 0)
    {
      const char *fname = ctf_type_name_raw (dict, tid);
      error (_("Error getting function type info: %s"),
	     fname == nullptr ? "noname" : fname);
    }
  rettype = fetch_tid_type (ccp, cfi.ctc_return);
  type->set_target_type (rettype);

  if (ssize_t align = ctf_type_align (dict, tid);
      align >= 0)
    set_type_align (type, align);
  else
    complaint (_("ctf_type_align read_func_kind_type failed - %s"),
	       ctf_errmsg (ctf_errno (dict)));

  /* Set up function's arguments.  */
  argc = cfi.ctc_argc;
  type->set_num_fields (argc);
  if ((cfi.ctc_flags & CTF_FUNC_VARARG) != 0)
    type->set_has_varargs (true);

  if (argc != 0)
    {
      std::vector<ctf_id_t> argv (argc);
      if (ctf_func_type_args (dict, tid, argc, argv.data ()) == CTF_ERR)
	return nullptr;

      type->alloc_fields (argc);
      struct type *void_type = builtin_type (objfile)->builtin_void;
      /* If failed to find the argument type, fill it with void_type.  */
      for (int iparam = 0; iparam < argc; iparam++)
	{
	  atype = fetch_tid_type (ccp, argv[iparam]);
	  if (atype != nullptr)
	    type->field (iparam).set_type (atype);
	  else
	    type->field (iparam).set_type (void_type);
	}
    }

  return set_tid_type (ccp, tid, type);
}

/* Given a TID of CTF_K_ENUM, process all the members of the
   enumeration, and create the symbol for the enumeration type.  */

static struct type *
read_enum_type (struct ctf_context *ccp, ctf_id_t tid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  struct type *type;

  type = type_allocator (objfile, language_c).new_type ();

  const char *name = ctf_type_name_raw (dict, tid);
  if (name != nullptr && *name != '\0')
    type->set_name (name);

  type->set_code (TYPE_CODE_ENUM);
  type->set_length (ctf_type_size (dict, tid));
  /* Set the underlying type based on its ctf_type_size bits.  */
  type->set_target_type (objfile_int_type (objfile, type->length (), false));

  if (ssize_t align = ctf_type_align (dict, tid);
      align >= 0)
    set_type_align (type, align);
  else
    complaint (_("ctf_type_align read_enum_type failed - %s"),
	       ctf_errmsg (ctf_errno (dict)));

  return set_tid_type (ccp, tid, type);
}

static void
process_enum_type (struct ctf_context *ccp, ctf_id_t tid)
{
  struct type *type;
  struct ctf_field_info fi;

  type = read_enum_type (ccp, tid);

  fi.cur_context = ccp;
  fi.ptype = type;
  if (ctf_enum_iter (ccp->dict, tid, ctf_add_enum_member_cb, &fi) == CTF_ERR)
    complaint (_("ctf_enum_iter process_enum_type failed - %s"),
	       ctf_errmsg (ctf_errno (ccp->dict)));

  /* Attach fields to the type.  */
  attach_fields_to_type (&fi, type);

  new_type_symbol (ccp, type, tid);
}

/* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID.  */

static struct type *
add_array_cv_type (struct ctf_context *ccp,
		   ctf_id_t tid,
		   struct type *base_type,
		   int cnst,
		   int voltl)
{
  struct type *el_type, *inner_array;

  base_type = copy_type (base_type);
  inner_array = base_type;

  while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
    {
      inner_array->set_target_type (copy_type (inner_array->target_type ()));
      inner_array = inner_array->target_type ();
    }

  el_type = inner_array->target_type ();
  cnst |= TYPE_CONST (el_type);
  voltl |= TYPE_VOLATILE (el_type);
  inner_array->set_target_type (make_cv_type (cnst, voltl, el_type));

  return set_tid_type (ccp, tid, base_type);
}

/* Read all information from a TID of CTF_K_ARRAY.  */

static struct type *
read_array_type (struct ctf_context *ccp, ctf_id_t tid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  struct type *element_type, *range_type, *idx_type;
  struct type *type;
  ctf_arinfo_t ar;

  if (ctf_array_info (dict, tid, &ar) == CTF_ERR)
    {
      complaint (_("ctf_array_info read_array_type failed - %s"),
		 ctf_errmsg (ctf_errno (dict)));
      return nullptr;
    }

  element_type = fetch_tid_type (ccp, ar.ctr_contents);
  if (element_type == nullptr)
    return nullptr;

  idx_type = fetch_tid_type (ccp, ar.ctr_index);
  if (idx_type == nullptr)
    idx_type = builtin_type (objfile)->builtin_int;

  type_allocator alloc (objfile, language_c);
  range_type = create_static_range_type (alloc, idx_type, 0, ar.ctr_nelems - 1);
  type = create_array_type (alloc, element_type, range_type);
  if (ar.ctr_nelems <= 1)	/* Check if undefined upper bound.  */
    {
      range_type->bounds ()->high.set_undefined ();
      type->set_length (0);
      type->set_target_is_stub (true);
    }
  else
    type->set_length (ctf_type_size (dict, tid));

  if (ssize_t align = ctf_type_align (dict, tid);
      align >= 0)
    set_type_align (type, align);
  else
    complaint (_("ctf_type_align read_array_type failed - %s"),
	       ctf_errmsg (ctf_errno (dict)));

  return set_tid_type (ccp, tid, type);
}

/* Read TID of kind CTF_K_CONST with base type BTID.  */

static struct type *
read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  struct type *base_type, *cv_type;

  base_type = fetch_tid_type (ccp, btid);
  if (base_type == nullptr)
    {
      base_type = read_type_record (ccp, btid);
      if (base_type == nullptr)
	{
	  complaint (_("read_const_type: NULL base type (%ld)"), btid);
	  base_type = builtin_type (objfile)->builtin_error;
	}
    }
  cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type);

  return set_tid_type (ccp, tid, cv_type);
}

/* Read TID of kind CTF_K_VOLATILE with base type BTID.  */

static struct type *
read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  struct type *base_type, *cv_type;

  base_type = fetch_tid_type (ccp, btid);
  if (base_type == nullptr)
    {
      base_type = read_type_record (ccp, btid);
      if (base_type == nullptr)
	{
	  complaint (_("read_volatile_type: NULL base type (%ld)"), btid);
	  base_type = builtin_type (objfile)->builtin_error;
	}
    }

  if (ctf_type_kind (dict, btid) == CTF_K_ARRAY)
    return add_array_cv_type (ccp, tid, base_type, 0, 1);
  cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type);

  return set_tid_type (ccp, tid, cv_type);
}

/* Read TID of kind CTF_K_RESTRICT with base type BTID.  */

static struct type *
read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  struct type *base_type, *cv_type;

  base_type = fetch_tid_type (ccp, btid);
  if (base_type == nullptr)
    {
      base_type = read_type_record (ccp, btid);
      if (base_type == nullptr)
	{
	  complaint (_("read_restrict_type: NULL base type (%ld)"), btid);
	  base_type = builtin_type (objfile)->builtin_error;
	}
    }
  cv_type = make_restrict_type (base_type);

  return set_tid_type (ccp, tid, cv_type);
}

/* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID.  */

static struct type *
read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
		   ctf_id_t btid, const char *name)
{
  objfile *objfile = ccp->per_objfile->objfile;
  struct type *this_type, *target_type;

  char *aname = obstack_strdup (&objfile->objfile_obstack, name);
  this_type = type_allocator (objfile, language_c).new_type (TYPE_CODE_TYPEDEF,
							     0, aname);
  set_tid_type (ccp, tid, this_type);
  target_type = fetch_tid_type (ccp, btid);
  if (target_type != this_type)
    this_type->set_target_type (target_type);
  else
    this_type->set_target_type (nullptr);

  this_type->set_target_is_stub (this_type->target_type () != nullptr);

  return this_type;
}

/* Read TID of kind CTF_K_POINTER with base type BTID.  */

static struct type *
read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  struct type *target_type, *type;

  target_type = fetch_tid_type (ccp, btid);
  if (target_type == nullptr)
    {
      target_type = read_type_record (ccp, btid);
      if (target_type == nullptr)
	{
	  complaint (_("read_pointer_type: NULL target type (%ld)"), btid);
	  target_type = builtin_type (objfile)->builtin_error;
	}
    }

  type = lookup_pointer_type (target_type);

  if (ssize_t align = ctf_type_align (ccp->dict, tid);
      align >= 0)
    set_type_align (type, align);
  else
    complaint (_("ctf_type_align read_pointer_type failed - %s"),
	       ctf_errmsg (ctf_errno (ccp->dict)));

  return set_tid_type (ccp, tid, type);
}

/* Read information from a TID of CTF_K_FORWARD.  */

static struct type *
read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
{
  objfile *objfile = ccp->per_objfile->objfile;
  ctf_dict_t *dict = ccp->dict;
  struct type *type;
  uint32_t kind;

  type = type_allocator (objfile, language_c).new_type ();

  const char *name = ctf_type_name_raw (dict, tid);
  if (name != nullptr && *name != '\0')
    type->set_name (name);

  kind = ctf_type_kind_forwarded (dict, tid);
  if (kind == CTF_K_UNION)
    type->set_code (TYPE_CODE_UNION);
  else
    type->set_code (TYPE_CODE_STRUCT);

  type->set_length (0);
  type->set_is_stub (true);

  return set_tid_type (ccp, tid, type);
}

/* Read information associated with type TID.  */

static struct type *
read_type_record (struct ctf_context *ccp, ctf_id_t tid)
{
  ctf_dict_t *dict = ccp->dict;
  uint32_t kind;
  struct type *type = nullptr;
  ctf_id_t btid;

  kind = ctf_type_kind (dict, tid);
  switch (kind)
    {
      case CTF_K_STRUCT:
      case CTF_K_UNION:
	type = read_structure_type (ccp, tid);
	break;
      case CTF_K_ENUM:
	type = read_enum_type (ccp, tid);
	break;
      case CTF_K_FUNCTION:
	type = read_func_kind_type (ccp, tid);
	break;
      case CTF_K_CONST:
	btid = ctf_type_reference (dict, tid);
	type = read_const_type (ccp, tid, btid);
	break;
      case CTF_K_TYPEDEF:
	{
	  const char *name = ctf_type_name_raw (dict, tid);
	  btid = ctf_type_reference (dict, tid);
	  type = read_typedef_type (ccp, tid, btid, name);
	}
	break;
      case CTF_K_VOLATILE:
	btid = ctf_type_reference (dict, tid);
	type = read_volatile_type (ccp, tid, btid);
	break;
      case CTF_K_RESTRICT:
	btid = ctf_type_reference (dict, tid);
	type = read_restrict_type (ccp, tid, btid);
	break;
      case CTF_K_POINTER:
	btid = ctf_type_reference (dict, tid);
	type = read_pointer_type (ccp, tid, btid);
	break;
      case CTF_K_INTEGER:
      case CTF_K_FLOAT:
	type = read_base_type (ccp, tid);
	break;
      case CTF_K_ARRAY:
	type = read_array_type (ccp, tid);
	break;
      case CTF_K_FORWARD:
	type = read_forward_type (ccp, tid);
	break;
      case CTF_K_UNKNOWN:
	break;
      default:
	break;
    }

  return type;
}

/* Callback to add type TID to the symbol table.  */

static int
ctf_add_type_cb (ctf_id_t tid, void *arg)
{
  struct ctf_context *ccp = (struct ctf_context *) arg;
  struct type *type;
  uint32_t kind;

  /* Check if tid's type has already been defined.  */
  type = get_tid_type (ccp, tid);
  if (type != nullptr)
    {
      ctf_debug_printf ("tid=%ld already defined, skipping", tid);
      return 0;
    }

  ctf_id_t btid = ctf_type_reference (ccp->dict, tid);
  kind = ctf_type_kind (ccp->dict, tid);

  ctf_debug_printf ("processing tid=%ld kind=%s name='%s' btid=%ld",
		    tid, ctf_kind_str (kind),
		    ctf_type_name_raw (ccp->dict, tid) ? : "(null)", btid);

  switch (kind)
    {
      case CTF_K_STRUCT:
      case CTF_K_UNION:
	process_structure_type (ccp, tid);
	break;
      case CTF_K_ENUM:
	process_enum_type (ccp, tid);
	break;
      case CTF_K_FUNCTION:
	type = read_func_kind_type (ccp, tid);
	new_type_symbol (ccp, type, tid);
	break;
      case CTF_K_INTEGER:
      case CTF_K_FLOAT:
	process_base_type (ccp, tid);
	break;
      case CTF_K_TYPEDEF:
	new_type_symbol (ccp, read_type_record (ccp, tid), tid);
	break;
      case CTF_K_CONST:
	type = read_const_type (ccp, tid, btid);
	new_type_symbol (ccp, type, tid);
	break;
      case CTF_K_VOLATILE:
	type = read_volatile_type (ccp, tid, btid);
	new_type_symbol (ccp, type, tid);
	break;
      case CTF_K_RESTRICT:
	type = read_restrict_type (ccp, tid, btid);
	new_type_symbol (ccp, type, tid);
	break;
      case CTF_K_POINTER:
	type = read_pointer_type (ccp, tid, btid);
	new_type_symbol (ccp, type, tid);
	break;
      case CTF_K_ARRAY:
	type = read_array_type (ccp, tid);
	new_type_symbol (ccp, type, tid);
	break;
      case CTF_K_UNKNOWN:
	break;
      default:
	break;
    }

  return 0;
}

/* Callback to add variable NAME with TID to the symbol table.  */

static int
ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
{
  struct ctf_context *ccp = (struct ctf_context *) arg;
  struct symbol *sym = nullptr;
  struct type *type;
  uint32_t kind;
  objfile *objfile = ccp->per_objfile->objfile;

  type = get_tid_type (ccp, id);

  kind = ctf_type_kind (ccp->dict, id);

  ctf_debug_printf ("adding variable name='%s' tid=%ld kind=%s",
		    name, id, ctf_kind_str (kind));

  if (type == nullptr)
    {
      complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
      type = builtin_type (objfile)->builtin_error;
    }
  sym = objfile->new_symbol<symbol> ();
  sym->set_type (type);
  sym->set_loc_class_index (LOC_OPTIMIZED_OUT);
  sym->compute_and_set_names (name, false, objfile->per_bfd);

  if (kind == CTF_K_FUNCTION)
    {
      sym->set_domain (FUNCTION_DOMAIN);
      if (name != nullptr && streq (name, "main"))
	set_objfile_main_name (objfile, name, language_c);
    }
  else
    sym->set_domain (VAR_DOMAIN);

  add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
  set_symbol_address (objfile, sym, name);

  return 0;
}

/* Add entries in either data objects or function info section, controlled
   by FUNCTIONS.  */

static void
add_stt_entries (struct ctf_context *ccp, int functions)
{
  ctf_next_t *i = nullptr;
  const char *tname;
  ctf_id_t tid;
  struct symbol *sym = nullptr;
  struct type *type;

  while ((tid = ctf_symbol_next (ccp->dict, &i, &tname, functions)) != CTF_ERR)
    {
      objfile *objfile = ccp->per_objfile->objfile;
      type = get_tid_type (ccp, tid);
      if (type == nullptr)
	{
	  ctf_debug_printf ("skipping '%s' tid=0x%lx (no type found)", tname,
			    tid);
	  continue;
	}

      ctf_debug_printf ("adding %s '%s' tid=0x%lx",
			functions ? "function" : "object", tname, tid);

      sym = objfile->new_symbol<symbol> ();
      sym->set_type (type);
      sym->set_domain (functions ? FUNCTION_DOMAIN : VAR_DOMAIN);
      sym->set_loc_class_index (LOC_STATIC);
      sym->compute_and_set_names (tname, false, objfile->per_bfd);
      add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
      set_symbol_address (objfile, sym, tname);
    }
}

/* Add entries in data objects section.  */

static void
add_stt_obj (struct ctf_context *ccp)
{
  add_stt_entries (ccp, 0);
}

/* Add entries in function info section.  */

static void
add_stt_func (struct ctf_context *ccp)
{
  add_stt_entries (ccp, 1);
}

/* Get text section base for OBJFILE, TSIZE contains the size.  */

static CORE_ADDR
get_objfile_text_range (struct objfile *objfile, size_t *tsize)
{
  bfd *abfd = objfile->obfd.get ();
  const asection *codes;

  codes = bfd_get_section_by_name (abfd, ".text");
  *tsize = codes ? bfd_section_size (codes) : 0;
  return objfile->text_section_offset ();
}

struct ctf_archive_iter_data
{
  ctf_per_objfile &per_objfile;
  std::vector<compunit_symtab *> compunit_symtabs;
};

/* ctf_archive_iter callback to build the ssymtab for archive member NAME.  */

static int
build_ctf_archive_member (ctf_dict_t *dict, const char *name, void *arg)
{
  CTF_SCOPED_DEBUG_START_END ("name='%s'", name);

  auto *iter_data = static_cast<ctf_archive_iter_data *> (arg);
  ctf_per_objfile &per_objfile = iter_data->per_objfile;

  if (!streq (name, ".ctf"))
    ctf_import (dict, per_objfile.parent_dict.get ());

  objfile *objfile = per_objfile.objfile;

  if (streq (name, ".ctf"))
    {
      name = bfd_get_filename (objfile->obfd.get ());
      ctf_debug_printf ("is parent, using name='%s'", name);
    }

  if (info_verbose)
    {
      gdb_printf (_("Reading in CTF data for %s..."), name);
      gdb_flush (gdb_stdout);
    }

  /* Start and size of the text segment.  */
  size_t tsize;
  CORE_ADDR offset = get_objfile_text_range (objfile, &tsize);

  ctf_debug_printf ("starting buildsym for '%s', offset=0x%s, tsize=%zu",
		    name, hex_string (offset), tsize);

  buildsym_compunit builder (objfile, name, nullptr, language_c, offset);
  builder.record_debugformat ("ctf");

  ctf_context ccx;
  ccx.per_objfile = &per_objfile;
  ccx.dict = dict;
  ccx.builder = &builder;

  /* Iterate over entries in data types section.  */
  if (ctf_type_iter (dict, ctf_add_type_cb, &ccx) == CTF_ERR)
    complaint (_("ctf_type_iter failed - %s"),
	       ctf_errmsg (ctf_errno (dict)));


  /* Iterate over entries in variable info section.  */
  if (ctf_variable_iter (dict, ctf_add_var_cb, &ccx) == CTF_ERR)
    complaint (_("ctf_variable_iter failed - %s"),
	       ctf_errmsg (ctf_errno (dict)));

  /* Add entries in data objects and function info sections.  */
  add_stt_obj (&ccx);
  add_stt_func (&ccx);

  iter_data->compunit_symtabs.push_back
   (builder.end_compunit_symtab (offset + tsize));

  /* Finish up the debug error message.  */
  if (info_verbose)
    gdb_printf (_("done.\n"));

  return 0;
}

/* Read CTF debugging information from a BFD section.  This is
   called from elfread.c.  */

void
elfctf_build_symtabs (objfile *objfile)
{
  bfd *abfd = objfile->obfd.get ();
  int err;
  scoped_time_it time_it (__func__);

  CTF_SCOPED_DEBUG_START_END ("building symtabs for %s",
			      bfd_get_filename (abfd));

  ctf_archive_up archive (ctf_bfdopen (abfd, &err));
  if (archive == nullptr)
    error (_("ctf_bfdopen failed on %ps - %s"),
	   styled_string (file_name_style.style (), bfd_get_filename (abfd)),
	   ctf_errmsg (err));

  ctf_dict_up dict (ctf_dict_open (archive.get (), NULL, &err));
  if (dict == nullptr)
    error (_("ctf_dict_open failed on %ps - %s"),
	   styled_string (file_name_style.style (), bfd_get_filename (abfd)),
	   ctf_errmsg (err));

  ctf_per_objfile &per_objfile
    = ctf_per_objfile_key.emplace (objfile, objfile, std::move (archive),
				   std::move (dict));
  ctf_archive_iter_data iter_data { per_objfile };

  if (ctf_archive_iter (per_objfile.archive.get (), build_ctf_archive_member,
			&iter_data)
      < 0)
    error (_("ctf_archive_iter failed in input file %ps: - %s"),
	   styled_string (file_name_style.style (), bfd_get_filename (abfd)),
	   ctf_errmsg (err));

  objfile->qf.emplace_front (std::make_unique<expanded_symbols_functions>
			     (std::move (iter_data.compunit_symtabs)));
}

#else

void
elfctf_build_symtabs (struct objfile *objfile)
{
  /* Nothing to do if CTF is disabled.  */
}

#endif /* ENABLE_LIBCTF */

INIT_GDB_FILE (_initialize_ctfread)
{
  add_setshow_boolean_cmd ("ctf", no_class, &debug_ctf,
			   _("Set CTF reading debugging."),
			   _("Show CTF reading debugging."),
			   _("When true, print debug messages related to "
			     "CTF reading."),
			   nullptr, nullptr,
			   &setdebuglist, &showdebuglist);
}
