Automatic date update in version.in
[binutils-gdb.git] / gdb / ctfread.c
blobee7c30f7d8737cc38aa7fae54d339438ae45377f
1 /* Compact ANSI-C Type Format (CTF) support in GDB.
3 Copyright (C) 2019-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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).
38 The CTF file or section itself has the following structure:
40 +--------+--------+---------+----------+----------+-------+--------+
41 | file | type | data | function | variable | data | string |
42 | header | labels | objects | info | info | types | table |
43 +--------+--------+---------+----------+----------+-------+--------+
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.
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].
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.
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.
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.
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. */
78 #include "buildsym.h"
79 #include "complaints.h"
80 #include "block.h"
81 #include "ctfread.h"
82 #include "psymtab.h"
84 #if ENABLE_LIBCTF
86 #include "ctf.h"
87 #include "ctf-api.h"
89 static const registry<objfile>::key<htab, htab_deleter> ctf_tid_key;
91 struct ctf_fp_info
93 explicit ctf_fp_info (ctf_dict_t *cfp) : fp (cfp) {}
94 ~ctf_fp_info ();
95 ctf_dict_t *fp;
98 /* Cleanup function for the ctf_dict_key data. */
99 ctf_fp_info::~ctf_fp_info ()
101 if (fp == nullptr)
102 return;
104 ctf_archive_t *arc = ctf_get_arc (fp);
105 ctf_dict_close (fp);
106 ctf_close (arc);
109 static const registry<objfile>::key<ctf_fp_info> ctf_dict_key;
111 /* A CTF context consists of a file pointer and an objfile pointer. */
113 struct ctf_context
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;
123 /* A partial symtab, specialized for this module. */
124 struct ctf_psymtab : public standard_psymtab
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)
134 void read_symtab (struct objfile *) override;
135 void expand_psymtab (struct objfile *) override;
137 struct ctf_context context;
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. */
144 struct ctf_nextfield
146 struct field field {};
149 struct ctf_field_info
151 /* List of data member fields. */
152 std::vector<struct ctf_nextfield> fields;
154 /* Context. */
155 struct ctf_context *cur_context;
157 /* Parent type. */
158 struct type *ptype;
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;
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;
169 /* Data held for a translation unit. */
171 struct ctf_per_tu_data
173 ctf_dict_t *fp;
174 struct objfile *of;
175 ctf_archive_t *arc;
176 psymtab_storage *pss;
177 psymbol_functions *psf;
180 /* Local function prototypes */
182 static int ctf_add_type_cb (ctf_id_t tid, void *arg);
184 static struct type *read_array_type (struct ctf_context *cp, ctf_id_t tid);
186 static struct type *read_pointer_type (struct ctf_context *cp, ctf_id_t tid,
187 ctf_id_t btid);
189 static struct type *read_structure_type (struct ctf_context *cp, ctf_id_t tid);
191 static struct type *read_enum_type (struct ctf_context *cp, ctf_id_t tid);
193 static struct type *read_typedef_type (struct ctf_context *cp, ctf_id_t tid,
194 ctf_id_t btid, const char *name);
196 static struct type *read_type_record (struct ctf_context *cp, ctf_id_t tid);
198 static void process_structure_type (struct ctf_context *cp, ctf_id_t tid);
200 static void process_struct_members (struct ctf_context *cp, ctf_id_t tid,
201 struct type *type);
203 static struct type *read_forward_type (struct ctf_context *cp, ctf_id_t tid);
205 static struct symbol *new_symbol (struct ctf_context *cp, struct type *type,
206 ctf_id_t tid);
208 struct ctf_tid_and_type
210 ctf_id_t tid;
211 struct type *type;
214 /* Hash function for a ctf_tid_and_type. */
216 static hashval_t
217 tid_and_type_hash (const void *item)
219 const struct ctf_tid_and_type *ids
220 = (const struct ctf_tid_and_type *) item;
222 return ids->tid;
225 /* Equality function for a ctf_tid_and_type. */
227 static int
228 tid_and_type_eq (const void *item_lhs, const void *item_rhs)
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;
235 return ids_lhs->tid == ids_rhs->tid;
238 /* Set the type associated with TID to TYP. */
240 static struct type *
241 set_tid_type (struct objfile *of, ctf_id_t tid, struct type *typ)
243 htab_t htab;
245 htab = ctf_tid_key.get (of);
246 if (htab == NULL)
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);
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;
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. */
267 static struct type *
268 get_tid_type (struct objfile *of, ctf_id_t tid)
270 struct ctf_tid_and_type *slot, ids;
271 htab_t htab;
273 htab = ctf_tid_key.get (of);
274 if (htab == NULL)
275 return nullptr;
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;
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. */
289 static struct type *
290 fetch_tid_type (struct ctf_context *ccp, ctf_id_t tid)
292 struct objfile *of = ccp->of;
293 struct type *typ;
295 typ = get_tid_type (of, tid);
296 if (typ == nullptr)
298 ctf_add_type_cb (tid, ccp);
299 typ = get_tid_type (of, tid);
302 return typ;
305 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
307 static int
308 get_bitsize (ctf_dict_t *fp, ctf_id_t tid, uint32_t kind)
310 ctf_encoding_t cet;
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;
318 return 0;
321 /* Set SYM's address, with NAME, from its minimal symbol entry. */
323 static void
324 set_symbol_address (struct objfile *of, struct symbol *sym, const char *name)
326 bound_minimal_symbol msym
327 = lookup_minimal_symbol (current_program_space, name, of);
328 if (msym.minsym != NULL)
330 sym->set_value_address (msym.value_address ());
331 sym->set_aclass_index (LOC_STATIC);
332 sym->set_section_index (msym.minsym->section_index ());
336 /* Create the vector of fields, and attach it to TYPE. */
338 static void
339 attach_fields_to_type (struct ctf_field_info *fip, struct type *type)
341 int nfields = fip->fields.size ();
343 if (nfields == 0)
344 return;
346 /* Record the field count, allocate space for the array of fields. */
347 type->alloc_fields (nfields);
349 /* Copy the saved-up fields into the field vector. */
350 for (int i = 0; i < nfields; ++i)
352 struct ctf_nextfield &field = fip->fields[i];
353 type->field (i) = field.field;
357 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
358 (which may be different from NAME) to the architecture back-end to allow
359 it to guess the correct format if necessary. */
361 static struct type *
362 ctf_init_float_type (struct objfile *objfile,
363 int bits,
364 const char *name,
365 const char *name_hint)
367 struct gdbarch *gdbarch = objfile->arch ();
368 const struct floatformat **format;
369 struct type *type;
371 type_allocator alloc (objfile, language_c);
372 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
373 if (format != nullptr)
374 type = init_float_type (alloc, bits, name, format);
375 else
376 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
378 return type;
381 /* Callback to add member NAME to a struct/union type. TID is the type
382 of struct/union member, OFFSET is the offset of member in bits,
383 and ARG contains the ctf_field_info. */
385 static int
386 ctf_add_member_cb (const char *name,
387 ctf_id_t tid,
388 unsigned long offset,
389 void *arg)
391 struct ctf_field_info *fip = (struct ctf_field_info *) arg;
392 struct ctf_context *ccp = fip->cur_context;
393 struct ctf_nextfield new_field;
394 struct field *fp;
395 struct type *t;
396 uint32_t kind;
398 fp = &new_field.field;
399 fp->set_name (name);
401 kind = ctf_type_kind (ccp->fp, tid);
402 t = fetch_tid_type (ccp, tid);
403 if (t == nullptr)
405 t = read_type_record (ccp, tid);
406 if (t == nullptr)
408 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name, tid);
409 t = builtin_type (ccp->of)->builtin_error;
410 set_tid_type (ccp->of, tid, t);
414 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
415 process_struct_members (ccp, tid, t);
417 fp->set_type (t);
418 fp->set_loc_bitpos (offset / TARGET_CHAR_BIT);
419 fp->set_bitsize (get_bitsize (ccp->fp, tid, kind));
421 fip->fields.emplace_back (new_field);
423 return 0;
426 /* Callback to add member NAME of EVAL to an enumeration type.
427 ARG contains the ctf_field_info. */
429 static int
430 ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)
432 struct ctf_field_info *fip = (struct ctf_field_info *) arg;
433 struct ctf_nextfield new_field;
434 struct field *fp;
435 struct ctf_context *ccp = fip->cur_context;
437 fp = &new_field.field;
438 fp->set_name (name);
439 fp->set_type (nullptr);
440 fp->set_loc_enumval (enum_value);
441 fp->set_bitsize (0);
443 if (name != nullptr)
445 struct symbol *sym = new (&ccp->of->objfile_obstack) symbol;
446 OBJSTAT (ccp->of, n_syms++);
448 sym->set_language (language_c, &ccp->of->objfile_obstack);
449 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
450 sym->set_aclass_index (LOC_CONST);
451 sym->set_domain (VAR_DOMAIN);
452 sym->set_type (fip->ptype);
453 add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
456 fip->fields.emplace_back (new_field);
458 return 0;
461 /* Add a new symbol entry, with its name from TID, its access index and
462 domain from TID's kind, and its type from TYPE. */
464 static struct symbol *
465 new_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)
467 struct objfile *objfile = ccp->of;
468 ctf_dict_t *fp = ccp->fp;
469 struct symbol *sym = nullptr;
471 const char *name = ctf_type_name_raw (fp, tid);
472 if (name != nullptr)
474 sym = new (&objfile->objfile_obstack) symbol;
475 OBJSTAT (objfile, n_syms++);
477 sym->set_language (language_c, &objfile->objfile_obstack);
478 sym->compute_and_set_names (name, false, objfile->per_bfd);
479 sym->set_domain (VAR_DOMAIN);
480 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
482 if (type != nullptr)
483 sym->set_type (type);
485 uint32_t kind = ctf_type_kind (fp, tid);
486 switch (kind)
488 case CTF_K_STRUCT:
489 case CTF_K_UNION:
490 case CTF_K_ENUM:
491 sym->set_aclass_index (LOC_TYPEDEF);
492 sym->set_domain (STRUCT_DOMAIN);
493 break;
494 case CTF_K_FUNCTION:
495 sym->set_aclass_index (LOC_STATIC);
496 set_symbol_address (objfile, sym, sym->linkage_name ());
497 break;
498 case CTF_K_CONST:
499 if (sym->type ()->code () == TYPE_CODE_VOID)
500 sym->set_type (builtin_type (objfile)->builtin_int);
501 break;
502 case CTF_K_TYPEDEF:
503 case CTF_K_INTEGER:
504 case CTF_K_FLOAT:
505 sym->set_aclass_index (LOC_TYPEDEF);
506 sym->set_domain (TYPE_DOMAIN);
507 break;
508 case CTF_K_POINTER:
509 break;
510 case CTF_K_VOLATILE:
511 case CTF_K_RESTRICT:
512 break;
513 case CTF_K_SLICE:
514 case CTF_K_ARRAY:
515 case CTF_K_UNKNOWN:
516 break;
519 add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
522 return sym;
525 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
526 and create the symbol for it. */
528 static struct type *
529 read_base_type (struct ctf_context *ccp, ctf_id_t tid)
531 struct objfile *of = ccp->of;
532 ctf_dict_t *fp = ccp->fp;
533 ctf_encoding_t cet;
534 struct type *type = nullptr;
535 const char *name;
536 uint32_t kind;
538 if (ctf_type_encoding (fp, tid, &cet))
540 complaint (_("ctf_type_encoding read_base_type failed - %s"),
541 ctf_errmsg (ctf_errno (fp)));
542 return nullptr;
545 name = ctf_type_name_raw (fp, tid);
546 if (name == nullptr || strlen (name) == 0)
548 name = ctf_type_aname (fp, tid);
549 if (name == nullptr)
550 complaint (_("ctf_type_aname read_base_type failed - %s"),
551 ctf_errmsg (ctf_errno (fp)));
554 type_allocator alloc (of, language_c);
555 kind = ctf_type_kind (fp, tid);
556 if (kind == CTF_K_INTEGER)
558 uint32_t issigned, ischar, isbool;
559 struct gdbarch *gdbarch = of->arch ();
561 issigned = cet.cte_format & CTF_INT_SIGNED;
562 ischar = cet.cte_format & CTF_INT_CHAR;
563 isbool = cet.cte_format & CTF_INT_BOOL;
564 if (ischar)
565 type = init_character_type (alloc, TARGET_CHAR_BIT, !issigned, name);
566 else if (isbool)
567 type = init_boolean_type (alloc, gdbarch_int_bit (gdbarch),
568 !issigned, name);
569 else
571 int bits;
572 if (cet.cte_bits && ((cet.cte_bits % TARGET_CHAR_BIT) == 0))
573 bits = cet.cte_bits;
574 else
575 bits = gdbarch_int_bit (gdbarch);
576 type = init_integer_type (alloc, bits, !issigned, name);
579 else if (kind == CTF_K_FLOAT)
581 uint32_t isflt;
582 isflt = !((cet.cte_format & CTF_FP_IMAGRY) == CTF_FP_IMAGRY
583 || (cet.cte_format & CTF_FP_DIMAGRY) == CTF_FP_DIMAGRY
584 || (cet.cte_format & CTF_FP_LDIMAGRY) == CTF_FP_LDIMAGRY);
585 if (isflt)
586 type = ctf_init_float_type (of, cet.cte_bits, name, name);
587 else
589 struct type *t
590 = ctf_init_float_type (of, cet.cte_bits / 2, NULL, name);
591 type = init_complex_type (name, t);
594 else
596 complaint (_("read_base_type: unsupported base kind (%d)"), kind);
597 type = alloc.new_type (TYPE_CODE_ERROR, cet.cte_bits, name);
600 if (name != nullptr && strcmp (name, "char") == 0)
601 type->set_has_no_signedness (true);
603 return set_tid_type (of, tid, type);
606 static void
607 process_base_type (struct ctf_context *ccp, ctf_id_t tid)
609 struct type *type;
611 type = read_base_type (ccp, tid);
612 new_symbol (ccp, type, tid);
615 /* Start a structure or union scope (definition) with TID to create a type
616 for the structure or union.
618 Fill in the type's name and general properties. The members will not be
619 processed, nor a symbol table entry be done until process_structure_type
620 (assuming the type has a name). */
622 static struct type *
623 read_structure_type (struct ctf_context *ccp, ctf_id_t tid)
625 struct objfile *of = ccp->of;
626 ctf_dict_t *fp = ccp->fp;
627 struct type *type;
628 uint32_t kind;
630 type = type_allocator (of, language_c).new_type ();
632 const char *name = ctf_type_name_raw (fp, tid);
633 if (name != nullptr && strlen (name) != 0)
634 type->set_name (name);
636 kind = ctf_type_kind (fp, tid);
637 if (kind == CTF_K_UNION)
638 type->set_code (TYPE_CODE_UNION);
639 else
640 type->set_code (TYPE_CODE_STRUCT);
642 type->set_length (ctf_type_size (fp, tid));
643 set_type_align (type, ctf_type_align (fp, tid));
645 return set_tid_type (ccp->of, tid, type);
648 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
649 and create the symbol for it. */
651 static void
652 process_struct_members (struct ctf_context *ccp,
653 ctf_id_t tid,
654 struct type *type)
656 struct ctf_field_info fi;
658 fi.cur_context = ccp;
659 if (ctf_member_iter (ccp->fp, tid, ctf_add_member_cb, &fi) == CTF_ERR)
660 complaint (_("ctf_member_iter process_struct_members failed - %s"),
661 ctf_errmsg (ctf_errno (ccp->fp)));
663 /* Attach fields to the type. */
664 attach_fields_to_type (&fi, type);
666 new_symbol (ccp, type, tid);
669 static void
670 process_structure_type (struct ctf_context *ccp, ctf_id_t tid)
672 struct type *type;
674 type = read_structure_type (ccp, tid);
675 process_struct_members (ccp, tid, type);
678 /* Create a function type for TID and set its return type. */
680 static struct type *
681 read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
683 struct objfile *of = ccp->of;
684 ctf_dict_t *fp = ccp->fp;
685 struct type *type, *rettype, *atype;
686 ctf_funcinfo_t cfi;
687 uint32_t argc;
689 type = type_allocator (of, language_c).new_type ();
691 type->set_code (TYPE_CODE_FUNC);
692 if (ctf_func_type_info (fp, tid, &cfi) < 0)
694 const char *fname = ctf_type_name_raw (fp, tid);
695 error (_("Error getting function type info: %s"),
696 fname == nullptr ? "noname" : fname);
698 rettype = fetch_tid_type (ccp, cfi.ctc_return);
699 type->set_target_type (rettype);
700 set_type_align (type, ctf_type_align (fp, tid));
702 /* Set up function's arguments. */
703 argc = cfi.ctc_argc;
704 type->set_num_fields (argc);
705 if ((cfi.ctc_flags & CTF_FUNC_VARARG) != 0)
706 type->set_has_varargs (true);
708 if (argc != 0)
710 std::vector<ctf_id_t> argv (argc);
711 if (ctf_func_type_args (fp, tid, argc, argv.data ()) == CTF_ERR)
712 return nullptr;
714 type->alloc_fields (argc);
715 struct type *void_type = builtin_type (of)->builtin_void;
716 /* If failed to find the argument type, fill it with void_type. */
717 for (int iparam = 0; iparam < argc; iparam++)
719 atype = fetch_tid_type (ccp, argv[iparam]);
720 if (atype != nullptr)
721 type->field (iparam).set_type (atype);
722 else
723 type->field (iparam).set_type (void_type);
727 return set_tid_type (of, tid, type);
730 /* Given a TID of CTF_K_ENUM, process all the members of the
731 enumeration, and create the symbol for the enumeration type. */
733 static struct type *
734 read_enum_type (struct ctf_context *ccp, ctf_id_t tid)
736 struct objfile *of = ccp->of;
737 ctf_dict_t *fp = ccp->fp;
738 struct type *type;
740 type = type_allocator (of, language_c).new_type ();
742 const char *name = ctf_type_name_raw (fp, tid);
743 if (name != nullptr && strlen (name) != 0)
744 type->set_name (name);
746 type->set_code (TYPE_CODE_ENUM);
747 type->set_length (ctf_type_size (fp, tid));
748 /* Set the underlying type based on its ctf_type_size bits. */
749 type->set_target_type (objfile_int_type (of, type->length (), false));
750 set_type_align (type, ctf_type_align (fp, tid));
752 return set_tid_type (of, tid, type);
755 static void
756 process_enum_type (struct ctf_context *ccp, ctf_id_t tid)
758 struct type *type;
759 struct ctf_field_info fi;
761 type = read_enum_type (ccp, tid);
763 fi.cur_context = ccp;
764 fi.ptype = type;
765 if (ctf_enum_iter (ccp->fp, tid, ctf_add_enum_member_cb, &fi) == CTF_ERR)
766 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
767 ctf_errmsg (ctf_errno (ccp->fp)));
769 /* Attach fields to the type. */
770 attach_fields_to_type (&fi, type);
772 new_symbol (ccp, type, tid);
775 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
777 static struct type *
778 add_array_cv_type (struct ctf_context *ccp,
779 ctf_id_t tid,
780 struct type *base_type,
781 int cnst,
782 int voltl)
784 struct type *el_type, *inner_array;
786 base_type = copy_type (base_type);
787 inner_array = base_type;
789 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
791 inner_array->set_target_type (copy_type (inner_array->target_type ()));
792 inner_array = inner_array->target_type ();
795 el_type = inner_array->target_type ();
796 cnst |= TYPE_CONST (el_type);
797 voltl |= TYPE_VOLATILE (el_type);
798 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, nullptr));
800 return set_tid_type (ccp->of, tid, base_type);
803 /* Read all information from a TID of CTF_K_ARRAY. */
805 static struct type *
806 read_array_type (struct ctf_context *ccp, ctf_id_t tid)
808 struct objfile *objfile = ccp->of;
809 ctf_dict_t *fp = ccp->fp;
810 struct type *element_type, *range_type, *idx_type;
811 struct type *type;
812 ctf_arinfo_t ar;
814 if (ctf_array_info (fp, tid, &ar) == CTF_ERR)
816 complaint (_("ctf_array_info read_array_type failed - %s"),
817 ctf_errmsg (ctf_errno (fp)));
818 return nullptr;
821 element_type = fetch_tid_type (ccp, ar.ctr_contents);
822 if (element_type == nullptr)
823 return nullptr;
825 idx_type = fetch_tid_type (ccp, ar.ctr_index);
826 if (idx_type == nullptr)
827 idx_type = builtin_type (objfile)->builtin_int;
829 type_allocator alloc (objfile, language_c);
830 range_type = create_static_range_type (alloc, idx_type, 0, ar.ctr_nelems - 1);
831 type = create_array_type (alloc, element_type, range_type);
832 if (ar.ctr_nelems <= 1) /* Check if undefined upper bound. */
834 range_type->bounds ()->high.set_undefined ();
835 type->set_length (0);
836 type->set_target_is_stub (true);
838 else
839 type->set_length (ctf_type_size (fp, tid));
841 set_type_align (type, ctf_type_align (fp, tid));
843 return set_tid_type (objfile, tid, type);
846 /* Read TID of kind CTF_K_CONST with base type BTID. */
848 static struct type *
849 read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
851 struct objfile *objfile = ccp->of;
852 struct type *base_type, *cv_type;
854 base_type = fetch_tid_type (ccp, btid);
855 if (base_type == nullptr)
857 base_type = read_type_record (ccp, btid);
858 if (base_type == nullptr)
860 complaint (_("read_const_type: NULL base type (%ld)"), btid);
861 base_type = builtin_type (objfile)->builtin_error;
864 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
866 return set_tid_type (objfile, tid, cv_type);
869 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
871 static struct type *
872 read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
874 struct objfile *objfile = ccp->of;
875 ctf_dict_t *fp = ccp->fp;
876 struct type *base_type, *cv_type;
878 base_type = fetch_tid_type (ccp, btid);
879 if (base_type == nullptr)
881 base_type = read_type_record (ccp, btid);
882 if (base_type == nullptr)
884 complaint (_("read_volatile_type: NULL base type (%ld)"), btid);
885 base_type = builtin_type (objfile)->builtin_error;
889 if (ctf_type_kind (fp, btid) == CTF_K_ARRAY)
890 return add_array_cv_type (ccp, tid, base_type, 0, 1);
891 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
893 return set_tid_type (objfile, tid, cv_type);
896 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
898 static struct type *
899 read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
901 struct objfile *objfile = ccp->of;
902 struct type *base_type, *cv_type;
904 base_type = fetch_tid_type (ccp, btid);
905 if (base_type == nullptr)
907 base_type = read_type_record (ccp, btid);
908 if (base_type == nullptr)
910 complaint (_("read_restrict_type: NULL base type (%ld)"), btid);
911 base_type = builtin_type (objfile)->builtin_error;
914 cv_type = make_restrict_type (base_type);
916 return set_tid_type (objfile, tid, cv_type);
919 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
921 static struct type *
922 read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
923 ctf_id_t btid, const char *name)
925 struct objfile *objfile = ccp->of;
926 struct type *this_type, *target_type;
928 char *aname = obstack_strdup (&objfile->objfile_obstack, name);
929 this_type = type_allocator (objfile, language_c).new_type (TYPE_CODE_TYPEDEF,
930 0, aname);
931 set_tid_type (objfile, tid, this_type);
932 target_type = fetch_tid_type (ccp, btid);
933 if (target_type != this_type)
934 this_type->set_target_type (target_type);
935 else
936 this_type->set_target_type (nullptr);
938 this_type->set_target_is_stub (this_type->target_type () != nullptr);
940 return set_tid_type (objfile, tid, this_type);
943 /* Read TID of kind CTF_K_POINTER with base type BTID. */
945 static struct type *
946 read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
948 struct objfile *of = ccp->of;
949 struct type *target_type, *type;
951 target_type = fetch_tid_type (ccp, btid);
952 if (target_type == nullptr)
954 target_type = read_type_record (ccp, btid);
955 if (target_type == nullptr)
957 complaint (_("read_pointer_type: NULL target type (%ld)"), btid);
958 target_type = builtin_type (ccp->of)->builtin_error;
962 type = lookup_pointer_type (target_type);
963 set_type_align (type, ctf_type_align (ccp->fp, tid));
965 return set_tid_type (of, tid, type);
968 /* Read information from a TID of CTF_K_FORWARD. */
970 static struct type *
971 read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
973 struct objfile *of = ccp->of;
974 ctf_dict_t *fp = ccp->fp;
975 struct type *type;
976 uint32_t kind;
978 type = type_allocator (of, language_c).new_type ();
980 const char *name = ctf_type_name_raw (fp, tid);
981 if (name != nullptr && strlen (name) != 0)
982 type->set_name (name);
984 kind = ctf_type_kind_forwarded (fp, tid);
985 if (kind == CTF_K_UNION)
986 type->set_code (TYPE_CODE_UNION);
987 else
988 type->set_code (TYPE_CODE_STRUCT);
990 type->set_length (0);
991 type->set_is_stub (true);
993 return set_tid_type (of, tid, type);
996 /* Read information associated with type TID. */
998 static struct type *
999 read_type_record (struct ctf_context *ccp, ctf_id_t tid)
1001 ctf_dict_t *fp = ccp->fp;
1002 uint32_t kind;
1003 struct type *type = nullptr;
1004 ctf_id_t btid;
1006 kind = ctf_type_kind (fp, tid);
1007 switch (kind)
1009 case CTF_K_STRUCT:
1010 case CTF_K_UNION:
1011 type = read_structure_type (ccp, tid);
1012 break;
1013 case CTF_K_ENUM:
1014 type = read_enum_type (ccp, tid);
1015 break;
1016 case CTF_K_FUNCTION:
1017 type = read_func_kind_type (ccp, tid);
1018 break;
1019 case CTF_K_CONST:
1020 btid = ctf_type_reference (fp, tid);
1021 type = read_const_type (ccp, tid, btid);
1022 break;
1023 case CTF_K_TYPEDEF:
1025 const char *name = ctf_type_name_raw (fp, tid);
1026 btid = ctf_type_reference (fp, tid);
1027 type = read_typedef_type (ccp, tid, btid, name);
1029 break;
1030 case CTF_K_VOLATILE:
1031 btid = ctf_type_reference (fp, tid);
1032 type = read_volatile_type (ccp, tid, btid);
1033 break;
1034 case CTF_K_RESTRICT:
1035 btid = ctf_type_reference (fp, tid);
1036 type = read_restrict_type (ccp, tid, btid);
1037 break;
1038 case CTF_K_POINTER:
1039 btid = ctf_type_reference (fp, tid);
1040 type = read_pointer_type (ccp, tid, btid);
1041 break;
1042 case CTF_K_INTEGER:
1043 case CTF_K_FLOAT:
1044 type = read_base_type (ccp, tid);
1045 break;
1046 case CTF_K_ARRAY:
1047 type = read_array_type (ccp, tid);
1048 break;
1049 case CTF_K_FORWARD:
1050 type = read_forward_type (ccp, tid);
1051 break;
1052 case CTF_K_UNKNOWN:
1053 break;
1054 default:
1055 break;
1058 return type;
1061 /* Callback to add type TID to the symbol table. */
1063 static int
1064 ctf_add_type_cb (ctf_id_t tid, void *arg)
1066 struct ctf_context *ccp = (struct ctf_context *) arg;
1067 struct type *type;
1068 uint32_t kind;
1070 /* Check if tid's type has already been defined. */
1071 type = get_tid_type (ccp->of, tid);
1072 if (type != nullptr)
1073 return 0;
1075 ctf_id_t btid = ctf_type_reference (ccp->fp, tid);
1076 kind = ctf_type_kind (ccp->fp, tid);
1077 switch (kind)
1079 case CTF_K_STRUCT:
1080 case CTF_K_UNION:
1081 process_structure_type (ccp, tid);
1082 break;
1083 case CTF_K_ENUM:
1084 process_enum_type (ccp, tid);
1085 break;
1086 case CTF_K_FUNCTION:
1087 type = read_func_kind_type (ccp, tid);
1088 new_symbol (ccp, type, tid);
1089 break;
1090 case CTF_K_INTEGER:
1091 case CTF_K_FLOAT:
1092 process_base_type (ccp, tid);
1093 break;
1094 case CTF_K_TYPEDEF:
1095 new_symbol (ccp, read_type_record (ccp, tid), tid);
1096 break;
1097 case CTF_K_CONST:
1098 type = read_const_type (ccp, tid, btid);
1099 new_symbol (ccp, type, tid);
1100 break;
1101 case CTF_K_VOLATILE:
1102 type = read_volatile_type (ccp, tid, btid);
1103 new_symbol (ccp, type, tid);
1104 break;
1105 case CTF_K_RESTRICT:
1106 type = read_restrict_type (ccp, tid, btid);
1107 new_symbol (ccp, type, tid);
1108 break;
1109 case CTF_K_POINTER:
1110 type = read_pointer_type (ccp, tid, btid);
1111 new_symbol (ccp, type, tid);
1112 break;
1113 case CTF_K_ARRAY:
1114 type = read_array_type (ccp, tid);
1115 new_symbol (ccp, type, tid);
1116 break;
1117 case CTF_K_UNKNOWN:
1118 break;
1119 default:
1120 break;
1123 return 0;
1126 /* Callback to add variable NAME with TID to the symbol table. */
1128 static int
1129 ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
1131 struct ctf_context *ccp = (struct ctf_context *) arg;
1132 struct symbol *sym = nullptr;
1133 struct type *type;
1134 uint32_t kind;
1136 type = get_tid_type (ccp->of, id);
1138 kind = ctf_type_kind (ccp->fp, id);
1139 switch (kind)
1141 case CTF_K_FUNCTION:
1142 if (name != nullptr && strcmp (name, "main") == 0)
1143 set_objfile_main_name (ccp->of, name, language_c);
1144 break;
1145 case CTF_K_INTEGER:
1146 case CTF_K_FLOAT:
1147 case CTF_K_VOLATILE:
1148 case CTF_K_RESTRICT:
1149 case CTF_K_TYPEDEF:
1150 case CTF_K_CONST:
1151 case CTF_K_POINTER:
1152 case CTF_K_ARRAY:
1153 if (type != nullptr)
1155 sym = new_symbol (ccp, type, id);
1156 if (sym != nullptr)
1157 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
1159 break;
1160 case CTF_K_STRUCT:
1161 case CTF_K_UNION:
1162 case CTF_K_ENUM:
1163 if (type == nullptr)
1165 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
1166 type = builtin_type (ccp->of)->builtin_error;
1168 sym = new (&ccp->of->objfile_obstack) symbol;
1169 OBJSTAT (ccp->of, n_syms++);
1170 sym->set_type (type);
1171 sym->set_domain (VAR_DOMAIN);
1172 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
1173 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
1174 add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
1175 break;
1176 default:
1177 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind);
1178 break;
1181 if (sym != nullptr)
1182 set_symbol_address (ccp->of, sym, name);
1184 return 0;
1187 /* Add entries in either data objects or function info section, controlled
1188 by FUNCTIONS. */
1190 static void
1191 add_stt_entries (struct ctf_context *ccp, int functions)
1193 ctf_next_t *i = nullptr;
1194 const char *tname;
1195 ctf_id_t tid;
1196 struct symbol *sym = nullptr;
1197 struct type *type;
1199 while ((tid = ctf_symbol_next (ccp->fp, &i, &tname, functions)) != CTF_ERR)
1201 type = get_tid_type (ccp->of, tid);
1202 if (type == nullptr)
1203 continue;
1204 sym = new (&ccp->of->objfile_obstack) symbol;
1205 OBJSTAT (ccp->of, n_syms++);
1206 sym->set_type (type);
1207 sym->set_domain (VAR_DOMAIN);
1208 sym->set_aclass_index (LOC_STATIC);
1209 sym->compute_and_set_names (tname, false, ccp->of->per_bfd);
1210 add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
1211 set_symbol_address (ccp->of, sym, tname);
1215 /* Add entries in data objects section. */
1217 static void
1218 add_stt_obj (struct ctf_context *ccp)
1220 add_stt_entries (ccp, 0);
1223 /* Add entries in function info section. */
1225 static void
1226 add_stt_func (struct ctf_context *ccp)
1228 add_stt_entries (ccp, 1);
1231 /* Get text section base for OBJFILE, TSIZE contains the size. */
1233 static CORE_ADDR
1234 get_objfile_text_range (struct objfile *of, size_t *tsize)
1236 bfd *abfd = of->obfd.get ();
1237 const asection *codes;
1239 codes = bfd_get_section_by_name (abfd, ".text");
1240 *tsize = codes ? bfd_section_size (codes) : 0;
1241 return of->text_section_offset ();
1244 /* Start a symtab for OBJFILE in CTF format. */
1246 static void
1247 ctf_start_compunit_symtab (ctf_psymtab *pst,
1248 struct objfile *of, CORE_ADDR text_offset)
1250 struct ctf_context *ccp;
1252 ccp = &pst->context;
1253 ccp->builder = new buildsym_compunit
1254 (of, pst->filename, nullptr,
1255 language_c, text_offset);
1256 ccp->builder->record_debugformat ("ctf");
1259 /* Finish reading symbol/type definitions in CTF format.
1260 END_ADDR is the end address of the file's text. */
1262 static struct compunit_symtab *
1263 ctf_end_compunit_symtab (ctf_psymtab *pst,
1264 CORE_ADDR end_addr)
1266 struct ctf_context *ccp;
1268 ccp = &pst->context;
1269 struct compunit_symtab *result
1270 = ccp->builder->end_compunit_symtab (end_addr);
1271 delete ccp->builder;
1272 ccp->builder = nullptr;
1273 return result;
1276 /* Add all members of an enum with type TID to partial symbol table. */
1278 static void
1279 ctf_psymtab_add_enums (struct ctf_context *ccp, ctf_id_t tid)
1281 int val;
1282 const char *ename;
1283 ctf_next_t *i = nullptr;
1285 while ((ename = ctf_enum_next (ccp->fp, tid, &i, &val)) != nullptr)
1287 ccp->pst->add_psymbol (ename, true,
1288 VAR_DOMAIN, LOC_CONST, -1,
1289 psymbol_placement::GLOBAL,
1290 unrelocated_addr (0),
1291 language_c, ccp->partial_symtabs, ccp->of);
1293 if (ctf_errno (ccp->fp) != ECTF_NEXT_END)
1294 complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
1295 ctf_errmsg (ctf_errno (ccp->fp)));
1298 /* Add entries in either data objects or function info section, controlled
1299 by FUNCTIONS, to psymtab. */
1301 static void
1302 ctf_psymtab_add_stt_entries (ctf_dict_t *cfp, ctf_psymtab *pst,
1303 struct objfile *of, int functions)
1305 ctf_next_t *i = nullptr;
1306 ctf_id_t tid;
1307 const char *tname;
1309 while ((tid = ctf_symbol_next (cfp, &i, &tname, functions)) != CTF_ERR)
1311 uint32_t kind = ctf_type_kind (cfp, tid);
1312 address_class aclass;
1313 domain_enum tdomain;
1314 switch (kind)
1316 case CTF_K_STRUCT:
1317 case CTF_K_UNION:
1318 case CTF_K_ENUM:
1319 tdomain = STRUCT_DOMAIN;
1320 break;
1321 default:
1322 tdomain = VAR_DOMAIN;
1323 break;
1326 if (kind == CTF_K_FUNCTION)
1327 aclass = LOC_STATIC;
1328 else if (kind == CTF_K_CONST)
1329 aclass = LOC_CONST;
1330 else
1331 aclass = LOC_TYPEDEF;
1333 pst->add_psymbol (tname, true,
1334 tdomain, aclass, -1,
1335 psymbol_placement::GLOBAL,
1336 unrelocated_addr (0),
1337 language_c, pst->context.partial_symtabs, of);
1341 /* Add entries in data objects section to psymtab. */
1343 static void
1344 ctf_psymtab_add_stt_obj (ctf_dict_t *cfp, ctf_psymtab *pst,
1345 struct objfile *of)
1347 ctf_psymtab_add_stt_entries (cfp, pst, of, 0);
1350 /* Add entries in function info section to psymtab. */
1352 static void
1353 ctf_psymtab_add_stt_func (ctf_dict_t *cfp, ctf_psymtab *pst,
1354 struct objfile *of)
1356 ctf_psymtab_add_stt_entries (cfp, pst, of, 1);
1359 /* Read in full symbols for PST, and anything it depends on. */
1361 void
1362 ctf_psymtab::expand_psymtab (struct objfile *objfile)
1364 struct ctf_context *ccp;
1366 gdb_assert (!readin);
1368 ccp = &context;
1370 /* Iterate over entries in data types section. */
1371 if (ctf_type_iter (ccp->fp, ctf_add_type_cb, ccp) == CTF_ERR)
1372 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1373 ctf_errmsg (ctf_errno (ccp->fp)));
1376 /* Iterate over entries in variable info section. */
1377 if (ctf_variable_iter (ccp->fp, ctf_add_var_cb, ccp) == CTF_ERR)
1378 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1379 ctf_errmsg (ctf_errno (ccp->fp)));
1381 /* Add entries in data objects and function info sections. */
1382 add_stt_obj (ccp);
1383 add_stt_func (ccp);
1385 readin = true;
1388 /* Expand partial symbol table PST into a full symbol table.
1389 PST is not NULL. */
1391 void
1392 ctf_psymtab::read_symtab (struct objfile *objfile)
1394 if (readin)
1395 warning (_("bug: psymtab for %s is already read in."), filename);
1396 else
1398 if (info_verbose)
1400 gdb_printf (_("Reading in CTF data for %s..."), filename);
1401 gdb_flush (gdb_stdout);
1404 /* Start a symtab. */
1405 CORE_ADDR offset; /* Start of text segment. */
1406 size_t tsize;
1408 offset = get_objfile_text_range (objfile, &tsize);
1409 ctf_start_compunit_symtab (this, objfile, offset);
1410 expand_psymtab (objfile);
1412 set_text_low (unrelocated_addr (0));
1413 set_text_high (unrelocated_addr (tsize));
1414 compunit_symtab = ctf_end_compunit_symtab (this, offset + tsize);
1416 /* Finish up the debug error message. */
1417 if (info_verbose)
1418 gdb_printf (_("done.\n"));
1422 /* Allocate a new partial_symtab NAME.
1424 Each source file that has not been fully read in is represented by
1425 a partial_symtab. This contains the information on where in the
1426 executable the debugging symbols for a specific file are, and a
1427 list of names of global symbols which are located in this file.
1428 They are all chained on partial symtab lists.
1430 Even after the source file has been read into a symtab, the
1431 partial_symtab remains around. They are allocated on an obstack,
1432 objfile_obstack. */
1434 static ctf_psymtab *
1435 create_partial_symtab (const char *name,
1436 ctf_archive_t *arc,
1437 ctf_dict_t *cfp,
1438 psymtab_storage *partial_symtabs,
1439 struct objfile *objfile)
1441 ctf_psymtab *pst;
1443 pst = new ctf_psymtab (name, partial_symtabs, objfile->per_bfd,
1444 unrelocated_addr (0));
1446 pst->context.arc = arc;
1447 pst->context.fp = cfp;
1448 pst->context.of = objfile;
1449 pst->context.partial_symtabs = partial_symtabs;
1450 pst->context.pst = pst;
1451 pst->context.builder = nullptr;
1453 return pst;
1456 /* Callback to add type TID to partial symbol table. */
1458 static int
1459 ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
1461 struct ctf_context *ccp;
1462 uint32_t kind;
1463 int section = -1;
1465 ccp = (struct ctf_context *) arg;
1467 domain_enum domain = UNDEF_DOMAIN;
1468 enum address_class aclass = LOC_UNDEF;
1469 kind = ctf_type_kind (ccp->fp, tid);
1470 switch (kind)
1472 case CTF_K_ENUM:
1473 ctf_psymtab_add_enums (ccp, tid);
1474 [[fallthrough]];
1475 case CTF_K_STRUCT:
1476 case CTF_K_UNION:
1477 domain = STRUCT_DOMAIN;
1478 aclass = LOC_TYPEDEF;
1479 break;
1480 case CTF_K_FUNCTION:
1481 case CTF_K_FORWARD:
1482 domain = VAR_DOMAIN;
1483 aclass = LOC_STATIC;
1484 section = SECT_OFF_TEXT (ccp->of);
1485 break;
1486 case CTF_K_CONST:
1487 domain = VAR_DOMAIN;
1488 aclass = LOC_STATIC;
1489 break;
1490 case CTF_K_TYPEDEF:
1491 case CTF_K_POINTER:
1492 case CTF_K_VOLATILE:
1493 case CTF_K_RESTRICT:
1494 domain = VAR_DOMAIN;
1495 aclass = LOC_TYPEDEF;
1496 break;
1497 case CTF_K_INTEGER:
1498 case CTF_K_FLOAT:
1499 domain = VAR_DOMAIN;
1500 aclass = LOC_TYPEDEF;
1501 break;
1502 case CTF_K_ARRAY:
1503 case CTF_K_UNKNOWN:
1504 return 0;
1507 const char *name = ctf_type_name_raw (ccp->fp, tid);
1508 if (name == nullptr || strlen (name) == 0)
1509 return 0;
1511 ccp->pst->add_psymbol (name, false,
1512 domain, aclass, section,
1513 psymbol_placement::STATIC,
1514 unrelocated_addr (0),
1515 language_c, ccp->partial_symtabs, ccp->of);
1517 return 0;
1520 /* Callback to add variable NAME with ID to partial symbol table. */
1522 static int
1523 ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
1525 struct ctf_context *ccp = (struct ctf_context *) arg;
1527 ccp->pst->add_psymbol (name, true,
1528 VAR_DOMAIN, LOC_STATIC, -1,
1529 psymbol_placement::GLOBAL,
1530 unrelocated_addr (0),
1531 language_c, ccp->partial_symtabs, ccp->of);
1532 return 0;
1535 /* Setup partial_symtab's describing each source file for which
1536 debugging information is available. */
1538 static void
1539 scan_partial_symbols (ctf_dict_t *cfp, psymtab_storage *partial_symtabs,
1540 struct ctf_per_tu_data *tup, const char *fname)
1542 struct objfile *of = tup->of;
1543 bool isparent = false;
1545 if (strcmp (fname, ".ctf") == 0)
1547 fname = bfd_get_filename (of->obfd.get ());
1548 isparent = true;
1551 ctf_psymtab *pst = create_partial_symtab (fname, tup->arc, cfp,
1552 partial_symtabs, of);
1554 struct ctf_context *ccx = &pst->context;
1555 if (isparent == false)
1556 ccx->pst = pst;
1558 if (ctf_type_iter (cfp, ctf_psymtab_type_cb, ccx) == CTF_ERR)
1559 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1560 ctf_errmsg (ctf_errno (cfp)));
1562 if (ctf_variable_iter (cfp, ctf_psymtab_var_cb, ccx) == CTF_ERR)
1563 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1564 ctf_errmsg (ctf_errno (cfp)));
1566 /* Scan CTF object and function sections which correspond to each
1567 STT_FUNC or STT_OBJECT entry in the symbol table,
1568 pick up what init_symtab has done. */
1569 ctf_psymtab_add_stt_obj (cfp, pst, of);
1570 ctf_psymtab_add_stt_func (cfp, pst, of);
1572 pst->end ();
1575 /* Callback to build the psymtab for archive member NAME. */
1577 static int
1578 build_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
1580 struct ctf_per_tu_data *tup = (struct ctf_per_tu_data *) arg;
1581 ctf_dict_t *parent = tup->fp;
1583 if (strcmp (name, ".ctf") != 0)
1584 ctf_import (ctf, parent);
1586 if (info_verbose)
1588 gdb_printf (_("Scanning archive member %s..."), name);
1589 gdb_flush (gdb_stdout);
1592 psymtab_storage *pss = tup->psf->get_partial_symtabs ().get ();
1593 scan_partial_symbols (ctf, pss, tup, name);
1595 return 0;
1598 /* Read CTF debugging information from a BFD section. This is
1599 called from elfread.c. It does a quick pass through the
1600 .ctf section to set up the partial symbol table. */
1602 void
1603 elfctf_build_psymtabs (struct objfile *of)
1605 struct ctf_per_tu_data pcu;
1606 bfd *abfd = of->obfd.get ();
1607 int err;
1609 ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
1610 if (arc == nullptr)
1611 error (_("ctf_bfdopen failed on %s - %s"),
1612 bfd_get_filename (abfd), ctf_errmsg (err));
1614 ctf_dict_t *fp = ctf_dict_open (arc, NULL, &err);
1615 if (fp == nullptr)
1616 error (_("ctf_dict_open failed on %s - %s"),
1617 bfd_get_filename (abfd), ctf_errmsg (err));
1618 ctf_dict_key.emplace (of, fp);
1620 pcu.fp = fp;
1621 pcu.of = of;
1622 pcu.arc = arc;
1624 psymbol_functions *psf = new psymbol_functions ();
1625 of->qf.emplace_front (psf);
1626 pcu.psf = psf;
1628 if (ctf_archive_iter (arc, build_ctf_archive_member, &pcu) < 0)
1629 error (_("ctf_archive_iter failed in input file %s: - %s"),
1630 bfd_get_filename (abfd), ctf_errmsg (err));
1633 #else
1635 void
1636 elfctf_build_psymtabs (struct objfile *of)
1638 /* Nothing to do if CTF is disabled. */
1641 #endif /* ENABLE_LIBCTF */