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