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