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 /* A partial symtab, specialized for this module. */
119 struct ctf_psymtab
: public partial_symtab
121 ctf_psymtab (const char *filename
, struct objfile
*objfile
, CORE_ADDR addr
)
122 : partial_symtab (filename
, objfile
, addr
)
126 void read_symtab (struct objfile
*) override
;
128 struct ctf_context
*context
;
131 /* The routines that read and process fields/members of a C struct, union,
132 or enumeration, pass lists of data member fields in an instance of a
133 ctf_field_info structure. It is derived from dwarf2read.c. */
137 struct field field
{};
140 struct ctf_field_info
142 /* List of data member fields. */
143 std::vector
<struct ctf_nextfield
> fields
;
146 struct ctf_context
*cur_context
;
151 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
152 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
153 std::vector
<struct decl_field
> typedef_field_list
;
155 /* Nested types defined by this struct and the number of elements in
157 std::vector
<struct decl_field
> nested_types_list
;
161 /* Local function prototypes */
163 static void psymtab_to_symtab (ctf_psymtab
*);
165 static int ctf_add_type_cb (ctf_id_t tid
, void *arg
);
167 static struct type
*read_array_type (struct ctf_context
*cp
, ctf_id_t tid
);
169 static struct type
*read_pointer_type (struct ctf_context
*cp
, ctf_id_t tid
,
172 static struct type
*read_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
174 static struct type
*read_enum_type (struct ctf_context
*cp
, ctf_id_t tid
);
176 static struct type
*read_typedef_type (struct ctf_context
*cp
, ctf_id_t tid
,
177 ctf_id_t btid
, const char *name
);
179 static struct type
*read_type_record (struct ctf_context
*cp
, ctf_id_t tid
);
181 static void process_structure_type (struct ctf_context
*cp
, ctf_id_t tid
);
183 static void process_struct_members (struct ctf_context
*cp
, ctf_id_t tid
,
186 static struct symbol
*new_symbol (struct ctf_context
*cp
, struct type
*type
,
189 struct ctf_tid_and_type
195 /* Hash function for a ctf_tid_and_type. */
198 tid_and_type_hash (const void *item
)
200 const struct ctf_tid_and_type
*ids
201 = (const struct ctf_tid_and_type
*) item
;
206 /* Equality function for a ctf_tid_and_type. */
209 tid_and_type_eq (const void *item_lhs
, const void *item_rhs
)
211 const struct ctf_tid_and_type
*ids_lhs
212 = (const struct ctf_tid_and_type
*) item_lhs
;
213 const struct ctf_tid_and_type
*ids_rhs
214 = (const struct ctf_tid_and_type
*) item_rhs
;
216 return ids_lhs
->tid
== ids_rhs
->tid
;
219 /* Set the type associated with TID to TYP. */
222 set_tid_type (struct objfile
*of
, ctf_id_t tid
, struct type
*typ
)
226 htab
= (htab_t
) ctf_tid_key
.get (of
);
229 htab
= htab_create_alloc (1, tid_and_type_hash
,
231 NULL
, xcalloc
, xfree
);
232 ctf_tid_key
.set (of
, htab
);
235 struct ctf_tid_and_type
**slot
, ids
;
238 slot
= (struct ctf_tid_and_type
**) htab_find_slot (htab
, &ids
, INSERT
);
240 complaint (_("An internal GDB problem: ctf_ id_t %ld type already set"),
242 *slot
= XOBNEW (&of
->objfile_obstack
, struct ctf_tid_and_type
);
247 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
248 empty or TID does not have a saved type. */
251 get_tid_type (struct objfile
*of
, ctf_id_t tid
)
253 struct ctf_tid_and_type
*slot
, ids
;
256 htab
= (htab_t
) ctf_tid_key
.get (of
);
262 slot
= (struct ctf_tid_and_type
*) htab_find (htab
, &ids
);
269 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
272 get_bitsize (ctf_file_t
*fp
, ctf_id_t tid
, uint32_t kind
)
276 if ((kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
277 || kind
== CTF_K_FLOAT
)
278 && ctf_type_reference (fp
, tid
) != CTF_ERR
279 && ctf_type_encoding (fp
, tid
, &cet
) != CTF_ERR
)
285 /* Set SYM's address, with NAME, from its minimal symbol entry. */
288 set_symbol_address (struct objfile
*of
, struct symbol
*sym
, const char *name
)
290 struct bound_minimal_symbol msym
;
292 msym
= lookup_minimal_symbol (name
, NULL
, of
);
293 if (msym
.minsym
!= NULL
)
295 SET_SYMBOL_VALUE_ADDRESS (sym
, BMSYMBOL_VALUE_ADDRESS (msym
));
296 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
297 SYMBOL_SECTION (sym
) = MSYMBOL_SECTION (msym
.minsym
);
301 /* Create the vector of fields, and attach it to TYPE. */
304 attach_fields_to_type (struct ctf_field_info
*fip
, struct type
*type
)
306 int nfields
= fip
->fields
.size ();
311 /* Record the field count, allocate space for the array of fields. */
312 TYPE_NFIELDS (type
) = nfields
;
314 = (struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
);
316 /* Copy the saved-up fields into the field vector. */
317 for (int i
= 0; i
< nfields
; ++i
)
319 struct ctf_nextfield
&field
= fip
->fields
[i
];
320 TYPE_FIELD (type
, i
) = field
.field
;
324 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
325 (which may be different from NAME) to the architecture back-end to allow
326 it to guess the correct format if necessary. */
329 ctf_init_float_type (struct objfile
*objfile
,
332 const char *name_hint
)
334 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
335 const struct floatformat
**format
;
338 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
340 type
= init_float_type (objfile
, bits
, name
, format
);
342 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
347 /* Callback to add member NAME to a struct/union type. TID is the type
348 of struct/union member, OFFSET is the offset of member in bits,
349 and ARG contains the ctf_field_info. */
352 ctf_add_member_cb (const char *name
,
354 unsigned long offset
,
357 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
358 struct ctf_context
*ccp
= fip
->cur_context
;
359 struct ctf_nextfield new_field
;
364 fp
= &new_field
.field
;
365 FIELD_NAME (*fp
) = name
;
367 kind
= ctf_type_kind (ccp
->fp
, tid
);
368 t
= get_tid_type (ccp
->of
, tid
);
371 t
= read_type_record (ccp
, tid
);
374 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name
, tid
);
375 t
= objfile_type (ccp
->of
)->builtin_error
;
376 set_tid_type (ccp
->of
, tid
, t
);
380 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
381 process_struct_members (ccp
, tid
, t
);
383 FIELD_TYPE (*fp
) = t
;
384 SET_FIELD_BITPOS (*fp
, offset
/ TARGET_CHAR_BIT
);
385 FIELD_BITSIZE (*fp
) = get_bitsize (ccp
->fp
, tid
, kind
);
387 fip
->fields
.emplace_back (new_field
);
392 /* Callback to add member NAME of EVAL to an enumeration type.
393 ARG contains the ctf_field_info. */
396 ctf_add_enum_member_cb (const char *name
, int enum_value
, void *arg
)
398 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
399 struct ctf_nextfield new_field
;
401 struct ctf_context
*ccp
= fip
->cur_context
;
403 fp
= &new_field
.field
;
404 FIELD_NAME (*fp
) = name
;
405 FIELD_TYPE (*fp
) = NULL
;
406 SET_FIELD_ENUMVAL (*fp
, enum_value
);
407 FIELD_BITSIZE (*fp
) = 0;
411 struct symbol
*sym
= allocate_symbol (ccp
->of
);
412 OBJSTAT (ccp
->of
, n_syms
++);
414 sym
->set_language (language_c
, &ccp
->of
->objfile_obstack
);
415 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
416 SYMBOL_ACLASS_INDEX (sym
) = LOC_CONST
;
417 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
418 SYMBOL_TYPE (sym
) = fip
->ptype
;
419 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
422 fip
->fields
.emplace_back (new_field
);
427 /* Add a new symbol entry, with its name from TID, its access index and
428 domain from TID's kind, and its type from TYPE. */
430 static struct symbol
*
431 new_symbol (struct ctf_context
*ccp
, struct type
*type
, ctf_id_t tid
)
433 struct objfile
*objfile
= ccp
->of
;
434 ctf_file_t
*fp
= ccp
->fp
;
435 struct symbol
*sym
= NULL
;
437 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
440 sym
= allocate_symbol (objfile
);
441 OBJSTAT (objfile
, n_syms
++);
443 sym
->set_language (language_c
, &objfile
->objfile_obstack
);
444 sym
->compute_and_set_names (name
.get (), true, objfile
->per_bfd
);
445 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
446 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
449 SYMBOL_TYPE (sym
) = type
;
451 uint32_t kind
= ctf_type_kind (fp
, tid
);
457 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
458 SYMBOL_DOMAIN (sym
) = STRUCT_DOMAIN
;
461 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
464 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_VOID
)
465 SYMBOL_TYPE (sym
) = objfile_type (objfile
)->builtin_int
;
470 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
471 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
484 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
490 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
491 and create the symbol for it. */
494 read_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
496 struct objfile
*of
= ccp
->of
;
497 ctf_file_t
*fp
= ccp
->fp
;
499 struct type
*type
= NULL
;
503 if (ctf_type_encoding (fp
, tid
, &cet
))
505 complaint (_("ctf_type_encoding read_base_type failed - %s"),
506 ctf_errmsg (ctf_errno (fp
)));
510 gdb::unique_xmalloc_ptr
<char> copied_name (ctf_type_aname_raw (fp
, tid
));
511 if (copied_name
== NULL
|| strlen (copied_name
.get ()) == 0)
513 name
= ctf_type_aname (fp
, tid
);
515 complaint (_("ctf_type_aname read_base_type failed - %s"),
516 ctf_errmsg (ctf_errno (fp
)));
519 name
= obstack_strdup (&of
->objfile_obstack
, copied_name
.get ());
521 kind
= ctf_type_kind (fp
, tid
);
522 if (kind
== CTF_K_INTEGER
)
524 uint32_t issigned
, ischar
, isbool
;
525 struct gdbarch
*gdbarch
= get_objfile_arch (of
);
527 issigned
= cet
.cte_format
& CTF_INT_SIGNED
;
528 ischar
= cet
.cte_format
& CTF_INT_CHAR
;
529 isbool
= cet
.cte_format
& CTF_INT_BOOL
;
531 type
= init_character_type (of
, TARGET_CHAR_BIT
, !issigned
, name
);
533 type
= init_boolean_type (of
, gdbarch_int_bit (gdbarch
),
538 if (cet
.cte_bits
&& ((cet
.cte_bits
% TARGET_CHAR_BIT
) == 0))
541 bits
= gdbarch_int_bit (gdbarch
);
542 type
= init_integer_type (of
, bits
, !issigned
, name
);
545 else if (kind
== CTF_K_FLOAT
)
548 isflt
= !((cet
.cte_format
& CTF_FP_IMAGRY
) == CTF_FP_IMAGRY
549 || (cet
.cte_format
& CTF_FP_DIMAGRY
) == CTF_FP_DIMAGRY
550 || (cet
.cte_format
& CTF_FP_LDIMAGRY
) == CTF_FP_LDIMAGRY
);
552 type
= ctf_init_float_type (of
, cet
.cte_bits
, name
, name
);
556 = ctf_init_float_type (of
, cet
.cte_bits
/ 2, NULL
, name
);
557 type
= init_complex_type (of
, name
, t
);
562 complaint (_("read_base_type: unsupported base kind (%d)"), kind
);
563 type
= init_type (of
, TYPE_CODE_ERROR
, cet
.cte_bits
, name
);
566 if (name
!= NULL
&& strcmp (name
, "char") == 0)
567 TYPE_NOSIGN (type
) = 1;
569 return set_tid_type (of
, tid
, type
);
573 process_base_type (struct ctf_context
*ccp
, ctf_id_t tid
)
577 type
= read_base_type (ccp
, tid
);
578 new_symbol (ccp
, type
, tid
);
581 /* Start a structure or union scope (definition) with TID to create a type
582 for the structure or union.
584 Fill in the type's name and general properties. The members will not be
585 processed, nor a symbol table entry be done until process_structure_type
586 (assuming the type has a name). */
589 read_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
591 struct objfile
*of
= ccp
->of
;
592 ctf_file_t
*fp
= ccp
->fp
;
596 type
= alloc_type (of
);
598 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
599 if (name
!= NULL
&& strlen (name
.get() ) != 0)
600 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
602 kind
= ctf_type_kind (fp
, tid
);
603 if (kind
== CTF_K_UNION
)
604 TYPE_CODE (type
) = TYPE_CODE_UNION
;
606 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
608 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
609 set_type_align (type
, ctf_type_align (fp
, tid
));
611 return set_tid_type (ccp
->of
, tid
, type
);
614 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
615 and create the symbol for it. */
618 process_struct_members (struct ctf_context
*ccp
,
622 struct ctf_field_info fi
;
624 fi
.cur_context
= ccp
;
625 if (ctf_member_iter (ccp
->fp
, tid
, ctf_add_member_cb
, &fi
) == CTF_ERR
)
626 complaint (_("ctf_member_iter process_struct_members failed - %s"),
627 ctf_errmsg (ctf_errno (ccp
->fp
)));
629 /* Attach fields to the type. */
630 attach_fields_to_type (&fi
, type
);
632 new_symbol (ccp
, type
, tid
);
636 process_structure_type (struct ctf_context
*ccp
, ctf_id_t tid
)
640 type
= read_structure_type (ccp
, tid
);
641 process_struct_members (ccp
, tid
, type
);
644 /* Create a function type for TID and set its return type. */
647 read_func_kind_type (struct ctf_context
*ccp
, ctf_id_t tid
)
649 struct objfile
*of
= ccp
->of
;
650 ctf_file_t
*fp
= ccp
->fp
;
651 struct type
*type
, *rettype
;
654 type
= alloc_type (of
);
656 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
657 if (name
!= NULL
&& strlen (name
.get ()) != 0)
658 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
660 TYPE_CODE (type
) = TYPE_CODE_FUNC
;
661 ctf_func_type_info (fp
, tid
, &cfi
);
662 rettype
= get_tid_type (of
, cfi
.ctc_return
);
663 TYPE_TARGET_TYPE (type
) = rettype
;
664 set_type_align (type
, ctf_type_align (fp
, tid
));
666 return set_tid_type (of
, tid
, type
);
669 /* Given a TID of CTF_K_ENUM, process all the members of the
670 enumeration, and create the symbol for the enumeration type. */
673 read_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
675 struct objfile
*of
= ccp
->of
;
676 ctf_file_t
*fp
= ccp
->fp
;
677 struct type
*type
, *target_type
;
680 type
= alloc_type (of
);
682 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
683 if (name
!= NULL
&& strlen (name
.get ()) != 0)
684 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
686 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
687 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
688 ctf_func_type_info (fp
, tid
, &fi
);
689 target_type
= get_tid_type (of
, fi
.ctc_return
);
690 TYPE_TARGET_TYPE (type
) = target_type
;
691 set_type_align (type
, ctf_type_align (fp
, tid
));
693 return set_tid_type (of
, tid
, type
);
697 process_enum_type (struct ctf_context
*ccp
, ctf_id_t tid
)
700 struct ctf_field_info fi
;
702 type
= read_enum_type (ccp
, tid
);
704 fi
.cur_context
= ccp
;
706 if (ctf_enum_iter (ccp
->fp
, tid
, ctf_add_enum_member_cb
, &fi
) == CTF_ERR
)
707 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
708 ctf_errmsg (ctf_errno (ccp
->fp
)));
710 /* Attach fields to the type. */
711 attach_fields_to_type (&fi
, type
);
713 new_symbol (ccp
, type
, tid
);
716 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
719 add_array_cv_type (struct ctf_context
*ccp
,
721 struct type
*base_type
,
725 struct type
*el_type
, *inner_array
;
727 base_type
= copy_type (base_type
);
728 inner_array
= base_type
;
730 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array
)) == TYPE_CODE_ARRAY
)
732 TYPE_TARGET_TYPE (inner_array
)
733 = copy_type (TYPE_TARGET_TYPE (inner_array
));
734 inner_array
= TYPE_TARGET_TYPE (inner_array
);
737 el_type
= TYPE_TARGET_TYPE (inner_array
);
738 cnst
|= TYPE_CONST (el_type
);
739 voltl
|= TYPE_VOLATILE (el_type
);
740 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
742 return set_tid_type (ccp
->of
, tid
, base_type
);
745 /* Read all information from a TID of CTF_K_ARRAY. */
748 read_array_type (struct ctf_context
*ccp
, ctf_id_t tid
)
750 struct objfile
*objfile
= ccp
->of
;
751 ctf_file_t
*fp
= ccp
->fp
;
752 struct type
*element_type
, *range_type
, *idx_type
;
756 if (ctf_array_info (fp
, tid
, &ar
) == CTF_ERR
)
758 complaint (_("ctf_array_info read_array_type failed - %s"),
759 ctf_errmsg (ctf_errno (fp
)));
763 element_type
= get_tid_type (objfile
, ar
.ctr_contents
);
764 if (element_type
== NULL
)
767 idx_type
= get_tid_type (objfile
, ar
.ctr_index
);
768 if (idx_type
== NULL
)
769 idx_type
= objfile_type (objfile
)->builtin_int
;
771 range_type
= create_static_range_type (NULL
, idx_type
, 0, ar
.ctr_nelems
- 1);
772 type
= create_array_type (NULL
, element_type
, range_type
);
773 if (ar
.ctr_nelems
<= 1) /* Check if undefined upper bound. */
775 TYPE_HIGH_BOUND_KIND (range_type
) = PROP_UNDEFINED
;
776 TYPE_LENGTH (type
) = 0;
777 TYPE_TARGET_STUB (type
) = 1;
780 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
782 set_type_align (type
, ctf_type_align (fp
, tid
));
784 return set_tid_type (objfile
, tid
, type
);
787 /* Read TID of kind CTF_K_CONST with base type BTID. */
790 read_const_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
792 struct objfile
*objfile
= ccp
->of
;
793 struct type
*base_type
, *cv_type
;
795 base_type
= get_tid_type (objfile
, btid
);
796 if (base_type
== NULL
)
798 base_type
= read_type_record (ccp
, btid
);
799 if (base_type
== NULL
)
801 complaint (_("read_const_type: NULL base type (%ld)"), btid
);
802 base_type
= objfile_type (objfile
)->builtin_error
;
805 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
807 return set_tid_type (objfile
, tid
, cv_type
);
810 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
813 read_volatile_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
815 struct objfile
*objfile
= ccp
->of
;
816 ctf_file_t
*fp
= ccp
->fp
;
817 struct type
*base_type
, *cv_type
;
819 base_type
= get_tid_type (objfile
, btid
);
820 if (base_type
== NULL
)
822 base_type
= read_type_record (ccp
, btid
);
823 if (base_type
== NULL
)
825 complaint (_("read_volatile_type: NULL base type (%ld)"), btid
);
826 base_type
= objfile_type (objfile
)->builtin_error
;
830 if (ctf_type_kind (fp
, btid
) == CTF_K_ARRAY
)
831 return add_array_cv_type (ccp
, tid
, base_type
, 0, 1);
832 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
834 return set_tid_type (objfile
, tid
, cv_type
);
837 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
840 read_restrict_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
842 struct objfile
*objfile
= ccp
->of
;
843 struct type
*base_type
, *cv_type
;
845 base_type
= get_tid_type (objfile
, btid
);
846 if (base_type
== NULL
)
848 base_type
= read_type_record (ccp
, btid
);
849 if (base_type
== NULL
)
851 complaint (_("read_restrict_type: NULL base type (%ld)"), btid
);
852 base_type
= objfile_type (objfile
)->builtin_error
;
855 cv_type
= make_restrict_type (base_type
);
857 return set_tid_type (objfile
, tid
, cv_type
);
860 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
863 read_typedef_type (struct ctf_context
*ccp
, ctf_id_t tid
,
864 ctf_id_t btid
, const char *name
)
866 struct objfile
*objfile
= ccp
->of
;
867 struct type
*this_type
, *target_type
;
869 char *aname
= obstack_strdup (&objfile
->objfile_obstack
, name
);
870 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, aname
);
871 set_tid_type (objfile
, tid
, this_type
);
872 target_type
= get_tid_type (objfile
, btid
);
873 if (target_type
!= this_type
)
874 TYPE_TARGET_TYPE (this_type
) = target_type
;
876 TYPE_TARGET_TYPE (this_type
) = NULL
;
877 TYPE_TARGET_STUB (this_type
) = TYPE_TARGET_TYPE (this_type
) ? 1 : 0;
879 return set_tid_type (objfile
, tid
, this_type
);
882 /* Read TID of kind CTF_K_POINTER with base type BTID. */
885 read_pointer_type (struct ctf_context
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
887 struct objfile
*of
= ccp
->of
;
888 struct type
*target_type
, *type
;
890 target_type
= get_tid_type (of
, btid
);
891 if (target_type
== NULL
)
893 target_type
= read_type_record (ccp
, btid
);
894 if (target_type
== NULL
)
896 complaint (_("read_pointer_type: NULL target type (%ld)"), btid
);
897 target_type
= objfile_type (ccp
->of
)->builtin_error
;
901 type
= lookup_pointer_type (target_type
);
902 set_type_align (type
, ctf_type_align (ccp
->fp
, tid
));
904 return set_tid_type (of
, tid
, type
);
907 /* Read information associated with type TID. */
910 read_type_record (struct ctf_context
*ccp
, ctf_id_t tid
)
912 ctf_file_t
*fp
= ccp
->fp
;
914 struct type
*type
= NULL
;
917 kind
= ctf_type_kind (fp
, tid
);
922 type
= read_structure_type (ccp
, tid
);
925 type
= read_enum_type (ccp
, tid
);
928 type
= read_func_kind_type (ccp
, tid
);
931 btid
= ctf_type_reference (fp
, tid
);
932 type
= read_const_type (ccp
, tid
, btid
);
936 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
937 btid
= ctf_type_reference (fp
, tid
);
938 type
= read_typedef_type (ccp
, tid
, btid
, name
.get ());
942 btid
= ctf_type_reference (fp
, tid
);
943 type
= read_volatile_type (ccp
, tid
, btid
);
946 btid
= ctf_type_reference (fp
, tid
);
947 type
= read_restrict_type (ccp
, tid
, btid
);
950 btid
= ctf_type_reference (fp
, tid
);
951 type
= read_pointer_type (ccp
, tid
, btid
);
955 type
= read_base_type (ccp
, tid
);
958 type
= read_array_type (ccp
, tid
);
969 /* Callback to add type TID to the symbol table. */
972 ctf_add_type_cb (ctf_id_t tid
, void *arg
)
974 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
978 /* Check if tid's type has already been defined. */
979 type
= get_tid_type (ccp
->of
, tid
);
983 ctf_id_t btid
= ctf_type_reference (ccp
->fp
, tid
);
984 kind
= ctf_type_kind (ccp
->fp
, tid
);
989 process_structure_type (ccp
, tid
);
992 process_enum_type (ccp
, tid
);
995 type
= read_func_kind_type (ccp
, tid
);
996 new_symbol (ccp
, type
, tid
);
1000 process_base_type (ccp
, tid
);
1003 new_symbol (ccp
, read_type_record (ccp
, tid
), tid
);
1006 type
= read_const_type (ccp
, tid
, btid
);
1007 new_symbol (ccp
, type
, tid
);
1009 case CTF_K_VOLATILE
:
1010 type
= read_volatile_type (ccp
, tid
, btid
);
1011 new_symbol (ccp
, type
, tid
);
1013 case CTF_K_RESTRICT
:
1014 type
= read_restrict_type (ccp
, tid
, btid
);
1015 new_symbol (ccp
, type
, tid
);
1018 type
= read_pointer_type (ccp
, tid
, btid
);
1019 new_symbol (ccp
, type
, tid
);
1022 type
= read_array_type (ccp
, tid
);
1023 new_symbol (ccp
, type
, tid
);
1034 /* Callback to add variable NAME with TID to the symbol table. */
1037 ctf_add_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1039 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1040 struct symbol
*sym
= NULL
;
1044 type
= get_tid_type (ccp
->of
, id
);
1046 kind
= ctf_type_kind (ccp
->fp
, id
);
1049 case CTF_K_FUNCTION
:
1050 if (name
&& !strcmp(name
, "main"))
1051 set_objfile_main_name (ccp
->of
, name
, language_c
);
1055 case CTF_K_VOLATILE
:
1056 case CTF_K_RESTRICT
:
1063 sym
= new_symbol (ccp
, type
, id
);
1064 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1072 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name
, id
);
1073 type
= objfile_type (ccp
->of
)->builtin_error
;
1075 sym
= allocate_symbol (ccp
->of
);
1076 OBJSTAT (ccp
->of
, n_syms
++);
1077 SYMBOL_TYPE (sym
) = type
;
1078 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1079 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
1080 sym
->compute_and_set_names (name
, false, ccp
->of
->per_bfd
);
1081 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
1084 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind
);
1089 set_symbol_address (ccp
->of
, sym
, name
);
1094 /* Add an ELF STT_OBJ symbol with index IDX to the symbol table. */
1096 static struct symbol
*
1097 add_stt_obj (struct ctf_context
*ccp
, unsigned long idx
)
1103 if ((tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
)) == CTF_ERR
)
1106 type
= get_tid_type (ccp
->of
, tid
);
1110 sym
= new_symbol (ccp
, type
, tid
);
1115 /* Add an ELF STT_FUNC symbol with index IDX to the symbol table. */
1117 static struct symbol
*
1118 add_stt_func (struct ctf_context
*ccp
, unsigned long idx
)
1120 struct type
*ftype
, *atyp
, *rettyp
;
1122 ctf_funcinfo_t finfo
;
1126 struct type
*void_type
= objfile_type (ccp
->of
)->builtin_void
;
1128 if (ctf_func_info (ccp
->fp
, idx
, &finfo
) == CTF_ERR
)
1131 argc
= finfo
.ctc_argc
;
1132 if (ctf_func_args (ccp
->fp
, idx
, argc
, argv
) == CTF_ERR
)
1135 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, idx
));
1139 tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
);
1140 ftype
= get_tid_type (ccp
->of
, tid
);
1141 if (finfo
.ctc_flags
& CTF_FUNC_VARARG
)
1142 TYPE_VARARGS (ftype
) = 1;
1143 TYPE_NFIELDS (ftype
) = argc
;
1145 /* If argc is 0, it has a "void" type. */
1148 = (struct field
*) TYPE_ZALLOC (ftype
, argc
* sizeof (struct field
));
1150 /* TYPE_FIELD_TYPE must never be NULL. Fill it with void_type, if failed
1151 to find the argument type. */
1152 for (int iparam
= 0; iparam
< argc
; iparam
++)
1154 atyp
= get_tid_type (ccp
->of
, argv
[iparam
]);
1156 TYPE_FIELD_TYPE (ftype
, iparam
) = atyp
;
1158 TYPE_FIELD_TYPE (ftype
, iparam
) = void_type
;
1161 sym
= new_symbol (ccp
, ftype
, tid
);
1162 rettyp
= get_tid_type (ccp
->of
, finfo
.ctc_return
);
1164 SYMBOL_TYPE (sym
) = rettyp
;
1166 SYMBOL_TYPE (sym
) = void_type
;
1171 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1174 get_objfile_text_range (struct objfile
*of
, int *tsize
)
1176 bfd
*abfd
= of
->obfd
;
1177 const asection
*codes
;
1179 codes
= bfd_get_section_by_name (abfd
, ".text");
1180 *tsize
= codes
? bfd_section_size (codes
) : 0;
1181 return of
->text_section_offset ();
1184 /* Start a symtab for OBJFILE in CTF format. */
1187 ctf_start_symtab (ctf_psymtab
*pst
,
1188 struct objfile
*of
, CORE_ADDR text_offset
)
1190 struct ctf_context
*ccp
;
1193 ccp
->builder
= new buildsym_compunit
1194 (of
, of
->original_name
, NULL
,
1195 language_c
, text_offset
);
1196 ccp
->builder
->record_debugformat ("ctf");
1199 /* Finish reading symbol/type definitions in CTF format.
1200 END_ADDR is the end address of the file's text. SECTION is
1201 the .text section number. */
1203 static struct compunit_symtab
*
1204 ctf_end_symtab (ctf_psymtab
*pst
,
1205 CORE_ADDR end_addr
, int section
)
1207 struct ctf_context
*ccp
;
1210 struct compunit_symtab
*result
1211 = ccp
->builder
->end_symtab (end_addr
, section
);
1212 delete ccp
->builder
;
1213 ccp
->builder
= NULL
;
1217 /* Read in full symbols for PST, and anything it depends on. */
1220 psymtab_to_symtab (ctf_psymtab
*pst
)
1223 struct ctf_context
*ccp
;
1225 gdb_assert (!pst
->readin
);
1229 /* Iterate over entries in data types section. */
1230 if (ctf_type_iter (ccp
->fp
, ctf_add_type_cb
, ccp
) == CTF_ERR
)
1231 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1232 ctf_errmsg (ctf_errno (ccp
->fp
)));
1235 /* Iterate over entries in variable info section. */
1236 if (ctf_variable_iter (ccp
->fp
, ctf_add_var_cb
, ccp
) == CTF_ERR
)
1237 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1238 ctf_errmsg (ctf_errno (ccp
->fp
)));
1240 /* Add entries in data objects and function info sections. */
1241 for (unsigned long i
= 0; ; i
++)
1243 sym
= add_stt_obj (ccp
, i
);
1246 if (ctf_errno (ccp
->fp
) == EINVAL
1247 || ctf_errno (ccp
->fp
) == ECTF_NOSYMTAB
)
1249 sym
= add_stt_func (ccp
, i
);
1254 set_symbol_address (ccp
->of
, sym
, sym
->linkage_name ());
1260 /* Expand partial symbol table PST into a full symbol table.
1264 ctf_psymtab::read_symtab (struct objfile
*objfile
)
1267 warning (_("bug: psymtab for %s is already read in."), filename
);
1272 printf_filtered (_("Reading in CTF data for %s..."), filename
);
1273 gdb_flush (gdb_stdout
);
1276 /* Start a symtab. */
1277 CORE_ADDR offset
; /* Start of text segment. */
1280 offset
= get_objfile_text_range (objfile
, &tsize
);
1281 ctf_start_symtab (this, objfile
, offset
);
1282 psymtab_to_symtab (this);
1284 set_text_low (offset
);
1285 set_text_high (offset
+ tsize
);
1286 compunit_symtab
= ctf_end_symtab (this, offset
+ tsize
,
1287 SECT_OFF_TEXT (objfile
));
1289 /* Finish up the debug error message. */
1291 printf_filtered (_("done.\n"));
1295 /* Allocate a new partial_symtab NAME.
1297 Each source file that has not been fully read in is represented by
1298 a partial_symtab. This contains the information on where in the
1299 executable the debugging symbols for a specific file are, and a
1300 list of names of global symbols which are located in this file.
1301 They are all chained on partial symtab lists.
1303 Even after the source file has been read into a symtab, the
1304 partial_symtab remains around. They are allocated on an obstack,
1307 static ctf_psymtab
*
1308 create_partial_symtab (const char *name
,
1310 struct objfile
*objfile
)
1313 struct ctf_context
*ccx
;
1315 pst
= new ctf_psymtab (name
, objfile
, 0);
1317 ccx
= XOBNEW (&objfile
->objfile_obstack
, struct ctf_context
);
1325 /* Callback to add type TID to partial symbol table. */
1328 ctf_psymtab_type_cb (ctf_id_t tid
, void *arg
)
1330 struct ctf_context
*ccp
;
1334 ccp
= (struct ctf_context
*) arg
;
1335 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, tid
));
1336 if (name
== NULL
|| strlen (name
.get ()) == 0)
1339 domain_enum domain
= UNDEF_DOMAIN
;
1340 enum address_class aclass
= LOC_UNDEF
;
1341 kind
= ctf_type_kind (ccp
->fp
, tid
);
1347 domain
= STRUCT_DOMAIN
;
1348 aclass
= LOC_TYPEDEF
;
1350 case CTF_K_FUNCTION
:
1352 domain
= VAR_DOMAIN
;
1353 aclass
= LOC_STATIC
;
1354 section
= SECT_OFF_TEXT (ccp
->of
);
1357 domain
= VAR_DOMAIN
;
1358 aclass
= LOC_STATIC
;
1362 case CTF_K_VOLATILE
:
1363 case CTF_K_RESTRICT
:
1364 domain
= VAR_DOMAIN
;
1365 aclass
= LOC_TYPEDEF
;
1369 domain
= VAR_DOMAIN
;
1370 aclass
= LOC_TYPEDEF
;
1377 add_psymbol_to_list (name
.get (), true,
1378 domain
, aclass
, section
,
1379 psymbol_placement::GLOBAL
,
1380 0, language_c
, ccp
->of
);
1385 /* Callback to add variable NAME with ID to partial symbol table. */
1388 ctf_psymtab_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1390 struct ctf_context
*ccp
= (struct ctf_context
*) arg
;
1392 add_psymbol_to_list (name
, true,
1393 VAR_DOMAIN
, LOC_STATIC
, -1,
1394 psymbol_placement::GLOBAL
,
1395 0, language_c
, ccp
->of
);
1399 /* Setup partial_symtab's describing each source file for which
1400 debugging information is available. */
1403 scan_partial_symbols (ctf_file_t
*cfp
, struct objfile
*of
)
1405 struct ctf_context ccx
;
1406 bfd
*abfd
= of
->obfd
;
1407 const char *name
= bfd_get_filename (abfd
);
1408 ctf_psymtab
*pst
= create_partial_symtab (name
, cfp
, of
);
1413 if (ctf_type_iter (cfp
, ctf_psymtab_type_cb
, &ccx
) == CTF_ERR
)
1414 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1415 ctf_errmsg (ctf_errno (cfp
)));
1417 if (ctf_variable_iter (cfp
, ctf_psymtab_var_cb
, &ccx
) == CTF_ERR
)
1418 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1419 ctf_errmsg (ctf_errno (cfp
)));
1421 /* Scan CTF object and function sections which correspond to each
1422 STT_FUNC or STT_OBJECT entry in the symbol table,
1423 pick up what init_symtab has done. */
1424 for (unsigned long idx
= 0; ; idx
++)
1427 if ((tid
= ctf_lookup_by_symbol (cfp
, idx
)) == CTF_ERR
)
1429 if (ctf_errno (cfp
) == EINVAL
|| ctf_errno (cfp
) == ECTF_NOSYMTAB
)
1430 break; // Done, reach end of the section.
1434 gdb::unique_xmalloc_ptr
<char> tname (ctf_type_aname_raw (cfp
, tid
));
1435 uint32_t kind
= ctf_type_kind (cfp
, tid
);
1436 address_class aclass
;
1437 domain_enum tdomain
;
1443 tdomain
= STRUCT_DOMAIN
;
1446 tdomain
= VAR_DOMAIN
;
1450 if (kind
== CTF_K_FUNCTION
)
1451 aclass
= LOC_STATIC
;
1452 else if (kind
== CTF_K_CONST
)
1455 aclass
= LOC_TYPEDEF
;
1457 add_psymbol_to_list (tname
.get (), true,
1458 tdomain
, aclass
, -1,
1459 psymbol_placement::STATIC
,
1463 end_psymtab_common (of
, pst
);
1466 /* Read CTF debugging information from a BFD section. This is
1467 called from elfread.c. It does a quick pass through the
1468 .ctf section to set up the partial symbol table. */
1471 elfctf_build_psymtabs (struct objfile
*of
)
1473 bfd
*abfd
= of
->obfd
;
1476 ctf_archive_t
*arc
= ctf_bfdopen (abfd
, &err
);
1478 error (_("ctf_bfdopen failed on %s - %s"),
1479 bfd_get_filename (abfd
), ctf_errmsg (err
));
1481 ctf_file_t
*fp
= ctf_arc_open_by_name (arc
, NULL
, &err
);
1483 error (_("ctf_arc_open_by_name failed on %s - %s"),
1484 bfd_get_filename (abfd
), ctf_errmsg (err
));
1485 ctf_file_key
.emplace (of
, fp
);
1487 scan_partial_symbols (fp
, of
);
1490 void _initialize_ctfread ();
1492 _initialize_ctfread ()