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