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 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"
87 static const struct objfile_key
<htab
, htab_deleter
> ctf_tid_key
;
91 explicit ctf_fp_info (ctf_file_t
*cfp
) : fp (cfp
) {}
96 /* Cleanup function for the ctf_file_key data. */
97 ctf_fp_info::~ctf_fp_info ()
102 ctf_archive_t
*arc
= ctf_get_arc (fp
);
107 static const objfile_key
<ctf_fp_info
> ctf_file_key
;
109 /* A CTF context consists of a file pointer and an objfile pointer. */
115 struct buildsym_compunit
*builder
;
118 /* The routines that read and process fields/members of a C struct, union,
119 or enumeration, pass lists of data member fields in an instance of a
120 ctf_field_info structure. It is derived from dwarf2read.c. */
124 struct field field
{};
127 struct ctf_field_info
129 /* List of data member fields. */
130 std::vector
<struct ctf_nextfield
> fields
;
133 struct ctf_context
*cur_context
;
138 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
139 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
140 std::vector
<struct decl_field
> typedef_field_list
;
142 /* Nested types defined by this struct and the number of elements in
144 std::vector
<struct decl_field
> nested_types_list
;
148 /* Local function prototypes */
150 static void psymtab_to_symtab (struct partial_symtab
*);
152 static int ctf_add_type_cb (ctf_id_t tid
, void *arg
);
154 static struct type
*read_array_type (struct ctf_context
*cp
, ctf_id_t tid
);
156 static struct type
*read_pointer_type (struct ctf_context
*cp
, ctf_id_t tid
,
159 static struct type
*read_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
161 static struct type
*read_enum_type (struct ctf_context
*cp
, ctf_id_t tid
);
163 static struct type
*read_typedef_type (struct ctf_context
*cp
, ctf_id_t tid
,
164 ctf_id_t btid
, const char *name
);
166 static struct type
*read_type_record (struct ctf_context
*cp
, ctf_id_t tid
);
168 static void process_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
170 static void process_struct_members (struct ctf_context
*cp
, ctf_id_t tid
,
173 static struct symbol
*new_symbol (struct ctf_context
*cp
, struct type
*type
,
176 struct ctf_tid_and_type
182 /* Hash function for a ctf_tid_and_type. */
185 tid_and_type_hash (const void *item
)
187 const struct ctf_tid_and_type
*ids
188 = (const struct ctf_tid_and_type
*) item
;
193 /* Equality function for a ctf_tid_and_type. */
196 tid_and_type_eq (const void *item_lhs
, const void *item_rhs
)
198 const struct ctf_tid_and_type
*ids_lhs
199 = (const struct ctf_tid_and_type
*) item_lhs
;
200 const struct ctf_tid_and_type
*ids_rhs
201 = (const struct ctf_tid_and_type
*) item_rhs
;
203 return ids_lhs
->tid
== ids_rhs
->tid
;
206 /* Set the type associated with TID to TYP. */
209 set_tid_type (struct objfile
*of
, ctf_id_t tid
, struct type
*typ
)
213 htab
= (htab_t
) ctf_tid_key
.get (of
);
216 htab
= htab_create_alloc (1, tid_and_type_hash
,
218 NULL
, xcalloc
, xfree
);
219 ctf_tid_key
.set (of
, htab
);
222 struct ctf_tid_and_type
**slot
, ids
;
225 slot
= (struct ctf_tid_and_type
**) htab_find_slot (htab
, &ids
, INSERT
);
227 complaint (_("An internal GDB problem: ctf_ id_t %ld type already set"),
229 *slot
= XOBNEW (&of
->objfile_obstack
, struct ctf_tid_and_type
);
234 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
235 empty or TID does not have a saved type. */
238 get_tid_type (struct objfile
*of
, ctf_id_t tid
)
240 struct ctf_tid_and_type
*slot
, ids
;
243 htab
= (htab_t
) ctf_tid_key
.get (of
);
249 slot
= (struct ctf_tid_and_type
*) htab_find (htab
, &ids
);
256 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
259 get_bitsize (ctf_file_t
*fp
, ctf_id_t tid
, uint32_t kind
)
263 if ((kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
264 || kind
== CTF_K_FLOAT
)
265 && ctf_type_reference (fp
, tid
) != CTF_ERR
266 && ctf_type_encoding (fp
, tid
, &cet
) != CTF_ERR
)
272 /* Set SYM's address, with NAME, from its minimal symbol entry. */
275 set_symbol_address (struct objfile
*of
, struct symbol
*sym
, const char *name
)
277 struct bound_minimal_symbol msym
;
279 msym
= lookup_minimal_symbol (name
, NULL
, of
);
280 if (msym
.minsym
!= NULL
)
282 SET_SYMBOL_VALUE_ADDRESS (sym
, BMSYMBOL_VALUE_ADDRESS (msym
));
283 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
284 SYMBOL_SECTION (sym
) = MSYMBOL_SECTION (msym
.minsym
);
288 /* Create the vector of fields, and attach it to TYPE. */
291 attach_fields_to_type (struct ctf_field_info
*fip
, struct type
*type
)
293 int nfields
= fip
->fields
.size ();
298 /* Record the field count, allocate space for the array of fields. */
299 TYPE_NFIELDS (type
) = nfields
;
301 = (struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
);
303 /* Copy the saved-up fields into the field vector. */
304 for (int i
= 0; i
< nfields
; ++i
)
306 struct ctf_nextfield
&field
= fip
->fields
[i
];
307 TYPE_FIELD (type
, i
) = field
.field
;
311 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
312 (which may be different from NAME) to the architecture back-end to allow
313 it to guess the correct format if necessary. */
316 ctf_init_float_type (struct objfile
*objfile
,
319 const char *name_hint
)
321 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
322 const struct floatformat
**format
;
325 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
327 type
= init_float_type (objfile
, bits
, name
, format
);
329 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
334 /* Callback to add member NAME to a struct/union type. TID is the type
335 of struct/union member, OFFSET is the offset of member in bits,
336 and ARG contains the ctf_field_info. */
339 ctf_add_member_cb (const char *name
,
341 unsigned long offset
,
344 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
345 struct ctf_context
*ccp
= fip
->cur_context
;
346 struct ctf_nextfield new_field
;
351 fp
= &new_field
.field
;
352 FIELD_NAME (*fp
) = name
;
354 kind
= ctf_type_kind (ccp
->fp
, tid
);
355 t
= get_tid_type (ccp
->of
, tid
);
358 t
= read_type_record (ccp
, tid
);
361 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name
, tid
);
362 t
= objfile_type (ccp
->of
)->builtin_error
;
363 set_tid_type (ccp
->of
, tid
, t
);
367 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
368 process_struct_members (ccp
, tid
, t
);
370 FIELD_TYPE (*fp
) = t
;
371 SET_FIELD_BITPOS (*fp
, offset
/ TARGET_CHAR_BIT
);
372 FIELD_BITSIZE (*fp
) = get_bitsize (ccp
->fp
, tid
, kind
);
374 fip
->fields
.emplace_back (new_field
);
379 /* Callback to add member NAME of EVAL to an enumeration type.
380 ARG contains the ctf_field_info. */
383 ctf_add_enum_member_cb (const char *name
, int enum_value
, void *arg
)
385 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
386 struct ctf_nextfield new_field
;
388 struct ctf_context
*ccp
= fip
->cur_context
;
390 fp
= &new_field
.field
;
391 FIELD_NAME (*fp
) = name
;
392 FIELD_TYPE (*fp
) = NULL
;
393 SET_FIELD_ENUMVAL (*fp
, enum_value
);
394 FIELD_BITSIZE (*fp
) = 0;
398 struct symbol
*sym
= allocate_symbol (ccp
->of
);
399 OBJSTAT (ccp
->of
, n_syms
++);
401 sym
->set_language (language_c
, &ccp
->of
->objfile_obstack
);
402 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
403 SYMBOL_ACLASS_INDEX (sym
) = LOC_CONST
;
404 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
405 SYMBOL_TYPE (sym
) = fip
->ptype
;
406 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
409 fip
->fields
.emplace_back (new_field
);
414 /* Add a new symbol entry, with its name from TID, its access index and
415 domain from TID's kind, and its type from TYPE. */
417 static struct symbol
*
418 new_symbol (struct ctf_context
*ccp
, struct type
*type
, ctf_id_t tid
)
420 struct objfile
*objfile
= ccp
->of
;
421 ctf_file_t
*fp
= ccp
->fp
;
422 struct symbol
*sym
= NULL
;
424 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
427 sym
= allocate_symbol (objfile
);
428 OBJSTAT (objfile
, n_syms
++);
430 sym
->set_language (language_c
, &objfile
->objfile_obstack
);
431 sym
->compute_and_set_names (name
.get (), true, objfile
->per_bfd
);
432 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
433 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
436 SYMBOL_TYPE (sym
) = type
;
438 uint32_t kind
= ctf_type_kind (fp
, tid
);
444 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
445 SYMBOL_DOMAIN (sym
) = STRUCT_DOMAIN
;
448 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
451 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_VOID
)
452 SYMBOL_TYPE (sym
) = objfile_type (objfile
)->builtin_int
;
457 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
458 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
471 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
477 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
478 and create the symbol for it. */
481 read_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
483 struct objfile
*of
= ccp
->of
;
484 ctf_file_t
*fp
= ccp
->fp
;
486 struct type
*type
= NULL
;
490 if (ctf_type_encoding (fp
, tid
, &cet
))
492 complaint (_("ctf_type_encoding read_base_type failed - %s"),
493 ctf_errmsg (ctf_errno (fp
)));
497 gdb::unique_xmalloc_ptr
<char> copied_name (ctf_type_aname_raw (fp
, tid
));
498 if (copied_name
== NULL
|| strlen (copied_name
.get ()) == 0)
500 name
= ctf_type_aname (fp
, tid
);
502 complaint (_("ctf_type_aname read_base_type failed - %s"),
503 ctf_errmsg (ctf_errno (fp
)));
506 name
= obstack_strdup (&of
->objfile_obstack
, copied_name
.get ());
508 kind
= ctf_type_kind (fp
, tid
);
509 if (kind
== CTF_K_INTEGER
)
511 uint32_t issigned
, ischar
, isbool
;
512 struct gdbarch
*gdbarch
= get_objfile_arch (of
);
514 issigned
= cet
.cte_format
& CTF_INT_SIGNED
;
515 ischar
= cet
.cte_format
& CTF_INT_CHAR
;
516 isbool
= cet
.cte_format
& CTF_INT_BOOL
;
518 type
= init_character_type (of
, TARGET_CHAR_BIT
, !issigned
, name
);
520 type
= init_boolean_type (of
, gdbarch_int_bit (gdbarch
),
525 if (cet
.cte_bits
&& ((cet
.cte_bits
% TARGET_CHAR_BIT
) == 0))
528 bits
= gdbarch_int_bit (gdbarch
);
529 type
= init_integer_type (of
, bits
, !issigned
, name
);
532 else if (kind
== CTF_K_FLOAT
)
535 isflt
= !((cet
.cte_format
& CTF_FP_IMAGRY
) == CTF_FP_IMAGRY
536 || (cet
.cte_format
& CTF_FP_DIMAGRY
) == CTF_FP_DIMAGRY
537 || (cet
.cte_format
& CTF_FP_LDIMAGRY
) == CTF_FP_LDIMAGRY
);
539 type
= ctf_init_float_type (of
, cet
.cte_bits
, name
, name
);
543 = ctf_init_float_type (of
, cet
.cte_bits
/ 2, NULL
, name
);
544 type
= init_complex_type (of
, name
, t
);
549 complaint (_("read_base_type: unsupported base kind (%d)"), kind
);
550 type
= init_type (of
, TYPE_CODE_ERROR
, cet
.cte_bits
, name
);
553 if (name
!= NULL
&& strcmp (name
, "char") == 0)
554 TYPE_NOSIGN (type
) = 1;
556 return set_tid_type (of
, tid
, type
);
560 process_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
564 type
= read_base_type (ccp
, tid
);
565 new_symbol (ccp
, type
, tid
);
568 /* Start a structure or union scope (definition) with TID to create a type
569 for the structure or union.
571 Fill in the type's name and general properties. The members will not be
572 processed, nor a symbol table entry be done until process_structure_type
573 (assuming the type has a name). */
576 read_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
578 struct objfile
*of
= ccp
->of
;
579 ctf_file_t
*fp
= ccp
->fp
;
583 type
= alloc_type (of
);
585 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
586 if (name
!= NULL
&& strlen (name
.get() ) != 0)
587 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
589 kind
= ctf_type_kind (fp
, tid
);
590 if (kind
== CTF_K_UNION
)
591 TYPE_CODE (type
) = TYPE_CODE_UNION
;
593 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
595 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
596 set_type_align (type
, ctf_type_align (fp
, tid
));
598 return set_tid_type (ccp
->of
, tid
, type
);
601 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
602 and create the symbol for it. */
605 process_struct_members (struct ctf_context
*ccp
,
609 struct ctf_field_info fi
;
611 fi
.cur_context
= ccp
;
612 if (ctf_member_iter (ccp
->fp
, tid
, ctf_add_member_cb
, &fi
) == CTF_ERR
)
613 complaint (_("ctf_member_iter process_struct_members failed - %s"),
614 ctf_errmsg (ctf_errno (ccp
->fp
)));
616 /* Attach fields to the type. */
617 attach_fields_to_type (&fi
, type
);
619 new_symbol (ccp
, type
, tid
);
623 process_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
627 type
= read_structure_type (ccp
, tid
);
628 process_struct_members (ccp
, tid
, type
);
631 /* Create a function type for TID and set its return type. */
634 read_func_kind_type (struct ctf_context
*ccp
, ctf_id_t tid
)
636 struct objfile
*of
= ccp
->of
;
637 ctf_file_t
*fp
= ccp
->fp
;
638 struct type
*type
, *rettype
;
641 type
= alloc_type (of
);
643 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
644 if (name
!= NULL
&& strlen (name
.get ()) != 0)
645 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
647 TYPE_CODE (type
) = TYPE_CODE_FUNC
;
648 ctf_func_type_info (fp
, tid
, &cfi
);
649 rettype
= get_tid_type (of
, cfi
.ctc_return
);
650 TYPE_TARGET_TYPE (type
) = rettype
;
651 set_type_align (type
, ctf_type_align (fp
, tid
));
653 return set_tid_type (of
, tid
, type
);
656 /* Given a TID of CTF_K_ENUM, process all the members of the
657 enumeration, and create the symbol for the enumeration type. */
660 read_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
662 struct objfile
*of
= ccp
->of
;
663 ctf_file_t
*fp
= ccp
->fp
;
664 struct type
*type
, *target_type
;
667 type
= alloc_type (of
);
669 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
670 if (name
!= NULL
&& strlen (name
.get ()) != 0)
671 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
673 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
674 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
675 ctf_func_type_info (fp
, tid
, &fi
);
676 target_type
= get_tid_type (of
, fi
.ctc_return
);
677 TYPE_TARGET_TYPE (type
) = target_type
;
678 set_type_align (type
, ctf_type_align (fp
, tid
));
680 return set_tid_type (of
, tid
, type
);
684 process_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
687 struct ctf_field_info fi
;
689 type
= read_enum_type (ccp
, tid
);
691 fi
.cur_context
= ccp
;
693 if (ctf_enum_iter (ccp
->fp
, tid
, ctf_add_enum_member_cb
, &fi
) == CTF_ERR
)
694 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
695 ctf_errmsg (ctf_errno (ccp
->fp
)));
697 /* Attach fields to the type. */
698 attach_fields_to_type (&fi
, type
);
700 new_symbol (ccp
, type
, tid
);
703 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
706 add_array_cv_type (struct ctf_context
*ccp
,
708 struct type
*base_type
,
712 struct type
*el_type
, *inner_array
;
714 base_type
= copy_type (base_type
);
715 inner_array
= base_type
;
717 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array
)) == TYPE_CODE_ARRAY
)
719 TYPE_TARGET_TYPE (inner_array
)
720 = copy_type (TYPE_TARGET_TYPE (inner_array
));
721 inner_array
= TYPE_TARGET_TYPE (inner_array
);
724 el_type
= TYPE_TARGET_TYPE (inner_array
);
725 cnst
|= TYPE_CONST (el_type
);
726 voltl
|= TYPE_VOLATILE (el_type
);
727 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
729 return set_tid_type (ccp
->of
, tid
, base_type
);
732 /* Read all information from a TID of CTF_K_ARRAY. */
735 read_array_type (struct ctf_context
*ccp
, ctf_id_t tid
)
737 struct objfile
*objfile
= ccp
->of
;
738 ctf_file_t
*fp
= ccp
->fp
;
739 struct type
*element_type
, *range_type
, *idx_type
;
743 if (ctf_array_info (fp
, tid
, &ar
) == CTF_ERR
)
745 complaint (_("ctf_array_info read_array_type failed - %s"),
746 ctf_errmsg (ctf_errno (fp
)));
750 element_type
= get_tid_type (objfile
, ar
.ctr_contents
);
751 if (element_type
== NULL
)
754 idx_type
= get_tid_type (objfile
, ar
.ctr_index
);
755 if (idx_type
== NULL
)
756 idx_type
= objfile_type (objfile
)->builtin_int
;
758 range_type
= create_static_range_type (NULL
, idx_type
, 0, ar
.ctr_nelems
- 1);
759 type
= create_array_type (NULL
, element_type
, range_type
);
760 if (ar
.ctr_nelems
<= 1) /* Check if undefined upper bound. */
762 TYPE_HIGH_BOUND_KIND (range_type
) = PROP_UNDEFINED
;
763 TYPE_LENGTH (type
) = 0;
764 TYPE_TARGET_STUB (type
) = 1;
767 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
769 set_type_align (type
, ctf_type_align (fp
, tid
));
771 return set_tid_type (objfile
, tid
, type
);
774 /* Read TID of kind CTF_K_CONST with base type BTID. */
777 read_const_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
779 struct objfile
*objfile
= ccp
->of
;
780 struct type
*base_type
, *cv_type
;
782 base_type
= get_tid_type (objfile
, btid
);
783 if (base_type
== NULL
)
785 base_type
= read_type_record (ccp
, btid
);
786 if (base_type
== NULL
)
788 complaint (_("read_const_type: NULL base type (%ld)"), btid
);
789 base_type
= objfile_type (objfile
)->builtin_error
;
792 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
794 return set_tid_type (objfile
, tid
, cv_type
);
797 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
800 read_volatile_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
802 struct objfile
*objfile
= ccp
->of
;
803 ctf_file_t
*fp
= ccp
->fp
;
804 struct type
*base_type
, *cv_type
;
806 base_type
= get_tid_type (objfile
, btid
);
807 if (base_type
== NULL
)
809 base_type
= read_type_record (ccp
, btid
);
810 if (base_type
== NULL
)
812 complaint (_("read_volatile_type: NULL base type (%ld)"), btid
);
813 base_type
= objfile_type (objfile
)->builtin_error
;
817 if (ctf_type_kind (fp
, btid
) == CTF_K_ARRAY
)
818 return add_array_cv_type (ccp
, tid
, base_type
, 0, 1);
819 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
821 return set_tid_type (objfile
, tid
, cv_type
);
824 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
827 read_restrict_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
829 struct objfile
*objfile
= ccp
->of
;
830 struct type
*base_type
, *cv_type
;
832 base_type
= get_tid_type (objfile
, btid
);
833 if (base_type
== NULL
)
835 base_type
= read_type_record (ccp
, btid
);
836 if (base_type
== NULL
)
838 complaint (_("read_restrict_type: NULL base type (%ld)"), btid
);
839 base_type
= objfile_type (objfile
)->builtin_error
;
842 cv_type
= make_restrict_type (base_type
);
844 return set_tid_type (objfile
, tid
, cv_type
);
847 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
850 read_typedef_type (struct ctf_context
*ccp
, ctf_id_t tid
,
851 ctf_id_t btid
, const char *name
)
853 struct objfile
*objfile
= ccp
->of
;
854 struct type
*this_type
, *target_type
;
856 char *aname
= obstack_strdup (&objfile
->objfile_obstack
, name
);
857 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, aname
);
858 set_tid_type (objfile
, tid
, this_type
);
859 target_type
= get_tid_type (objfile
, btid
);
860 if (target_type
!= this_type
)
861 TYPE_TARGET_TYPE (this_type
) = target_type
;
863 TYPE_TARGET_TYPE (this_type
) = NULL
;
864 TYPE_TARGET_STUB (this_type
) = TYPE_TARGET_TYPE (this_type
) ? 1 : 0;
866 return set_tid_type (objfile
, tid
, this_type
);
869 /* Read TID of kind CTF_K_POINTER with base type BTID. */
872 read_pointer_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
874 struct objfile
*of
= ccp
->of
;
875 struct type
*target_type
, *type
;
877 target_type
= get_tid_type (of
, btid
);
878 if (target_type
== NULL
)
880 target_type
= read_type_record (ccp
, btid
);
881 if (target_type
== NULL
)
883 complaint (_("read_pointer_type: NULL target type (%ld)"), btid
);
884 target_type
= objfile_type (ccp
->of
)->builtin_error
;
888 type
= lookup_pointer_type (target_type
);
889 set_type_align (type
, ctf_type_align (ccp
->fp
, tid
));
891 return set_tid_type (of
, tid
, type
);
894 /* Read information associated with type TID. */
897 read_type_record (struct ctf_context
*ccp
, ctf_id_t tid
)
899 ctf_file_t
*fp
= ccp
->fp
;
901 struct type
*type
= NULL
;
904 kind
= ctf_type_kind (fp
, tid
);
909 type
= read_structure_type (ccp
, tid
);
912 type
= read_enum_type (ccp
, tid
);
915 type
= read_func_kind_type (ccp
, tid
);
918 btid
= ctf_type_reference (fp
, tid
);
919 type
= read_const_type (ccp
, tid
, btid
);
923 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
924 btid
= ctf_type_reference (fp
, tid
);
925 type
= read_typedef_type (ccp
, tid
, btid
, name
.get ());
929 btid
= ctf_type_reference (fp
, tid
);
930 type
= read_volatile_type (ccp
, tid
, btid
);
933 btid
= ctf_type_reference (fp
, tid
);
934 type
= read_restrict_type (ccp
, tid
, btid
);
937 btid
= ctf_type_reference (fp
, tid
);
938 type
= read_pointer_type (ccp
, tid
, btid
);
942 type
= read_base_type (ccp
, tid
);
945 type
= read_array_type (ccp
, tid
);
956 /* Callback to add type TID to the symbol table. */
959 ctf_add_type_cb (ctf_id_t tid
, void *arg
)
961 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
965 /* Check if tid's type has already been defined. */
966 type
= get_tid_type (ccp
->of
, tid
);
970 ctf_id_t btid
= ctf_type_reference (ccp
->fp
, tid
);
971 kind
= ctf_type_kind (ccp
->fp
, tid
);
976 process_structure_type (ccp
, tid
);
979 process_enum_type (ccp
, tid
);
982 type
= read_func_kind_type (ccp
, tid
);
983 new_symbol (ccp
, type
, tid
);
987 process_base_type (ccp
, tid
);
990 new_symbol (ccp
, read_type_record (ccp
, tid
), tid
);
993 type
= read_const_type (ccp
, tid
, btid
);
994 new_symbol (ccp
, type
, tid
);
997 type
= read_volatile_type (ccp
, tid
, btid
);
998 new_symbol (ccp
, type
, tid
);
1000 case CTF_K_RESTRICT
:
1001 type
= read_restrict_type (ccp
, tid
, btid
);
1002 new_symbol (ccp
, type
, tid
);
1005 type
= read_pointer_type (ccp
, tid
, btid
);
1006 new_symbol (ccp
, type
, tid
);
1009 type
= read_array_type (ccp
, tid
);
1010 new_symbol (ccp
, type
, tid
);
1021 /* Callback to add variable NAME with TID to the symbol table. */
1024 ctf_add_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1026 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1027 struct symbol
*sym
= NULL
;
1031 type
= get_tid_type (ccp
->of
, id
);
1033 kind
= ctf_type_kind (ccp
->fp
, id
);
1036 case CTF_K_FUNCTION
:
1037 if (name
&& !strcmp(name
, "main"))
1038 set_objfile_main_name (ccp
->of
, name
, language_c
);
1042 case CTF_K_VOLATILE
:
1043 case CTF_K_RESTRICT
:
1050 sym
= new_symbol (ccp
, type
, id
);
1051 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1059 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name
, id
);
1060 type
= objfile_type (ccp
->of
)->builtin_error
;
1062 sym
= allocate_symbol (ccp
->of
);
1063 OBJSTAT (ccp
->of
, n_syms
++);
1064 SYMBOL_TYPE (sym
) = type
;
1065 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1066 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
1067 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1068 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
1071 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind
);
1076 set_symbol_address (ccp
->of
, sym
, name
);
1081 /* Add an ELF STT_OBJ symbol with index IDX to the symbol table. */
1083 static struct symbol
*
1084 add_stt_obj (struct ctf_context
*ccp
, unsigned long idx
)
1090 if ((tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
)) == CTF_ERR
)
1093 type
= get_tid_type (ccp
->of
, tid
);
1097 sym
= new_symbol (ccp
, type
, tid
);
1102 /* Add an ELF STT_FUNC symbol with index IDX to the symbol table. */
1104 static struct symbol
*
1105 add_stt_func (struct ctf_context
*ccp
, unsigned long idx
)
1107 struct type
*ftype
, *atyp
, *rettyp
;
1109 ctf_funcinfo_t finfo
;
1113 struct type
*void_type
= objfile_type (ccp
->of
)->builtin_void
;
1115 if (ctf_func_info (ccp
->fp
, idx
, &finfo
) == CTF_ERR
)
1118 argc
= finfo
.ctc_argc
;
1119 if (ctf_func_args (ccp
->fp
, idx
, argc
, argv
) == CTF_ERR
)
1122 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, idx
));
1126 tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
);
1127 ftype
= get_tid_type (ccp
->of
, tid
);
1128 if (finfo
.ctc_flags
& CTF_FUNC_VARARG
)
1129 TYPE_VARARGS (ftype
) = 1;
1130 TYPE_NFIELDS (ftype
) = argc
;
1132 /* If argc is 0, it has a "void" type. */
1135 = (struct field
*) TYPE_ZALLOC (ftype
, argc
* sizeof (struct field
));
1137 /* TYPE_FIELD_TYPE must never be NULL. Fill it with void_type, if failed
1138 to find the argument type. */
1139 for (int iparam
= 0; iparam
< argc
; iparam
++)
1141 atyp
= get_tid_type (ccp
->of
, argv
[iparam
]);
1143 TYPE_FIELD_TYPE (ftype
, iparam
) = atyp
;
1145 TYPE_FIELD_TYPE (ftype
, iparam
) = void_type
;
1148 sym
= new_symbol (ccp
, ftype
, tid
);
1149 rettyp
= get_tid_type (ccp
->of
, finfo
.ctc_return
);
1151 SYMBOL_TYPE (sym
) = rettyp
;
1153 SYMBOL_TYPE (sym
) = void_type
;
1158 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1161 get_objfile_text_range (struct objfile
*of
, int *tsize
)
1163 bfd
*abfd
= of
->obfd
;
1164 const asection
*codes
;
1166 codes
= bfd_get_section_by_name (abfd
, ".text");
1167 *tsize
= codes
? bfd_section_size (codes
) : 0;
1168 return of
->section_offsets
[SECT_OFF_TEXT (of
)];
1171 /* Start a symtab for OBJFILE in CTF format. */
1174 ctf_start_symtab (struct partial_symtab
*pst
,
1175 struct objfile
*of
, CORE_ADDR text_offset
)
1177 struct ctf_context
*ccp
;
1179 ccp
= (struct ctf_context
*) pst
->read_symtab_private
;
1180 ccp
->builder
= new buildsym_compunit
1181 (of
, of
->original_name
, NULL
,
1182 language_c
, text_offset
);
1183 ccp
->builder
->record_debugformat ("ctf");
1186 /* Finish reading symbol/type definitions in CTF format.
1187 END_ADDR is the end address of the file's text. SECTION is
1188 the .text section number. */
1190 static struct compunit_symtab
*
1191 ctf_end_symtab (struct partial_symtab
*pst
,
1192 CORE_ADDR end_addr
, int section
)
1194 struct ctf_context
*ccp
;
1196 ccp
= (struct ctf_context
*) pst
->read_symtab_private
;
1197 struct compunit_symtab
*result
1198 = ccp
->builder
->end_symtab (end_addr
, section
);
1199 delete ccp
->builder
;
1200 ccp
->builder
= NULL
;
1204 /* Read in full symbols for PST, and anything it depends on. */
1207 psymtab_to_symtab (struct partial_symtab
*pst
)
1210 struct ctf_context
*ccp
;
1212 gdb_assert (!pst
->readin
);
1214 ccp
= (struct ctf_context
*) pst
->read_symtab_private
;
1216 /* Iterate over entries in data types section. */
1217 if (ctf_type_iter (ccp
->fp
, ctf_add_type_cb
, ccp
) == CTF_ERR
)
1218 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1219 ctf_errmsg (ctf_errno (ccp
->fp
)));
1222 /* Iterate over entries in variable info section. */
1223 if (ctf_variable_iter (ccp
->fp
, ctf_add_var_cb
, ccp
) == CTF_ERR
)
1224 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1225 ctf_errmsg (ctf_errno (ccp
->fp
)));
1227 /* Add entries in data objects and function info sections. */
1228 for (unsigned long i
= 0; ; i
++)
1230 sym
= add_stt_obj (ccp
, i
);
1233 if (ctf_errno (ccp
->fp
) == EINVAL
1234 || ctf_errno (ccp
->fp
) == ECTF_NOSYMTAB
)
1236 sym
= add_stt_func (ccp
, i
);
1241 set_symbol_address (ccp
->of
, sym
, sym
->linkage_name ());
1247 /* Expand partial symbol table PST into a full symbol table.
1251 ctf_read_symtab (struct partial_symtab
*pst
, struct objfile
*objfile
)
1254 warning (_("bug: psymtab for %s is already read in."), pst
->filename
);
1259 printf_filtered (_("Reading in CTF data for %s..."), pst
->filename
);
1260 gdb_flush (gdb_stdout
);
1263 /* Start a symtab. */
1264 CORE_ADDR text_offset
; /* Start of text segment. */
1267 text_offset
= get_objfile_text_range (objfile
, &tsize
);
1268 ctf_start_symtab (pst
, objfile
, text_offset
);
1269 psymtab_to_symtab (pst
);
1271 pst
->set_text_low (text_offset
);
1272 pst
->set_text_high (text_offset
+ tsize
);
1273 pst
->compunit_symtab
= ctf_end_symtab (pst
, text_offset
+ tsize
,
1274 SECT_OFF_TEXT (objfile
));
1276 /* Finish up the debug error message. */
1278 printf_filtered (_("done.\n"));
1282 /* Allocate a new partial_symtab NAME.
1284 Each source file that has not been fully read in is represented by
1285 a partial_symtab. This contains the information on where in the
1286 executable the debugging symbols for a specific file are, and a
1287 list of names of global symbols which are located in this file.
1288 They are all chained on partial symtab lists.
1290 Even after the source file has been read into a symtab, the
1291 partial_symtab remains around. They are allocated on an obstack,
1294 static struct partial_symtab
*
1295 create_partial_symtab (const char *name
,
1297 struct objfile
*objfile
)
1299 struct partial_symtab
*pst
;
1300 struct ctf_context
*ccx
;
1302 pst
= start_psymtab_common (objfile
, name
, 0);
1304 ccx
= XOBNEW (&objfile
->objfile_obstack
, struct ctf_context
);
1307 pst
->read_symtab_private
= (void *) ccx
;
1308 pst
->read_symtab
= ctf_read_symtab
;
1313 /* Callback to add type TID to partial symbol table. */
1316 ctf_psymtab_type_cb (ctf_id_t tid
, void *arg
)
1318 struct ctf_context
*ccp
;
1322 ccp
= (struct ctf_context
*) arg
;
1323 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, tid
));
1324 if (name
== NULL
|| strlen (name
.get ()) == 0)
1327 domain_enum domain
= UNDEF_DOMAIN
;
1328 enum address_class aclass
= LOC_UNDEF
;
1329 kind
= ctf_type_kind (ccp
->fp
, tid
);
1335 domain
= STRUCT_DOMAIN
;
1336 aclass
= LOC_TYPEDEF
;
1338 case CTF_K_FUNCTION
:
1340 domain
= VAR_DOMAIN
;
1341 aclass
= LOC_STATIC
;
1342 section
= SECT_OFF_TEXT (ccp
->of
);
1345 domain
= VAR_DOMAIN
;
1346 aclass
= LOC_STATIC
;
1350 case CTF_K_VOLATILE
:
1351 case CTF_K_RESTRICT
:
1352 domain
= VAR_DOMAIN
;
1353 aclass
= LOC_TYPEDEF
;
1357 domain
= VAR_DOMAIN
;
1358 aclass
= LOC_TYPEDEF
;
1365 add_psymbol_to_list (name
.get (), true,
1366 domain
, aclass
, section
,
1367 psymbol_placement::GLOBAL
,
1368 0, language_c
, ccp
->of
);
1373 /* Callback to add variable NAME with ID to partial symbol table. */
1376 ctf_psymtab_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1378 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1380 add_psymbol_to_list (name
, true,
1381 VAR_DOMAIN
, LOC_STATIC
, -1,
1382 psymbol_placement::GLOBAL
,
1383 0, language_c
, ccp
->of
);
1387 /* Setup partial_symtab's describing each source file for which
1388 debugging information is available. */
1391 scan_partial_symbols (ctf_file_t
*cfp
, struct objfile
*of
)
1393 struct ctf_context ccx
;
1394 bfd
*abfd
= of
->obfd
;
1395 const char *name
= bfd_get_filename (abfd
);
1396 struct partial_symtab
*pst
= create_partial_symtab (name
, cfp
, of
);
1401 if (ctf_type_iter (cfp
, ctf_psymtab_type_cb
, &ccx
) == CTF_ERR
)
1402 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1403 ctf_errmsg (ctf_errno (cfp
)));
1405 if (ctf_variable_iter (cfp
, ctf_psymtab_var_cb
, &ccx
) == CTF_ERR
)
1406 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1407 ctf_errmsg (ctf_errno (cfp
)));
1409 /* Scan CTF object and function sections which correspond to each
1410 STT_FUNC or STT_OBJECT entry in the symbol table,
1411 pick up what init_symtab has done. */
1412 for (unsigned long idx
= 0; ; idx
++)
1415 if ((tid
= ctf_lookup_by_symbol (cfp
, idx
)) == CTF_ERR
)
1417 if (ctf_errno (cfp
) == EINVAL
|| ctf_errno (cfp
) == ECTF_NOSYMTAB
)
1418 break; // Done, reach end of the section.
1422 gdb::unique_xmalloc_ptr
<char> tname (ctf_type_aname_raw (cfp
, tid
));
1423 uint32_t kind
= ctf_type_kind (cfp
, tid
);
1424 address_class aclass
;
1425 domain_enum tdomain
;
1431 tdomain
= STRUCT_DOMAIN
;
1434 tdomain
= VAR_DOMAIN
;
1438 if (kind
== CTF_K_FUNCTION
)
1439 aclass
= LOC_STATIC
;
1440 else if (kind
== CTF_K_CONST
)
1443 aclass
= LOC_TYPEDEF
;
1445 add_psymbol_to_list (tname
.get (), true,
1446 tdomain
, aclass
, -1,
1447 psymbol_placement::STATIC
,
1451 end_psymtab_common (of
, pst
);
1454 /* Read CTF debugging information from a BFD section. This is
1455 called from elfread.c. It does a quick pass through the
1456 .ctf section to set up the partial symbol table. */
1459 elfctf_build_psymtabs (struct objfile
*of
)
1461 bfd
*abfd
= of
->obfd
;
1464 ctf_archive_t
*arc
= ctf_bfdopen (abfd
, &err
);
1466 error (_("ctf_bfdopen failed on %s - %s"),
1467 bfd_get_filename (abfd
), ctf_errmsg (err
));
1469 ctf_file_t
*fp
= ctf_arc_open_by_name (arc
, NULL
, &err
);
1471 error (_("ctf_arc_open_by_name failed on %s - %s"),
1472 bfd_get_filename (abfd
), ctf_errmsg (err
));
1473 ctf_file_key
.emplace (of
, fp
);
1475 scan_partial_symbols (fp
, of
);
1478 void _initialize_ctfread ();
1480 _initialize_ctfread ()