Use type allocator for range types
[binutils-gdb.git] / gdb / ctfread.c
1 /* Compact ANSI-C Type Format (CTF) support in GDB.
2
3 Copyright (C) 2019-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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).
37
38 The CTF file or section itself has the following structure:
39
40 +--------+--------+---------+----------+----------+-------+--------+
41 | file | type | data | function | variable | data | string |
42 | header | labels | objects | info | info | types | table |
43 +--------+--------+---------+----------+----------+-------+--------+
44
45 The file header stores a magic number and version information, encoding
46 flags, and the byte offset of each of the sections relative to the end of the
47 header itself. If the CTF data has been uniquified against another set of
48 CTF data, a reference to that data also appears in the header. This
49 reference is the name of the label corresponding to the types uniquified
50 against.
51
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].
55
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.
61
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.
65
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,
69 denoted by their ID.
70
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. */
77
78 #include "defs.h"
79 #include "buildsym.h"
80 #include "complaints.h"
81 #include "block.h"
82 #include "ctfread.h"
83 #include "psympriv.h"
84
85 #if ENABLE_LIBCTF
86
87 #include "ctf.h"
88 #include "ctf-api.h"
89
90 static const registry<objfile>::key<htab, htab_deleter> ctf_tid_key;
91
92 struct ctf_fp_info
93 {
94 explicit ctf_fp_info (ctf_dict_t *cfp) : fp (cfp) {}
95 ~ctf_fp_info ();
96 ctf_dict_t *fp;
97 };
98
99 /* Cleanup function for the ctf_dict_key data. */
100 ctf_fp_info::~ctf_fp_info ()
101 {
102 if (fp == nullptr)
103 return;
104
105 ctf_archive_t *arc = ctf_get_arc (fp);
106 ctf_dict_close (fp);
107 ctf_close (arc);
108 }
109
110 static const registry<objfile>::key<ctf_fp_info> ctf_dict_key;
111
112 /* A CTF context consists of a file pointer and an objfile pointer. */
113
114 struct ctf_context
115 {
116 ctf_dict_t *fp;
117 struct objfile *of;
118 psymtab_storage *partial_symtabs;
119 partial_symtab *pst;
120 ctf_archive_t *arc;
121 struct buildsym_compunit *builder;
122 };
123
124 /* A partial symtab, specialized for this module. */
125 struct ctf_psymtab : public standard_psymtab
126 {
127 ctf_psymtab (const char *filename,
128 psymtab_storage *partial_symtabs,
129 objfile_per_bfd_storage *objfile_per_bfd,
130 CORE_ADDR addr)
131 : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
132 {
133 }
134
135 void read_symtab (struct objfile *) override;
136 void expand_psymtab (struct objfile *) override;
137
138 struct ctf_context context;
139 };
140
141 /* The routines that read and process fields/members of a C struct, union,
142 or enumeration, pass lists of data member fields in an instance of a
143 ctf_field_info structure. It is derived from dwarf2read.c. */
144
145 struct ctf_nextfield
146 {
147 struct field field {};
148 };
149
150 struct ctf_field_info
151 {
152 /* List of data member fields. */
153 std::vector<struct ctf_nextfield> fields;
154
155 /* Context. */
156 struct ctf_context *cur_context;
157
158 /* Parent type. */
159 struct type *ptype;
160
161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head
162 of a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
163 std::vector<struct decl_field> typedef_field_list;
164
165 /* Nested types defined by this struct and the number of elements in
166 this list. */
167 std::vector<struct decl_field> nested_types_list;
168 };
169
170 /* Data held for a translation unit. */
171
172 struct ctf_per_tu_data
173 {
174 ctf_dict_t *fp;
175 struct objfile *of;
176 ctf_archive_t *arc;
177 psymtab_storage *pss;
178 psymbol_functions *psf;
179 };
180
181 /* Local function prototypes */
182
183 static int ctf_add_type_cb (ctf_id_t tid, void *arg);
184
185 static struct type *read_array_type (struct ctf_context *cp, ctf_id_t tid);
186
187 static struct type *read_pointer_type (struct ctf_context *cp, ctf_id_t tid,
188 ctf_id_t btid);
189
190 static struct type *read_structure_type (struct ctf_context *cp, ctf_id_t tid);
191
192 static struct type *read_enum_type (struct ctf_context *cp, ctf_id_t tid);
193
194 static struct type *read_typedef_type (struct ctf_context *cp, ctf_id_t tid,
195 ctf_id_t btid, const char *name);
196
197 static struct type *read_type_record (struct ctf_context *cp, ctf_id_t tid);
198
199 static void process_structure_type (struct ctf_context *cp, ctf_id_t tid);
200
201 static void process_struct_members (struct ctf_context *cp, ctf_id_t tid,
202 struct type *type);
203
204 static struct type *read_forward_type (struct ctf_context *cp, ctf_id_t tid);
205
206 static struct symbol *new_symbol (struct ctf_context *cp, struct type *type,
207 ctf_id_t tid);
208
209 struct ctf_tid_and_type
210 {
211 ctf_id_t tid;
212 struct type *type;
213 };
214
215 /* Hash function for a ctf_tid_and_type. */
216
217 static hashval_t
218 tid_and_type_hash (const void *item)
219 {
220 const struct ctf_tid_and_type *ids
221 = (const struct ctf_tid_and_type *) item;
222
223 return ids->tid;
224 }
225
226 /* Equality function for a ctf_tid_and_type. */
227
228 static int
229 tid_and_type_eq (const void *item_lhs, const void *item_rhs)
230 {
231 const struct ctf_tid_and_type *ids_lhs
232 = (const struct ctf_tid_and_type *) item_lhs;
233 const struct ctf_tid_and_type *ids_rhs
234 = (const struct ctf_tid_and_type *) item_rhs;
235
236 return ids_lhs->tid == ids_rhs->tid;
237 }
238
239 /* Set the type associated with TID to TYP. */
240
241 static struct type *
242 set_tid_type (struct objfile *of, ctf_id_t tid, struct type *typ)
243 {
244 htab_t htab;
245
246 htab = ctf_tid_key.get (of);
247 if (htab == NULL)
248 {
249 htab = htab_create_alloc (1, tid_and_type_hash,
250 tid_and_type_eq,
251 NULL, xcalloc, xfree);
252 ctf_tid_key.set (of, htab);
253 }
254
255 struct ctf_tid_and_type **slot, ids;
256 ids.tid = tid;
257 ids.type = typ;
258 slot = (struct ctf_tid_and_type **) htab_find_slot (htab, &ids, INSERT);
259 if (*slot == nullptr)
260 *slot = XOBNEW (&of->objfile_obstack, struct ctf_tid_and_type);
261 **slot = ids;
262 return typ;
263 }
264
265 /* Look up the type for TID in tid_and_type hash, return NULL if hash is
266 empty or TID does not have a saved type. */
267
268 static struct type *
269 get_tid_type (struct objfile *of, ctf_id_t tid)
270 {
271 struct ctf_tid_and_type *slot, ids;
272 htab_t htab;
273
274 htab = ctf_tid_key.get (of);
275 if (htab == NULL)
276 return nullptr;
277
278 ids.tid = tid;
279 ids.type = nullptr;
280 slot = (struct ctf_tid_and_type *) htab_find (htab, &ids);
281 if (slot)
282 return slot->type;
283 else
284 return nullptr;
285 }
286
287 /* Fetch the type for TID in CCP OF's tid_and_type hash, add the type to
288 * context CCP if hash is empty or TID does not have a saved type. */
289
290 static struct type *
291 fetch_tid_type (struct ctf_context *ccp, ctf_id_t tid)
292 {
293 struct objfile *of = ccp->of;
294 struct type *typ;
295
296 typ = get_tid_type (of, tid);
297 if (typ == nullptr)
298 {
299 ctf_add_type_cb (tid, ccp);
300 typ = get_tid_type (of, tid);
301 }
302
303 return typ;
304 }
305
306 /* Return the size of storage in bits for INTEGER, FLOAT, or ENUM. */
307
308 static int
309 get_bitsize (ctf_dict_t *fp, ctf_id_t tid, uint32_t kind)
310 {
311 ctf_encoding_t cet;
312
313 if ((kind == CTF_K_INTEGER || kind == CTF_K_ENUM
314 || kind == CTF_K_FLOAT)
315 && ctf_type_reference (fp, tid) != CTF_ERR
316 && ctf_type_encoding (fp, tid, &cet) != CTF_ERR)
317 return cet.cte_bits;
318
319 return 0;
320 }
321
322 /* Set SYM's address, with NAME, from its minimal symbol entry. */
323
324 static void
325 set_symbol_address (struct objfile *of, struct symbol *sym, const char *name)
326 {
327 struct bound_minimal_symbol msym;
328
329 msym = lookup_minimal_symbol (name, nullptr, of);
330 if (msym.minsym != NULL)
331 {
332 sym->set_value_address (msym.value_address ());
333 sym->set_aclass_index (LOC_STATIC);
334 sym->set_section_index (msym.minsym->section_index ());
335 }
336 }
337
338 /* Create the vector of fields, and attach it to TYPE. */
339
340 static void
341 attach_fields_to_type (struct ctf_field_info *fip, struct type *type)
342 {
343 int nfields = fip->fields.size ();
344
345 if (nfields == 0)
346 return;
347
348 /* Record the field count, allocate space for the array of fields. */
349 type->set_num_fields (nfields);
350 type->set_fields
351 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
352
353 /* Copy the saved-up fields into the field vector. */
354 for (int i = 0; i < nfields; ++i)
355 {
356 struct ctf_nextfield &field = fip->fields[i];
357 type->field (i) = field.field;
358 }
359 }
360
361 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
362 (which may be different from NAME) to the architecture back-end to allow
363 it to guess the correct format if necessary. */
364
365 static struct type *
366 ctf_init_float_type (struct objfile *objfile,
367 int bits,
368 const char *name,
369 const char *name_hint)
370 {
371 struct gdbarch *gdbarch = objfile->arch ();
372 const struct floatformat **format;
373 struct type *type;
374
375 type_allocator alloc (objfile);
376 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
377 if (format != nullptr)
378 type = init_float_type (alloc, bits, name, format);
379 else
380 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
381
382 return type;
383 }
384
385 /* Callback to add member NAME to a struct/union type. TID is the type
386 of struct/union member, OFFSET is the offset of member in bits,
387 and ARG contains the ctf_field_info. */
388
389 static int
390 ctf_add_member_cb (const char *name,
391 ctf_id_t tid,
392 unsigned long offset,
393 void *arg)
394 {
395 struct ctf_field_info *fip = (struct ctf_field_info *) arg;
396 struct ctf_context *ccp = fip->cur_context;
397 struct ctf_nextfield new_field;
398 struct field *fp;
399 struct type *t;
400 uint32_t kind;
401
402 fp = &new_field.field;
403 fp->set_name (name);
404
405 kind = ctf_type_kind (ccp->fp, tid);
406 t = fetch_tid_type (ccp, tid);
407 if (t == nullptr)
408 {
409 t = read_type_record (ccp, tid);
410 if (t == nullptr)
411 {
412 complaint (_("ctf_add_member_cb: %s has NO type (%ld)"), name, tid);
413 t = objfile_type (ccp->of)->builtin_error;
414 set_tid_type (ccp->of, tid, t);
415 }
416 }
417
418 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
419 process_struct_members (ccp, tid, t);
420
421 fp->set_type (t);
422 fp->set_loc_bitpos (offset / TARGET_CHAR_BIT);
423 FIELD_BITSIZE (*fp) = get_bitsize (ccp->fp, tid, kind);
424
425 fip->fields.emplace_back (new_field);
426
427 return 0;
428 }
429
430 /* Callback to add member NAME of EVAL to an enumeration type.
431 ARG contains the ctf_field_info. */
432
433 static int
434 ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)
435 {
436 struct ctf_field_info *fip = (struct ctf_field_info *) arg;
437 struct ctf_nextfield new_field;
438 struct field *fp;
439 struct ctf_context *ccp = fip->cur_context;
440
441 fp = &new_field.field;
442 fp->set_name (name);
443 fp->set_type (nullptr);
444 fp->set_loc_enumval (enum_value);
445 FIELD_BITSIZE (*fp) = 0;
446
447 if (name != nullptr)
448 {
449 struct symbol *sym = new (&ccp->of->objfile_obstack) symbol;
450 OBJSTAT (ccp->of, n_syms++);
451
452 sym->set_language (language_c, &ccp->of->objfile_obstack);
453 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
454 sym->set_aclass_index (LOC_CONST);
455 sym->set_domain (VAR_DOMAIN);
456 sym->set_type (fip->ptype);
457 add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
458 }
459
460 fip->fields.emplace_back (new_field);
461
462 return 0;
463 }
464
465 /* Add a new symbol entry, with its name from TID, its access index and
466 domain from TID's kind, and its type from TYPE. */
467
468 static struct symbol *
469 new_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)
470 {
471 struct objfile *objfile = ccp->of;
472 ctf_dict_t *fp = ccp->fp;
473 struct symbol *sym = nullptr;
474
475 const char *name = ctf_type_name_raw (fp, tid);
476 if (name != nullptr)
477 {
478 sym = new (&objfile->objfile_obstack) symbol;
479 OBJSTAT (objfile, n_syms++);
480
481 sym->set_language (language_c, &objfile->objfile_obstack);
482 sym->compute_and_set_names (name, false, objfile->per_bfd);
483 sym->set_domain (VAR_DOMAIN);
484 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
485
486 if (type != nullptr)
487 sym->set_type (type);
488
489 uint32_t kind = ctf_type_kind (fp, tid);
490 switch (kind)
491 {
492 case CTF_K_STRUCT:
493 case CTF_K_UNION:
494 case CTF_K_ENUM:
495 sym->set_aclass_index (LOC_TYPEDEF);
496 sym->set_domain (STRUCT_DOMAIN);
497 break;
498 case CTF_K_FUNCTION:
499 sym->set_aclass_index (LOC_STATIC);
500 set_symbol_address (objfile, sym, sym->linkage_name ());
501 break;
502 case CTF_K_CONST:
503 if (sym->type ()->code () == TYPE_CODE_VOID)
504 sym->set_type (objfile_type (objfile)->builtin_int);
505 break;
506 case CTF_K_TYPEDEF:
507 case CTF_K_INTEGER:
508 case CTF_K_FLOAT:
509 sym->set_aclass_index (LOC_TYPEDEF);
510 sym->set_domain (VAR_DOMAIN);
511 break;
512 case CTF_K_POINTER:
513 break;
514 case CTF_K_VOLATILE:
515 case CTF_K_RESTRICT:
516 break;
517 case CTF_K_SLICE:
518 case CTF_K_ARRAY:
519 case CTF_K_UNKNOWN:
520 break;
521 }
522
523 add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
524 }
525
526 return sym;
527 }
528
529 /* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
530 and create the symbol for it. */
531
532 static struct type *
533 read_base_type (struct ctf_context *ccp, ctf_id_t tid)
534 {
535 struct objfile *of = ccp->of;
536 ctf_dict_t *fp = ccp->fp;
537 ctf_encoding_t cet;
538 struct type *type = nullptr;
539 const char *name;
540 uint32_t kind;
541
542 if (ctf_type_encoding (fp, tid, &cet))
543 {
544 complaint (_("ctf_type_encoding read_base_type failed - %s"),
545 ctf_errmsg (ctf_errno (fp)));
546 return nullptr;
547 }
548
549 name = ctf_type_name_raw (fp, tid);
550 if (name == nullptr || strlen (name) == 0)
551 {
552 name = ctf_type_aname (fp, tid);
553 if (name == nullptr)
554 complaint (_("ctf_type_aname read_base_type failed - %s"),
555 ctf_errmsg (ctf_errno (fp)));
556 }
557
558 type_allocator alloc (of);
559 kind = ctf_type_kind (fp, tid);
560 if (kind == CTF_K_INTEGER)
561 {
562 uint32_t issigned, ischar, isbool;
563 struct gdbarch *gdbarch = of->arch ();
564
565 issigned = cet.cte_format & CTF_INT_SIGNED;
566 ischar = cet.cte_format & CTF_INT_CHAR;
567 isbool = cet.cte_format & CTF_INT_BOOL;
568 if (ischar)
569 type = init_character_type (alloc, TARGET_CHAR_BIT, !issigned, name);
570 else if (isbool)
571 type = init_boolean_type (alloc, gdbarch_int_bit (gdbarch),
572 !issigned, name);
573 else
574 {
575 int bits;
576 if (cet.cte_bits && ((cet.cte_bits % TARGET_CHAR_BIT) == 0))
577 bits = cet.cte_bits;
578 else
579 bits = gdbarch_int_bit (gdbarch);
580 type = init_integer_type (alloc, bits, !issigned, name);
581 }
582 }
583 else if (kind == CTF_K_FLOAT)
584 {
585 uint32_t isflt;
586 isflt = !((cet.cte_format & CTF_FP_IMAGRY) == CTF_FP_IMAGRY
587 || (cet.cte_format & CTF_FP_DIMAGRY) == CTF_FP_DIMAGRY
588 || (cet.cte_format & CTF_FP_LDIMAGRY) == CTF_FP_LDIMAGRY);
589 if (isflt)
590 type = ctf_init_float_type (of, cet.cte_bits, name, name);
591 else
592 {
593 struct type *t
594 = ctf_init_float_type (of, cet.cte_bits / 2, NULL, name);
595 type = init_complex_type (name, t);
596 }
597 }
598 else
599 {
600 complaint (_("read_base_type: unsupported base kind (%d)"), kind);
601 type = alloc.new_type (TYPE_CODE_ERROR, cet.cte_bits, name);
602 }
603
604 if (name != nullptr && strcmp (name, "char") == 0)
605 type->set_has_no_signedness (true);
606
607 return set_tid_type (of, tid, type);
608 }
609
610 static void
611 process_base_type (struct ctf_context *ccp, ctf_id_t tid)
612 {
613 struct type *type;
614
615 type = read_base_type (ccp, tid);
616 new_symbol (ccp, type, tid);
617 }
618
619 /* Start a structure or union scope (definition) with TID to create a type
620 for the structure or union.
621
622 Fill in the type's name and general properties. The members will not be
623 processed, nor a symbol table entry be done until process_structure_type
624 (assuming the type has a name). */
625
626 static struct type *
627 read_structure_type (struct ctf_context *ccp, ctf_id_t tid)
628 {
629 struct objfile *of = ccp->of;
630 ctf_dict_t *fp = ccp->fp;
631 struct type *type;
632 uint32_t kind;
633
634 type = type_allocator (of).new_type ();
635
636 const char *name = ctf_type_name_raw (fp, tid);
637 if (name != nullptr && strlen (name) != 0)
638 type->set_name (name);
639
640 kind = ctf_type_kind (fp, tid);
641 if (kind == CTF_K_UNION)
642 type->set_code (TYPE_CODE_UNION);
643 else
644 type->set_code (TYPE_CODE_STRUCT);
645
646 type->set_length (ctf_type_size (fp, tid));
647 set_type_align (type, ctf_type_align (fp, tid));
648
649 return set_tid_type (ccp->of, tid, type);
650 }
651
652 /* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
653 and create the symbol for it. */
654
655 static void
656 process_struct_members (struct ctf_context *ccp,
657 ctf_id_t tid,
658 struct type *type)
659 {
660 struct ctf_field_info fi;
661
662 fi.cur_context = ccp;
663 if (ctf_member_iter (ccp->fp, tid, ctf_add_member_cb, &fi) == CTF_ERR)
664 complaint (_("ctf_member_iter process_struct_members failed - %s"),
665 ctf_errmsg (ctf_errno (ccp->fp)));
666
667 /* Attach fields to the type. */
668 attach_fields_to_type (&fi, type);
669
670 new_symbol (ccp, type, tid);
671 }
672
673 static void
674 process_structure_type (struct ctf_context *ccp, ctf_id_t tid)
675 {
676 struct type *type;
677
678 type = read_structure_type (ccp, tid);
679 process_struct_members (ccp, tid, type);
680 }
681
682 /* Create a function type for TID and set its return type. */
683
684 static struct type *
685 read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
686 {
687 struct objfile *of = ccp->of;
688 ctf_dict_t *fp = ccp->fp;
689 struct type *type, *rettype, *atype;
690 ctf_funcinfo_t cfi;
691 uint32_t argc;
692
693 type = type_allocator (of).new_type ();
694
695 type->set_code (TYPE_CODE_FUNC);
696 if (ctf_func_type_info (fp, tid, &cfi) < 0)
697 {
698 const char *fname = ctf_type_name_raw (fp, tid);
699 error (_("Error getting function type info: %s"),
700 fname == nullptr ? "noname" : fname);
701 }
702 rettype = fetch_tid_type (ccp, cfi.ctc_return);
703 type->set_target_type (rettype);
704 set_type_align (type, ctf_type_align (fp, tid));
705
706 /* Set up function's arguments. */
707 argc = cfi.ctc_argc;
708 type->set_num_fields (argc);
709 if ((cfi.ctc_flags & CTF_FUNC_VARARG) != 0)
710 type->set_has_varargs (true);
711
712 if (argc != 0)
713 {
714 std::vector<ctf_id_t> argv (argc);
715 if (ctf_func_type_args (fp, tid, argc, argv.data ()) == CTF_ERR)
716 return nullptr;
717
718 type->set_fields
719 ((struct field *) TYPE_ZALLOC (type, argc * sizeof (struct field)));
720 struct type *void_type = objfile_type (of)->builtin_void;
721 /* If failed to find the argument type, fill it with void_type. */
722 for (int iparam = 0; iparam < argc; iparam++)
723 {
724 atype = fetch_tid_type (ccp, argv[iparam]);
725 if (atype != nullptr)
726 type->field (iparam).set_type (atype);
727 else
728 type->field (iparam).set_type (void_type);
729 }
730 }
731
732 return set_tid_type (of, tid, type);
733 }
734
735 /* Given a TID of CTF_K_ENUM, process all the members of the
736 enumeration, and create the symbol for the enumeration type. */
737
738 static struct type *
739 read_enum_type (struct ctf_context *ccp, ctf_id_t tid)
740 {
741 struct objfile *of = ccp->of;
742 ctf_dict_t *fp = ccp->fp;
743 struct type *type;
744
745 type = type_allocator (of).new_type ();
746
747 const char *name = ctf_type_name_raw (fp, tid);
748 if (name != nullptr && strlen (name) != 0)
749 type->set_name (name);
750
751 type->set_code (TYPE_CODE_ENUM);
752 type->set_length (ctf_type_size (fp, tid));
753 /* Set the underlying type based on its ctf_type_size bits. */
754 type->set_target_type (objfile_int_type (of, type->length (), false));
755 set_type_align (type, ctf_type_align (fp, tid));
756
757 return set_tid_type (of, tid, type);
758 }
759
760 static void
761 process_enum_type (struct ctf_context *ccp, ctf_id_t tid)
762 {
763 struct type *type;
764 struct ctf_field_info fi;
765
766 type = read_enum_type (ccp, tid);
767
768 fi.cur_context = ccp;
769 fi.ptype = type;
770 if (ctf_enum_iter (ccp->fp, tid, ctf_add_enum_member_cb, &fi) == CTF_ERR)
771 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
772 ctf_errmsg (ctf_errno (ccp->fp)));
773
774 /* Attach fields to the type. */
775 attach_fields_to_type (&fi, type);
776
777 new_symbol (ccp, type, tid);
778 }
779
780 /* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
781
782 static struct type *
783 add_array_cv_type (struct ctf_context *ccp,
784 ctf_id_t tid,
785 struct type *base_type,
786 int cnst,
787 int voltl)
788 {
789 struct type *el_type, *inner_array;
790
791 base_type = copy_type (base_type);
792 inner_array = base_type;
793
794 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
795 {
796 inner_array->set_target_type (copy_type (inner_array->target_type ()));
797 inner_array = inner_array->target_type ();
798 }
799
800 el_type = inner_array->target_type ();
801 cnst |= TYPE_CONST (el_type);
802 voltl |= TYPE_VOLATILE (el_type);
803 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, nullptr));
804
805 return set_tid_type (ccp->of, tid, base_type);
806 }
807
808 /* Read all information from a TID of CTF_K_ARRAY. */
809
810 static struct type *
811 read_array_type (struct ctf_context *ccp, ctf_id_t tid)
812 {
813 struct objfile *objfile = ccp->of;
814 ctf_dict_t *fp = ccp->fp;
815 struct type *element_type, *range_type, *idx_type;
816 struct type *type;
817 ctf_arinfo_t ar;
818
819 if (ctf_array_info (fp, tid, &ar) == CTF_ERR)
820 {
821 complaint (_("ctf_array_info read_array_type failed - %s"),
822 ctf_errmsg (ctf_errno (fp)));
823 return nullptr;
824 }
825
826 element_type = fetch_tid_type (ccp, ar.ctr_contents);
827 if (element_type == nullptr)
828 return nullptr;
829
830 idx_type = fetch_tid_type (ccp, ar.ctr_index);
831 if (idx_type == nullptr)
832 idx_type = objfile_type (objfile)->builtin_int;
833
834 type_allocator alloc (objfile);
835 range_type = create_static_range_type (alloc, idx_type, 0, ar.ctr_nelems - 1);
836 type = create_array_type (NULL, element_type, range_type);
837 if (ar.ctr_nelems <= 1) /* Check if undefined upper bound. */
838 {
839 range_type->bounds ()->high.set_undefined ();
840 type->set_length (0);
841 type->set_target_is_stub (true);
842 }
843 else
844 type->set_length (ctf_type_size (fp, tid));
845
846 set_type_align (type, ctf_type_align (fp, tid));
847
848 return set_tid_type (objfile, tid, type);
849 }
850
851 /* Read TID of kind CTF_K_CONST with base type BTID. */
852
853 static struct type *
854 read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
855 {
856 struct objfile *objfile = ccp->of;
857 struct type *base_type, *cv_type;
858
859 base_type = fetch_tid_type (ccp, btid);
860 if (base_type == nullptr)
861 {
862 base_type = read_type_record (ccp, btid);
863 if (base_type == nullptr)
864 {
865 complaint (_("read_const_type: NULL base type (%ld)"), btid);
866 base_type = objfile_type (objfile)->builtin_error;
867 }
868 }
869 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
870
871 return set_tid_type (objfile, tid, cv_type);
872 }
873
874 /* Read TID of kind CTF_K_VOLATILE with base type BTID. */
875
876 static struct type *
877 read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
878 {
879 struct objfile *objfile = ccp->of;
880 ctf_dict_t *fp = ccp->fp;
881 struct type *base_type, *cv_type;
882
883 base_type = fetch_tid_type (ccp, btid);
884 if (base_type == nullptr)
885 {
886 base_type = read_type_record (ccp, btid);
887 if (base_type == nullptr)
888 {
889 complaint (_("read_volatile_type: NULL base type (%ld)"), btid);
890 base_type = objfile_type (objfile)->builtin_error;
891 }
892 }
893
894 if (ctf_type_kind (fp, btid) == CTF_K_ARRAY)
895 return add_array_cv_type (ccp, tid, base_type, 0, 1);
896 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
897
898 return set_tid_type (objfile, tid, cv_type);
899 }
900
901 /* Read TID of kind CTF_K_RESTRICT with base type BTID. */
902
903 static struct type *
904 read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
905 {
906 struct objfile *objfile = ccp->of;
907 struct type *base_type, *cv_type;
908
909 base_type = fetch_tid_type (ccp, btid);
910 if (base_type == nullptr)
911 {
912 base_type = read_type_record (ccp, btid);
913 if (base_type == nullptr)
914 {
915 complaint (_("read_restrict_type: NULL base type (%ld)"), btid);
916 base_type = objfile_type (objfile)->builtin_error;
917 }
918 }
919 cv_type = make_restrict_type (base_type);
920
921 return set_tid_type (objfile, tid, cv_type);
922 }
923
924 /* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
925
926 static struct type *
927 read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
928 ctf_id_t btid, const char *name)
929 {
930 struct objfile *objfile = ccp->of;
931 struct type *this_type, *target_type;
932
933 char *aname = obstack_strdup (&objfile->objfile_obstack, name);
934 this_type = type_allocator (objfile).new_type (TYPE_CODE_TYPEDEF, 0, aname);
935 set_tid_type (objfile, tid, this_type);
936 target_type = fetch_tid_type (ccp, btid);
937 if (target_type != this_type)
938 this_type->set_target_type (target_type);
939 else
940 this_type->set_target_type (nullptr);
941
942 this_type->set_target_is_stub (this_type->target_type () != nullptr);
943
944 return set_tid_type (objfile, tid, this_type);
945 }
946
947 /* Read TID of kind CTF_K_POINTER with base type BTID. */
948
949 static struct type *
950 read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
951 {
952 struct objfile *of = ccp->of;
953 struct type *target_type, *type;
954
955 target_type = fetch_tid_type (ccp, btid);
956 if (target_type == nullptr)
957 {
958 target_type = read_type_record (ccp, btid);
959 if (target_type == nullptr)
960 {
961 complaint (_("read_pointer_type: NULL target type (%ld)"), btid);
962 target_type = objfile_type (ccp->of)->builtin_error;
963 }
964 }
965
966 type = lookup_pointer_type (target_type);
967 set_type_align (type, ctf_type_align (ccp->fp, tid));
968
969 return set_tid_type (of, tid, type);
970 }
971
972 /* Read information from a TID of CTF_K_FORWARD. */
973
974 static struct type *
975 read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
976 {
977 struct objfile *of = ccp->of;
978 ctf_dict_t *fp = ccp->fp;
979 struct type *type;
980 uint32_t kind;
981
982 type = type_allocator (of).new_type ();
983
984 const char *name = ctf_type_name_raw (fp, tid);
985 if (name != nullptr && strlen (name) != 0)
986 type->set_name (name);
987
988 kind = ctf_type_kind_forwarded (fp, tid);
989 if (kind == CTF_K_UNION)
990 type->set_code (TYPE_CODE_UNION);
991 else
992 type->set_code (TYPE_CODE_STRUCT);
993
994 type->set_length (0);
995 type->set_is_stub (true);
996
997 return set_tid_type (of, tid, type);
998 }
999
1000 /* Read information associated with type TID. */
1001
1002 static struct type *
1003 read_type_record (struct ctf_context *ccp, ctf_id_t tid)
1004 {
1005 ctf_dict_t *fp = ccp->fp;
1006 uint32_t kind;
1007 struct type *type = nullptr;
1008 ctf_id_t btid;
1009
1010 kind = ctf_type_kind (fp, tid);
1011 switch (kind)
1012 {
1013 case CTF_K_STRUCT:
1014 case CTF_K_UNION:
1015 type = read_structure_type (ccp, tid);
1016 break;
1017 case CTF_K_ENUM:
1018 type = read_enum_type (ccp, tid);
1019 break;
1020 case CTF_K_FUNCTION:
1021 type = read_func_kind_type (ccp, tid);
1022 break;
1023 case CTF_K_CONST:
1024 btid = ctf_type_reference (fp, tid);
1025 type = read_const_type (ccp, tid, btid);
1026 break;
1027 case CTF_K_TYPEDEF:
1028 {
1029 const char *name = ctf_type_name_raw (fp, tid);
1030 btid = ctf_type_reference (fp, tid);
1031 type = read_typedef_type (ccp, tid, btid, name);
1032 }
1033 break;
1034 case CTF_K_VOLATILE:
1035 btid = ctf_type_reference (fp, tid);
1036 type = read_volatile_type (ccp, tid, btid);
1037 break;
1038 case CTF_K_RESTRICT:
1039 btid = ctf_type_reference (fp, tid);
1040 type = read_restrict_type (ccp, tid, btid);
1041 break;
1042 case CTF_K_POINTER:
1043 btid = ctf_type_reference (fp, tid);
1044 type = read_pointer_type (ccp, tid, btid);
1045 break;
1046 case CTF_K_INTEGER:
1047 case CTF_K_FLOAT:
1048 type = read_base_type (ccp, tid);
1049 break;
1050 case CTF_K_ARRAY:
1051 type = read_array_type (ccp, tid);
1052 break;
1053 case CTF_K_FORWARD:
1054 type = read_forward_type (ccp, tid);
1055 break;
1056 case CTF_K_UNKNOWN:
1057 break;
1058 default:
1059 break;
1060 }
1061
1062 return type;
1063 }
1064
1065 /* Callback to add type TID to the symbol table. */
1066
1067 static int
1068 ctf_add_type_cb (ctf_id_t tid, void *arg)
1069 {
1070 struct ctf_context *ccp = (struct ctf_context *) arg;
1071 struct type *type;
1072 uint32_t kind;
1073
1074 /* Check if tid's type has already been defined. */
1075 type = get_tid_type (ccp->of, tid);
1076 if (type != nullptr)
1077 return 0;
1078
1079 ctf_id_t btid = ctf_type_reference (ccp->fp, tid);
1080 kind = ctf_type_kind (ccp->fp, tid);
1081 switch (kind)
1082 {
1083 case CTF_K_STRUCT:
1084 case CTF_K_UNION:
1085 process_structure_type (ccp, tid);
1086 break;
1087 case CTF_K_ENUM:
1088 process_enum_type (ccp, tid);
1089 break;
1090 case CTF_K_FUNCTION:
1091 type = read_func_kind_type (ccp, tid);
1092 new_symbol (ccp, type, tid);
1093 break;
1094 case CTF_K_INTEGER:
1095 case CTF_K_FLOAT:
1096 process_base_type (ccp, tid);
1097 break;
1098 case CTF_K_TYPEDEF:
1099 new_symbol (ccp, read_type_record (ccp, tid), tid);
1100 break;
1101 case CTF_K_CONST:
1102 type = read_const_type (ccp, tid, btid);
1103 new_symbol (ccp, type, tid);
1104 break;
1105 case CTF_K_VOLATILE:
1106 type = read_volatile_type (ccp, tid, btid);
1107 new_symbol (ccp, type, tid);
1108 break;
1109 case CTF_K_RESTRICT:
1110 type = read_restrict_type (ccp, tid, btid);
1111 new_symbol (ccp, type, tid);
1112 break;
1113 case CTF_K_POINTER:
1114 type = read_pointer_type (ccp, tid, btid);
1115 new_symbol (ccp, type, tid);
1116 break;
1117 case CTF_K_ARRAY:
1118 type = read_array_type (ccp, tid);
1119 new_symbol (ccp, type, tid);
1120 break;
1121 case CTF_K_UNKNOWN:
1122 break;
1123 default:
1124 break;
1125 }
1126
1127 return 0;
1128 }
1129
1130 /* Callback to add variable NAME with TID to the symbol table. */
1131
1132 static int
1133 ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
1134 {
1135 struct ctf_context *ccp = (struct ctf_context *) arg;
1136 struct symbol *sym = nullptr;
1137 struct type *type;
1138 uint32_t kind;
1139
1140 type = get_tid_type (ccp->of, id);
1141
1142 kind = ctf_type_kind (ccp->fp, id);
1143 switch (kind)
1144 {
1145 case CTF_K_FUNCTION:
1146 if (name != nullptr && strcmp (name, "main") == 0)
1147 set_objfile_main_name (ccp->of, name, language_c);
1148 break;
1149 case CTF_K_INTEGER:
1150 case CTF_K_FLOAT:
1151 case CTF_K_VOLATILE:
1152 case CTF_K_RESTRICT:
1153 case CTF_K_TYPEDEF:
1154 case CTF_K_CONST:
1155 case CTF_K_POINTER:
1156 case CTF_K_ARRAY:
1157 if (type != nullptr)
1158 {
1159 sym = new_symbol (ccp, type, id);
1160 if (sym != nullptr)
1161 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
1162 }
1163 break;
1164 case CTF_K_STRUCT:
1165 case CTF_K_UNION:
1166 case CTF_K_ENUM:
1167 if (type == nullptr)
1168 {
1169 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
1170 type = objfile_type (ccp->of)->builtin_error;
1171 }
1172 sym = new (&ccp->of->objfile_obstack) symbol;
1173 OBJSTAT (ccp->of, n_syms++);
1174 sym->set_type (type);
1175 sym->set_domain (VAR_DOMAIN);
1176 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
1177 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
1178 add_symbol_to_list (sym, ccp->builder->get_file_symbols ());
1179 break;
1180 default:
1181 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind);
1182 break;
1183 }
1184
1185 if (sym != nullptr)
1186 set_symbol_address (ccp->of, sym, name);
1187
1188 return 0;
1189 }
1190
1191 /* Add entries in either data objects or function info section, controlled
1192 by FUNCTIONS. */
1193
1194 static void
1195 add_stt_entries (struct ctf_context *ccp, int functions)
1196 {
1197 ctf_next_t *i = nullptr;
1198 const char *tname;
1199 ctf_id_t tid;
1200 struct symbol *sym = nullptr;
1201 struct type *type;
1202
1203 while ((tid = ctf_symbol_next (ccp->fp, &i, &tname, functions)) != CTF_ERR)
1204 {
1205 type = get_tid_type (ccp->of, tid);
1206 if (type == nullptr)
1207 continue;
1208 sym = new (&ccp->of->objfile_obstack) symbol;
1209 OBJSTAT (ccp->of, n_syms++);
1210 sym->set_type (type);
1211 sym->set_domain (VAR_DOMAIN);
1212 sym->set_aclass_index (LOC_STATIC);
1213 sym->compute_and_set_names (tname, false, ccp->of->per_bfd);
1214 add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
1215 set_symbol_address (ccp->of, sym, tname);
1216 }
1217 }
1218
1219 /* Add entries in data objects section. */
1220
1221 static void
1222 add_stt_obj (struct ctf_context *ccp)
1223 {
1224 add_stt_entries (ccp, 0);
1225 }
1226
1227 /* Add entries in function info section. */
1228
1229 static void
1230 add_stt_func (struct ctf_context *ccp)
1231 {
1232 add_stt_entries (ccp, 1);
1233 }
1234
1235 /* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1236
1237 static CORE_ADDR
1238 get_objfile_text_range (struct objfile *of, int *tsize)
1239 {
1240 bfd *abfd = of->obfd.get ();
1241 const asection *codes;
1242
1243 codes = bfd_get_section_by_name (abfd, ".text");
1244 *tsize = codes ? bfd_section_size (codes) : 0;
1245 return of->text_section_offset ();
1246 }
1247
1248 /* Start a symtab for OBJFILE in CTF format. */
1249
1250 static void
1251 ctf_start_compunit_symtab (ctf_psymtab *pst,
1252 struct objfile *of, CORE_ADDR text_offset)
1253 {
1254 struct ctf_context *ccp;
1255
1256 ccp = &pst->context;
1257 ccp->builder = new buildsym_compunit
1258 (of, pst->filename, nullptr,
1259 language_c, text_offset);
1260 ccp->builder->record_debugformat ("ctf");
1261 }
1262
1263 /* Finish reading symbol/type definitions in CTF format.
1264 END_ADDR is the end address of the file's text. */
1265
1266 static struct compunit_symtab *
1267 ctf_end_compunit_symtab (ctf_psymtab *pst,
1268 CORE_ADDR end_addr)
1269 {
1270 struct ctf_context *ccp;
1271
1272 ccp = &pst->context;
1273 struct compunit_symtab *result
1274 = ccp->builder->end_compunit_symtab (end_addr);
1275 delete ccp->builder;
1276 ccp->builder = nullptr;
1277 return result;
1278 }
1279
1280 /* Add all members of an enum with type TID to partial symbol table. */
1281
1282 static void
1283 ctf_psymtab_add_enums (struct ctf_context *ccp, ctf_id_t tid)
1284 {
1285 int val;
1286 const char *ename;
1287 ctf_next_t *i = nullptr;
1288
1289 while ((ename = ctf_enum_next (ccp->fp, tid, &i, &val)) != nullptr)
1290 {
1291 ccp->pst->add_psymbol (ename, true,
1292 VAR_DOMAIN, LOC_CONST, -1,
1293 psymbol_placement::GLOBAL,
1294 0, language_c, ccp->partial_symtabs, ccp->of);
1295 }
1296 if (ctf_errno (ccp->fp) != ECTF_NEXT_END)
1297 complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
1298 ctf_errmsg (ctf_errno (ccp->fp)));
1299 }
1300
1301 /* Add entries in either data objects or function info section, controlled
1302 by FUNCTIONS, to psymtab. */
1303
1304 static void
1305 ctf_psymtab_add_stt_entries (ctf_dict_t *cfp, ctf_psymtab *pst,
1306 struct objfile *of, int functions)
1307 {
1308 ctf_next_t *i = nullptr;
1309 ctf_id_t tid;
1310 const char *tname;
1311
1312 while ((tid = ctf_symbol_next (cfp, &i, &tname, functions)) != CTF_ERR)
1313 {
1314 uint32_t kind = ctf_type_kind (cfp, tid);
1315 address_class aclass;
1316 domain_enum tdomain;
1317 switch (kind)
1318 {
1319 case CTF_K_STRUCT:
1320 case CTF_K_UNION:
1321 case CTF_K_ENUM:
1322 tdomain = STRUCT_DOMAIN;
1323 break;
1324 default:
1325 tdomain = VAR_DOMAIN;
1326 break;
1327 }
1328
1329 if (kind == CTF_K_FUNCTION)
1330 aclass = LOC_STATIC;
1331 else if (kind == CTF_K_CONST)
1332 aclass = LOC_CONST;
1333 else
1334 aclass = LOC_TYPEDEF;
1335
1336 pst->add_psymbol (tname, true,
1337 tdomain, aclass, -1,
1338 psymbol_placement::GLOBAL,
1339 0, language_c, pst->context.partial_symtabs, of);
1340 }
1341 }
1342
1343 /* Add entries in data objects section to psymtab. */
1344
1345 static void
1346 ctf_psymtab_add_stt_obj (ctf_dict_t *cfp, ctf_psymtab *pst,
1347 struct objfile *of)
1348 {
1349 ctf_psymtab_add_stt_entries (cfp, pst, of, 0);
1350 }
1351
1352 /* Add entries in function info section to psymtab. */
1353
1354 static void
1355 ctf_psymtab_add_stt_func (ctf_dict_t *cfp, ctf_psymtab *pst,
1356 struct objfile *of)
1357 {
1358 ctf_psymtab_add_stt_entries (cfp, pst, of, 1);
1359 }
1360
1361 /* Read in full symbols for PST, and anything it depends on. */
1362
1363 void
1364 ctf_psymtab::expand_psymtab (struct objfile *objfile)
1365 {
1366 struct ctf_context *ccp;
1367
1368 gdb_assert (!readin);
1369
1370 ccp = &context;
1371
1372 /* Iterate over entries in data types section. */
1373 if (ctf_type_iter (ccp->fp, ctf_add_type_cb, ccp) == CTF_ERR)
1374 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1375 ctf_errmsg (ctf_errno (ccp->fp)));
1376
1377
1378 /* Iterate over entries in variable info section. */
1379 if (ctf_variable_iter (ccp->fp, ctf_add_var_cb, ccp) == CTF_ERR)
1380 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1381 ctf_errmsg (ctf_errno (ccp->fp)));
1382
1383 /* Add entries in data objects and function info sections. */
1384 add_stt_obj (ccp);
1385 add_stt_func (ccp);
1386
1387 readin = true;
1388 }
1389
1390 /* Expand partial symbol table PST into a full symbol table.
1391 PST is not NULL. */
1392
1393 void
1394 ctf_psymtab::read_symtab (struct objfile *objfile)
1395 {
1396 if (readin)
1397 warning (_("bug: psymtab for %s is already read in."), filename);
1398 else
1399 {
1400 if (info_verbose)
1401 {
1402 gdb_printf (_("Reading in CTF data for %s..."), filename);
1403 gdb_flush (gdb_stdout);
1404 }
1405
1406 /* Start a symtab. */
1407 CORE_ADDR offset; /* Start of text segment. */
1408 int tsize;
1409
1410 offset = get_objfile_text_range (objfile, &tsize);
1411 ctf_start_compunit_symtab (this, objfile, offset);
1412 expand_psymtab (objfile);
1413
1414 set_text_low (offset);
1415 set_text_high (offset + tsize);
1416 compunit_symtab = ctf_end_compunit_symtab (this, offset + tsize);
1417
1418 /* Finish up the debug error message. */
1419 if (info_verbose)
1420 gdb_printf (_("done.\n"));
1421 }
1422 }
1423
1424 /* Allocate a new partial_symtab NAME.
1425
1426 Each source file that has not been fully read in is represented by
1427 a partial_symtab. This contains the information on where in the
1428 executable the debugging symbols for a specific file are, and a
1429 list of names of global symbols which are located in this file.
1430 They are all chained on partial symtab lists.
1431
1432 Even after the source file has been read into a symtab, the
1433 partial_symtab remains around. They are allocated on an obstack,
1434 objfile_obstack. */
1435
1436 static ctf_psymtab *
1437 create_partial_symtab (const char *name,
1438 ctf_archive_t *arc,
1439 ctf_dict_t *cfp,
1440 psymtab_storage *partial_symtabs,
1441 struct objfile *objfile)
1442 {
1443 ctf_psymtab *pst;
1444
1445 pst = new ctf_psymtab (name, partial_symtabs, objfile->per_bfd, 0);
1446
1447 pst->context.arc = arc;
1448 pst->context.fp = cfp;
1449 pst->context.of = objfile;
1450 pst->context.partial_symtabs = partial_symtabs;
1451 pst->context.pst = pst;
1452 pst->context.builder = nullptr;
1453
1454 return pst;
1455 }
1456
1457 /* Callback to add type TID to partial symbol table. */
1458
1459 static int
1460 ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
1461 {
1462 struct ctf_context *ccp;
1463 uint32_t kind;
1464 short section = -1;
1465
1466 ccp = (struct ctf_context *) arg;
1467
1468 domain_enum domain = UNDEF_DOMAIN;
1469 enum address_class aclass = LOC_UNDEF;
1470 kind = ctf_type_kind (ccp->fp, tid);
1471 switch (kind)
1472 {
1473 case CTF_K_ENUM:
1474 ctf_psymtab_add_enums (ccp, tid);
1475 /* FALL THROUGH */
1476 case CTF_K_STRUCT:
1477 case CTF_K_UNION:
1478 domain = STRUCT_DOMAIN;
1479 aclass = LOC_TYPEDEF;
1480 break;
1481 case CTF_K_FUNCTION:
1482 case CTF_K_FORWARD:
1483 domain = VAR_DOMAIN;
1484 aclass = LOC_STATIC;
1485 section = SECT_OFF_TEXT (ccp->of);
1486 break;
1487 case CTF_K_CONST:
1488 domain = VAR_DOMAIN;
1489 aclass = LOC_STATIC;
1490 break;
1491 case CTF_K_TYPEDEF:
1492 case CTF_K_POINTER:
1493 case CTF_K_VOLATILE:
1494 case CTF_K_RESTRICT:
1495 domain = VAR_DOMAIN;
1496 aclass = LOC_TYPEDEF;
1497 break;
1498 case CTF_K_INTEGER:
1499 case CTF_K_FLOAT:
1500 domain = VAR_DOMAIN;
1501 aclass = LOC_TYPEDEF;
1502 break;
1503 case CTF_K_ARRAY:
1504 case CTF_K_UNKNOWN:
1505 return 0;
1506 }
1507
1508 const char *name = ctf_type_name_raw (ccp->fp, tid);
1509 if (name == nullptr || strlen (name) == 0)
1510 return 0;
1511
1512 ccp->pst->add_psymbol (name, false,
1513 domain, aclass, section,
1514 psymbol_placement::STATIC,
1515 0, language_c, ccp->partial_symtabs, ccp->of);
1516
1517 return 0;
1518 }
1519
1520 /* Callback to add variable NAME with ID to partial symbol table. */
1521
1522 static int
1523 ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
1524 {
1525 struct ctf_context *ccp = (struct ctf_context *) arg;
1526
1527 ccp->pst->add_psymbol (name, true,
1528 VAR_DOMAIN, LOC_STATIC, -1,
1529 psymbol_placement::GLOBAL,
1530 0, language_c, ccp->partial_symtabs, ccp->of);
1531 return 0;
1532 }
1533
1534 /* Setup partial_symtab's describing each source file for which
1535 debugging information is available. */
1536
1537 static void
1538 scan_partial_symbols (ctf_dict_t *cfp, psymtab_storage *partial_symtabs,
1539 struct ctf_per_tu_data *tup, const char *fname)
1540 {
1541 struct objfile *of = tup->of;
1542 bool isparent = false;
1543
1544 if (strcmp (fname, ".ctf") == 0)
1545 {
1546 fname = bfd_get_filename (of->obfd.get ());
1547 isparent = true;
1548 }
1549
1550 ctf_psymtab *pst = create_partial_symtab (fname, tup->arc, cfp,
1551 partial_symtabs, of);
1552
1553 struct ctf_context *ccx = &pst->context;
1554 if (isparent == false)
1555 ccx->pst = pst;
1556
1557 if (ctf_type_iter (cfp, ctf_psymtab_type_cb, ccx) == CTF_ERR)
1558 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1559 ctf_errmsg (ctf_errno (cfp)));
1560
1561 if (ctf_variable_iter (cfp, ctf_psymtab_var_cb, ccx) == CTF_ERR)
1562 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1563 ctf_errmsg (ctf_errno (cfp)));
1564
1565 /* Scan CTF object and function sections which correspond to each
1566 STT_FUNC or STT_OBJECT entry in the symbol table,
1567 pick up what init_symtab has done. */
1568 ctf_psymtab_add_stt_obj (cfp, pst, of);
1569 ctf_psymtab_add_stt_func (cfp, pst, of);
1570
1571 pst->end ();
1572 }
1573
1574 /* Callback to build the psymtab for archive member NAME. */
1575
1576 static int
1577 build_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
1578 {
1579 struct ctf_per_tu_data *tup = (struct ctf_per_tu_data *) arg;
1580 ctf_dict_t *parent = tup->fp;
1581
1582 if (strcmp (name, ".ctf") != 0)
1583 ctf_import (ctf, parent);
1584
1585 if (info_verbose)
1586 {
1587 gdb_printf (_("Scanning archive member %s..."), name);
1588 gdb_flush (gdb_stdout);
1589 }
1590
1591 psymtab_storage *pss = tup->psf->get_partial_symtabs ().get ();
1592 scan_partial_symbols (ctf, pss, tup, name);
1593
1594 return 0;
1595 }
1596
1597 /* Read CTF debugging information from a BFD section. This is
1598 called from elfread.c. It does a quick pass through the
1599 .ctf section to set up the partial symbol table. */
1600
1601 void
1602 elfctf_build_psymtabs (struct objfile *of)
1603 {
1604 struct ctf_per_tu_data pcu;
1605 bfd *abfd = of->obfd.get ();
1606 int err;
1607
1608 ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
1609 if (arc == nullptr)
1610 error (_("ctf_bfdopen failed on %s - %s"),
1611 bfd_get_filename (abfd), ctf_errmsg (err));
1612
1613 ctf_dict_t *fp = ctf_dict_open (arc, NULL, &err);
1614 if (fp == nullptr)
1615 error (_("ctf_dict_open failed on %s - %s"),
1616 bfd_get_filename (abfd), ctf_errmsg (err));
1617 ctf_dict_key.emplace (of, fp);
1618
1619 pcu.fp = fp;
1620 pcu.of = of;
1621 pcu.arc = arc;
1622
1623 psymbol_functions *psf = new psymbol_functions ();
1624 of->qf.emplace_front (psf);
1625 pcu.psf = psf;
1626
1627 if (ctf_archive_iter (arc, build_ctf_archive_member, &pcu) < 0)
1628 error (_("ctf_archive_iter failed in input file %s: - %s"),
1629 bfd_get_filename (abfd), ctf_errmsg (err));
1630 }
1631
1632 #else
1633
1634 void
1635 elfctf_build_psymtabs (struct objfile *of)
1636 {
1637 /* Nothing to do if CTF is disabled. */
1638 }
1639
1640 #endif /* ENABLE_LIBCTF */