1 /* Compact ANSI-C Type Format (CTF) support in GDB.
3 Copyright (C) 2019-2023 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
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,
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. */
80 #include "complaints.h"
90 static const registry
<objfile
>::key
<htab
, htab_deleter
> ctf_tid_key
;
94 explicit ctf_fp_info (ctf_dict_t
*cfp
) : fp (cfp
) {}
99 /* Cleanup function for the ctf_dict_key data. */
100 ctf_fp_info::~ctf_fp_info ()
105 ctf_archive_t
*arc
= ctf_get_arc (fp
);
110 static const registry
<objfile
>::key
<ctf_fp_info
> ctf_dict_key
;
112 /* A CTF context consists of a file pointer and an objfile pointer. */
118 psymtab_storage
*partial_symtabs
;
121 struct buildsym_compunit
*builder
;
124 /* A partial symtab, specialized for this module. */
125 struct ctf_psymtab
: public standard_psymtab
127 ctf_psymtab (const char *filename
,
128 psymtab_storage
*partial_symtabs
,
129 objfile_per_bfd_storage
*objfile_per_bfd
,
131 : standard_psymtab (filename
, partial_symtabs
, objfile_per_bfd
, addr
)
135 void read_symtab (struct objfile
*) override
;
136 void expand_psymtab (struct objfile
*) override
;
138 struct ctf_context context
;
141 /* The routines that read and process fields/members of a C struct, union,
142 or enumeration, pass lists of data member fields in an instance of a
143 ctf_field_info structure. It is derived from dwarf2read.c. */
147 struct field field
{};
150 struct ctf_field_info
152 /* List of data member fields. */
153 std::vector
<struct ctf_nextfield
> fields
;
156 struct ctf_context
*cur_context
;
161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
162 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
163 std::vector
<struct decl_field
> typedef_field_list
;
165 /* Nested types defined by this struct and the number of elements in
167 std::vector
<struct decl_field
> nested_types_list
;
170 /* Data held for a translation unit. */
172 struct ctf_per_tu_data
177 psymtab_storage
*pss
;
178 psymbol_functions
*psf
;
181 /* Local function prototypes */
183 static int ctf_add_type_cb (ctf_id_t tid
, void *arg
);
185 static struct type
*read_array_type (struct ctf_context
*cp
, ctf_id_t tid
);
187 static struct type
*read_pointer_type (struct ctf_context
*cp
, ctf_id_t tid
,
190 static struct type
*read_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
192 static struct type
*read_enum_type (struct ctf_context
*cp
, ctf_id_t tid
);
194 static struct type
*read_typedef_type (struct ctf_context
*cp
, ctf_id_t tid
,
195 ctf_id_t btid
, const char *name
);
197 static struct type
*read_type_record (struct ctf_context
*cp
, ctf_id_t tid
);
199 static void process_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
201 static void process_struct_members (struct ctf_context
*cp
, ctf_id_t tid
,
204 static struct type
*read_forward_type (struct ctf_context
*cp
, ctf_id_t tid
);
206 static struct symbol
*new_symbol (struct ctf_context
*cp
, struct type
*type
,
209 struct ctf_tid_and_type
215 /* Hash function for a ctf_tid_and_type. */
218 tid_and_type_hash (const void *item
)
220 const struct ctf_tid_and_type
*ids
221 = (const struct ctf_tid_and_type
*) item
;
226 /* Equality function for a ctf_tid_and_type. */
229 tid_and_type_eq (const void *item_lhs
, const void *item_rhs
)
231 const struct ctf_tid_and_type
*ids_lhs
232 = (const struct ctf_tid_and_type
*) item_lhs
;
233 const struct ctf_tid_and_type
*ids_rhs
234 = (const struct ctf_tid_and_type
*) item_rhs
;
236 return ids_lhs
->tid
== ids_rhs
->tid
;
239 /* Set the type associated with TID to TYP. */
242 set_tid_type (struct objfile
*of
, ctf_id_t tid
, struct type
*typ
)
246 htab
= ctf_tid_key
.get (of
);
249 htab
= htab_create_alloc (1, tid_and_type_hash
,
251 NULL
, xcalloc
, xfree
);
252 ctf_tid_key
.set (of
, htab
);
255 struct ctf_tid_and_type
**slot
, ids
;
258 slot
= (struct ctf_tid_and_type
**) htab_find_slot (htab
, &ids
, INSERT
);
259 if (*slot
== nullptr)
260 *slot
= XOBNEW (&of
->objfile_obstack
, struct ctf_tid_and_type
);
265 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
266 empty or TID does not have a saved type. */
269 get_tid_type (struct objfile
*of
, ctf_id_t tid
)
271 struct ctf_tid_and_type
*slot
, ids
;
274 htab
= ctf_tid_key
.get (of
);
280 slot
= (struct ctf_tid_and_type
*) htab_find (htab
, &ids
);
287 /* Fetch the type for TID in CCP OF's tid_and_type hash, add the type to
288 * context CCP if hash is empty or TID does not have a saved type. */
291 fetch_tid_type (struct ctf_context
*ccp
, ctf_id_t tid
)
293 struct objfile
*of
= ccp
->of
;
296 typ
= get_tid_type (of
, tid
);
299 ctf_add_type_cb (tid
, ccp
);
300 typ
= get_tid_type (of
, tid
);
306 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
309 get_bitsize (ctf_dict_t
*fp
, ctf_id_t tid
, uint32_t kind
)
313 if ((kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
314 || kind
== CTF_K_FLOAT
)
315 && ctf_type_reference (fp
, tid
) != CTF_ERR
316 && ctf_type_encoding (fp
, tid
, &cet
) != CTF_ERR
)
322 /* Set SYM's address, with NAME, from its minimal symbol entry. */
325 set_symbol_address (struct objfile
*of
, struct symbol
*sym
, const char *name
)
327 struct bound_minimal_symbol msym
;
329 msym
= lookup_minimal_symbol (name
, nullptr, of
);
330 if (msym
.minsym
!= NULL
)
332 sym
->set_value_address (msym
.value_address ());
333 sym
->set_aclass_index (LOC_STATIC
);
334 sym
->set_section_index (msym
.minsym
->section_index ());
338 /* Create the vector of fields, and attach it to TYPE. */
341 attach_fields_to_type (struct ctf_field_info
*fip
, struct type
*type
)
343 int nfields
= fip
->fields
.size ();
348 /* Record the field count, allocate space for the array of fields. */
349 type
->set_num_fields (nfields
);
351 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
353 /* Copy the saved-up fields into the field vector. */
354 for (int i
= 0; i
< nfields
; ++i
)
356 struct ctf_nextfield
&field
= fip
->fields
[i
];
357 type
->field (i
) = field
.field
;
361 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
362 (which may be different from NAME) to the architecture back-end to allow
363 it to guess the correct format if necessary. */
366 ctf_init_float_type (struct objfile
*objfile
,
369 const char *name_hint
)
371 struct gdbarch
*gdbarch
= objfile
->arch ();
372 const struct floatformat
**format
;
375 type_allocator
alloc (objfile
);
376 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
377 if (format
!= nullptr)
378 type
= init_float_type (objfile
, bits
, name
, format
);
380 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
385 /* Callback to add member NAME to a struct/union type. TID is the type
386 of struct/union member, OFFSET is the offset of member in bits,
387 and ARG contains the ctf_field_info. */
390 ctf_add_member_cb (const char *name
,
392 unsigned long offset
,
395 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
396 struct ctf_context
*ccp
= fip
->cur_context
;
397 struct ctf_nextfield new_field
;
402 fp
= &new_field
.field
;
405 kind
= ctf_type_kind (ccp
->fp
, tid
);
406 t
= fetch_tid_type (ccp
, tid
);
409 t
= read_type_record (ccp
, tid
);
412 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name
, tid
);
413 t
= objfile_type (ccp
->of
)->builtin_error
;
414 set_tid_type (ccp
->of
, tid
, t
);
418 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
419 process_struct_members (ccp
, tid
, t
);
422 fp
->set_loc_bitpos (offset
/ TARGET_CHAR_BIT
);
423 FIELD_BITSIZE (*fp
) = get_bitsize (ccp
->fp
, tid
, kind
);
425 fip
->fields
.emplace_back (new_field
);
430 /* Callback to add member NAME of EVAL to an enumeration type.
431 ARG contains the ctf_field_info. */
434 ctf_add_enum_member_cb (const char *name
, int enum_value
, void *arg
)
436 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
437 struct ctf_nextfield new_field
;
439 struct ctf_context
*ccp
= fip
->cur_context
;
441 fp
= &new_field
.field
;
443 fp
->set_type (nullptr);
444 fp
->set_loc_enumval (enum_value
);
445 FIELD_BITSIZE (*fp
) = 0;
449 struct symbol
*sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
450 OBJSTAT (ccp
->of
, n_syms
++);
452 sym
->set_language (language_c
, &ccp
->of
->objfile_obstack
);
453 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
454 sym
->set_aclass_index (LOC_CONST
);
455 sym
->set_domain (VAR_DOMAIN
);
456 sym
->set_type (fip
->ptype
);
457 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
460 fip
->fields
.emplace_back (new_field
);
465 /* Add a new symbol entry, with its name from TID, its access index and
466 domain from TID's kind, and its type from TYPE. */
468 static struct symbol
*
469 new_symbol (struct ctf_context
*ccp
, struct type
*type
, ctf_id_t tid
)
471 struct objfile
*objfile
= ccp
->of
;
472 ctf_dict_t
*fp
= ccp
->fp
;
473 struct symbol
*sym
= nullptr;
475 const char *name
= ctf_type_name_raw (fp
, tid
);
478 sym
= new (&objfile
->objfile_obstack
) symbol
;
479 OBJSTAT (objfile
, n_syms
++);
481 sym
->set_language (language_c
, &objfile
->objfile_obstack
);
482 sym
->compute_and_set_names (name
, false, objfile
->per_bfd
);
483 sym
->set_domain (VAR_DOMAIN
);
484 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
487 sym
->set_type (type
);
489 uint32_t kind
= ctf_type_kind (fp
, tid
);
495 sym
->set_aclass_index (LOC_TYPEDEF
);
496 sym
->set_domain (STRUCT_DOMAIN
);
499 sym
->set_aclass_index (LOC_STATIC
);
500 set_symbol_address (objfile
, sym
, sym
->linkage_name ());
503 if (sym
->type ()->code () == TYPE_CODE_VOID
)
504 sym
->set_type (objfile_type (objfile
)->builtin_int
);
509 sym
->set_aclass_index (LOC_TYPEDEF
);
510 sym
->set_domain (VAR_DOMAIN
);
523 add_symbol_to_list (sym
, ccp
->builder
->get_file_symbols ());
529 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
530 and create the symbol for it. */
533 read_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
535 struct objfile
*of
= ccp
->of
;
536 ctf_dict_t
*fp
= ccp
->fp
;
538 struct type
*type
= nullptr;
542 if (ctf_type_encoding (fp
, tid
, &cet
))
544 complaint (_("ctf_type_encoding read_base_type failed - %s"),
545 ctf_errmsg (ctf_errno (fp
)));
549 name
= ctf_type_name_raw (fp
, tid
);
550 if (name
== nullptr || strlen (name
) == 0)
552 name
= ctf_type_aname (fp
, tid
);
554 complaint (_("ctf_type_aname read_base_type failed - %s"),
555 ctf_errmsg (ctf_errno (fp
)));
558 type_allocator
alloc (of
);
559 kind
= ctf_type_kind (fp
, tid
);
560 if (kind
== CTF_K_INTEGER
)
562 uint32_t issigned
, ischar
, isbool
;
563 struct gdbarch
*gdbarch
= of
->arch ();
565 issigned
= cet
.cte_format
& CTF_INT_SIGNED
;
566 ischar
= cet
.cte_format
& CTF_INT_CHAR
;
567 isbool
= cet
.cte_format
& CTF_INT_BOOL
;
569 type
= init_character_type (alloc
, TARGET_CHAR_BIT
, !issigned
, name
);
571 type
= init_boolean_type (of
, gdbarch_int_bit (gdbarch
),
576 if (cet
.cte_bits
&& ((cet
.cte_bits
% TARGET_CHAR_BIT
) == 0))
579 bits
= gdbarch_int_bit (gdbarch
);
580 type
= init_integer_type (alloc
, bits
, !issigned
, name
);
583 else if (kind
== CTF_K_FLOAT
)
586 isflt
= !((cet
.cte_format
& CTF_FP_IMAGRY
) == CTF_FP_IMAGRY
587 || (cet
.cte_format
& CTF_FP_DIMAGRY
) == CTF_FP_DIMAGRY
588 || (cet
.cte_format
& CTF_FP_LDIMAGRY
) == CTF_FP_LDIMAGRY
);
590 type
= ctf_init_float_type (of
, cet
.cte_bits
, name
, name
);
594 = ctf_init_float_type (of
, cet
.cte_bits
/ 2, NULL
, name
);
595 type
= init_complex_type (name
, t
);
600 complaint (_("read_base_type: unsupported base kind (%d)"), kind
);
601 type
= alloc
.new_type (TYPE_CODE_ERROR
, cet
.cte_bits
, name
);
604 if (name
!= nullptr && strcmp (name
, "char") == 0)
605 type
->set_has_no_signedness (true);
607 return set_tid_type (of
, tid
, type
);
611 process_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
615 type
= read_base_type (ccp
, tid
);
616 new_symbol (ccp
, type
, tid
);
619 /* Start a structure or union scope (definition) with TID to create a type
620 for the structure or union.
622 Fill in the type's name and general properties. The members will not be
623 processed, nor a symbol table entry be done until process_structure_type
624 (assuming the type has a name). */
627 read_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
629 struct objfile
*of
= ccp
->of
;
630 ctf_dict_t
*fp
= ccp
->fp
;
634 type
= type_allocator (of
).new_type ();
636 const char *name
= ctf_type_name_raw (fp
, tid
);
637 if (name
!= nullptr && strlen (name
) != 0)
638 type
->set_name (name
);
640 kind
= ctf_type_kind (fp
, tid
);
641 if (kind
== CTF_K_UNION
)
642 type
->set_code (TYPE_CODE_UNION
);
644 type
->set_code (TYPE_CODE_STRUCT
);
646 type
->set_length (ctf_type_size (fp
, tid
));
647 set_type_align (type
, ctf_type_align (fp
, tid
));
649 return set_tid_type (ccp
->of
, tid
, type
);
652 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
653 and create the symbol for it. */
656 process_struct_members (struct ctf_context
*ccp
,
660 struct ctf_field_info fi
;
662 fi
.cur_context
= ccp
;
663 if (ctf_member_iter (ccp
->fp
, tid
, ctf_add_member_cb
, &fi
) == CTF_ERR
)
664 complaint (_("ctf_member_iter process_struct_members failed - %s"),
665 ctf_errmsg (ctf_errno (ccp
->fp
)));
667 /* Attach fields to the type. */
668 attach_fields_to_type (&fi
, type
);
670 new_symbol (ccp
, type
, tid
);
674 process_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
678 type
= read_structure_type (ccp
, tid
);
679 process_struct_members (ccp
, tid
, type
);
682 /* Create a function type for TID and set its return type. */
685 read_func_kind_type (struct ctf_context
*ccp
, ctf_id_t tid
)
687 struct objfile
*of
= ccp
->of
;
688 ctf_dict_t
*fp
= ccp
->fp
;
689 struct type
*type
, *rettype
, *atype
;
693 type
= type_allocator (of
).new_type ();
695 type
->set_code (TYPE_CODE_FUNC
);
696 if (ctf_func_type_info (fp
, tid
, &cfi
) < 0)
698 const char *fname
= ctf_type_name_raw (fp
, tid
);
699 error (_("Error getting function type info: %s"),
700 fname
== nullptr ? "noname" : fname
);
702 rettype
= fetch_tid_type (ccp
, cfi
.ctc_return
);
703 type
->set_target_type (rettype
);
704 set_type_align (type
, ctf_type_align (fp
, tid
));
706 /* Set up function's arguments. */
708 type
->set_num_fields (argc
);
709 if ((cfi
.ctc_flags
& CTF_FUNC_VARARG
) != 0)
710 type
->set_has_varargs (true);
714 std::vector
<ctf_id_t
> argv (argc
);
715 if (ctf_func_type_args (fp
, tid
, argc
, argv
.data ()) == CTF_ERR
)
719 ((struct field
*) TYPE_ZALLOC (type
, argc
* sizeof (struct field
)));
720 struct type
*void_type
= objfile_type (of
)->builtin_void
;
721 /* If failed to find the argument type, fill it with void_type. */
722 for (int iparam
= 0; iparam
< argc
; iparam
++)
724 atype
= fetch_tid_type (ccp
, argv
[iparam
]);
725 if (atype
!= nullptr)
726 type
->field (iparam
).set_type (atype
);
728 type
->field (iparam
).set_type (void_type
);
732 return set_tid_type (of
, tid
, type
);
735 /* Given a TID of CTF_K_ENUM, process all the members of the
736 enumeration, and create the symbol for the enumeration type. */
739 read_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
741 struct objfile
*of
= ccp
->of
;
742 ctf_dict_t
*fp
= ccp
->fp
;
745 type
= type_allocator (of
).new_type ();
747 const char *name
= ctf_type_name_raw (fp
, tid
);
748 if (name
!= nullptr && strlen (name
) != 0)
749 type
->set_name (name
);
751 type
->set_code (TYPE_CODE_ENUM
);
752 type
->set_length (ctf_type_size (fp
, tid
));
753 /* Set the underlying type based on its ctf_type_size bits. */
754 type
->set_target_type (objfile_int_type (of
, type
->length (), false));
755 set_type_align (type
, ctf_type_align (fp
, tid
));
757 return set_tid_type (of
, tid
, type
);
761 process_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
764 struct ctf_field_info fi
;
766 type
= read_enum_type (ccp
, tid
);
768 fi
.cur_context
= ccp
;
770 if (ctf_enum_iter (ccp
->fp
, tid
, ctf_add_enum_member_cb
, &fi
) == CTF_ERR
)
771 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
772 ctf_errmsg (ctf_errno (ccp
->fp
)));
774 /* Attach fields to the type. */
775 attach_fields_to_type (&fi
, type
);
777 new_symbol (ccp
, type
, tid
);
780 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
783 add_array_cv_type (struct ctf_context
*ccp
,
785 struct type
*base_type
,
789 struct type
*el_type
, *inner_array
;
791 base_type
= copy_type (base_type
);
792 inner_array
= base_type
;
794 while (inner_array
->target_type ()->code () == TYPE_CODE_ARRAY
)
796 inner_array
->set_target_type (copy_type (inner_array
->target_type ()));
797 inner_array
= inner_array
->target_type ();
800 el_type
= inner_array
->target_type ();
801 cnst
|= TYPE_CONST (el_type
);
802 voltl
|= TYPE_VOLATILE (el_type
);
803 inner_array
->set_target_type (make_cv_type (cnst
, voltl
, el_type
, nullptr));
805 return set_tid_type (ccp
->of
, tid
, base_type
);
808 /* Read all information from a TID of CTF_K_ARRAY. */
811 read_array_type (struct ctf_context
*ccp
, ctf_id_t tid
)
813 struct objfile
*objfile
= ccp
->of
;
814 ctf_dict_t
*fp
= ccp
->fp
;
815 struct type
*element_type
, *range_type
, *idx_type
;
819 if (ctf_array_info (fp
, tid
, &ar
) == CTF_ERR
)
821 complaint (_("ctf_array_info read_array_type failed - %s"),
822 ctf_errmsg (ctf_errno (fp
)));
826 element_type
= fetch_tid_type (ccp
, ar
.ctr_contents
);
827 if (element_type
== nullptr)
830 idx_type
= fetch_tid_type (ccp
, ar
.ctr_index
);
831 if (idx_type
== nullptr)
832 idx_type
= objfile_type (objfile
)->builtin_int
;
834 range_type
= create_static_range_type (NULL
, idx_type
, 0, ar
.ctr_nelems
- 1);
835 type
= create_array_type (NULL
, element_type
, range_type
);
836 if (ar
.ctr_nelems
<= 1) /* Check if undefined upper bound. */
838 range_type
->bounds ()->high
.set_undefined ();
839 type
->set_length (0);
840 type
->set_target_is_stub (true);
843 type
->set_length (ctf_type_size (fp
, tid
));
845 set_type_align (type
, ctf_type_align (fp
, tid
));
847 return set_tid_type (objfile
, tid
, type
);
850 /* Read TID of kind CTF_K_CONST with base type BTID. */
853 read_const_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
855 struct objfile
*objfile
= ccp
->of
;
856 struct type
*base_type
, *cv_type
;
858 base_type
= fetch_tid_type (ccp
, btid
);
859 if (base_type
== nullptr)
861 base_type
= read_type_record (ccp
, btid
);
862 if (base_type
== nullptr)
864 complaint (_("read_const_type: NULL base type (%ld)"), btid
);
865 base_type
= objfile_type (objfile
)->builtin_error
;
868 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
870 return set_tid_type (objfile
, tid
, cv_type
);
873 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
876 read_volatile_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
878 struct objfile
*objfile
= ccp
->of
;
879 ctf_dict_t
*fp
= ccp
->fp
;
880 struct type
*base_type
, *cv_type
;
882 base_type
= fetch_tid_type (ccp
, btid
);
883 if (base_type
== nullptr)
885 base_type
= read_type_record (ccp
, btid
);
886 if (base_type
== nullptr)
888 complaint (_("read_volatile_type: NULL base type (%ld)"), btid
);
889 base_type
= objfile_type (objfile
)->builtin_error
;
893 if (ctf_type_kind (fp
, btid
) == CTF_K_ARRAY
)
894 return add_array_cv_type (ccp
, tid
, base_type
, 0, 1);
895 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
897 return set_tid_type (objfile
, tid
, cv_type
);
900 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
903 read_restrict_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
905 struct objfile
*objfile
= ccp
->of
;
906 struct type
*base_type
, *cv_type
;
908 base_type
= fetch_tid_type (ccp
, btid
);
909 if (base_type
== nullptr)
911 base_type
= read_type_record (ccp
, btid
);
912 if (base_type
== nullptr)
914 complaint (_("read_restrict_type: NULL base type (%ld)"), btid
);
915 base_type
= objfile_type (objfile
)->builtin_error
;
918 cv_type
= make_restrict_type (base_type
);
920 return set_tid_type (objfile
, tid
, cv_type
);
923 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
926 read_typedef_type (struct ctf_context
*ccp
, ctf_id_t tid
,
927 ctf_id_t btid
, const char *name
)
929 struct objfile
*objfile
= ccp
->of
;
930 struct type
*this_type
, *target_type
;
932 char *aname
= obstack_strdup (&objfile
->objfile_obstack
, name
);
933 this_type
= type_allocator (objfile
).new_type (TYPE_CODE_TYPEDEF
, 0, aname
);
934 set_tid_type (objfile
, tid
, this_type
);
935 target_type
= fetch_tid_type (ccp
, btid
);
936 if (target_type
!= this_type
)
937 this_type
->set_target_type (target_type
);
939 this_type
->set_target_type (nullptr);
941 this_type
->set_target_is_stub (this_type
->target_type () != nullptr);
943 return set_tid_type (objfile
, tid
, this_type
);
946 /* Read TID of kind CTF_K_POINTER with base type BTID. */
949 read_pointer_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
951 struct objfile
*of
= ccp
->of
;
952 struct type
*target_type
, *type
;
954 target_type
= fetch_tid_type (ccp
, btid
);
955 if (target_type
== nullptr)
957 target_type
= read_type_record (ccp
, btid
);
958 if (target_type
== nullptr)
960 complaint (_("read_pointer_type: NULL target type (%ld)"), btid
);
961 target_type
= objfile_type (ccp
->of
)->builtin_error
;
965 type
= lookup_pointer_type (target_type
);
966 set_type_align (type
, ctf_type_align (ccp
->fp
, tid
));
968 return set_tid_type (of
, tid
, type
);
971 /* Read information from a TID of CTF_K_FORWARD. */
974 read_forward_type (struct ctf_context
*ccp
, ctf_id_t tid
)
976 struct objfile
*of
= ccp
->of
;
977 ctf_dict_t
*fp
= ccp
->fp
;
981 type
= type_allocator (of
).new_type ();
983 const char *name
= ctf_type_name_raw (fp
, tid
);
984 if (name
!= nullptr && strlen (name
) != 0)
985 type
->set_name (name
);
987 kind
= ctf_type_kind_forwarded (fp
, tid
);
988 if (kind
== CTF_K_UNION
)
989 type
->set_code (TYPE_CODE_UNION
);
991 type
->set_code (TYPE_CODE_STRUCT
);
993 type
->set_length (0);
994 type
->set_is_stub (true);
996 return set_tid_type (of
, tid
, type
);
999 /* Read information associated with type TID. */
1001 static struct type
*
1002 read_type_record (struct ctf_context
*ccp
, ctf_id_t tid
)
1004 ctf_dict_t
*fp
= ccp
->fp
;
1006 struct type
*type
= nullptr;
1009 kind
= ctf_type_kind (fp
, tid
);
1014 type
= read_structure_type (ccp
, tid
);
1017 type
= read_enum_type (ccp
, tid
);
1019 case CTF_K_FUNCTION
:
1020 type
= read_func_kind_type (ccp
, tid
);
1023 btid
= ctf_type_reference (fp
, tid
);
1024 type
= read_const_type (ccp
, tid
, btid
);
1028 const char *name
= ctf_type_name_raw (fp
, tid
);
1029 btid
= ctf_type_reference (fp
, tid
);
1030 type
= read_typedef_type (ccp
, tid
, btid
, name
);
1033 case CTF_K_VOLATILE
:
1034 btid
= ctf_type_reference (fp
, tid
);
1035 type
= read_volatile_type (ccp
, tid
, btid
);
1037 case CTF_K_RESTRICT
:
1038 btid
= ctf_type_reference (fp
, tid
);
1039 type
= read_restrict_type (ccp
, tid
, btid
);
1042 btid
= ctf_type_reference (fp
, tid
);
1043 type
= read_pointer_type (ccp
, tid
, btid
);
1047 type
= read_base_type (ccp
, tid
);
1050 type
= read_array_type (ccp
, tid
);
1053 type
= read_forward_type (ccp
, tid
);
1064 /* Callback to add type TID to the symbol table. */
1067 ctf_add_type_cb (ctf_id_t tid
, void *arg
)
1069 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1073 /* Check if tid's type has already been defined. */
1074 type
= get_tid_type (ccp
->of
, tid
);
1075 if (type
!= nullptr)
1078 ctf_id_t btid
= ctf_type_reference (ccp
->fp
, tid
);
1079 kind
= ctf_type_kind (ccp
->fp
, tid
);
1084 process_structure_type (ccp
, tid
);
1087 process_enum_type (ccp
, tid
);
1089 case CTF_K_FUNCTION
:
1090 type
= read_func_kind_type (ccp
, tid
);
1091 new_symbol (ccp
, type
, tid
);
1095 process_base_type (ccp
, tid
);
1098 new_symbol (ccp
, read_type_record (ccp
, tid
), tid
);
1101 type
= read_const_type (ccp
, tid
, btid
);
1102 new_symbol (ccp
, type
, tid
);
1104 case CTF_K_VOLATILE
:
1105 type
= read_volatile_type (ccp
, tid
, btid
);
1106 new_symbol (ccp
, type
, tid
);
1108 case CTF_K_RESTRICT
:
1109 type
= read_restrict_type (ccp
, tid
, btid
);
1110 new_symbol (ccp
, type
, tid
);
1113 type
= read_pointer_type (ccp
, tid
, btid
);
1114 new_symbol (ccp
, type
, tid
);
1117 type
= read_array_type (ccp
, tid
);
1118 new_symbol (ccp
, type
, tid
);
1129 /* Callback to add variable NAME with TID to the symbol table. */
1132 ctf_add_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1134 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1135 struct symbol
*sym
= nullptr;
1139 type
= get_tid_type (ccp
->of
, id
);
1141 kind
= ctf_type_kind (ccp
->fp
, id
);
1144 case CTF_K_FUNCTION
:
1145 if (name
!= nullptr && strcmp (name
, "main") == 0)
1146 set_objfile_main_name (ccp
->of
, name
, language_c
);
1150 case CTF_K_VOLATILE
:
1151 case CTF_K_RESTRICT
:
1156 if (type
!= nullptr)
1158 sym
= new_symbol (ccp
, type
, id
);
1160 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1166 if (type
== nullptr)
1168 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name
, id
);
1169 type
= objfile_type (ccp
->of
)->builtin_error
;
1171 sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
1172 OBJSTAT (ccp
->of
, n_syms
++);
1173 sym
->set_type (type
);
1174 sym
->set_domain (VAR_DOMAIN
);
1175 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
1176 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1177 add_symbol_to_list (sym
, ccp
->builder
->get_file_symbols ());
1180 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind
);
1185 set_symbol_address (ccp
->of
, sym
, name
);
1190 /* Add entries in either data objects or function info section, controlled
1194 add_stt_entries (struct ctf_context
*ccp
, int functions
)
1196 ctf_next_t
*i
= nullptr;
1199 struct symbol
*sym
= nullptr;
1202 while ((tid
= ctf_symbol_next (ccp
->fp
, &i
, &tname
, functions
)) != CTF_ERR
)
1204 type
= get_tid_type (ccp
->of
, tid
);
1205 if (type
== nullptr)
1207 sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
1208 OBJSTAT (ccp
->of
, n_syms
++);
1209 sym
->set_type (type
);
1210 sym
->set_domain (VAR_DOMAIN
);
1211 sym
->set_aclass_index (LOC_STATIC
);
1212 sym
->compute_and_set_names (tname
, false, ccp
->of
->per_bfd
);
1213 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
1214 set_symbol_address (ccp
->of
, sym
, tname
);
1218 /* Add entries in data objects section. */
1221 add_stt_obj (struct ctf_context
*ccp
)
1223 add_stt_entries (ccp
, 0);
1226 /* Add entries in function info section. */
1229 add_stt_func (struct ctf_context
*ccp
)
1231 add_stt_entries (ccp
, 1);
1234 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1237 get_objfile_text_range (struct objfile
*of
, int *tsize
)
1239 bfd
*abfd
= of
->obfd
.get ();
1240 const asection
*codes
;
1242 codes
= bfd_get_section_by_name (abfd
, ".text");
1243 *tsize
= codes
? bfd_section_size (codes
) : 0;
1244 return of
->text_section_offset ();
1247 /* Start a symtab for OBJFILE in CTF format. */
1250 ctf_start_compunit_symtab (ctf_psymtab
*pst
,
1251 struct objfile
*of
, CORE_ADDR text_offset
)
1253 struct ctf_context
*ccp
;
1255 ccp
= &pst
->context
;
1256 ccp
->builder
= new buildsym_compunit
1257 (of
, pst
->filename
, nullptr,
1258 language_c
, text_offset
);
1259 ccp
->builder
->record_debugformat ("ctf");
1262 /* Finish reading symbol/type definitions in CTF format.
1263 END_ADDR is the end address of the file's text. */
1265 static struct compunit_symtab
*
1266 ctf_end_compunit_symtab (ctf_psymtab
*pst
,
1269 struct ctf_context
*ccp
;
1271 ccp
= &pst
->context
;
1272 struct compunit_symtab
*result
1273 = ccp
->builder
->end_compunit_symtab (end_addr
);
1274 delete ccp
->builder
;
1275 ccp
->builder
= nullptr;
1279 /* Add all members of an enum with type TID to partial symbol table. */
1282 ctf_psymtab_add_enums (struct ctf_context
*ccp
, ctf_id_t tid
)
1286 ctf_next_t
*i
= nullptr;
1288 while ((ename
= ctf_enum_next (ccp
->fp
, tid
, &i
, &val
)) != nullptr)
1290 ccp
->pst
->add_psymbol (ename
, true,
1291 VAR_DOMAIN
, LOC_CONST
, -1,
1292 psymbol_placement::GLOBAL
,
1293 0, language_c
, ccp
->partial_symtabs
, ccp
->of
);
1295 if (ctf_errno (ccp
->fp
) != ECTF_NEXT_END
)
1296 complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
1297 ctf_errmsg (ctf_errno (ccp
->fp
)));
1300 /* Add entries in either data objects or function info section, controlled
1301 by FUNCTIONS, to psymtab. */
1304 ctf_psymtab_add_stt_entries (ctf_dict_t
*cfp
, ctf_psymtab
*pst
,
1305 struct objfile
*of
, int functions
)
1307 ctf_next_t
*i
= nullptr;
1311 while ((tid
= ctf_symbol_next (cfp
, &i
, &tname
, functions
)) != CTF_ERR
)
1313 uint32_t kind
= ctf_type_kind (cfp
, tid
);
1314 address_class aclass
;
1315 domain_enum tdomain
;
1321 tdomain
= STRUCT_DOMAIN
;
1324 tdomain
= VAR_DOMAIN
;
1328 if (kind
== CTF_K_FUNCTION
)
1329 aclass
= LOC_STATIC
;
1330 else if (kind
== CTF_K_CONST
)
1333 aclass
= LOC_TYPEDEF
;
1335 pst
->add_psymbol (tname
, true,
1336 tdomain
, aclass
, -1,
1337 psymbol_placement::GLOBAL
,
1338 0, language_c
, pst
->context
.partial_symtabs
, of
);
1342 /* Add entries in data objects section to psymtab. */
1345 ctf_psymtab_add_stt_obj (ctf_dict_t
*cfp
, ctf_psymtab
*pst
,
1348 ctf_psymtab_add_stt_entries (cfp
, pst
, of
, 0);
1351 /* Add entries in function info section to psymtab. */
1354 ctf_psymtab_add_stt_func (ctf_dict_t
*cfp
, ctf_psymtab
*pst
,
1357 ctf_psymtab_add_stt_entries (cfp
, pst
, of
, 1);
1360 /* Read in full symbols for PST, and anything it depends on. */
1363 ctf_psymtab::expand_psymtab (struct objfile
*objfile
)
1365 struct ctf_context
*ccp
;
1367 gdb_assert (!readin
);
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
)));
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
)));
1382 /* Add entries in data objects and function info sections. */
1389 /* Expand partial symbol table PST into a full symbol table.
1393 ctf_psymtab::read_symtab (struct objfile
*objfile
)
1396 warning (_("bug: psymtab for %s is already read in."), filename
);
1401 gdb_printf (_("Reading in CTF data for %s..."), filename
);
1402 gdb_flush (gdb_stdout
);
1405 /* Start a symtab. */
1406 CORE_ADDR offset
; /* Start of text segment. */
1409 offset
= get_objfile_text_range (objfile
, &tsize
);
1410 ctf_start_compunit_symtab (this, objfile
, offset
);
1411 expand_psymtab (objfile
);
1413 set_text_low (offset
);
1414 set_text_high (offset
+ tsize
);
1415 compunit_symtab
= ctf_end_compunit_symtab (this, offset
+ tsize
);
1417 /* Finish up the debug error message. */
1419 gdb_printf (_("done.\n"));
1423 /* Allocate a new partial_symtab NAME.
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.
1431 Even after the source file has been read into a symtab, the
1432 partial_symtab remains around. They are allocated on an obstack,
1435 static ctf_psymtab
*
1436 create_partial_symtab (const char *name
,
1439 psymtab_storage
*partial_symtabs
,
1440 struct objfile
*objfile
)
1444 pst
= new ctf_psymtab (name
, partial_symtabs
, objfile
->per_bfd
, 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;
1456 /* Callback to add type TID to partial symbol table. */
1459 ctf_psymtab_type_cb (ctf_id_t tid
, void *arg
)
1461 struct ctf_context
*ccp
;
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
);
1473 ctf_psymtab_add_enums (ccp
, tid
);
1477 domain
= STRUCT_DOMAIN
;
1478 aclass
= LOC_TYPEDEF
;
1480 case CTF_K_FUNCTION
:
1482 domain
= VAR_DOMAIN
;
1483 aclass
= LOC_STATIC
;
1484 section
= SECT_OFF_TEXT (ccp
->of
);
1487 domain
= VAR_DOMAIN
;
1488 aclass
= LOC_STATIC
;
1492 case CTF_K_VOLATILE
:
1493 case CTF_K_RESTRICT
:
1494 domain
= VAR_DOMAIN
;
1495 aclass
= LOC_TYPEDEF
;
1499 domain
= VAR_DOMAIN
;
1500 aclass
= LOC_TYPEDEF
;
1507 const char *name
= ctf_type_name_raw (ccp
->fp
, tid
);
1508 if (name
== nullptr || strlen (name
) == 0)
1511 ccp
->pst
->add_psymbol (name
, false,
1512 domain
, aclass
, section
,
1513 psymbol_placement::STATIC
,
1514 0, language_c
, ccp
->partial_symtabs
, ccp
->of
);
1519 /* Callback to add variable NAME with ID to partial symbol table. */
1522 ctf_psymtab_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1524 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1526 ccp
->pst
->add_psymbol (name
, true,
1527 VAR_DOMAIN
, LOC_STATIC
, -1,
1528 psymbol_placement::GLOBAL
,
1529 0, language_c
, ccp
->partial_symtabs
, ccp
->of
);
1533 /* Setup partial_symtab's describing each source file for which
1534 debugging information is available. */
1537 scan_partial_symbols (ctf_dict_t
*cfp
, psymtab_storage
*partial_symtabs
,
1538 struct ctf_per_tu_data
*tup
, const char *fname
)
1540 struct objfile
*of
= tup
->of
;
1541 bool isparent
= false;
1543 if (strcmp (fname
, ".ctf") == 0)
1545 fname
= bfd_get_filename (of
->obfd
.get ());
1549 ctf_psymtab
*pst
= create_partial_symtab (fname
, tup
->arc
, cfp
,
1550 partial_symtabs
, of
);
1552 struct ctf_context
*ccx
= &pst
->context
;
1553 if (isparent
== false)
1556 if (ctf_type_iter (cfp
, ctf_psymtab_type_cb
, ccx
) == CTF_ERR
)
1557 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1558 ctf_errmsg (ctf_errno (cfp
)));
1560 if (ctf_variable_iter (cfp
, ctf_psymtab_var_cb
, ccx
) == CTF_ERR
)
1561 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1562 ctf_errmsg (ctf_errno (cfp
)));
1564 /* Scan CTF object and function sections which correspond to each
1565 STT_FUNC or STT_OBJECT entry in the symbol table,
1566 pick up what init_symtab has done. */
1567 ctf_psymtab_add_stt_obj (cfp
, pst
, of
);
1568 ctf_psymtab_add_stt_func (cfp
, pst
, of
);
1573 /* Callback to build the psymtab for archive member NAME. */
1576 build_ctf_archive_member (ctf_dict_t
*ctf
, const char *name
, void *arg
)
1578 struct ctf_per_tu_data
*tup
= (struct ctf_per_tu_data
*) arg
;
1579 ctf_dict_t
*parent
= tup
->fp
;
1581 if (strcmp (name
, ".ctf") != 0)
1582 ctf_import (ctf
, parent
);
1586 gdb_printf (_("Scanning archive member %s..."), name
);
1587 gdb_flush (gdb_stdout
);
1590 psymtab_storage
*pss
= tup
->psf
->get_partial_symtabs ().get ();
1591 scan_partial_symbols (ctf
, pss
, tup
, name
);
1596 /* Read CTF debugging information from a BFD section. This is
1597 called from elfread.c. It does a quick pass through the
1598 .ctf section to set up the partial symbol table. */
1601 elfctf_build_psymtabs (struct objfile
*of
)
1603 struct ctf_per_tu_data pcu
;
1604 bfd
*abfd
= of
->obfd
.get ();
1607 ctf_archive_t
*arc
= ctf_bfdopen (abfd
, &err
);
1609 error (_("ctf_bfdopen failed on %s - %s"),
1610 bfd_get_filename (abfd
), ctf_errmsg (err
));
1612 ctf_dict_t
*fp
= ctf_dict_open (arc
, NULL
, &err
);
1614 error (_("ctf_dict_open failed on %s - %s"),
1615 bfd_get_filename (abfd
), ctf_errmsg (err
));
1616 ctf_dict_key
.emplace (of
, fp
);
1622 psymbol_functions
*psf
= new psymbol_functions ();
1623 of
->qf
.emplace_front (psf
);
1626 if (ctf_archive_iter (arc
, build_ctf_archive_member
, &pcu
) < 0)
1627 error (_("ctf_archive_iter failed in input file %s: - %s"),
1628 bfd_get_filename (abfd
), ctf_errmsg (err
));
1634 elfctf_build_psymtabs (struct objfile
*of
)
1636 /* Nothing to do if CTF is disabled. */
1639 #endif /* ENABLE_LIBCTF */