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_file_t
*cfp
) : fp (cfp
) {}
99 /* Cleanup function for the ctf_file_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_file_key
;
112 /* A CTF context consists of a file pointer and an objfile pointer. */
118 struct buildsym_compunit
*builder
;
121 /* A partial symtab, specialized for this module. */
122 struct ctf_psymtab
: public standard_psymtab
124 ctf_psymtab (const char *filename
, struct objfile
*objfile
, CORE_ADDR addr
)
125 : standard_psymtab (filename
, objfile
, addr
)
129 void read_symtab (struct objfile
*) override
;
130 void expand_psymtab (struct objfile
*) override
;
132 struct ctf_context
*context
;
135 /* The routines that read and process fields/members of a C struct, union,
136 or enumeration, pass lists of data member fields in an instance of a
137 ctf_field_info structure. It is derived from dwarf2read.c. */
141 struct field field
{};
144 struct ctf_field_info
146 /* List of data member fields. */
147 std::vector
<struct ctf_nextfield
> fields
;
150 struct ctf_context
*cur_context
;
155 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
156 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
157 std::vector
<struct decl_field
> typedef_field_list
;
159 /* Nested types defined by this struct and the number of elements in
161 std::vector
<struct decl_field
> nested_types_list
;
165 /* Local function prototypes */
167 static int ctf_add_type_cb (ctf_id_t tid
, void *arg
);
169 static struct type
*read_array_type (struct ctf_context
*cp
, ctf_id_t tid
);
171 static struct type
*read_pointer_type (struct ctf_context
*cp
, ctf_id_t tid
,
174 static struct type
*read_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
176 static struct type
*read_enum_type (struct ctf_context
*cp
, ctf_id_t tid
);
178 static struct type
*read_typedef_type (struct ctf_context
*cp
, ctf_id_t tid
,
179 ctf_id_t btid
, const char *name
);
181 static struct type
*read_type_record (struct ctf_context
*cp
, ctf_id_t tid
);
183 static void process_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
185 static void process_struct_members (struct ctf_context
*cp
, ctf_id_t tid
,
188 static struct symbol
*new_symbol (struct ctf_context
*cp
, struct type
*type
,
191 struct ctf_tid_and_type
197 /* Hash function for a ctf_tid_and_type. */
200 tid_and_type_hash (const void *item
)
202 const struct ctf_tid_and_type
*ids
203 = (const struct ctf_tid_and_type
*) item
;
208 /* Equality function for a ctf_tid_and_type. */
211 tid_and_type_eq (const void *item_lhs
, const void *item_rhs
)
213 const struct ctf_tid_and_type
*ids_lhs
214 = (const struct ctf_tid_and_type
*) item_lhs
;
215 const struct ctf_tid_and_type
*ids_rhs
216 = (const struct ctf_tid_and_type
*) item_rhs
;
218 return ids_lhs
->tid
== ids_rhs
->tid
;
221 /* Set the type associated with TID to TYP. */
224 set_tid_type (struct objfile
*of
, ctf_id_t tid
, struct type
*typ
)
228 htab
= (htab_t
) ctf_tid_key
.get (of
);
231 htab
= htab_create_alloc (1, tid_and_type_hash
,
233 NULL
, xcalloc
, xfree
);
234 ctf_tid_key
.set (of
, htab
);
237 struct ctf_tid_and_type
**slot
, ids
;
240 slot
= (struct ctf_tid_and_type
**) htab_find_slot (htab
, &ids
, INSERT
);
242 complaint (_("An internal GDB problem: ctf_ id_t %ld type already set"),
244 *slot
= XOBNEW (&of
->objfile_obstack
, struct ctf_tid_and_type
);
249 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
250 empty or TID does not have a saved type. */
253 get_tid_type (struct objfile
*of
, ctf_id_t tid
)
255 struct ctf_tid_and_type
*slot
, ids
;
258 htab
= (htab_t
) ctf_tid_key
.get (of
);
264 slot
= (struct ctf_tid_and_type
*) htab_find (htab
, &ids
);
271 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
274 get_bitsize (ctf_file_t
*fp
, ctf_id_t tid
, uint32_t kind
)
278 if ((kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
279 || kind
== CTF_K_FLOAT
)
280 && ctf_type_reference (fp
, tid
) != CTF_ERR
281 && ctf_type_encoding (fp
, tid
, &cet
) != CTF_ERR
)
287 /* Set SYM's address, with NAME, from its minimal symbol entry. */
290 set_symbol_address (struct objfile
*of
, struct symbol
*sym
, const char *name
)
292 struct bound_minimal_symbol msym
;
294 msym
= lookup_minimal_symbol (name
, NULL
, of
);
295 if (msym
.minsym
!= NULL
)
297 SET_SYMBOL_VALUE_ADDRESS (sym
, BMSYMBOL_VALUE_ADDRESS (msym
));
298 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
299 SYMBOL_SECTION (sym
) = MSYMBOL_SECTION (msym
.minsym
);
303 /* Create the vector of fields, and attach it to TYPE. */
306 attach_fields_to_type (struct ctf_field_info
*fip
, struct type
*type
)
308 int nfields
= fip
->fields
.size ();
313 /* Record the field count, allocate space for the array of fields. */
314 type
->set_num_fields (nfields
);
316 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
318 /* Copy the saved-up fields into the field vector. */
319 for (int i
= 0; i
< nfields
; ++i
)
321 struct ctf_nextfield
&field
= fip
->fields
[i
];
322 type
->field (i
) = field
.field
;
326 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
327 (which may be different from NAME) to the architecture back-end to allow
328 it to guess the correct format if necessary. */
331 ctf_init_float_type (struct objfile
*objfile
,
334 const char *name_hint
)
336 struct gdbarch
*gdbarch
= objfile
->arch ();
337 const struct floatformat
**format
;
340 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
342 type
= init_float_type (objfile
, bits
, name
, format
);
344 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
349 /* Callback to add member NAME to a struct/union type. TID is the type
350 of struct/union member, OFFSET is the offset of member in bits,
351 and ARG contains the ctf_field_info. */
354 ctf_add_member_cb (const char *name
,
356 unsigned long offset
,
359 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
360 struct ctf_context
*ccp
= fip
->cur_context
;
361 struct ctf_nextfield new_field
;
366 fp
= &new_field
.field
;
367 FIELD_NAME (*fp
) = name
;
369 kind
= ctf_type_kind (ccp
->fp
, tid
);
370 t
= get_tid_type (ccp
->of
, tid
);
373 t
= read_type_record (ccp
, tid
);
376 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name
, tid
);
377 t
= objfile_type (ccp
->of
)->builtin_error
;
378 set_tid_type (ccp
->of
, tid
, t
);
382 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
383 process_struct_members (ccp
, tid
, t
);
386 SET_FIELD_BITPOS (*fp
, offset
/ TARGET_CHAR_BIT
);
387 FIELD_BITSIZE (*fp
) = get_bitsize (ccp
->fp
, tid
, kind
);
389 fip
->fields
.emplace_back (new_field
);
394 /* Callback to add member NAME of EVAL to an enumeration type.
395 ARG contains the ctf_field_info. */
398 ctf_add_enum_member_cb (const char *name
, int enum_value
, void *arg
)
400 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
401 struct ctf_nextfield new_field
;
403 struct ctf_context
*ccp
= fip
->cur_context
;
405 fp
= &new_field
.field
;
406 FIELD_NAME (*fp
) = name
;
408 SET_FIELD_ENUMVAL (*fp
, enum_value
);
409 FIELD_BITSIZE (*fp
) = 0;
413 struct symbol
*sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
414 OBJSTAT (ccp
->of
, n_syms
++);
416 sym
->set_language (language_c
, &ccp
->of
->objfile_obstack
);
417 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
418 SYMBOL_ACLASS_INDEX (sym
) = LOC_CONST
;
419 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
420 SYMBOL_TYPE (sym
) = fip
->ptype
;
421 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
424 fip
->fields
.emplace_back (new_field
);
429 /* Add a new symbol entry, with its name from TID, its access index and
430 domain from TID's kind, and its type from TYPE. */
432 static struct symbol
*
433 new_symbol (struct ctf_context
*ccp
, struct type
*type
, ctf_id_t tid
)
435 struct objfile
*objfile
= ccp
->of
;
436 ctf_file_t
*fp
= ccp
->fp
;
437 struct symbol
*sym
= NULL
;
439 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
442 sym
= new (&objfile
->objfile_obstack
) symbol
;
443 OBJSTAT (objfile
, n_syms
++);
445 sym
->set_language (language_c
, &objfile
->objfile_obstack
);
446 sym
->compute_and_set_names (name
.get (), true, objfile
->per_bfd
);
447 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
448 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
451 SYMBOL_TYPE (sym
) = type
;
453 uint32_t kind
= ctf_type_kind (fp
, tid
);
459 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
460 SYMBOL_DOMAIN (sym
) = STRUCT_DOMAIN
;
463 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
466 if (SYMBOL_TYPE (sym
)->code () == TYPE_CODE_VOID
)
467 SYMBOL_TYPE (sym
) = objfile_type (objfile
)->builtin_int
;
472 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
473 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
486 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
492 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
493 and create the symbol for it. */
496 read_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
498 struct objfile
*of
= ccp
->of
;
499 ctf_file_t
*fp
= ccp
->fp
;
501 struct type
*type
= NULL
;
505 if (ctf_type_encoding (fp
, tid
, &cet
))
507 complaint (_("ctf_type_encoding read_base_type failed - %s"),
508 ctf_errmsg (ctf_errno (fp
)));
512 gdb::unique_xmalloc_ptr
<char> copied_name (ctf_type_aname_raw (fp
, tid
));
513 if (copied_name
== NULL
|| strlen (copied_name
.get ()) == 0)
515 name
= ctf_type_aname (fp
, tid
);
517 complaint (_("ctf_type_aname read_base_type failed - %s"),
518 ctf_errmsg (ctf_errno (fp
)));
521 name
= obstack_strdup (&of
->objfile_obstack
, copied_name
.get ());
523 kind
= ctf_type_kind (fp
, tid
);
524 if (kind
== CTF_K_INTEGER
)
526 uint32_t issigned
, ischar
, isbool
;
527 struct gdbarch
*gdbarch
= of
->arch ();
529 issigned
= cet
.cte_format
& CTF_INT_SIGNED
;
530 ischar
= cet
.cte_format
& CTF_INT_CHAR
;
531 isbool
= cet
.cte_format
& CTF_INT_BOOL
;
533 type
= init_character_type (of
, TARGET_CHAR_BIT
, !issigned
, name
);
535 type
= init_boolean_type (of
, gdbarch_int_bit (gdbarch
),
540 if (cet
.cte_bits
&& ((cet
.cte_bits
% TARGET_CHAR_BIT
) == 0))
543 bits
= gdbarch_int_bit (gdbarch
);
544 type
= init_integer_type (of
, bits
, !issigned
, name
);
547 else if (kind
== CTF_K_FLOAT
)
550 isflt
= !((cet
.cte_format
& CTF_FP_IMAGRY
) == CTF_FP_IMAGRY
551 || (cet
.cte_format
& CTF_FP_DIMAGRY
) == CTF_FP_DIMAGRY
552 || (cet
.cte_format
& CTF_FP_LDIMAGRY
) == CTF_FP_LDIMAGRY
);
554 type
= ctf_init_float_type (of
, cet
.cte_bits
, name
, name
);
558 = ctf_init_float_type (of
, cet
.cte_bits
/ 2, NULL
, name
);
559 type
= init_complex_type (name
, t
);
564 complaint (_("read_base_type: unsupported base kind (%d)"), kind
);
565 type
= init_type (of
, TYPE_CODE_ERROR
, cet
.cte_bits
, name
);
568 if (name
!= NULL
&& strcmp (name
, "char") == 0)
569 TYPE_NOSIGN (type
) = 1;
571 return set_tid_type (of
, tid
, type
);
575 process_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
579 type
= read_base_type (ccp
, tid
);
580 new_symbol (ccp
, type
, tid
);
583 /* Start a structure or union scope (definition) with TID to create a type
584 for the structure or union.
586 Fill in the type's name and general properties. The members will not be
587 processed, nor a symbol table entry be done until process_structure_type
588 (assuming the type has a name). */
591 read_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
593 struct objfile
*of
= ccp
->of
;
594 ctf_file_t
*fp
= ccp
->fp
;
598 type
= alloc_type (of
);
600 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
601 if (name
!= NULL
&& strlen (name
.get() ) != 0)
602 type
->set_name (obstack_strdup (&of
->objfile_obstack
, name
.get ()));
604 kind
= ctf_type_kind (fp
, tid
);
605 if (kind
== CTF_K_UNION
)
606 type
->set_code (TYPE_CODE_UNION
);
608 type
->set_code (TYPE_CODE_STRUCT
);
610 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
611 set_type_align (type
, ctf_type_align (fp
, tid
));
613 return set_tid_type (ccp
->of
, tid
, type
);
616 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
617 and create the symbol for it. */
620 process_struct_members (struct ctf_context
*ccp
,
624 struct ctf_field_info fi
;
626 fi
.cur_context
= ccp
;
627 if (ctf_member_iter (ccp
->fp
, tid
, ctf_add_member_cb
, &fi
) == CTF_ERR
)
628 complaint (_("ctf_member_iter process_struct_members failed - %s"),
629 ctf_errmsg (ctf_errno (ccp
->fp
)));
631 /* Attach fields to the type. */
632 attach_fields_to_type (&fi
, type
);
634 new_symbol (ccp
, type
, tid
);
638 process_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
642 type
= read_structure_type (ccp
, tid
);
643 process_struct_members (ccp
, tid
, type
);
646 /* Create a function type for TID and set its return type. */
649 read_func_kind_type (struct ctf_context
*ccp
, ctf_id_t tid
)
651 struct objfile
*of
= ccp
->of
;
652 ctf_file_t
*fp
= ccp
->fp
;
653 struct type
*type
, *rettype
;
656 type
= alloc_type (of
);
658 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
659 if (name
!= NULL
&& strlen (name
.get ()) != 0)
660 type
->set_name (obstack_strdup (&of
->objfile_obstack
, name
.get ()));
662 type
->set_code (TYPE_CODE_FUNC
);
663 ctf_func_type_info (fp
, tid
, &cfi
);
664 rettype
= get_tid_type (of
, cfi
.ctc_return
);
665 TYPE_TARGET_TYPE (type
) = rettype
;
666 set_type_align (type
, ctf_type_align (fp
, tid
));
668 return set_tid_type (of
, tid
, type
);
671 /* Given a TID of CTF_K_ENUM, process all the members of the
672 enumeration, and create the symbol for the enumeration type. */
675 read_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
677 struct objfile
*of
= ccp
->of
;
678 ctf_file_t
*fp
= ccp
->fp
;
679 struct type
*type
, *target_type
;
682 type
= alloc_type (of
);
684 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
685 if (name
!= NULL
&& strlen (name
.get ()) != 0)
686 type
->set_name (obstack_strdup (&of
->objfile_obstack
, name
.get ()));
688 type
->set_code (TYPE_CODE_ENUM
);
689 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
690 ctf_func_type_info (fp
, tid
, &fi
);
691 target_type
= get_tid_type (of
, fi
.ctc_return
);
692 TYPE_TARGET_TYPE (type
) = target_type
;
693 set_type_align (type
, ctf_type_align (fp
, tid
));
695 return set_tid_type (of
, tid
, type
);
699 process_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
702 struct ctf_field_info fi
;
704 type
= read_enum_type (ccp
, tid
);
706 fi
.cur_context
= ccp
;
708 if (ctf_enum_iter (ccp
->fp
, tid
, ctf_add_enum_member_cb
, &fi
) == CTF_ERR
)
709 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
710 ctf_errmsg (ctf_errno (ccp
->fp
)));
712 /* Attach fields to the type. */
713 attach_fields_to_type (&fi
, type
);
715 new_symbol (ccp
, type
, tid
);
718 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
721 add_array_cv_type (struct ctf_context
*ccp
,
723 struct type
*base_type
,
727 struct type
*el_type
, *inner_array
;
729 base_type
= copy_type (base_type
);
730 inner_array
= base_type
;
732 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
734 TYPE_TARGET_TYPE (inner_array
)
735 = copy_type (TYPE_TARGET_TYPE (inner_array
));
736 inner_array
= TYPE_TARGET_TYPE (inner_array
);
739 el_type
= TYPE_TARGET_TYPE (inner_array
);
740 cnst
|= TYPE_CONST (el_type
);
741 voltl
|= TYPE_VOLATILE (el_type
);
742 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
744 return set_tid_type (ccp
->of
, tid
, base_type
);
747 /* Read all information from a TID of CTF_K_ARRAY. */
750 read_array_type (struct ctf_context
*ccp
, ctf_id_t tid
)
752 struct objfile
*objfile
= ccp
->of
;
753 ctf_file_t
*fp
= ccp
->fp
;
754 struct type
*element_type
, *range_type
, *idx_type
;
758 if (ctf_array_info (fp
, tid
, &ar
) == CTF_ERR
)
760 complaint (_("ctf_array_info read_array_type failed - %s"),
761 ctf_errmsg (ctf_errno (fp
)));
765 element_type
= get_tid_type (objfile
, ar
.ctr_contents
);
766 if (element_type
== NULL
)
769 idx_type
= get_tid_type (objfile
, ar
.ctr_index
);
770 if (idx_type
== NULL
)
771 idx_type
= objfile_type (objfile
)->builtin_int
;
773 range_type
= create_static_range_type (NULL
, idx_type
, 0, ar
.ctr_nelems
- 1);
774 type
= create_array_type (NULL
, element_type
, range_type
);
775 if (ar
.ctr_nelems
<= 1) /* Check if undefined upper bound. */
777 range_type
->bounds ()->high
.set_undefined ();
778 TYPE_LENGTH (type
) = 0;
779 TYPE_TARGET_STUB (type
) = 1;
782 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
784 set_type_align (type
, ctf_type_align (fp
, tid
));
786 return set_tid_type (objfile
, tid
, type
);
789 /* Read TID of kind CTF_K_CONST with base type BTID. */
792 read_const_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
794 struct objfile
*objfile
= ccp
->of
;
795 struct type
*base_type
, *cv_type
;
797 base_type
= get_tid_type (objfile
, btid
);
798 if (base_type
== NULL
)
800 base_type
= read_type_record (ccp
, btid
);
801 if (base_type
== NULL
)
803 complaint (_("read_const_type: NULL base type (%ld)"), btid
);
804 base_type
= objfile_type (objfile
)->builtin_error
;
807 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
809 return set_tid_type (objfile
, tid
, cv_type
);
812 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
815 read_volatile_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
817 struct objfile
*objfile
= ccp
->of
;
818 ctf_file_t
*fp
= ccp
->fp
;
819 struct type
*base_type
, *cv_type
;
821 base_type
= get_tid_type (objfile
, btid
);
822 if (base_type
== NULL
)
824 base_type
= read_type_record (ccp
, btid
);
825 if (base_type
== NULL
)
827 complaint (_("read_volatile_type: NULL base type (%ld)"), btid
);
828 base_type
= objfile_type (objfile
)->builtin_error
;
832 if (ctf_type_kind (fp
, btid
) == CTF_K_ARRAY
)
833 return add_array_cv_type (ccp
, tid
, base_type
, 0, 1);
834 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
836 return set_tid_type (objfile
, tid
, cv_type
);
839 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
842 read_restrict_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
844 struct objfile
*objfile
= ccp
->of
;
845 struct type
*base_type
, *cv_type
;
847 base_type
= get_tid_type (objfile
, btid
);
848 if (base_type
== NULL
)
850 base_type
= read_type_record (ccp
, btid
);
851 if (base_type
== NULL
)
853 complaint (_("read_restrict_type: NULL base type (%ld)"), btid
);
854 base_type
= objfile_type (objfile
)->builtin_error
;
857 cv_type
= make_restrict_type (base_type
);
859 return set_tid_type (objfile
, tid
, cv_type
);
862 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
865 read_typedef_type (struct ctf_context
*ccp
, ctf_id_t tid
,
866 ctf_id_t btid
, const char *name
)
868 struct objfile
*objfile
= ccp
->of
;
869 struct type
*this_type
, *target_type
;
871 char *aname
= obstack_strdup (&objfile
->objfile_obstack
, name
);
872 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, aname
);
873 set_tid_type (objfile
, tid
, this_type
);
874 target_type
= get_tid_type (objfile
, btid
);
875 if (target_type
!= this_type
)
876 TYPE_TARGET_TYPE (this_type
) = target_type
;
878 TYPE_TARGET_TYPE (this_type
) = NULL
;
879 TYPE_TARGET_STUB (this_type
) = TYPE_TARGET_TYPE (this_type
) ? 1 : 0;
881 return set_tid_type (objfile
, tid
, this_type
);
884 /* Read TID of kind CTF_K_POINTER with base type BTID. */
887 read_pointer_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
889 struct objfile
*of
= ccp
->of
;
890 struct type
*target_type
, *type
;
892 target_type
= get_tid_type (of
, btid
);
893 if (target_type
== NULL
)
895 target_type
= read_type_record (ccp
, btid
);
896 if (target_type
== NULL
)
898 complaint (_("read_pointer_type: NULL target type (%ld)"), btid
);
899 target_type
= objfile_type (ccp
->of
)->builtin_error
;
903 type
= lookup_pointer_type (target_type
);
904 set_type_align (type
, ctf_type_align (ccp
->fp
, tid
));
906 return set_tid_type (of
, tid
, type
);
909 /* Read information associated with type TID. */
912 read_type_record (struct ctf_context
*ccp
, ctf_id_t tid
)
914 ctf_file_t
*fp
= ccp
->fp
;
916 struct type
*type
= NULL
;
919 kind
= ctf_type_kind (fp
, tid
);
924 type
= read_structure_type (ccp
, tid
);
927 type
= read_enum_type (ccp
, tid
);
930 type
= read_func_kind_type (ccp
, tid
);
933 btid
= ctf_type_reference (fp
, tid
);
934 type
= read_const_type (ccp
, tid
, btid
);
938 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
939 btid
= ctf_type_reference (fp
, tid
);
940 type
= read_typedef_type (ccp
, tid
, btid
, name
.get ());
944 btid
= ctf_type_reference (fp
, tid
);
945 type
= read_volatile_type (ccp
, tid
, btid
);
948 btid
= ctf_type_reference (fp
, tid
);
949 type
= read_restrict_type (ccp
, tid
, btid
);
952 btid
= ctf_type_reference (fp
, tid
);
953 type
= read_pointer_type (ccp
, tid
, btid
);
957 type
= read_base_type (ccp
, tid
);
960 type
= read_array_type (ccp
, tid
);
971 /* Callback to add type TID to the symbol table. */
974 ctf_add_type_cb (ctf_id_t tid
, void *arg
)
976 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
980 /* Check if tid's type has already been defined. */
981 type
= get_tid_type (ccp
->of
, tid
);
985 ctf_id_t btid
= ctf_type_reference (ccp
->fp
, tid
);
986 kind
= ctf_type_kind (ccp
->fp
, tid
);
991 process_structure_type (ccp
, tid
);
994 process_enum_type (ccp
, tid
);
997 type
= read_func_kind_type (ccp
, tid
);
998 new_symbol (ccp
, type
, tid
);
1002 process_base_type (ccp
, tid
);
1005 new_symbol (ccp
, read_type_record (ccp
, tid
), tid
);
1008 type
= read_const_type (ccp
, tid
, btid
);
1009 new_symbol (ccp
, type
, tid
);
1011 case CTF_K_VOLATILE
:
1012 type
= read_volatile_type (ccp
, tid
, btid
);
1013 new_symbol (ccp
, type
, tid
);
1015 case CTF_K_RESTRICT
:
1016 type
= read_restrict_type (ccp
, tid
, btid
);
1017 new_symbol (ccp
, type
, tid
);
1020 type
= read_pointer_type (ccp
, tid
, btid
);
1021 new_symbol (ccp
, type
, tid
);
1024 type
= read_array_type (ccp
, tid
);
1025 new_symbol (ccp
, type
, tid
);
1036 /* Callback to add variable NAME with TID to the symbol table. */
1039 ctf_add_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1041 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1042 struct symbol
*sym
= NULL
;
1046 type
= get_tid_type (ccp
->of
, id
);
1048 kind
= ctf_type_kind (ccp
->fp
, id
);
1051 case CTF_K_FUNCTION
:
1052 if (name
&& !strcmp(name
, "main"))
1053 set_objfile_main_name (ccp
->of
, name
, language_c
);
1057 case CTF_K_VOLATILE
:
1058 case CTF_K_RESTRICT
:
1065 sym
= new_symbol (ccp
, type
, id
);
1066 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1074 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name
, id
);
1075 type
= objfile_type (ccp
->of
)->builtin_error
;
1077 sym
= new (&ccp
->of
->objfile_obstack
) symbol
;
1078 OBJSTAT (ccp
->of
, n_syms
++);
1079 SYMBOL_TYPE (sym
) = type
;
1080 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1081 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
1082 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1083 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
1086 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind
);
1091 set_symbol_address (ccp
->of
, sym
, name
);
1096 /* Add an ELF STT_OBJ symbol with index IDX to the symbol table. */
1098 static struct symbol
*
1099 add_stt_obj (struct ctf_context
*ccp
, unsigned long idx
)
1105 if ((tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
)) == CTF_ERR
)
1108 type
= get_tid_type (ccp
->of
, tid
);
1112 sym
= new_symbol (ccp
, type
, tid
);
1117 /* Add an ELF STT_FUNC symbol with index IDX to the symbol table. */
1119 static struct symbol
*
1120 add_stt_func (struct ctf_context
*ccp
, unsigned long idx
)
1122 struct type
*ftype
, *atyp
, *rettyp
;
1124 ctf_funcinfo_t finfo
;
1128 struct type
*void_type
= objfile_type (ccp
->of
)->builtin_void
;
1130 if (ctf_func_info (ccp
->fp
, idx
, &finfo
) == CTF_ERR
)
1133 argc
= finfo
.ctc_argc
;
1134 if (ctf_func_args (ccp
->fp
, idx
, argc
, argv
) == CTF_ERR
)
1137 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, idx
));
1141 tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
);
1142 ftype
= get_tid_type (ccp
->of
, tid
);
1143 if (finfo
.ctc_flags
& CTF_FUNC_VARARG
)
1144 TYPE_VARARGS (ftype
) = 1;
1145 ftype
->set_num_fields (argc
);
1147 /* If argc is 0, it has a "void" type. */
1150 ((struct field
*) TYPE_ZALLOC (ftype
, argc
* sizeof (struct field
)));
1152 /* TYPE_FIELD_TYPE must never be NULL. Fill it with void_type, if failed
1153 to find the argument type. */
1154 for (int iparam
= 0; iparam
< argc
; iparam
++)
1156 atyp
= get_tid_type (ccp
->of
, argv
[iparam
]);
1158 ftype
->field (iparam
).set_type (atyp
);
1160 ftype
->field (iparam
).set_type (void_type
);
1163 sym
= new_symbol (ccp
, ftype
, tid
);
1164 rettyp
= get_tid_type (ccp
->of
, finfo
.ctc_return
);
1166 SYMBOL_TYPE (sym
) = rettyp
;
1168 SYMBOL_TYPE (sym
) = void_type
;
1173 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1176 get_objfile_text_range (struct objfile
*of
, int *tsize
)
1178 bfd
*abfd
= of
->obfd
;
1179 const asection
*codes
;
1181 codes
= bfd_get_section_by_name (abfd
, ".text");
1182 *tsize
= codes
? bfd_section_size (codes
) : 0;
1183 return of
->text_section_offset ();
1186 /* Start a symtab for OBJFILE in CTF format. */
1189 ctf_start_symtab (ctf_psymtab
*pst
,
1190 struct objfile
*of
, CORE_ADDR text_offset
)
1192 struct ctf_context
*ccp
;
1195 ccp
->builder
= new buildsym_compunit
1196 (of
, of
->original_name
, NULL
,
1197 language_c
, text_offset
);
1198 ccp
->builder
->record_debugformat ("ctf");
1201 /* Finish reading symbol/type definitions in CTF format.
1202 END_ADDR is the end address of the file's text. SECTION is
1203 the .text section number. */
1205 static struct compunit_symtab
*
1206 ctf_end_symtab (ctf_psymtab
*pst
,
1207 CORE_ADDR end_addr
, int section
)
1209 struct ctf_context
*ccp
;
1212 struct compunit_symtab
*result
1213 = ccp
->builder
->end_symtab (end_addr
, section
);
1214 delete ccp
->builder
;
1215 ccp
->builder
= NULL
;
1219 /* Read in full symbols for PST, and anything it depends on. */
1222 ctf_psymtab::expand_psymtab (struct objfile
*objfile
)
1225 struct ctf_context
*ccp
;
1227 gdb_assert (!readin
);
1231 /* Iterate over entries in data types section. */
1232 if (ctf_type_iter (ccp
->fp
, ctf_add_type_cb
, ccp
) == CTF_ERR
)
1233 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1234 ctf_errmsg (ctf_errno (ccp
->fp
)));
1237 /* Iterate over entries in variable info section. */
1238 if (ctf_variable_iter (ccp
->fp
, ctf_add_var_cb
, ccp
) == CTF_ERR
)
1239 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1240 ctf_errmsg (ctf_errno (ccp
->fp
)));
1242 /* Add entries in data objects and function info sections. */
1243 for (unsigned long i
= 0; ; i
++)
1245 sym
= add_stt_obj (ccp
, i
);
1248 if (ctf_errno (ccp
->fp
) == EINVAL
1249 || ctf_errno (ccp
->fp
) == ECTF_NOSYMTAB
)
1251 sym
= add_stt_func (ccp
, i
);
1256 set_symbol_address (ccp
->of
, sym
, sym
->linkage_name ());
1262 /* Expand partial symbol table PST into a full symbol table.
1266 ctf_psymtab::read_symtab (struct objfile
*objfile
)
1269 warning (_("bug: psymtab for %s is already read in."), filename
);
1274 printf_filtered (_("Reading in CTF data for %s..."), filename
);
1275 gdb_flush (gdb_stdout
);
1278 /* Start a symtab. */
1279 CORE_ADDR offset
; /* Start of text segment. */
1282 offset
= get_objfile_text_range (objfile
, &tsize
);
1283 ctf_start_symtab (this, objfile
, offset
);
1284 expand_psymtab (objfile
);
1286 set_text_low (offset
);
1287 set_text_high (offset
+ tsize
);
1288 compunit_symtab
= ctf_end_symtab (this, offset
+ tsize
,
1289 SECT_OFF_TEXT (objfile
));
1291 /* Finish up the debug error message. */
1293 printf_filtered (_("done.\n"));
1297 /* Allocate a new partial_symtab NAME.
1299 Each source file that has not been fully read in is represented by
1300 a partial_symtab. This contains the information on where in the
1301 executable the debugging symbols for a specific file are, and a
1302 list of names of global symbols which are located in this file.
1303 They are all chained on partial symtab lists.
1305 Even after the source file has been read into a symtab, the
1306 partial_symtab remains around. They are allocated on an obstack,
1309 static ctf_psymtab
*
1310 create_partial_symtab (const char *name
,
1312 struct objfile
*objfile
)
1315 struct ctf_context
*ccx
;
1317 pst
= new ctf_psymtab (name
, objfile
, 0);
1319 ccx
= XOBNEW (&objfile
->objfile_obstack
, struct ctf_context
);
1327 /* Callback to add type TID to partial symbol table. */
1330 ctf_psymtab_type_cb (ctf_id_t tid
, void *arg
)
1332 struct ctf_context
*ccp
;
1336 ccp
= (struct ctf_context
*) arg
;
1337 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, tid
));
1338 if (name
== NULL
|| strlen (name
.get ()) == 0)
1341 domain_enum domain
= UNDEF_DOMAIN
;
1342 enum address_class aclass
= LOC_UNDEF
;
1343 kind
= ctf_type_kind (ccp
->fp
, tid
);
1349 domain
= STRUCT_DOMAIN
;
1350 aclass
= LOC_TYPEDEF
;
1352 case CTF_K_FUNCTION
:
1354 domain
= VAR_DOMAIN
;
1355 aclass
= LOC_STATIC
;
1356 section
= SECT_OFF_TEXT (ccp
->of
);
1359 domain
= VAR_DOMAIN
;
1360 aclass
= LOC_STATIC
;
1364 case CTF_K_VOLATILE
:
1365 case CTF_K_RESTRICT
:
1366 domain
= VAR_DOMAIN
;
1367 aclass
= LOC_TYPEDEF
;
1371 domain
= VAR_DOMAIN
;
1372 aclass
= LOC_TYPEDEF
;
1379 add_psymbol_to_list (name
.get (), true,
1380 domain
, aclass
, section
,
1381 psymbol_placement::GLOBAL
,
1382 0, language_c
, ccp
->of
);
1387 /* Callback to add variable NAME with ID to partial symbol table. */
1390 ctf_psymtab_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1392 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1394 add_psymbol_to_list (name
, true,
1395 VAR_DOMAIN
, LOC_STATIC
, -1,
1396 psymbol_placement::GLOBAL
,
1397 0, language_c
, ccp
->of
);
1401 /* Setup partial_symtab's describing each source file for which
1402 debugging information is available. */
1405 scan_partial_symbols (ctf_file_t
*cfp
, struct objfile
*of
)
1407 struct ctf_context ccx
;
1408 bfd
*abfd
= of
->obfd
;
1409 const char *name
= bfd_get_filename (abfd
);
1410 ctf_psymtab
*pst
= create_partial_symtab (name
, cfp
, of
);
1415 if (ctf_type_iter (cfp
, ctf_psymtab_type_cb
, &ccx
) == CTF_ERR
)
1416 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1417 ctf_errmsg (ctf_errno (cfp
)));
1419 if (ctf_variable_iter (cfp
, ctf_psymtab_var_cb
, &ccx
) == CTF_ERR
)
1420 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1421 ctf_errmsg (ctf_errno (cfp
)));
1423 /* Scan CTF object and function sections which correspond to each
1424 STT_FUNC or STT_OBJECT entry in the symbol table,
1425 pick up what init_symtab has done. */
1426 for (unsigned long idx
= 0; ; idx
++)
1429 if ((tid
= ctf_lookup_by_symbol (cfp
, idx
)) == CTF_ERR
)
1431 if (ctf_errno (cfp
) == EINVAL
|| ctf_errno (cfp
) == ECTF_NOSYMTAB
)
1432 break; // Done, reach end of the section.
1436 gdb::unique_xmalloc_ptr
<char> tname (ctf_type_aname_raw (cfp
, tid
));
1437 uint32_t kind
= ctf_type_kind (cfp
, tid
);
1438 address_class aclass
;
1439 domain_enum tdomain
;
1445 tdomain
= STRUCT_DOMAIN
;
1448 tdomain
= VAR_DOMAIN
;
1452 if (kind
== CTF_K_FUNCTION
)
1453 aclass
= LOC_STATIC
;
1454 else if (kind
== CTF_K_CONST
)
1457 aclass
= LOC_TYPEDEF
;
1459 add_psymbol_to_list (tname
.get (), true,
1460 tdomain
, aclass
, -1,
1461 psymbol_placement::STATIC
,
1465 end_psymtab_common (of
, pst
);
1468 /* Read CTF debugging information from a BFD section. This is
1469 called from elfread.c. It does a quick pass through the
1470 .ctf section to set up the partial symbol table. */
1473 elfctf_build_psymtabs (struct objfile
*of
)
1475 bfd
*abfd
= of
->obfd
;
1478 ctf_archive_t
*arc
= ctf_bfdopen (abfd
, &err
);
1480 error (_("ctf_bfdopen failed on %s - %s"),
1481 bfd_get_filename (abfd
), ctf_errmsg (err
));
1483 ctf_file_t
*fp
= ctf_arc_open_by_name (arc
, NULL
, &err
);
1485 error (_("ctf_arc_open_by_name failed on %s - %s"),
1486 bfd_get_filename (abfd
), ctf_errmsg (err
));
1487 ctf_file_key
.emplace (of
, fp
);
1489 scan_partial_symbols (fp
, of
);
1495 elfctf_build_psymtabs (struct objfile
*of
)
1497 /* Nothing to do if CTF is disabled. */
1500 #endif /* ENABLE_LIBCTF */