1 /* Compact ANSI-C Type Format (CTF) support in GDB.
3 Copyright (C) 2019 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
;
88 static const struct objfile_data
*ctf_file_key
;
90 /* A CTF context consists of a file pointer and an objfile pointer. */
92 typedef struct ctf_context
96 struct buildsym_compunit
*builder
;
99 /* The routines that read and process fields/members of a C struct, union,
100 or enumeration, pass lists of data member fields in an instance of a
101 ctf_field_info structure. It is derived from dwarf2read.c. */
105 struct field field
{};
108 struct ctf_field_info
110 /* List of data member fields. */
111 std::vector
<struct ctf_nextfield
> fields
;
114 ctf_context_t
*cur_context
;
119 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
120 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
121 std::vector
<struct decl_field
> typedef_field_list
;
123 /* Nested types defined by this struct and the number of elements in
125 std::vector
<struct decl_field
> nested_types_list
;
129 /* Local function prototypes */
131 static void psymtab_to_symtab (struct partial_symtab
*);
133 static int ctf_add_type_cb (ctf_id_t tid
, void *arg
);
135 static struct type
*read_array_type (ctf_context_t
*ccp
, ctf_id_t tid
);
137 static struct type
*read_pointer_type (ctf_context_t
*ccp
, ctf_id_t tid
,
140 static struct type
*read_structure_type (ctf_context_t
*ccp
, ctf_id_t tid
);
142 static struct type
*read_enum_type (ctf_context_t
*ccp
, ctf_id_t tid
);
144 static struct type
*read_typedef_type (ctf_context_t
*ccp
, ctf_id_t tid
,
145 ctf_id_t btid
, const char *name
);
147 static struct type
*read_type_record (ctf_context_t
*ccp
, ctf_id_t tid
);
149 static void process_structure_type (ctf_context_t
*ccp
, ctf_id_t tid
);
151 static void process_struct_members (ctf_context_t
*ccp
, ctf_id_t tid
,
154 static struct symbol
*new_symbol (ctf_context_t
*ccp
, struct type
*type
,
157 struct ctf_tid_and_type
163 /* Hash function for a ctf_tid_and_type. */
166 tid_and_type_hash (const void *item
)
168 const struct ctf_tid_and_type
*ids
169 = (const struct ctf_tid_and_type
*) item
;
174 /* Equality function for a ctf_tid_and_type. */
177 tid_and_type_eq (const void *item_lhs
, const void *item_rhs
)
179 const struct ctf_tid_and_type
*ids_lhs
180 = (const struct ctf_tid_and_type
*) item_lhs
;
181 const struct ctf_tid_and_type
*ids_rhs
182 = (const struct ctf_tid_and_type
*) item_rhs
;
184 return ids_lhs
->tid
== ids_rhs
->tid
;
187 /* Set the type associated with TID to TYP. */
190 set_tid_type (struct objfile
*of
, ctf_id_t tid
, struct type
*typ
)
194 htab
= (htab_t
) ctf_tid_key
.get (of
);
197 htab
= htab_create_alloc (1, tid_and_type_hash
,
199 NULL
, xcalloc
, xfree
);
200 ctf_tid_key
.set (of
, htab
);
203 struct ctf_tid_and_type
**slot
, ids
;
206 slot
= (struct ctf_tid_and_type
**) htab_find_slot (htab
, &ids
, INSERT
);
208 complaint (_("An internal GDB problem: ctf_ id_t %ld type already set"),
210 *slot
= XOBNEW (&of
->objfile_obstack
, struct ctf_tid_and_type
);
215 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
216 empty or TID does not have a saved type. */
219 get_tid_type (struct objfile
*of
, ctf_id_t tid
)
221 struct ctf_tid_and_type
*slot
, ids
;
224 htab
= (htab_t
) ctf_tid_key
.get (of
);
230 slot
= (struct ctf_tid_and_type
*) htab_find (htab
, &ids
);
237 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
240 get_bitsize (ctf_file_t
*fp
, ctf_id_t tid
, uint32_t kind
)
244 if ((kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
245 || kind
== CTF_K_FLOAT
)
246 && ctf_type_reference (fp
, tid
) != CTF_ERR
247 && ctf_type_encoding (fp
, tid
, &cet
) != CTF_ERR
)
253 /* Set SYM's address, with NAME, from its minimal symbol entry. */
256 set_symbol_address (struct objfile
*of
, struct symbol
*sym
, const char *name
)
258 struct bound_minimal_symbol msym
;
260 msym
= lookup_minimal_symbol (name
, NULL
, of
);
261 if (msym
.minsym
!= NULL
)
263 SET_SYMBOL_VALUE_ADDRESS (sym
, BMSYMBOL_VALUE_ADDRESS (msym
));
264 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
265 SYMBOL_SECTION (sym
) = MSYMBOL_SECTION (msym
.minsym
);
269 /* Create the vector of fields, and attach it to TYPE. */
272 attach_fields_to_type (struct ctf_field_info
*fip
, struct type
*type
)
274 int nfields
= fip
->fields
.size ();
279 /* Record the field count, allocate space for the array of fields. */
280 TYPE_NFIELDS (type
) = nfields
;
282 = (struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
);
284 /* Copy the saved-up fields into the field vector. */
285 for (int i
= 0; i
< nfields
; ++i
)
287 struct ctf_nextfield
&field
= fip
->fields
[i
];
288 TYPE_FIELD (type
, i
) = field
.field
;
292 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
293 (which may be different from NAME) to the architecture back-end to allow
294 it to guess the correct format if necessary. */
297 ctf_init_float_type (struct objfile
*objfile
,
300 const char *name_hint
)
302 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
303 const struct floatformat
**format
;
306 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
308 type
= init_float_type (objfile
, bits
, name
, format
);
310 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
315 /* Callback to add member NAME to a struct/union type. TID is the type
316 of struct/union member, OFFSET is the offset of member in bits,
317 and ARG contains the ctf_field_info. */
320 ctf_add_member_cb (const char *name
,
322 unsigned long offset
,
325 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
326 ctf_context_t
*ccp
= fip
->cur_context
;
327 struct ctf_nextfield new_field
;
332 fp
= &new_field
.field
;
333 FIELD_NAME (*fp
) = name
;
335 kind
= ctf_type_kind (ccp
->fp
, tid
);
336 t
= get_tid_type (ccp
->of
, tid
);
339 t
= read_type_record (ccp
, tid
);
342 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name
, tid
);
343 t
= objfile_type (ccp
->of
)->builtin_error
;
344 set_tid_type (ccp
->of
, tid
, t
);
348 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
349 process_struct_members (ccp
, tid
, t
);
351 FIELD_TYPE (*fp
) = t
;
352 SET_FIELD_BITPOS (*fp
, offset
/ TARGET_CHAR_BIT
);
353 FIELD_BITSIZE (*fp
) = get_bitsize (ccp
->fp
, tid
, kind
);
355 fip
->fields
.emplace_back (new_field
);
360 /* Callback to add member NAME of EVAL to an enumeration type.
361 ARG contains the ctf_field_info. */
364 ctf_add_enum_member_cb (const char *name
, int enum_value
, void *arg
)
366 struct ctf_field_info
*fip
= (struct ctf_field_info
*) arg
;
367 struct ctf_nextfield new_field
;
369 ctf_context_t
*ccp
= fip
->cur_context
;
371 fp
= &new_field
.field
;
372 FIELD_NAME (*fp
) = name
;
373 FIELD_TYPE (*fp
) = NULL
;
374 SET_FIELD_ENUMVAL (*fp
, enum_value
);
375 FIELD_BITSIZE (*fp
) = 0;
379 struct symbol
*sym
= allocate_symbol (ccp
->of
);
380 OBJSTAT (ccp
->of
, n_syms
++);
382 sym
->set_language (language_c
, &ccp
->of
->objfile_obstack
);
383 SYMBOL_SET_NAMES (sym
, name
, false, ccp
->of
);
384 SYMBOL_ACLASS_INDEX (sym
) = LOC_CONST
;
385 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
386 SYMBOL_TYPE (sym
) = fip
->ptype
;
387 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
390 fip
->fields
.emplace_back (new_field
);
395 /* Add a new symbol entry, with its name from TID, its access index and
396 domain from TID's kind, and its type from TYPE. */
398 static struct symbol
*
399 new_symbol (ctf_context_t
*ccp
, struct type
*type
, ctf_id_t tid
)
401 struct objfile
*objfile
= ccp
->of
;
402 ctf_file_t
*fp
= ccp
->fp
;
403 struct symbol
*sym
= NULL
;
405 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
408 sym
= allocate_symbol (objfile
);
409 OBJSTAT (objfile
, n_syms
++);
411 sym
->set_language (language_c
, &objfile
->objfile_obstack
);
412 SYMBOL_SET_NAMES (sym
, name
.get (), true, objfile
);
413 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
414 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
417 SYMBOL_TYPE (sym
) = type
;
419 uint32_t kind
= ctf_type_kind (fp
, tid
);
425 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
426 SYMBOL_DOMAIN (sym
) = STRUCT_DOMAIN
;
429 SYMBOL_ACLASS_INDEX (sym
) = LOC_STATIC
;
432 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_VOID
)
433 SYMBOL_TYPE (sym
) = objfile_type (objfile
)->builtin_int
;
438 SYMBOL_ACLASS_INDEX (sym
) = LOC_TYPEDEF
;
439 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
452 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
458 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
459 and create the symbol for it. */
462 read_base_type (ctf_context_t
*ccp
, ctf_id_t tid
)
464 struct objfile
*of
= ccp
->of
;
465 ctf_file_t
*fp
= ccp
->fp
;
467 struct type
*type
= NULL
;
471 if (ctf_type_encoding (fp
, tid
, &cet
))
473 complaint (_("ctf_type_encoding read_base_type failed - %s"),
474 ctf_errmsg (ctf_errno (fp
)));
478 gdb::unique_xmalloc_ptr
<char> copied_name (ctf_type_aname_raw (fp
, tid
));
479 if (copied_name
== NULL
|| strlen (copied_name
.get ()) == 0)
481 name
= ctf_type_aname (fp
, tid
);
483 complaint (_("ctf_type_aname read_base_type failed - %s"),
484 ctf_errmsg (ctf_errno (fp
)));
487 name
= obstack_strdup (&of
->objfile_obstack
, copied_name
.get ());
489 kind
= ctf_type_kind (fp
, tid
);
490 if (kind
== CTF_K_INTEGER
)
492 uint32_t issigned
, ischar
, isbool
;
493 struct gdbarch
*gdbarch
= get_objfile_arch (of
);
495 issigned
= cet
.cte_format
& CTF_INT_SIGNED
;
496 ischar
= cet
.cte_format
& CTF_INT_CHAR
;
497 isbool
= cet
.cte_format
& CTF_INT_BOOL
;
499 type
= init_character_type (of
, TARGET_CHAR_BIT
, !issigned
, name
);
501 type
= init_boolean_type (of
, gdbarch_int_bit (gdbarch
),
506 if (cet
.cte_bits
&& ((cet
.cte_bits
% TARGET_CHAR_BIT
) == 0))
509 bits
= gdbarch_int_bit (gdbarch
);
510 type
= init_integer_type (of
, bits
, !issigned
, name
);
513 else if (kind
== CTF_K_FLOAT
)
516 isflt
= !((cet
.cte_format
& CTF_FP_IMAGRY
) == CTF_FP_IMAGRY
517 || (cet
.cte_format
& CTF_FP_DIMAGRY
) == CTF_FP_DIMAGRY
518 || (cet
.cte_format
& CTF_FP_LDIMAGRY
) == CTF_FP_LDIMAGRY
);
520 type
= ctf_init_float_type (of
, cet
.cte_bits
, name
, name
);
524 = ctf_init_float_type (of
, cet
.cte_bits
/ 2, NULL
, name
);
525 type
= init_complex_type (of
, name
, t
);
530 complaint (_("read_base_type: unsupported base kind (%d)"), kind
);
531 type
= init_type (of
, TYPE_CODE_ERROR
, cet
.cte_bits
, name
);
534 if (name
!= NULL
&& strcmp (name
, "char") == 0)
535 TYPE_NOSIGN (type
) = 1;
537 return set_tid_type (of
, tid
, type
);
541 process_base_type (ctf_context_t
*ccp
, ctf_id_t tid
)
545 type
= read_base_type (ccp
, tid
);
546 new_symbol (ccp
, type
, tid
);
549 /* Start a structure or union scope (definition) with TID to create a type
550 for the structure or union.
552 Fill in the type's name and general properties. The members will not be
553 processed, nor a symbol table entry be done until process_structure_type
554 (assuming the type has a name). */
557 read_structure_type (ctf_context_t
*ccp
, ctf_id_t tid
)
559 struct objfile
*of
= ccp
->of
;
560 ctf_file_t
*fp
= ccp
->fp
;
564 type
= alloc_type (of
);
566 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
567 if (name
!= NULL
&& strlen (name
.get() ) != 0)
568 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
570 kind
= ctf_type_kind (fp
, tid
);
571 if (kind
== CTF_K_UNION
)
572 TYPE_CODE (type
) = TYPE_CODE_UNION
;
574 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
576 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
577 set_type_align (type
, ctf_type_align (fp
, tid
));
579 return set_tid_type (ccp
->of
, tid
, type
);
582 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
583 and create the symbol for it. */
586 process_struct_members (ctf_context_t
*ccp
,
590 struct ctf_field_info fi
;
592 fi
.cur_context
= ccp
;
593 if (ctf_member_iter (ccp
->fp
, tid
, ctf_add_member_cb
, &fi
) == CTF_ERR
)
594 complaint (_("ctf_member_iter process_struct_members failed - %s"),
595 ctf_errmsg (ctf_errno (ccp
->fp
)));
597 /* Attach fields to the type. */
598 attach_fields_to_type (&fi
, type
);
600 new_symbol (ccp
, type
, tid
);
604 process_structure_type (ctf_context_t
*ccp
, ctf_id_t tid
)
608 type
= read_structure_type (ccp
, tid
);
609 process_struct_members (ccp
, tid
, type
);
612 /* Create a function type for TID and set its return type. */
615 read_func_kind_type (ctf_context_t
*ccp
, ctf_id_t tid
)
617 struct objfile
*of
= ccp
->of
;
618 ctf_file_t
*fp
= ccp
->fp
;
619 struct type
*type
, *rettype
;
622 type
= alloc_type (of
);
624 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
625 if (name
!= NULL
&& strlen (name
.get ()) != 0)
626 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
628 TYPE_CODE (type
) = TYPE_CODE_FUNC
;
629 ctf_func_type_info (fp
, tid
, &cfi
);
630 rettype
= get_tid_type (of
, cfi
.ctc_return
);
631 TYPE_TARGET_TYPE (type
) = rettype
;
632 set_type_align (type
, ctf_type_align (fp
, tid
));
634 return set_tid_type (of
, tid
, type
);
637 /* Given a TID of CTF_K_ENUM, process all the members of the
638 enumeration, and create the symbol for the enumeration type. */
641 read_enum_type (ctf_context_t
*ccp
, ctf_id_t tid
)
643 struct objfile
*of
= ccp
->of
;
644 ctf_file_t
*fp
= ccp
->fp
;
645 struct type
*type
, *target_type
;
648 type
= alloc_type (of
);
650 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
651 if (name
!= NULL
&& strlen (name
.get ()) != 0)
652 TYPE_NAME (type
) = obstack_strdup (&of
->objfile_obstack
, name
.get ());
654 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
655 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
656 ctf_func_type_info (fp
, tid
, &fi
);
657 target_type
= get_tid_type (of
, fi
.ctc_return
);
658 TYPE_TARGET_TYPE (type
) = target_type
;
659 set_type_align (type
, ctf_type_align (fp
, tid
));
661 return set_tid_type (of
, tid
, type
);
665 process_enum_type (ctf_context_t
*ccp
, ctf_id_t tid
)
668 struct ctf_field_info fi
;
670 type
= read_enum_type (ccp
, tid
);
672 fi
.cur_context
= ccp
;
674 if (ctf_enum_iter (ccp
->fp
, tid
, ctf_add_enum_member_cb
, &fi
) == CTF_ERR
)
675 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
676 ctf_errmsg (ctf_errno (ccp
->fp
)));
678 /* Attach fields to the type. */
679 attach_fields_to_type (&fi
, type
);
681 new_symbol (ccp
, type
, tid
);
684 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
687 add_array_cv_type (ctf_context_t
*ccp
,
689 struct type
*base_type
,
693 struct type
*el_type
, *inner_array
;
695 base_type
= copy_type (base_type
);
696 inner_array
= base_type
;
698 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array
)) == TYPE_CODE_ARRAY
)
700 TYPE_TARGET_TYPE (inner_array
)
701 = copy_type (TYPE_TARGET_TYPE (inner_array
));
702 inner_array
= TYPE_TARGET_TYPE (inner_array
);
705 el_type
= TYPE_TARGET_TYPE (inner_array
);
706 cnst
|= TYPE_CONST (el_type
);
707 voltl
|= TYPE_VOLATILE (el_type
);
708 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
710 return set_tid_type (ccp
->of
, tid
, base_type
);
713 /* Read all information from a TID of CTF_K_ARRAY. */
716 read_array_type (ctf_context_t
*ccp
, ctf_id_t tid
)
718 struct objfile
*objfile
= ccp
->of
;
719 ctf_file_t
*fp
= ccp
->fp
;
720 struct type
*element_type
, *range_type
, *idx_type
;
724 if (ctf_array_info (fp
, tid
, &ar
) == CTF_ERR
)
726 complaint (_("ctf_array_info read_array_type failed - %s"),
727 ctf_errmsg (ctf_errno (fp
)));
731 element_type
= get_tid_type (objfile
, ar
.ctr_contents
);
732 if (element_type
== NULL
)
735 idx_type
= get_tid_type (objfile
, ar
.ctr_index
);
736 if (idx_type
== NULL
)
737 idx_type
= objfile_type (objfile
)->builtin_int
;
739 range_type
= create_static_range_type (NULL
, idx_type
, 0, ar
.ctr_nelems
- 1);
740 type
= create_array_type (NULL
, element_type
, range_type
);
741 if (ar
.ctr_nelems
<= 1) /* Check if undefined upper bound. */
743 TYPE_HIGH_BOUND_KIND (range_type
) = PROP_UNDEFINED
;
744 TYPE_LENGTH (type
) = 0;
745 TYPE_TARGET_STUB (type
) = 1;
748 TYPE_LENGTH (type
) = ctf_type_size (fp
, tid
);
750 set_type_align (type
, ctf_type_align (fp
, tid
));
752 return set_tid_type (objfile
, tid
, type
);
755 /* Read TID of kind CTF_K_CONST with base type BTID. */
758 read_const_type (ctf_context_t
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
760 struct objfile
*objfile
= ccp
->of
;
761 struct type
*base_type
, *cv_type
;
763 base_type
= get_tid_type (objfile
, btid
);
764 if (base_type
== NULL
)
766 base_type
= read_type_record (ccp
, btid
);
767 if (base_type
== NULL
)
769 complaint (_("read_const_type: NULL base type (%ld)"), btid
);
770 base_type
= objfile_type (objfile
)->builtin_error
;
773 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
775 return set_tid_type (objfile
, tid
, cv_type
);
778 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
781 read_volatile_type (ctf_context_t
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
783 struct objfile
*objfile
= ccp
->of
;
784 ctf_file_t
*fp
= ccp
->fp
;
785 struct type
*base_type
, *cv_type
;
787 base_type
= get_tid_type (objfile
, btid
);
788 if (base_type
== NULL
)
790 base_type
= read_type_record (ccp
, btid
);
791 if (base_type
== NULL
)
793 complaint (_("read_volatile_type: NULL base type (%ld)"), btid
);
794 base_type
= objfile_type (objfile
)->builtin_error
;
798 if (ctf_type_kind (fp
, btid
) == CTF_K_ARRAY
)
799 return add_array_cv_type (ccp
, tid
, base_type
, 0, 1);
800 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
802 return set_tid_type (objfile
, tid
, cv_type
);
805 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
808 read_restrict_type (ctf_context_t
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
810 struct objfile
*objfile
= ccp
->of
;
811 struct type
*base_type
, *cv_type
;
813 base_type
= get_tid_type (objfile
, btid
);
814 if (base_type
== NULL
)
816 base_type
= read_type_record (ccp
, btid
);
817 if (base_type
== NULL
)
819 complaint (_("read_restrict_type: NULL base type (%ld)"), btid
);
820 base_type
= objfile_type (objfile
)->builtin_error
;
823 cv_type
= make_restrict_type (base_type
);
825 return set_tid_type (objfile
, tid
, cv_type
);
828 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
831 read_typedef_type (ctf_context_t
*ccp
, ctf_id_t tid
,
832 ctf_id_t btid
, const char *name
)
834 struct objfile
*objfile
= ccp
->of
;
835 struct type
*this_type
, *target_type
;
837 char *aname
= obstack_strdup (&objfile
->objfile_obstack
, name
);
838 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, aname
);
839 set_tid_type (objfile
, tid
, this_type
);
840 target_type
= get_tid_type (objfile
, btid
);
841 if (target_type
!= this_type
)
842 TYPE_TARGET_TYPE (this_type
) = target_type
;
844 TYPE_TARGET_TYPE (this_type
) = NULL
;
845 TYPE_TARGET_STUB (this_type
) = TYPE_TARGET_TYPE (this_type
) ? 1 : 0;
847 return set_tid_type (objfile
, tid
, this_type
);
850 /* Read TID of kind CTF_K_POINTER with base type BTID. */
853 read_pointer_type (ctf_context_t
*ccp
, ctf_id_t tid
, ctf_id_t btid
)
855 struct objfile
*of
= ccp
->of
;
856 struct type
*target_type
, *type
;
858 target_type
= get_tid_type (of
, btid
);
859 if (target_type
== NULL
)
861 target_type
= read_type_record (ccp
, btid
);
862 if (target_type
== NULL
)
864 complaint (_("read_pointer_type: NULL target type (%ld)"), btid
);
865 target_type
= objfile_type (ccp
->of
)->builtin_error
;
869 type
= lookup_pointer_type (target_type
);
870 set_type_align (type
, ctf_type_align (ccp
->fp
, tid
));
872 return set_tid_type (of
, tid
, type
);
875 /* Read information associated with type TID. */
878 read_type_record (ctf_context_t
*ccp
, ctf_id_t tid
)
880 ctf_file_t
*fp
= ccp
->fp
;
882 struct type
*type
= NULL
;
885 kind
= ctf_type_kind (fp
, tid
);
890 type
= read_structure_type (ccp
, tid
);
893 type
= read_enum_type (ccp
, tid
);
896 type
= read_func_kind_type (ccp
, tid
);
899 btid
= ctf_type_reference (fp
, tid
);
900 type
= read_const_type (ccp
, tid
, btid
);
904 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (fp
, tid
));
905 btid
= ctf_type_reference (fp
, tid
);
906 type
= read_typedef_type (ccp
, tid
, btid
, name
.get ());
910 btid
= ctf_type_reference (fp
, tid
);
911 type
= read_volatile_type (ccp
, tid
, btid
);
914 btid
= ctf_type_reference (fp
, tid
);
915 type
= read_restrict_type (ccp
, tid
, btid
);
918 btid
= ctf_type_reference (fp
, tid
);
919 type
= read_pointer_type (ccp
, tid
, btid
);
923 type
= read_base_type (ccp
, tid
);
926 type
= read_array_type (ccp
, tid
);
937 /* Callback to add type TID to the symbol table. */
940 ctf_add_type_cb (ctf_id_t tid
, void *arg
)
942 ctf_context_t
*ccp
= (ctf_context_t
*) arg
;
946 /* Check if tid's type has already been defined. */
947 type
= get_tid_type (ccp
->of
, tid
);
951 ctf_id_t btid
= ctf_type_reference (ccp
->fp
, tid
);
952 kind
= ctf_type_kind (ccp
->fp
, tid
);
957 process_structure_type (ccp
, tid
);
960 process_enum_type (ccp
, tid
);
963 type
= read_func_kind_type (ccp
, tid
);
964 new_symbol (ccp
, type
, tid
);
968 process_base_type (ccp
, tid
);
971 new_symbol (ccp
, read_type_record (ccp
, tid
), tid
);
974 type
= read_const_type (ccp
, tid
, btid
);
975 new_symbol (ccp
, type
, tid
);
978 type
= read_volatile_type (ccp
, tid
, btid
);
979 new_symbol (ccp
, type
, tid
);
982 type
= read_restrict_type (ccp
, tid
, btid
);
983 new_symbol (ccp
, type
, tid
);
986 type
= read_pointer_type (ccp
, tid
, btid
);
987 new_symbol (ccp
, type
, tid
);
990 type
= read_array_type (ccp
, tid
);
991 new_symbol (ccp
, type
, tid
);
1002 /* Callback to add variable NAME with TID to the symbol table. */
1005 ctf_add_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1007 ctf_context_t
*ccp
= (ctf_context_t
*) arg
;
1008 struct symbol
*sym
= NULL
;
1012 type
= get_tid_type (ccp
->of
, id
);
1014 kind
= ctf_type_kind (ccp
->fp
, id
);
1017 case CTF_K_FUNCTION
:
1018 if (name
&& !strcmp(name
, "main"))
1019 set_objfile_main_name (ccp
->of
, name
, language_c
);
1023 case CTF_K_VOLATILE
:
1024 case CTF_K_RESTRICT
:
1031 sym
= new_symbol (ccp
, type
, id
);
1032 SYMBOL_SET_NAMES (sym
, name
, false, ccp
->of
);
1040 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name
, id
);
1041 type
= objfile_type (ccp
->of
)->builtin_error
;
1043 sym
= allocate_symbol (ccp
->of
);
1044 OBJSTAT (ccp
->of
, n_syms
++);
1045 SYMBOL_TYPE (sym
) = type
;
1046 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1047 SYMBOL_ACLASS_INDEX (sym
) = LOC_OPTIMIZED_OUT
;
1048 SYMBOL_SET_NAMES (sym
, name
, false, ccp
->of
);
1049 add_symbol_to_list (sym
, ccp
->builder
->get_global_symbols ());
1052 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind
);
1057 set_symbol_address (ccp
->of
, sym
, name
);
1062 /* Add an ELF STT_OBJ symbol with index IDX to the symbol table. */
1064 static struct symbol
*
1065 add_stt_obj (ctf_context_t
*ccp
, unsigned long idx
)
1071 if ((tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
)) == CTF_ERR
)
1074 type
= get_tid_type (ccp
->of
, tid
);
1078 sym
= new_symbol (ccp
, type
, tid
);
1083 /* Add an ELF STT_FUNC symbol with index IDX to the symbol table. */
1085 static struct symbol
*
1086 add_stt_func (ctf_context_t
*ccp
, unsigned long idx
)
1088 struct type
*ftype
, *atyp
, *rettyp
;
1090 ctf_funcinfo_t finfo
;
1094 struct type
*void_type
= objfile_type (ccp
->of
)->builtin_void
;
1096 if (ctf_func_info (ccp
->fp
, idx
, &finfo
) == CTF_ERR
)
1099 argc
= finfo
.ctc_argc
;
1100 if (ctf_func_args (ccp
->fp
, idx
, argc
, argv
) == CTF_ERR
)
1103 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, idx
));
1107 tid
= ctf_lookup_by_symbol (ccp
->fp
, idx
);
1108 ftype
= get_tid_type (ccp
->of
, tid
);
1109 if (finfo
.ctc_flags
& CTF_FUNC_VARARG
)
1110 TYPE_VARARGS (ftype
) = 1;
1111 TYPE_NFIELDS (ftype
) = argc
;
1113 /* If argc is 0, it has a "void" type. */
1116 = (struct field
*) TYPE_ZALLOC (ftype
, argc
* sizeof (struct field
));
1118 /* TYPE_FIELD_TYPE must never be NULL. Fill it with void_type, if failed
1119 to find the argument type. */
1120 for (int iparam
= 0; iparam
< argc
; iparam
++)
1122 atyp
= get_tid_type (ccp
->of
, argv
[iparam
]);
1124 TYPE_FIELD_TYPE (ftype
, iparam
) = atyp
;
1126 TYPE_FIELD_TYPE (ftype
, iparam
) = void_type
;
1129 sym
= new_symbol (ccp
, ftype
, tid
);
1130 rettyp
= get_tid_type (ccp
->of
, finfo
.ctc_return
);
1132 SYMBOL_TYPE (sym
) = rettyp
;
1134 SYMBOL_TYPE (sym
) = void_type
;
1139 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1142 get_objfile_text_range (struct objfile
*of
, int *tsize
)
1144 CORE_ADDR text_base
;
1145 bfd
*abfd
= of
->obfd
;
1146 const asection
*codes
;
1148 codes
= bfd_get_section_by_name (abfd
, ".text");
1156 text_base
= bfd_section_vma (codes
);
1157 *tsize
= codes
->size
;
1163 /* Start a symtab for OBJFILE in CTF format. */
1166 ctf_start_symtab (struct partial_symtab
*pst
,
1167 struct objfile
*of
, CORE_ADDR text_offset
)
1171 ccp
= (ctf_context_t
*) pst
->read_symtab_private
;
1172 ccp
->builder
= new buildsym_compunit
1173 (of
, of
->original_name
, NULL
,
1174 language_c
, text_offset
);
1175 ccp
->builder
->record_debugformat ("ctf");
1178 /* Finish reading symbol/type definitions in CTF format.
1179 END_ADDR is the end address of the file's text. SECTION is
1180 the .text section number. */
1182 static struct compunit_symtab
*
1183 ctf_end_symtab (struct partial_symtab
*pst
,
1184 CORE_ADDR end_addr
, int section
)
1188 ccp
= (ctf_context_t
*) pst
->read_symtab_private
;
1189 struct compunit_symtab
*result
1190 = ccp
->builder
->end_symtab (end_addr
, section
);
1191 delete ccp
->builder
;
1192 ccp
->builder
= NULL
;
1196 /* Read in full symbols for PST, and anything it depends on. */
1199 psymtab_to_symtab (struct partial_symtab
*pst
)
1204 gdb_assert (!pst
->readin
);
1206 ccp
= (ctf_context_t
*) pst
->read_symtab_private
;
1208 /* Iterate over entries in data types section. */
1209 if (ctf_type_iter (ccp
->fp
, ctf_add_type_cb
, ccp
) == CTF_ERR
)
1210 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1211 ctf_errmsg (ctf_errno (ccp
->fp
)));
1214 /* Iterate over entries in variable info section. */
1215 if (ctf_variable_iter (ccp
->fp
, ctf_add_var_cb
, ccp
) == CTF_ERR
)
1216 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1217 ctf_errmsg (ctf_errno (ccp
->fp
)));
1219 /* Add entries in data objects and function info sections. */
1220 for (unsigned long i
= 0; ; i
++)
1222 sym
= add_stt_obj (ccp
, i
);
1225 if (ctf_errno (ccp
->fp
) == EINVAL
1226 || ctf_errno (ccp
->fp
) == ECTF_NOSYMTAB
)
1228 sym
= add_stt_func (ccp
, i
);
1233 set_symbol_address (ccp
->of
, sym
, sym
->linkage_name ());
1239 /* Expand partial symbol table PST into a full symbol table.
1243 ctf_read_symtab (struct partial_symtab
*pst
, struct objfile
*objfile
)
1246 warning (_("bug: psymtab for %s is already read in."), pst
->filename
);
1251 printf_filtered (_("Reading in CTF data for %s..."), pst
->filename
);
1252 gdb_flush (gdb_stdout
);
1255 /* Start a symtab. */
1256 CORE_ADDR text_offset
; /* Start of text segment. */
1259 text_offset
= get_objfile_text_range (objfile
, &tsize
);
1260 ctf_start_symtab (pst
, objfile
, text_offset
);
1261 psymtab_to_symtab (pst
);
1263 pst
->set_text_low (text_offset
);
1264 pst
->set_text_high (text_offset
+ tsize
);
1265 pst
->compunit_symtab
= ctf_end_symtab (pst
, text_offset
+ tsize
,
1266 SECT_OFF_TEXT (objfile
));
1268 /* Finish up the debug error message. */
1270 printf_filtered (_("done.\n"));
1274 /* Cleanup function for the ctf_file_key data. */
1277 ctf_close_objfile (struct objfile
*of
, void *datum
)
1279 ctf_file_t
*fp
= static_cast<ctf_file_t
*> (datum
);
1280 ctf_archive_t
*arc
= ctf_get_arc (fp
);
1281 ctf_file_close (fp
);
1285 /* Allocate a new partial_symtab NAME.
1287 Each source file that has not been fully read in is represented by
1288 a partial_symtab. This contains the information on where in the
1289 executable the debugging symbols for a specific file are, and a
1290 list of names of global symbols which are located in this file.
1291 They are all chained on partial symtab lists.
1293 Even after the source file has been read into a symtab, the
1294 partial_symtab remains around. They are allocated on an obstack,
1297 static struct partial_symtab
*
1298 create_partial_symtab (const char *name
,
1300 struct objfile
*objfile
)
1302 struct partial_symtab
*pst
;
1305 pst
= start_psymtab_common (objfile
, name
, 0);
1307 ccx
= XOBNEW (&objfile
->objfile_obstack
, ctf_context_t
);
1310 pst
->read_symtab_private
= (void *) ccx
;
1311 pst
->read_symtab
= ctf_read_symtab
;
1316 /* Callback to add type TID to partial symbol table. */
1319 ctf_psymtab_type_cb (ctf_id_t tid
, void *arg
)
1325 ccp
= (ctf_context_t
*) arg
;
1326 gdb::unique_xmalloc_ptr
<char> name (ctf_type_aname_raw (ccp
->fp
, tid
));
1327 if (name
== NULL
|| strlen (name
.get ()) == 0)
1330 domain_enum domain
= UNDEF_DOMAIN
;
1331 enum address_class aclass
= LOC_UNDEF
;
1332 kind
= ctf_type_kind (ccp
->fp
, tid
);
1338 domain
= STRUCT_DOMAIN
;
1339 aclass
= LOC_TYPEDEF
;
1341 case CTF_K_FUNCTION
:
1343 domain
= VAR_DOMAIN
;
1344 aclass
= LOC_STATIC
;
1345 section
= SECT_OFF_TEXT (ccp
->of
);
1348 domain
= VAR_DOMAIN
;
1349 aclass
= LOC_STATIC
;
1353 case CTF_K_VOLATILE
:
1354 case CTF_K_RESTRICT
:
1355 domain
= VAR_DOMAIN
;
1356 aclass
= LOC_TYPEDEF
;
1360 domain
= VAR_DOMAIN
;
1361 aclass
= LOC_TYPEDEF
;
1368 add_psymbol_to_list (name
.get (), true,
1369 domain
, aclass
, section
,
1370 psymbol_placement::GLOBAL
,
1371 0, language_c
, ccp
->of
);
1376 /* Callback to add variable NAME with ID to partial symbol table. */
1379 ctf_psymtab_var_cb (const char *name
, ctf_id_t id
, void *arg
)
1381 ctf_context_t
*ccp
= (ctf_context_t
*) arg
;
1383 add_psymbol_to_list (name
, true,
1384 VAR_DOMAIN
, LOC_STATIC
, -1,
1385 psymbol_placement::GLOBAL
,
1386 0, language_c
, ccp
->of
);
1390 /* Setup partial_symtab's describing each source file for which
1391 debugging information is available. */
1394 scan_partial_symbols (ctf_file_t
*cfp
, struct objfile
*of
)
1397 bfd
*abfd
= of
->obfd
;
1398 const char *name
= bfd_get_filename (abfd
);
1399 struct partial_symtab
*pst
= create_partial_symtab (name
, cfp
, of
);
1404 if (ctf_type_iter (cfp
, ctf_psymtab_type_cb
, &ccx
) == CTF_ERR
)
1405 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1406 ctf_errmsg (ctf_errno (cfp
)));
1408 if (ctf_variable_iter (cfp
, ctf_psymtab_var_cb
, &ccx
) == CTF_ERR
)
1409 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1410 ctf_errmsg (ctf_errno (cfp
)));
1412 /* Scan CTF object and function sections which correspond to each
1413 STT_FUNC or STT_OBJECT entry in the symbol table,
1414 pick up what init_symtab has done. */
1415 for (unsigned long idx
= 0; ; idx
++)
1418 if ((tid
= ctf_lookup_by_symbol (cfp
, idx
)) == CTF_ERR
)
1420 if (ctf_errno (cfp
) == EINVAL
|| ctf_errno (cfp
) == ECTF_NOSYMTAB
)
1421 break; // Done, reach end of the section.
1425 gdb::unique_xmalloc_ptr
<char> tname (ctf_type_aname_raw (cfp
, tid
));
1426 uint32_t kind
= ctf_type_kind (cfp
, tid
);
1427 address_class aclass
;
1428 domain_enum tdomain
;
1434 tdomain
= STRUCT_DOMAIN
;
1437 tdomain
= VAR_DOMAIN
;
1441 if (kind
== CTF_K_FUNCTION
)
1442 aclass
= LOC_STATIC
;
1443 else if (kind
== CTF_K_CONST
)
1446 aclass
= LOC_TYPEDEF
;
1448 add_psymbol_to_list (tname
.get (), true,
1449 tdomain
, aclass
, -1,
1450 psymbol_placement::STATIC
,
1454 end_psymtab_common (of
, pst
);
1457 /* Read CTF debugging information from a BFD section. This is
1458 called from elfread.c. It does a quick pass through the
1459 .ctf section to set up the partial symbol table. */
1462 elfctf_build_psymtabs (struct objfile
*of
)
1464 bfd
*abfd
= of
->obfd
;
1467 ctf_archive_t
*arc
= ctf_bfdopen (abfd
, &err
);
1469 error (_("ctf_bfdopen failed on %s - %s"),
1470 bfd_get_filename (abfd
), ctf_errmsg (err
));
1472 ctf_file_t
*fp
= ctf_arc_open_by_name (arc
, NULL
, &err
);
1474 error (_("ctf_arc_open_by_name failed on %s - %s"),
1475 bfd_get_filename (abfd
), ctf_errmsg (err
));
1476 set_objfile_data (of
, ctf_file_key
, fp
);
1478 scan_partial_symbols (fp
, of
);
1482 _initialize_ctfread (void)
1485 = register_objfile_data_with_cleanup (NULL
, ctf_close_objfile
);