1 /* Compact ANSI-C Type Format (CTF) support in GDB.
3 Copyright (C) 2019-2020 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 struct 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 objfile_key
<ctf_fp_info
> ctf_dict_key
;
112 /* A CTF context consists of a file pointer and an objfile pointer. */
119 struct buildsym_compunit
*builder
;
122 /* A partial symtab, specialized for this module. */
123 struct ctf_psymtab
: public standard_psymtab
125 ctf_psymtab (const char *filename
, struct objfile
*objfile
, CORE_ADDR addr
)
126 : standard_psymtab (filename
, objfile
, addr
)
130 void read_symtab (struct objfile
*) override
;
131 void expand_psymtab (struct objfile
*) override
;
133 struct ctf_context
*context
;
136 /* The routines that read and process fields/members of a C struct, union,
137 or enumeration, pass lists of data member fields in an instance of a
138 ctf_field_info structure. It is derived from dwarf2read.c. */
142 struct field field
{};
145 struct ctf_field_info
147 /* List of data member fields. */
148 std::vector
<struct ctf_nextfield
> fields
;
151 struct ctf_context
*cur_context
;
156 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
157 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
158 std::vector
<struct decl_field
> typedef_field_list
;
160 /* Nested types defined by this struct and the number of elements in
162 std::vector
<struct decl_field
> nested_types_list
;
166 /* Local function prototypes */
168 static int ctf_add_type_cb (ctf_id_t tid
, void *arg
);
170 static struct type
*read_array_type (struct ctf_context
*cp
, ctf_id_t tid
);
172 static struct type
*read_pointer_type (struct ctf_context
*cp
, ctf_id_t tid
,
175 static struct type
*read_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
177 static struct type
*read_enum_type (struct ctf_context
*cp
, ctf_id_t tid
);
179 static struct type
*read_typedef_type (struct ctf_context
*cp
, ctf_id_t tid
,
180 ctf_id_t btid
, const char *name
);
182 static struct type
*read_type_record (struct ctf_context
*cp
, ctf_id_t tid
);
184 static void process_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
186 static void process_struct_members (struct ctf_context
*cp
, ctf_id_t tid
,
189 static struct symbol
*new_symbol (struct ctf_context
*cp
, struct type
*type
,
192 struct ctf_tid_and_type
198 /* Hash function for a ctf_tid_and_type. */
201 tid_and_type_hash (const void *item
)
203 const struct ctf_tid_and_type
*ids
204 = (const struct ctf_tid_and_type
*) item
;
209 /* Equality function for a ctf_tid_and_type. */
212 tid_and_type_eq (const void *item_lhs
, const void *item_rhs
)
214 const struct ctf_tid_and_type
*ids_lhs
215 = (const struct ctf_tid_and_type
*) item_lhs
;
216 const struct ctf_tid_and_type
*ids_rhs
217 = (const struct ctf_tid_and_type
*) item_rhs
;
219 return ids_lhs
->tid
== ids_rhs
->tid
;
222 /* Set the type associated with TID to TYP. */
225 set_tid_type (struct objfile
*of
, ctf_id_t tid
, struct type
*typ
)
229 htab
= (htab_t
) ctf_tid_key
.get (of
);
232 htab
= htab_create_alloc (1, tid_and_type_hash
,
234 NULL
, xcalloc
, xfree
);
235 ctf_tid_key
.set (of
, htab
);
238 struct ctf_tid_and_type
**slot
, ids
;
241 slot
= (struct ctf_tid_and_type
**) htab_find_slot (htab
, &ids
, INSERT
);
243 complaint (_("An internal GDB problem: ctf_ id_t %ld type already set"),
245 *slot
= XOBNEW (&of
->objfile_obstack
, struct ctf_tid_and_type
);
250 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
251 empty or TID does not have a saved type. */
254 get_tid_type (struct objfile
*of
, ctf_id_t tid
)
256 struct ctf_tid_and_type
*slot
, ids
;
259 htab
= (htab_t
) ctf_tid_key
.get (of
);
265 slot
= (struct ctf_tid_and_type
*) htab_find (htab
, &ids
);
272 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
275 get_bitsize (ctf_dict_t
*fp
, ctf_id_t tid
, uint32_t kind
)
279 if ((kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
280 || kind
== CTF_K_FLOAT
)
281 && ctf_type_reference (fp
, tid
) != CTF_ERR
282 && ctf_type_encoding (fp
, tid
, &cet
) != CTF_ERR
)
288 /* Set SYM's address, with NAME, from its minimal symbol entry. */
291 set_symbol_address (struct objfile
*of
, struct symbol
*sym
, const char *name
)
293 struct bound_minimal_symbol msym
;
295 msym
= lookup_minimal_symbol (name
, NULL
, of
);
296 if (msym
.minsym
!= NULL
)
298 SET_SYMBOL_VALUE_ADDRESS (sym
, BMSYMBOL_VALUE_ADDRESS (msym
));
299 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
300 SYMBOL_SECTION (sym
) = MSYMBOL_SECTION (msym
.minsym
);
304 /* Create the vector of fields, and attach it to TYPE. */
307 attach_fields_to_type (struct ctf_field_info
*fip
, struct type
*type
)
309 int nfields
= fip
->fields
.size ();
314 /* Record the field count, allocate space for the array of fields. */
315 type
->set_num_fields (nfields
);
317 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
319 /* Copy the saved-up fields into the field vector. */
320 for (int i
= 0; i
< nfields
; ++i
)
322 struct ctf_nextfield
&field
= fip
->fields
[i
];
323 type
->field (i
) = field
.field
;
327 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
328 (which may be different from NAME) to the architecture back-end to allow
329 it to guess the correct format if necessary. */
332 ctf_init_float_type (struct objfile
*objfile
,
335 const char *name_hint
)
337 struct gdbarch
*gdbarch
= objfile
->arch ();
338 const struct floatformat
**format
;
341 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
343 type
= init_float_type (objfile
, bits
, name
, format
);
345 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
350 /* Callback to add member NAME to a struct/union type. TID is the type
351 of struct/union member, OFFSET is the offset of member in bits,
352 and ARG contains the ctf_field_info. */
355 ctf_add_member_cb (const char *name
,
357 unsigned long offset
,
360 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
361 struct ctf_context
*ccp
= fip
->cur_context
;
362 struct ctf_nextfield new_field
;
367 fp
= &new_field
.field
;
368 FIELD_NAME (*fp
) = name
;
370 kind
= ctf_type_kind (ccp
->fp
, tid
);
371 t
= get_tid_type (ccp
->of
, tid
);
374 t
= read_type_record (ccp
, tid
);
377 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name
, tid
);
378 t
= objfile_type (ccp
->of
)->builtin_error
;
379 set_tid_type (ccp
->of
, tid
, t
);
383 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
384 process_struct_members (ccp
, tid
, t
);
387 SET_FIELD_BITPOS (*fp
, offset
/ TARGET_CHAR_BIT
);
388 FIELD_BITSIZE (*fp
) = get_bitsize (ccp
->fp
, tid
, kind
);
390 fip
->fields
.emplace_back (new_field
);
395 /* Callback to add member NAME of EVAL to an enumeration type.
396 ARG contains the ctf_field_info. */
399 ctf_add_enum_member_cb (const char *name
, int enum_value
, void *arg
)
401 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
402 struct ctf_nextfield new_field
;
404 struct ctf_context
*ccp
= fip
->cur_context
;
406 fp
= &new_field
.field
;
407 FIELD_NAME (*fp
) = name
;
409 SET_FIELD_ENUMVAL (*fp
, enum_value
);
410 FIELD_BITSIZE (*fp
) = 0;
414 struct symbol
*sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
415 OBJSTAT (ccp
->of
, n_syms
++);
417 sym
->set_language (language_c
, &ccp
->of
->objfile_obstack
);
418 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
419 SYMBOL_ACLASS_INDEX (sym
) = LOC_CONST
;
420 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
421 SYMBOL_TYPE (sym
) = fip
->ptype
;
422 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
425 fip
->fields
.emplace_back (new_field
);
430 /* Add a new symbol entry, with its name from TID, its access index and
431 domain from TID's kind, and its type from TYPE. */
433 static struct symbol
*
434 new_symbol (struct ctf_context
*ccp
, struct type
*type
, ctf_id_t tid
)
436 struct objfile
*objfile
= ccp
->of
;
437 ctf_dict_t
*fp
= ccp
->fp
;
438 struct symbol
*sym
= NULL
;
440 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
443 sym
= new (&objfile
->objfile_obstack
) symbol
;
444 OBJSTAT (objfile
, n_syms
++);
446 sym
->set_language (language_c
, &objfile
->objfile_obstack
);
447 sym
->compute_and_set_names (name
.get (), true, objfile
->per_bfd
);
448 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
449 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
452 SYMBOL_TYPE (sym
) = type
;
454 uint32_t kind
= ctf_type_kind (fp
, tid
);
460 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
461 SYMBOL_DOMAIN (sym
) = STRUCT_DOMAIN
;
464 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
467 if (SYMBOL_TYPE (sym
)->code () == TYPE_CODE_VOID
)
468 SYMBOL_TYPE (sym
) = objfile_type (objfile
)->builtin_int
;
473 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
474 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
487 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
493 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
494 and create the symbol for it. */
497 read_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
499 struct objfile
*of
= ccp
->of
;
500 ctf_dict_t
*fp
= ccp
->fp
;
502 struct type
*type
= NULL
;
506 if (ctf_type_encoding (fp
, tid
, &cet
))
508 complaint (_("ctf_type_encoding read_base_type failed - %s"),
509 ctf_errmsg (ctf_errno (fp
)));
513 gdb::unique_xmalloc_ptr
<char> copied_name (ctf_type_aname_raw (fp
, tid
));
514 if (copied_name
== NULL
|| strlen (copied_name
.get ()) == 0)
516 name
= ctf_type_aname (fp
, tid
);
518 complaint (_("ctf_type_aname read_base_type failed - %s"),
519 ctf_errmsg (ctf_errno (fp
)));
522 name
= obstack_strdup (&of
->objfile_obstack
, copied_name
.get ());
524 kind
= ctf_type_kind (fp
, tid
);
525 if (kind
== CTF_K_INTEGER
)
527 uint32_t issigned
, ischar
, isbool
;
528 struct gdbarch
*gdbarch
= of
->arch ();
530 issigned
= cet
.cte_format
& CTF_INT_SIGNED
;
531 ischar
= cet
.cte_format
& CTF_INT_CHAR
;
532 isbool
= cet
.cte_format
& CTF_INT_BOOL
;
534 type
= init_character_type (of
, TARGET_CHAR_BIT
, !issigned
, name
);
536 type
= init_boolean_type (of
, gdbarch_int_bit (gdbarch
),
541 if (cet
.cte_bits
&& ((cet
.cte_bits
% TARGET_CHAR_BIT
) == 0))
544 bits
= gdbarch_int_bit (gdbarch
);
545 type
= init_integer_type (of
, bits
, !issigned
, name
);
548 else if (kind
== CTF_K_FLOAT
)
551 isflt
= !((cet
.cte_format
& CTF_FP_IMAGRY
) == CTF_FP_IMAGRY
552 || (cet
.cte_format
& CTF_FP_DIMAGRY
) == CTF_FP_DIMAGRY
553 || (cet
.cte_format
& CTF_FP_LDIMAGRY
) == CTF_FP_LDIMAGRY
);
555 type
= ctf_init_float_type (of
, cet
.cte_bits
, name
, name
);
559 = ctf_init_float_type (of
, cet
.cte_bits
/ 2, NULL
, name
);
560 type
= init_complex_type (name
, t
);
565 complaint (_("read_base_type: unsupported base kind (%d)"), kind
);
566 type
= init_type (of
, TYPE_CODE_ERROR
, cet
.cte_bits
, name
);
569 if (name
!= NULL
&& strcmp (name
, "char") == 0)
570 type
->set_has_no_signedness (true);
572 return set_tid_type (of
, tid
, type
);
576 process_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
580 type
= read_base_type (ccp
, tid
);
581 new_symbol (ccp
, type
, tid
);
584 /* Start a structure or union scope (definition) with TID to create a type
585 for the structure or union.
587 Fill in the type's name and general properties. The members will not be
588 processed, nor a symbol table entry be done until process_structure_type
589 (assuming the type has a name). */
592 read_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
594 struct objfile
*of
= ccp
->of
;
595 ctf_dict_t
*fp
= ccp
->fp
;
599 type
= alloc_type (of
);
601 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
602 if (name
!= NULL
&& strlen (name
.get() ) != 0)
603 type
->set_name (obstack_strdup (&of
->objfile_obstack
, name
.get ()));
605 kind
= ctf_type_kind (fp
, tid
);
606 if (kind
== CTF_K_UNION
)
607 type
->set_code (TYPE_CODE_UNION
);
609 type
->set_code (TYPE_CODE_STRUCT
);
611 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
612 set_type_align (type
, ctf_type_align (fp
, tid
));
614 return set_tid_type (ccp
->of
, tid
, type
);
617 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
618 and create the symbol for it. */
621 process_struct_members (struct ctf_context
*ccp
,
625 struct ctf_field_info fi
;
627 fi
.cur_context
= ccp
;
628 if (ctf_member_iter (ccp
->fp
, tid
, ctf_add_member_cb
, &fi
) == CTF_ERR
)
629 complaint (_("ctf_member_iter process_struct_members failed - %s"),
630 ctf_errmsg (ctf_errno (ccp
->fp
)));
632 /* Attach fields to the type. */
633 attach_fields_to_type (&fi
, type
);
635 new_symbol (ccp
, type
, tid
);
639 process_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
643 type
= read_structure_type (ccp
, tid
);
644 process_struct_members (ccp
, tid
, type
);
647 /* Create a function type for TID and set its return type. */
650 read_func_kind_type (struct ctf_context
*ccp
, ctf_id_t tid
)
652 struct objfile
*of
= ccp
->of
;
653 ctf_dict_t
*fp
= ccp
->fp
;
654 struct type
*type
, *rettype
;
657 type
= alloc_type (of
);
659 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
660 if (name
!= NULL
&& strlen (name
.get ()) != 0)
661 type
->set_name (obstack_strdup (&of
->objfile_obstack
, name
.get ()));
663 type
->set_code (TYPE_CODE_FUNC
);
664 ctf_func_type_info (fp
, tid
, &cfi
);
665 rettype
= get_tid_type (of
, cfi
.ctc_return
);
666 TYPE_TARGET_TYPE (type
) = rettype
;
667 set_type_align (type
, ctf_type_align (fp
, tid
));
669 return set_tid_type (of
, tid
, type
);
672 /* Given a TID of CTF_K_ENUM, process all the members of the
673 enumeration, and create the symbol for the enumeration type. */
676 read_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
678 struct objfile
*of
= ccp
->of
;
679 ctf_dict_t
*fp
= ccp
->fp
;
680 struct type
*type
, *target_type
;
683 type
= alloc_type (of
);
685 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
686 if (name
!= NULL
&& strlen (name
.get ()) != 0)
687 type
->set_name (obstack_strdup (&of
->objfile_obstack
, name
.get ()));
689 type
->set_code (TYPE_CODE_ENUM
);
690 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
691 ctf_func_type_info (fp
, tid
, &fi
);
692 target_type
= get_tid_type (of
, fi
.ctc_return
);
693 TYPE_TARGET_TYPE (type
) = target_type
;
694 set_type_align (type
, ctf_type_align (fp
, tid
));
696 return set_tid_type (of
, tid
, type
);
700 process_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
703 struct ctf_field_info fi
;
705 type
= read_enum_type (ccp
, tid
);
707 fi
.cur_context
= ccp
;
709 if (ctf_enum_iter (ccp
->fp
, tid
, ctf_add_enum_member_cb
, &fi
) == CTF_ERR
)
710 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
711 ctf_errmsg (ctf_errno (ccp
->fp
)));
713 /* Attach fields to the type. */
714 attach_fields_to_type (&fi
, type
);
716 new_symbol (ccp
, type
, tid
);
719 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
722 add_array_cv_type (struct ctf_context
*ccp
,
724 struct type
*base_type
,
728 struct type
*el_type
, *inner_array
;
730 base_type
= copy_type (base_type
);
731 inner_array
= base_type
;
733 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
735 TYPE_TARGET_TYPE (inner_array
)
736 = copy_type (TYPE_TARGET_TYPE (inner_array
));
737 inner_array
= TYPE_TARGET_TYPE (inner_array
);
740 el_type
= TYPE_TARGET_TYPE (inner_array
);
741 cnst
|= TYPE_CONST (el_type
);
742 voltl
|= TYPE_VOLATILE (el_type
);
743 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
745 return set_tid_type (ccp
->of
, tid
, base_type
);
748 /* Read all information from a TID of CTF_K_ARRAY. */
751 read_array_type (struct ctf_context
*ccp
, ctf_id_t tid
)
753 struct objfile
*objfile
= ccp
->of
;
754 ctf_dict_t
*fp
= ccp
->fp
;
755 struct type
*element_type
, *range_type
, *idx_type
;
759 if (ctf_array_info (fp
, tid
, &ar
) == CTF_ERR
)
761 complaint (_("ctf_array_info read_array_type failed - %s"),
762 ctf_errmsg (ctf_errno (fp
)));
766 element_type
= get_tid_type (objfile
, ar
.ctr_contents
);
767 if (element_type
== NULL
)
770 idx_type
= get_tid_type (objfile
, ar
.ctr_index
);
771 if (idx_type
== NULL
)
772 idx_type
= objfile_type (objfile
)->builtin_int
;
774 range_type
= create_static_range_type (NULL
, idx_type
, 0, ar
.ctr_nelems
- 1);
775 type
= create_array_type (NULL
, element_type
, range_type
);
776 if (ar
.ctr_nelems
<= 1) /* Check if undefined upper bound. */
778 range_type
->bounds ()->high
.set_undefined ();
779 TYPE_LENGTH (type
) = 0;
780 type
->set_target_is_stub (true);
783 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
785 set_type_align (type
, ctf_type_align (fp
, tid
));
787 return set_tid_type (objfile
, tid
, type
);
790 /* Read TID of kind CTF_K_CONST with base type BTID. */
793 read_const_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
795 struct objfile
*objfile
= ccp
->of
;
796 struct type
*base_type
, *cv_type
;
798 base_type
= get_tid_type (objfile
, btid
);
799 if (base_type
== NULL
)
801 base_type
= read_type_record (ccp
, btid
);
802 if (base_type
== NULL
)
804 complaint (_("read_const_type: NULL base type (%ld)"), btid
);
805 base_type
= objfile_type (objfile
)->builtin_error
;
808 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
810 return set_tid_type (objfile
, tid
, cv_type
);
813 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
816 read_volatile_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
818 struct objfile
*objfile
= ccp
->of
;
819 ctf_dict_t
*fp
= ccp
->fp
;
820 struct type
*base_type
, *cv_type
;
822 base_type
= get_tid_type (objfile
, btid
);
823 if (base_type
== NULL
)
825 base_type
= read_type_record (ccp
, btid
);
826 if (base_type
== NULL
)
828 complaint (_("read_volatile_type: NULL base type (%ld)"), btid
);
829 base_type
= objfile_type (objfile
)->builtin_error
;
833 if (ctf_type_kind (fp
, btid
) == CTF_K_ARRAY
)
834 return add_array_cv_type (ccp
, tid
, base_type
, 0, 1);
835 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
837 return set_tid_type (objfile
, tid
, cv_type
);
840 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
843 read_restrict_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
845 struct objfile
*objfile
= ccp
->of
;
846 struct type
*base_type
, *cv_type
;
848 base_type
= get_tid_type (objfile
, btid
);
849 if (base_type
== NULL
)
851 base_type
= read_type_record (ccp
, btid
);
852 if (base_type
== NULL
)
854 complaint (_("read_restrict_type: NULL base type (%ld)"), btid
);
855 base_type
= objfile_type (objfile
)->builtin_error
;
858 cv_type
= make_restrict_type (base_type
);
860 return set_tid_type (objfile
, tid
, cv_type
);
863 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
866 read_typedef_type (struct ctf_context
*ccp
, ctf_id_t tid
,
867 ctf_id_t btid
, const char *name
)
869 struct objfile
*objfile
= ccp
->of
;
870 struct type
*this_type
, *target_type
;
872 char *aname
= obstack_strdup (&objfile
->objfile_obstack
, name
);
873 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, aname
);
874 set_tid_type (objfile
, tid
, this_type
);
875 target_type
= get_tid_type (objfile
, btid
);
876 if (target_type
!= this_type
)
877 TYPE_TARGET_TYPE (this_type
) = target_type
;
879 TYPE_TARGET_TYPE (this_type
) = NULL
;
881 this_type
->set_target_is_stub (TYPE_TARGET_TYPE (this_type
) != nullptr);
883 return set_tid_type (objfile
, tid
, this_type
);
886 /* Read TID of kind CTF_K_POINTER with base type BTID. */
889 read_pointer_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
891 struct objfile
*of
= ccp
->of
;
892 struct type
*target_type
, *type
;
894 target_type
= get_tid_type (of
, btid
);
895 if (target_type
== NULL
)
897 target_type
= read_type_record (ccp
, btid
);
898 if (target_type
== NULL
)
900 complaint (_("read_pointer_type: NULL target type (%ld)"), btid
);
901 target_type
= objfile_type (ccp
->of
)->builtin_error
;
905 type
= lookup_pointer_type (target_type
);
906 set_type_align (type
, ctf_type_align (ccp
->fp
, tid
));
908 return set_tid_type (of
, tid
, type
);
911 /* Read information associated with type TID. */
914 read_type_record (struct ctf_context
*ccp
, ctf_id_t tid
)
916 ctf_dict_t
*fp
= ccp
->fp
;
918 struct type
*type
= NULL
;
921 kind
= ctf_type_kind (fp
, tid
);
926 type
= read_structure_type (ccp
, tid
);
929 type
= read_enum_type (ccp
, tid
);
932 type
= read_func_kind_type (ccp
, tid
);
935 btid
= ctf_type_reference (fp
, tid
);
936 type
= read_const_type (ccp
, tid
, btid
);
940 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
941 btid
= ctf_type_reference (fp
, tid
);
942 type
= read_typedef_type (ccp
, tid
, btid
, name
.get ());
946 btid
= ctf_type_reference (fp
, tid
);
947 type
= read_volatile_type (ccp
, tid
, btid
);
950 btid
= ctf_type_reference (fp
, tid
);
951 type
= read_restrict_type (ccp
, tid
, btid
);
954 btid
= ctf_type_reference (fp
, tid
);
955 type
= read_pointer_type (ccp
, tid
, btid
);
959 type
= read_base_type (ccp
, tid
);
962 type
= read_array_type (ccp
, tid
);
973 /* Callback to add type TID to the symbol table. */
976 ctf_add_type_cb (ctf_id_t tid
, void *arg
)
978 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
982 /* Check if tid's type has already been defined. */
983 type
= get_tid_type (ccp
->of
, tid
);
987 ctf_id_t btid
= ctf_type_reference (ccp
->fp
, tid
);
988 kind
= ctf_type_kind (ccp
->fp
, tid
);
993 process_structure_type (ccp
, tid
);
996 process_enum_type (ccp
, tid
);
999 type
= read_func_kind_type (ccp
, tid
);
1000 new_symbol (ccp
, type
, tid
);
1004 process_base_type (ccp
, tid
);
1007 new_symbol (ccp
, read_type_record (ccp
, tid
), tid
);
1010 type
= read_const_type (ccp
, tid
, btid
);
1011 new_symbol (ccp
, type
, tid
);
1013 case CTF_K_VOLATILE
:
1014 type
= read_volatile_type (ccp
, tid
, btid
);
1015 new_symbol (ccp
, type
, tid
);
1017 case CTF_K_RESTRICT
:
1018 type
= read_restrict_type (ccp
, tid
, btid
);
1019 new_symbol (ccp
, type
, tid
);
1022 type
= read_pointer_type (ccp
, tid
, btid
);
1023 new_symbol (ccp
, type
, tid
);
1026 type
= read_array_type (ccp
, tid
);
1027 new_symbol (ccp
, type
, tid
);
1038 /* Callback to add variable NAME with TID to the symbol table. */
1041 ctf_add_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1043 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1044 struct symbol
*sym
= NULL
;
1048 type
= get_tid_type (ccp
->of
, id
);
1050 kind
= ctf_type_kind (ccp
->fp
, id
);
1053 case CTF_K_FUNCTION
:
1054 if (name
&& !strcmp(name
, "main"))
1055 set_objfile_main_name (ccp
->of
, name
, language_c
);
1059 case CTF_K_VOLATILE
:
1060 case CTF_K_RESTRICT
:
1067 sym
= new_symbol (ccp
, type
, id
);
1068 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1076 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name
, id
);
1077 type
= objfile_type (ccp
->of
)->builtin_error
;
1079 sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
1080 OBJSTAT (ccp
->of
, n_syms
++);
1081 SYMBOL_TYPE (sym
) = type
;
1082 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1083 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
1084 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1085 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
1088 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind
);
1093 set_symbol_address (ccp
->of
, sym
, name
);
1098 /* Add an ELF STT_OBJ symbol with index IDX to the symbol table. */
1100 static struct symbol
*
1101 add_stt_obj (struct ctf_context
*ccp
, unsigned long idx
)
1107 if ((tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
)) == CTF_ERR
)
1110 type
= get_tid_type (ccp
->of
, tid
);
1114 sym
= new_symbol (ccp
, type
, tid
);
1119 /* Add an ELF STT_FUNC symbol with index IDX to the symbol table. */
1121 static struct symbol
*
1122 add_stt_func (struct ctf_context
*ccp
, unsigned long idx
)
1124 struct type
*ftype
, *atyp
, *rettyp
;
1126 ctf_funcinfo_t finfo
;
1130 struct type
*void_type
= objfile_type (ccp
->of
)->builtin_void
;
1132 if (ctf_func_info (ccp
->fp
, idx
, &finfo
) == CTF_ERR
)
1135 argc
= finfo
.ctc_argc
;
1136 if (ctf_func_args (ccp
->fp
, idx
, argc
, argv
) == CTF_ERR
)
1139 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, idx
));
1143 tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
);
1144 ftype
= get_tid_type (ccp
->of
, tid
);
1145 if (finfo
.ctc_flags
& CTF_FUNC_VARARG
)
1146 ftype
->set_has_varargs (true);
1147 ftype
->set_num_fields (argc
);
1149 /* If argc is 0, it has a "void" type. */
1152 ((struct field
*) TYPE_ZALLOC (ftype
, argc
* sizeof (struct field
)));
1154 /* TYPE_FIELD_TYPE must never be NULL. Fill it with void_type, if failed
1155 to find the argument type. */
1156 for (int iparam
= 0; iparam
< argc
; iparam
++)
1158 atyp
= get_tid_type (ccp
->of
, argv
[iparam
]);
1160 ftype
->field (iparam
).set_type (atyp
);
1162 ftype
->field (iparam
).set_type (void_type
);
1165 sym
= new_symbol (ccp
, ftype
, tid
);
1166 rettyp
= get_tid_type (ccp
->of
, finfo
.ctc_return
);
1168 SYMBOL_TYPE (sym
) = rettyp
;
1170 SYMBOL_TYPE (sym
) = void_type
;
1175 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1178 get_objfile_text_range (struct objfile
*of
, int *tsize
)
1180 bfd
*abfd
= of
->obfd
;
1181 const asection
*codes
;
1183 codes
= bfd_get_section_by_name (abfd
, ".text");
1184 *tsize
= codes
? bfd_section_size (codes
) : 0;
1185 return of
->text_section_offset ();
1188 /* Start a symtab for OBJFILE in CTF format. */
1191 ctf_start_symtab (ctf_psymtab
*pst
,
1192 struct objfile
*of
, CORE_ADDR text_offset
)
1194 struct ctf_context
*ccp
;
1197 ccp
->builder
= new buildsym_compunit
1198 (of
, of
->original_name
, NULL
,
1199 language_c
, text_offset
);
1200 ccp
->builder
->record_debugformat ("ctf");
1203 /* Finish reading symbol/type definitions in CTF format.
1204 END_ADDR is the end address of the file's text. SECTION is
1205 the .text section number. */
1207 static struct compunit_symtab
*
1208 ctf_end_symtab (ctf_psymtab
*pst
,
1209 CORE_ADDR end_addr
, int section
)
1211 struct ctf_context
*ccp
;
1214 struct compunit_symtab
*result
1215 = ccp
->builder
->end_symtab (end_addr
, section
);
1216 delete ccp
->builder
;
1217 ccp
->builder
= NULL
;
1221 /* Read in full symbols for PST, and anything it depends on. */
1224 ctf_psymtab::expand_psymtab (struct objfile
*objfile
)
1227 struct ctf_context
*ccp
;
1229 gdb_assert (!readin
);
1233 /* Iterate over entries in data types section. */
1234 if (ctf_type_iter (ccp
->fp
, ctf_add_type_cb
, ccp
) == CTF_ERR
)
1235 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1236 ctf_errmsg (ctf_errno (ccp
->fp
)));
1239 /* Iterate over entries in variable info section. */
1240 if (ctf_variable_iter (ccp
->fp
, ctf_add_var_cb
, ccp
) == CTF_ERR
)
1241 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1242 ctf_errmsg (ctf_errno (ccp
->fp
)));
1244 /* Add entries in data objects and function info sections. */
1245 for (unsigned long i
= 0; ; i
++)
1247 sym
= add_stt_obj (ccp
, i
);
1250 if (ctf_errno (ccp
->fp
) == EINVAL
1251 || ctf_errno (ccp
->fp
) == ECTF_NOSYMTAB
)
1253 sym
= add_stt_func (ccp
, i
);
1258 set_symbol_address (ccp
->of
, sym
, sym
->linkage_name ());
1264 /* Expand partial symbol table PST into a full symbol table.
1268 ctf_psymtab::read_symtab (struct objfile
*objfile
)
1271 warning (_("bug: psymtab for %s is already read in."), filename
);
1276 printf_filtered (_("Reading in CTF data for %s..."), filename
);
1277 gdb_flush (gdb_stdout
);
1280 /* Start a symtab. */
1281 CORE_ADDR offset
; /* Start of text segment. */
1284 offset
= get_objfile_text_range (objfile
, &tsize
);
1285 ctf_start_symtab (this, objfile
, offset
);
1286 expand_psymtab (objfile
);
1288 set_text_low (offset
);
1289 set_text_high (offset
+ tsize
);
1290 compunit_symtab
= ctf_end_symtab (this, offset
+ tsize
,
1291 SECT_OFF_TEXT (objfile
));
1293 /* Finish up the debug error message. */
1295 printf_filtered (_("done.\n"));
1299 /* Allocate a new partial_symtab NAME.
1301 Each source file that has not been fully read in is represented by
1302 a partial_symtab. This contains the information on where in the
1303 executable the debugging symbols for a specific file are, and a
1304 list of names of global symbols which are located in this file.
1305 They are all chained on partial symtab lists.
1307 Even after the source file has been read into a symtab, the
1308 partial_symtab remains around. They are allocated on an obstack,
1311 static ctf_psymtab
*
1312 create_partial_symtab (const char *name
,
1314 struct objfile
*objfile
)
1317 struct ctf_context
*ccx
;
1319 pst
= new ctf_psymtab (name
, objfile
, 0);
1321 ccx
= XOBNEW (&objfile
->objfile_obstack
, struct ctf_context
);
1325 ccx
->builder
= nullptr;
1331 /* Callback to add type TID to partial symbol table. */
1334 ctf_psymtab_type_cb (ctf_id_t tid
, void *arg
)
1336 struct ctf_context
*ccp
;
1340 ccp
= (struct ctf_context
*) arg
;
1341 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, tid
));
1342 if (name
== NULL
|| strlen (name
.get ()) == 0)
1345 domain_enum domain
= UNDEF_DOMAIN
;
1346 enum address_class aclass
= LOC_UNDEF
;
1347 kind
= ctf_type_kind (ccp
->fp
, tid
);
1353 domain
= STRUCT_DOMAIN
;
1354 aclass
= LOC_TYPEDEF
;
1356 case CTF_K_FUNCTION
:
1358 domain
= VAR_DOMAIN
;
1359 aclass
= LOC_STATIC
;
1360 section
= SECT_OFF_TEXT (ccp
->of
);
1363 domain
= VAR_DOMAIN
;
1364 aclass
= LOC_STATIC
;
1368 case CTF_K_VOLATILE
:
1369 case CTF_K_RESTRICT
:
1370 domain
= VAR_DOMAIN
;
1371 aclass
= LOC_TYPEDEF
;
1375 domain
= VAR_DOMAIN
;
1376 aclass
= LOC_TYPEDEF
;
1383 ccp
->pst
->add_psymbol (name
.get (), true,
1384 domain
, aclass
, section
,
1385 psymbol_placement::GLOBAL
,
1386 0, language_c
, ccp
->of
);
1391 /* Callback to add variable NAME with ID to partial symbol table. */
1394 ctf_psymtab_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1396 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1398 ccp
->pst
->add_psymbol (name
, true,
1399 VAR_DOMAIN
, LOC_STATIC
, -1,
1400 psymbol_placement::GLOBAL
,
1401 0, language_c
, ccp
->of
);
1405 /* Setup partial_symtab's describing each source file for which
1406 debugging information is available. */
1409 scan_partial_symbols (ctf_dict_t
*cfp
, struct objfile
*of
)
1411 bfd
*abfd
= of
->obfd
;
1412 const char *name
= bfd_get_filename (abfd
);
1413 ctf_psymtab
*pst
= create_partial_symtab (name
, cfp
, of
);
1415 struct ctf_context
*ccx
= pst
->context
;
1417 if (ctf_type_iter (cfp
, ctf_psymtab_type_cb
, ccx
) == CTF_ERR
)
1418 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1419 ctf_errmsg (ctf_errno (cfp
)));
1421 if (ctf_variable_iter (cfp
, ctf_psymtab_var_cb
, ccx
) == CTF_ERR
)
1422 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1423 ctf_errmsg (ctf_errno (cfp
)));
1425 /* Scan CTF object and function sections which correspond to each
1426 STT_FUNC or STT_OBJECT entry in the symbol table,
1427 pick up what init_symtab has done. */
1428 for (unsigned long idx
= 0; ; idx
++)
1431 if ((tid
= ctf_lookup_by_symbol (cfp
, idx
)) == CTF_ERR
)
1433 if (ctf_errno (cfp
) == EINVAL
|| ctf_errno (cfp
) == ECTF_NOSYMTAB
)
1434 break; // Done, reach end of the section.
1438 gdb::unique_xmalloc_ptr
<char> tname (ctf_type_aname_raw (cfp
, tid
));
1439 uint32_t kind
= ctf_type_kind (cfp
, tid
);
1440 address_class aclass
;
1441 domain_enum tdomain
;
1447 tdomain
= STRUCT_DOMAIN
;
1450 tdomain
= VAR_DOMAIN
;
1454 if (kind
== CTF_K_FUNCTION
)
1455 aclass
= LOC_STATIC
;
1456 else if (kind
== CTF_K_CONST
)
1459 aclass
= LOC_TYPEDEF
;
1461 pst
->add_psymbol (tname
.get (), true,
1462 tdomain
, aclass
, -1,
1463 psymbol_placement::STATIC
,
1470 /* Read CTF debugging information from a BFD section. This is
1471 called from elfread.c. It does a quick pass through the
1472 .ctf section to set up the partial symbol table. */
1475 elfctf_build_psymtabs (struct objfile
*of
)
1477 bfd
*abfd
= of
->obfd
;
1480 ctf_archive_t
*arc
= ctf_bfdopen (abfd
, &err
);
1482 error (_("ctf_bfdopen failed on %s - %s"),
1483 bfd_get_filename (abfd
), ctf_errmsg (err
));
1485 ctf_dict_t
*fp
= ctf_dict_open (arc
, NULL
, &err
);
1487 error (_("ctf_dict_open failed on %s - %s"),
1488 bfd_get_filename (abfd
), ctf_errmsg (err
));
1489 ctf_dict_key
.emplace (of
, fp
);
1491 scan_partial_symbols (fp
, of
);
1497 elfctf_build_psymtabs (struct objfile
*of
)
1499 /* Nothing to do if CTF is disabled. */
1502 #endif /* ENABLE_LIBCTF */