gdb/mi: add new --group-by-objfile flag for -file-list-exec-source-files
[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 464
ea11a98d 465 const char *name = ctf_type_name_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);
ea11a98d 472 sym->compute_and_set_names (name, false, 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;
ea11a98d 490 set_symbol_address (objfile, sym, sym->linkage_name ());
30d1f018
WP
491 break;
492 case CTF_K_CONST:
78134374 493 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
30d1f018
WP
494 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
495 break;
496 case CTF_K_TYPEDEF:
497 case CTF_K_INTEGER:
498 case CTF_K_FLOAT:
499 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
500 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
501 break;
502 case CTF_K_POINTER:
503 break;
504 case CTF_K_VOLATILE:
505 case CTF_K_RESTRICT:
506 break;
507 case CTF_K_SLICE:
508 case CTF_K_ARRAY:
509 case CTF_K_UNKNOWN:
510 break;
511 }
512
513 add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
514 }
515
516 return sym;
517}
518
519/* Given a TID of kind CTF_K_INTEGER or CTF_K_FLOAT, find a representation
520 and create the symbol for it. */
521
522static struct type *
1c7148dd 523read_base_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
524{
525 struct objfile *of = ccp->of;
139633c3 526 ctf_dict_t *fp = ccp->fp;
30d1f018 527 ctf_encoding_t cet;
844be3f2 528 struct type *type = nullptr;
ea11a98d 529 const char *name;
30d1f018
WP
530 uint32_t kind;
531
532 if (ctf_type_encoding (fp, tid, &cet))
533 {
534 complaint (_("ctf_type_encoding read_base_type failed - %s"),
535 ctf_errmsg (ctf_errno (fp)));
844be3f2 536 return nullptr;
30d1f018
WP
537 }
538
ea11a98d
WP
539 name = ctf_type_name_raw (fp, tid);
540 if (name == nullptr || strlen (name) == 0)
30d1f018
WP
541 {
542 name = ctf_type_aname (fp, tid);
844be3f2 543 if (name == nullptr)
30d1f018
WP
544 complaint (_("ctf_type_aname read_base_type failed - %s"),
545 ctf_errmsg (ctf_errno (fp)));
546 }
30d1f018
WP
547
548 kind = ctf_type_kind (fp, tid);
549 if (kind == CTF_K_INTEGER)
550 {
551 uint32_t issigned, ischar, isbool;
08feed99 552 struct gdbarch *gdbarch = of->arch ();
30d1f018
WP
553
554 issigned = cet.cte_format & CTF_INT_SIGNED;
555 ischar = cet.cte_format & CTF_INT_CHAR;
556 isbool = cet.cte_format & CTF_INT_BOOL;
557 if (ischar)
558 type = init_character_type (of, TARGET_CHAR_BIT, !issigned, name);
559 else if (isbool)
560 type = init_boolean_type (of, gdbarch_int_bit (gdbarch),
561 !issigned, name);
562 else
563 {
564 int bits;
565 if (cet.cte_bits && ((cet.cte_bits % TARGET_CHAR_BIT) == 0))
566 bits = cet.cte_bits;
567 else
568 bits = gdbarch_int_bit (gdbarch);
569 type = init_integer_type (of, bits, !issigned, name);
570 }
571 }
572 else if (kind == CTF_K_FLOAT)
573 {
574 uint32_t isflt;
575 isflt = !((cet.cte_format & CTF_FP_IMAGRY) == CTF_FP_IMAGRY
576 || (cet.cte_format & CTF_FP_DIMAGRY) == CTF_FP_DIMAGRY
577 || (cet.cte_format & CTF_FP_LDIMAGRY) == CTF_FP_LDIMAGRY);
578 if (isflt)
579 type = ctf_init_float_type (of, cet.cte_bits, name, name);
580 else
581 {
582 struct type *t
583 = ctf_init_float_type (of, cet.cte_bits / 2, NULL, name);
5b930b45 584 type = init_complex_type (name, t);
30d1f018
WP
585 }
586 }
587 else
588 {
589 complaint (_("read_base_type: unsupported base kind (%d)"), kind);
590 type = init_type (of, TYPE_CODE_ERROR, cet.cte_bits, name);
591 }
592
844be3f2 593 if (name != nullptr && strcmp (name, "char") == 0)
15152a54 594 type->set_has_no_signedness (true);
30d1f018
WP
595
596 return set_tid_type (of, tid, type);
597}
598
599static void
1c7148dd 600process_base_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
601{
602 struct type *type;
603
604 type = read_base_type (ccp, tid);
605 new_symbol (ccp, type, tid);
606}
607
608/* Start a structure or union scope (definition) with TID to create a type
609 for the structure or union.
610
611 Fill in the type's name and general properties. The members will not be
612 processed, nor a symbol table entry be done until process_structure_type
613 (assuming the type has a name). */
614
615static struct type *
1c7148dd 616read_structure_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
617{
618 struct objfile *of = ccp->of;
139633c3 619 ctf_dict_t *fp = ccp->fp;
30d1f018
WP
620 struct type *type;
621 uint32_t kind;
622
623 type = alloc_type (of);
624
ea11a98d
WP
625 const char *name = ctf_type_name_raw (fp, tid);
626 if (name != nullptr && strlen (name) != 0)
627 type->set_name (name);
30d1f018
WP
628
629 kind = ctf_type_kind (fp, tid);
630 if (kind == CTF_K_UNION)
67607e24 631 type->set_code (TYPE_CODE_UNION);
30d1f018 632 else
67607e24 633 type->set_code (TYPE_CODE_STRUCT);
30d1f018
WP
634
635 TYPE_LENGTH (type) = ctf_type_size (fp, tid);
636 set_type_align (type, ctf_type_align (fp, tid));
637
638 return set_tid_type (ccp->of, tid, type);
639}
640
641/* Given a tid of CTF_K_STRUCT or CTF_K_UNION, process all its members
642 and create the symbol for it. */
643
644static void
1c7148dd 645process_struct_members (struct ctf_context *ccp,
30d1f018
WP
646 ctf_id_t tid,
647 struct type *type)
648{
b2caee6a 649 struct ctf_field_info fi;
30d1f018
WP
650
651 fi.cur_context = ccp;
652 if (ctf_member_iter (ccp->fp, tid, ctf_add_member_cb, &fi) == CTF_ERR)
653 complaint (_("ctf_member_iter process_struct_members failed - %s"),
654 ctf_errmsg (ctf_errno (ccp->fp)));
655
656 /* Attach fields to the type. */
657 attach_fields_to_type (&fi, type);
658
659 new_symbol (ccp, type, tid);
660}
661
662static void
1c7148dd 663process_structure_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
664{
665 struct type *type;
666
667 type = read_structure_type (ccp, tid);
668 process_struct_members (ccp, tid, type);
669}
670
671/* Create a function type for TID and set its return type. */
672
673static struct type *
1c7148dd 674read_func_kind_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
675{
676 struct objfile *of = ccp->of;
139633c3 677 ctf_dict_t *fp = ccp->fp;
844be3f2 678 struct type *type, *rettype, *atype;
30d1f018 679 ctf_funcinfo_t cfi;
844be3f2 680 uint32_t argc;
30d1f018
WP
681
682 type = alloc_type (of);
683
67607e24 684 type->set_code (TYPE_CODE_FUNC);
30d1f018 685 ctf_func_type_info (fp, tid, &cfi);
dc2b480f 686 rettype = fetch_tid_type (ccp, cfi.ctc_return);
30d1f018
WP
687 TYPE_TARGET_TYPE (type) = rettype;
688 set_type_align (type, ctf_type_align (fp, tid));
689
844be3f2
WP
690 /* Set up function's arguments. */
691 argc = cfi.ctc_argc;
692 type->set_num_fields (argc);
693 if ((cfi.ctc_flags & CTF_FUNC_VARARG) != 0)
694 type->set_has_varargs (true);
695
696 if (argc != 0)
697 {
698 std::vector<ctf_id_t> argv (argc);
699 if (ctf_func_type_args (fp, tid, argc, argv.data ()) == CTF_ERR)
700 return nullptr;
701
702 type->set_fields
703 ((struct field *) TYPE_ZALLOC (type, argc * sizeof (struct field)));
704 struct type *void_type = objfile_type (of)->builtin_void;
705 /* If failed to find the argument type, fill it with void_type. */
706 for (int iparam = 0; iparam < argc; iparam++)
707 {
708 atype = get_tid_type (of, argv[iparam]);
709 if (atype != nullptr)
710 type->field (iparam).set_type (atype);
711 else
712 type->field (iparam).set_type (void_type);
713 }
714 }
715
30d1f018
WP
716 return set_tid_type (of, tid, type);
717}
718
719/* Given a TID of CTF_K_ENUM, process all the members of the
720 enumeration, and create the symbol for the enumeration type. */
721
722static struct type *
1c7148dd 723read_enum_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
724{
725 struct objfile *of = ccp->of;
139633c3 726 ctf_dict_t *fp = ccp->fp;
30d1f018
WP
727 struct type *type, *target_type;
728 ctf_funcinfo_t fi;
729
730 type = alloc_type (of);
731
ea11a98d
WP
732 const char *name = ctf_type_name_raw (fp, tid);
733 if (name != nullptr && strlen (name) != 0)
734 type->set_name (name);
30d1f018 735
67607e24 736 type->set_code (TYPE_CODE_ENUM);
30d1f018
WP
737 TYPE_LENGTH (type) = ctf_type_size (fp, tid);
738 ctf_func_type_info (fp, tid, &fi);
739 target_type = get_tid_type (of, fi.ctc_return);
740 TYPE_TARGET_TYPE (type) = target_type;
741 set_type_align (type, ctf_type_align (fp, tid));
742
743 return set_tid_type (of, tid, type);
744}
745
746static void
1c7148dd 747process_enum_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
748{
749 struct type *type;
b2caee6a 750 struct ctf_field_info fi;
30d1f018
WP
751
752 type = read_enum_type (ccp, tid);
753
754 fi.cur_context = ccp;
755 fi.ptype = type;
756 if (ctf_enum_iter (ccp->fp, tid, ctf_add_enum_member_cb, &fi) == CTF_ERR)
757 complaint (_("ctf_enum_iter process_enum_type failed - %s"),
758 ctf_errmsg (ctf_errno (ccp->fp)));
759
760 /* Attach fields to the type. */
761 attach_fields_to_type (&fi, type);
762
763 new_symbol (ccp, type, tid);
764}
765
766/* Add given cv-qualifiers CNST+VOLTL to the BASE_TYPE of array TID. */
767
768static struct type *
1c7148dd 769add_array_cv_type (struct ctf_context *ccp,
30d1f018
WP
770 ctf_id_t tid,
771 struct type *base_type,
772 int cnst,
773 int voltl)
774{
775 struct type *el_type, *inner_array;
776
777 base_type = copy_type (base_type);
778 inner_array = base_type;
779
78134374 780 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
30d1f018
WP
781 {
782 TYPE_TARGET_TYPE (inner_array)
783 = copy_type (TYPE_TARGET_TYPE (inner_array));
784 inner_array = TYPE_TARGET_TYPE (inner_array);
785 }
786
787 el_type = TYPE_TARGET_TYPE (inner_array);
788 cnst |= TYPE_CONST (el_type);
789 voltl |= TYPE_VOLATILE (el_type);
844be3f2 790 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, nullptr);
30d1f018
WP
791
792 return set_tid_type (ccp->of, tid, base_type);
793}
794
795/* Read all information from a TID of CTF_K_ARRAY. */
796
797static struct type *
1c7148dd 798read_array_type (struct ctf_context *ccp, ctf_id_t tid)
30d1f018
WP
799{
800 struct objfile *objfile = ccp->of;
139633c3 801 ctf_dict_t *fp = ccp->fp;
30d1f018
WP
802 struct type *element_type, *range_type, *idx_type;
803 struct type *type;
804 ctf_arinfo_t ar;
805
806 if (ctf_array_info (fp, tid, &ar) == CTF_ERR)
807 {
808 complaint (_("ctf_array_info read_array_type failed - %s"),
809 ctf_errmsg (ctf_errno (fp)));
844be3f2 810 return nullptr;
30d1f018
WP
811 }
812
dc2b480f 813 element_type = fetch_tid_type (ccp, ar.ctr_contents);
844be3f2
WP
814 if (element_type == nullptr)
815 return nullptr;
30d1f018 816
dc2b480f 817 idx_type = fetch_tid_type (ccp, ar.ctr_index);
844be3f2 818 if (idx_type == nullptr)
30d1f018
WP
819 idx_type = objfile_type (objfile)->builtin_int;
820
821 range_type = create_static_range_type (NULL, idx_type, 0, ar.ctr_nelems - 1);
822 type = create_array_type (NULL, element_type, range_type);
823 if (ar.ctr_nelems <= 1) /* Check if undefined upper bound. */
824 {
8c2e4e06 825 range_type->bounds ()->high.set_undefined ();
30d1f018 826 TYPE_LENGTH (type) = 0;
8f53807e 827 type->set_target_is_stub (true);
30d1f018
WP
828 }
829 else
830 TYPE_LENGTH (type) = ctf_type_size (fp, tid);
831
832 set_type_align (type, ctf_type_align (fp, tid));
833
834 return set_tid_type (objfile, tid, type);
835}
836
837/* Read TID of kind CTF_K_CONST with base type BTID. */
838
839static struct type *
1c7148dd 840read_const_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
30d1f018
WP
841{
842 struct objfile *objfile = ccp->of;
843 struct type *base_type, *cv_type;
844
dc2b480f 845 base_type = fetch_tid_type (ccp, btid);
844be3f2 846 if (base_type == nullptr)
30d1f018
WP
847 {
848 base_type = read_type_record (ccp, btid);
844be3f2 849 if (base_type == nullptr)
30d1f018
WP
850 {
851 complaint (_("read_const_type: NULL base type (%ld)"), btid);
852 base_type = objfile_type (objfile)->builtin_error;
853 }
854 }
855 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
856
857 return set_tid_type (objfile, tid, cv_type);
858}
859
860/* Read TID of kind CTF_K_VOLATILE with base type BTID. */
861
862static struct type *
1c7148dd 863read_volatile_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
30d1f018
WP
864{
865 struct objfile *objfile = ccp->of;
139633c3 866 ctf_dict_t *fp = ccp->fp;
30d1f018
WP
867 struct type *base_type, *cv_type;
868
dc2b480f 869 base_type = fetch_tid_type (ccp, btid);
844be3f2 870 if (base_type == nullptr)
30d1f018
WP
871 {
872 base_type = read_type_record (ccp, btid);
844be3f2 873 if (base_type == nullptr)
30d1f018
WP
874 {
875 complaint (_("read_volatile_type: NULL base type (%ld)"), btid);
876 base_type = objfile_type (objfile)->builtin_error;
877 }
878 }
879
880 if (ctf_type_kind (fp, btid) == CTF_K_ARRAY)
881 return add_array_cv_type (ccp, tid, base_type, 0, 1);
882 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
883
884 return set_tid_type (objfile, tid, cv_type);
885}
886
887/* Read TID of kind CTF_K_RESTRICT with base type BTID. */
888
889static struct type *
1c7148dd 890read_restrict_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
30d1f018
WP
891{
892 struct objfile *objfile = ccp->of;
893 struct type *base_type, *cv_type;
894
dc2b480f 895 base_type = fetch_tid_type (ccp, btid);
844be3f2 896 if (base_type == nullptr)
30d1f018
WP
897 {
898 base_type = read_type_record (ccp, btid);
844be3f2 899 if (base_type == nullptr)
30d1f018
WP
900 {
901 complaint (_("read_restrict_type: NULL base type (%ld)"), btid);
902 base_type = objfile_type (objfile)->builtin_error;
903 }
904 }
905 cv_type = make_restrict_type (base_type);
906
907 return set_tid_type (objfile, tid, cv_type);
908}
909
910/* Read TID of kind CTF_K_TYPEDEF with its NAME and base type BTID. */
911
912static struct type *
1c7148dd 913read_typedef_type (struct ctf_context *ccp, ctf_id_t tid,
30d1f018
WP
914 ctf_id_t btid, const char *name)
915{
916 struct objfile *objfile = ccp->of;
917 struct type *this_type, *target_type;
918
919 char *aname = obstack_strdup (&objfile->objfile_obstack, name);
920 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, aname);
921 set_tid_type (objfile, tid, this_type);
dc2b480f 922 target_type = fetch_tid_type (ccp, btid);
30d1f018
WP
923 if (target_type != this_type)
924 TYPE_TARGET_TYPE (this_type) = target_type;
925 else
844be3f2 926 TYPE_TARGET_TYPE (this_type) = nullptr;
8f53807e
SM
927
928 this_type->set_target_is_stub (TYPE_TARGET_TYPE (this_type) != nullptr);
30d1f018
WP
929
930 return set_tid_type (objfile, tid, this_type);
931}
932
933/* Read TID of kind CTF_K_POINTER with base type BTID. */
934
935static struct type *
1c7148dd 936read_pointer_type (struct ctf_context *ccp, ctf_id_t tid, ctf_id_t btid)
30d1f018
WP
937{
938 struct objfile *of = ccp->of;
939 struct type *target_type, *type;
940
dc2b480f 941 target_type = fetch_tid_type (ccp, btid);
844be3f2 942 if (target_type == nullptr)
30d1f018
WP
943 {
944 target_type = read_type_record (ccp, btid);
844be3f2 945 if (target_type == nullptr)
30d1f018
WP
946 {
947 complaint (_("read_pointer_type: NULL target type (%ld)"), btid);
948 target_type = objfile_type (ccp->of)->builtin_error;
949 }
950 }
951
952 type = lookup_pointer_type (target_type);
953 set_type_align (type, ctf_type_align (ccp->fp, tid));
954
955 return set_tid_type (of, tid, type);
956}
957
dc2b480f
WP
958/* Read information from a TID of CTF_K_FORWARD. */
959
960static struct type *
961read_forward_type (struct ctf_context *ccp, ctf_id_t tid)
962{
963 struct objfile *of = ccp->of;
964 ctf_dict_t *fp = ccp->fp;
965 struct type *type;
966 uint32_t kind;
967
968 type = alloc_type (of);
969
ea11a98d
WP
970 const char *name = ctf_type_name_raw (fp, tid);
971 if (name != nullptr && strlen (name) != 0)
972 type->set_name (name);
dc2b480f
WP
973
974 kind = ctf_type_kind_forwarded (fp, tid);
975 if (kind == CTF_K_UNION)
976 type->set_code (TYPE_CODE_UNION);
977 else
978 type->set_code (TYPE_CODE_STRUCT);
979
980 TYPE_LENGTH (type) = 0;
981 type->set_is_stub (true);
982
983 return set_tid_type (of, tid, type);
984}
985
30d1f018
WP
986/* Read information associated with type TID. */
987
988static struct type *
1c7148dd 989read_type_record (struct ctf_context *ccp, ctf_id_t tid)
30d1f018 990{
139633c3 991 ctf_dict_t *fp = ccp->fp;
30d1f018 992 uint32_t kind;
844be3f2 993 struct type *type = nullptr;
30d1f018
WP
994 ctf_id_t btid;
995
996 kind = ctf_type_kind (fp, tid);
997 switch (kind)
998 {
999 case CTF_K_STRUCT:
1000 case CTF_K_UNION:
1001 type = read_structure_type (ccp, tid);
1002 break;
1003 case CTF_K_ENUM:
1004 type = read_enum_type (ccp, tid);
1005 break;
1006 case CTF_K_FUNCTION:
1007 type = read_func_kind_type (ccp, tid);
1008 break;
1009 case CTF_K_CONST:
1010 btid = ctf_type_reference (fp, tid);
1011 type = read_const_type (ccp, tid, btid);
1012 break;
1013 case CTF_K_TYPEDEF:
1014 {
ea11a98d 1015 const char *name = ctf_type_name_raw (fp, tid);
30d1f018 1016 btid = ctf_type_reference (fp, tid);
ea11a98d 1017 type = read_typedef_type (ccp, tid, btid, name);
30d1f018
WP
1018 }
1019 break;
1020 case CTF_K_VOLATILE:
1021 btid = ctf_type_reference (fp, tid);
1022 type = read_volatile_type (ccp, tid, btid);
1023 break;
1024 case CTF_K_RESTRICT:
1025 btid = ctf_type_reference (fp, tid);
1026 type = read_restrict_type (ccp, tid, btid);
1027 break;
1028 case CTF_K_POINTER:
1029 btid = ctf_type_reference (fp, tid);
1030 type = read_pointer_type (ccp, tid, btid);
1031 break;
1032 case CTF_K_INTEGER:
1033 case CTF_K_FLOAT:
1034 type = read_base_type (ccp, tid);
1035 break;
1036 case CTF_K_ARRAY:
1037 type = read_array_type (ccp, tid);
1038 break;
dc2b480f
WP
1039 case CTF_K_FORWARD:
1040 type = read_forward_type (ccp, tid);
1041 break;
30d1f018
WP
1042 case CTF_K_UNKNOWN:
1043 break;
1044 default:
1045 break;
1046 }
1047
1048 return type;
1049}
1050
1051/* Callback to add type TID to the symbol table. */
1052
1053static int
1054ctf_add_type_cb (ctf_id_t tid, void *arg)
1055{
1c7148dd 1056 struct ctf_context *ccp = (struct ctf_context *) arg;
30d1f018
WP
1057 struct type *type;
1058 uint32_t kind;
1059
1060 /* Check if tid's type has already been defined. */
1061 type = get_tid_type (ccp->of, tid);
844be3f2 1062 if (type != nullptr)
30d1f018
WP
1063 return 0;
1064
1065 ctf_id_t btid = ctf_type_reference (ccp->fp, tid);
1066 kind = ctf_type_kind (ccp->fp, tid);
1067 switch (kind)
1068 {
1069 case CTF_K_STRUCT:
1070 case CTF_K_UNION:
1071 process_structure_type (ccp, tid);
1072 break;
1073 case CTF_K_ENUM:
1074 process_enum_type (ccp, tid);
1075 break;
1076 case CTF_K_FUNCTION:
1077 type = read_func_kind_type (ccp, tid);
1078 new_symbol (ccp, type, tid);
1079 break;
1080 case CTF_K_INTEGER:
1081 case CTF_K_FLOAT:
1082 process_base_type (ccp, tid);
1083 break;
1084 case CTF_K_TYPEDEF:
1085 new_symbol (ccp, read_type_record (ccp, tid), tid);
1086 break;
1087 case CTF_K_CONST:
1088 type = read_const_type (ccp, tid, btid);
1089 new_symbol (ccp, type, tid);
1090 break;
1091 case CTF_K_VOLATILE:
1092 type = read_volatile_type (ccp, tid, btid);
1093 new_symbol (ccp, type, tid);
1094 break;
1095 case CTF_K_RESTRICT:
1096 type = read_restrict_type (ccp, tid, btid);
1097 new_symbol (ccp, type, tid);
1098 break;
1099 case CTF_K_POINTER:
1100 type = read_pointer_type (ccp, tid, btid);
1101 new_symbol (ccp, type, tid);
1102 break;
1103 case CTF_K_ARRAY:
1104 type = read_array_type (ccp, tid);
1105 new_symbol (ccp, type, tid);
1106 break;
1107 case CTF_K_UNKNOWN:
1108 break;
1109 default:
1110 break;
1111 }
1112
1113 return 0;
1114}
1115
1116/* Callback to add variable NAME with TID to the symbol table. */
1117
1118static int
1119ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
1120{
1c7148dd 1121 struct ctf_context *ccp = (struct ctf_context *) arg;
844be3f2 1122 struct symbol *sym = nullptr;
30d1f018
WP
1123 struct type *type;
1124 uint32_t kind;
1125
1126 type = get_tid_type (ccp->of, id);
1127
1128 kind = ctf_type_kind (ccp->fp, id);
1129 switch (kind)
1130 {
1131 case CTF_K_FUNCTION:
844be3f2 1132 if (name != nullptr && strcmp (name, "main") == 0)
30d1f018
WP
1133 set_objfile_main_name (ccp->of, name, language_c);
1134 break;
1135 case CTF_K_INTEGER:
1136 case CTF_K_FLOAT:
1137 case CTF_K_VOLATILE:
1138 case CTF_K_RESTRICT:
1139 case CTF_K_TYPEDEF:
1140 case CTF_K_CONST:
1141 case CTF_K_POINTER:
1142 case CTF_K_ARRAY:
1143 if (type)
1144 {
1145 sym = new_symbol (ccp, type, id);
4d4eaa30 1146 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
30d1f018
WP
1147 }
1148 break;
1149 case CTF_K_STRUCT:
1150 case CTF_K_UNION:
1151 case CTF_K_ENUM:
844be3f2 1152 if (type == nullptr)
01add95b
SM
1153 {
1154 complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
1155 type = objfile_type (ccp->of)->builtin_error;
1156 }
8c14c3a3 1157 sym = new (&ccp->of->objfile_obstack) symbol;
30d1f018
WP
1158 OBJSTAT (ccp->of, n_syms++);
1159 SYMBOL_TYPE (sym) = type;
1160 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1161 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4d4eaa30 1162 sym->compute_and_set_names (name, false, ccp->of->per_bfd);
30d1f018
WP
1163 add_symbol_to_list (sym, ccp->builder->get_global_symbols ());
1164 break;
1165 default:
1166 complaint (_("ctf_add_var_cb: kind unsupported (%d)"), kind);
1167 break;
1168 }
1169
844be3f2 1170 if (sym != nullptr)
30d1f018
WP
1171 set_symbol_address (ccp->of, sym, name);
1172
1173 return 0;
1174}
1175
1176/* Add an ELF STT_OBJ symbol with index IDX to the symbol table. */
1177
1178static struct symbol *
1c7148dd 1179add_stt_obj (struct ctf_context *ccp, unsigned long idx)
30d1f018
WP
1180{
1181 struct symbol *sym;
1182 struct type *type;
1183 ctf_id_t tid;
1184
1185 if ((tid = ctf_lookup_by_symbol (ccp->fp, idx)) == CTF_ERR)
844be3f2 1186 return nullptr;
30d1f018 1187
dc2b480f 1188 type = fetch_tid_type (ccp, tid);
844be3f2
WP
1189 if (type == nullptr)
1190 return nullptr;
30d1f018
WP
1191
1192 sym = new_symbol (ccp, type, tid);
1193
1194 return sym;
1195}
1196
1197/* Add an ELF STT_FUNC symbol with index IDX to the symbol table. */
1198
1199static struct symbol *
1c7148dd 1200add_stt_func (struct ctf_context *ccp, unsigned long idx)
30d1f018
WP
1201{
1202 struct type *ftype, *atyp, *rettyp;
1203 struct symbol *sym;
1204 ctf_funcinfo_t finfo;
1205 ctf_id_t argv[32];
1206 uint32_t argc;
1207 ctf_id_t tid;
1208 struct type *void_type = objfile_type (ccp->of)->builtin_void;
1209
1210 if (ctf_func_info (ccp->fp, idx, &finfo) == CTF_ERR)
844be3f2 1211 return nullptr;
30d1f018
WP
1212
1213 argc = finfo.ctc_argc;
1214 if (ctf_func_args (ccp->fp, idx, argc, argv) == CTF_ERR)
844be3f2 1215 return nullptr;
30d1f018
WP
1216
1217 gdb::unique_xmalloc_ptr<char> name (ctf_type_aname_raw (ccp->fp, idx));
844be3f2
WP
1218 if (name == nullptr)
1219 return nullptr;
30d1f018
WP
1220
1221 tid = ctf_lookup_by_symbol (ccp->fp, idx);
dc2b480f 1222 ftype = fetch_tid_type (ccp, tid);
844be3f2 1223 if ((finfo.ctc_flags & CTF_FUNC_VARARG) != 0)
1d6286ed 1224 ftype->set_has_varargs (true);
5e33d5f4 1225 ftype->set_num_fields (argc);
30d1f018
WP
1226
1227 /* If argc is 0, it has a "void" type. */
1228 if (argc != 0)
3cabb6b0
SM
1229 ftype->set_fields
1230 ((struct field *) TYPE_ZALLOC (ftype, argc * sizeof (struct field)));
30d1f018
WP
1231
1232 /* TYPE_FIELD_TYPE must never be NULL. Fill it with void_type, if failed
1233 to find the argument type. */
1234 for (int iparam = 0; iparam < argc; iparam++)
1235 {
dc2b480f 1236 atyp = fetch_tid_type (ccp, argv[iparam]);
30d1f018 1237 if (atyp)
5d14b6e5 1238 ftype->field (iparam).set_type (atyp);
30d1f018 1239 else
5d14b6e5 1240 ftype->field (iparam).set_type (void_type);
30d1f018
WP
1241 }
1242
1243 sym = new_symbol (ccp, ftype, tid);
dc2b480f 1244 rettyp = fetch_tid_type (ccp, finfo.ctc_return);
844be3f2 1245 if (rettyp != nullptr)
30d1f018
WP
1246 SYMBOL_TYPE (sym) = rettyp;
1247 else
1248 SYMBOL_TYPE (sym) = void_type;
1249
1250 return sym;
1251}
1252
1253/* Get text segment base for OBJFILE, TSIZE contains the segment size. */
1254
1255static CORE_ADDR
1256get_objfile_text_range (struct objfile *of, int *tsize)
1257{
30d1f018
WP
1258 bfd *abfd = of->obfd;
1259 const asection *codes;
1260
1261 codes = bfd_get_section_by_name (abfd, ".text");
1c7148dd 1262 *tsize = codes ? bfd_section_size (codes) : 0;
b3b3bada 1263 return of->text_section_offset ();
30d1f018
WP
1264}
1265
1266/* Start a symtab for OBJFILE in CTF format. */
1267
1268static void
891813be 1269ctf_start_symtab (ctf_psymtab *pst,
30d1f018
WP
1270 struct objfile *of, CORE_ADDR text_offset)
1271{
1c7148dd 1272 struct ctf_context *ccp;
30d1f018 1273
891813be 1274 ccp = pst->context;
30d1f018 1275 ccp->builder = new buildsym_compunit
844be3f2 1276 (of, of->original_name, nullptr,
30d1f018
WP
1277 language_c, text_offset);
1278 ccp->builder->record_debugformat ("ctf");
1279}
1280
1281/* Finish reading symbol/type definitions in CTF format.
1282 END_ADDR is the end address of the file's text. SECTION is
1283 the .text section number. */
1284
1285static struct compunit_symtab *
891813be 1286ctf_end_symtab (ctf_psymtab *pst,
30d1f018
WP
1287 CORE_ADDR end_addr, int section)
1288{
1c7148dd 1289 struct ctf_context *ccp;
30d1f018 1290
891813be 1291 ccp = pst->context;
30d1f018
WP
1292 struct compunit_symtab *result
1293 = ccp->builder->end_symtab (end_addr, section);
1294 delete ccp->builder;
844be3f2 1295 ccp->builder = nullptr;
30d1f018
WP
1296 return result;
1297}
1298
dd99cf0c
WP
1299/* Add all members of an enum with type TID to partial symbol table. */
1300
1301static void
1302ctf_psymtab_add_enums (struct ctf_context *ccp, ctf_id_t tid)
1303{
1304 int val;
1305 const char *ename;
1306 ctf_next_t *i = nullptr;
1307
1308 while ((ename = ctf_enum_next (ccp->fp, tid, &i, &val)) != nullptr)
1309 {
1310 ccp->pst->add_psymbol (ename, true,
1311 VAR_DOMAIN, LOC_CONST, -1,
1312 psymbol_placement::GLOBAL,
7b249e47 1313 0, language_c, ccp->partial_symtabs, ccp->of);
dd99cf0c
WP
1314 }
1315 if (ctf_errno (ccp->fp) != ECTF_NEXT_END)
1316 complaint (_("ctf_enum_next ctf_psymtab_add_enums failed - %s"),
1317 ctf_errmsg (ctf_errno (ccp->fp)));
1318}
1319
30d1f018
WP
1320/* Read in full symbols for PST, and anything it depends on. */
1321
8566b89b
TT
1322void
1323ctf_psymtab::expand_psymtab (struct objfile *objfile)
30d1f018
WP
1324{
1325 struct symbol *sym;
1c7148dd 1326 struct ctf_context *ccp;
30d1f018 1327
8566b89b 1328 gdb_assert (!readin);
30d1f018 1329
8566b89b 1330 ccp = context;
30d1f018
WP
1331
1332 /* Iterate over entries in data types section. */
1333 if (ctf_type_iter (ccp->fp, ctf_add_type_cb, ccp) == CTF_ERR)
1334 complaint (_("ctf_type_iter psymtab_to_symtab failed - %s"),
1335 ctf_errmsg (ctf_errno (ccp->fp)));
1336
1337
1338 /* Iterate over entries in variable info section. */
1339 if (ctf_variable_iter (ccp->fp, ctf_add_var_cb, ccp) == CTF_ERR)
1340 complaint (_("ctf_variable_iter psymtab_to_symtab failed - %s"),
1341 ctf_errmsg (ctf_errno (ccp->fp)));
1342
1343 /* Add entries in data objects and function info sections. */
1344 for (unsigned long i = 0; ; i++)
1345 {
1346 sym = add_stt_obj (ccp, i);
844be3f2 1347 if (sym == nullptr)
30d1f018
WP
1348 {
1349 if (ctf_errno (ccp->fp) == EINVAL
1350 || ctf_errno (ccp->fp) == ECTF_NOSYMTAB)
1351 break;
1352 sym = add_stt_func (ccp, i);
1353 }
844be3f2 1354 if (sym == nullptr)
30d1f018
WP
1355 continue;
1356
987012b8 1357 set_symbol_address (ccp->of, sym, sym->linkage_name ());
30d1f018
WP
1358 }
1359
8566b89b 1360 readin = true;
30d1f018
WP
1361}
1362
1363/* Expand partial symbol table PST into a full symbol table.
1364 PST is not NULL. */
1365
891813be
TT
1366void
1367ctf_psymtab::read_symtab (struct objfile *objfile)
30d1f018 1368{
891813be
TT
1369 if (readin)
1370 warning (_("bug: psymtab for %s is already read in."), filename);
30d1f018
WP
1371 else
1372 {
1373 if (info_verbose)
1374 {
891813be 1375 printf_filtered (_("Reading in CTF data for %s..."), filename);
30d1f018
WP
1376 gdb_flush (gdb_stdout);
1377 }
1378
1379 /* Start a symtab. */
891813be 1380 CORE_ADDR offset; /* Start of text segment. */
30d1f018
WP
1381 int tsize;
1382
891813be
TT
1383 offset = get_objfile_text_range (objfile, &tsize);
1384 ctf_start_symtab (this, objfile, offset);
8566b89b 1385 expand_psymtab (objfile);
30d1f018 1386
891813be
TT
1387 set_text_low (offset);
1388 set_text_high (offset + tsize);
1389 compunit_symtab = ctf_end_symtab (this, offset + tsize,
1390 SECT_OFF_TEXT (objfile));
30d1f018
WP
1391
1392 /* Finish up the debug error message. */
1393 if (info_verbose)
1394 printf_filtered (_("done.\n"));
1395 }
1396}
1397
30d1f018
WP
1398/* Allocate a new partial_symtab NAME.
1399
1400 Each source file that has not been fully read in is represented by
1401 a partial_symtab. This contains the information on where in the
1402 executable the debugging symbols for a specific file are, and a
1403 list of names of global symbols which are located in this file.
1404 They are all chained on partial symtab lists.
1405
1406 Even after the source file has been read into a symtab, the
1407 partial_symtab remains around. They are allocated on an obstack,
1408 objfile_obstack. */
1409
891813be 1410static ctf_psymtab *
30d1f018 1411create_partial_symtab (const char *name,
139633c3 1412 ctf_dict_t *cfp,
7b249e47 1413 psymtab_storage *partial_symtabs,
30d1f018
WP
1414 struct objfile *objfile)
1415{
891813be 1416 ctf_psymtab *pst;
1c7148dd 1417 struct ctf_context *ccx;
30d1f018 1418
0072c873 1419 pst = new ctf_psymtab (name, partial_symtabs, objfile->per_bfd, 0);
30d1f018 1420
1c7148dd 1421 ccx = XOBNEW (&objfile->objfile_obstack, struct ctf_context);
30d1f018
WP
1422 ccx->fp = cfp;
1423 ccx->of = objfile;
7b249e47 1424 ccx->partial_symtabs = partial_symtabs;
932539d7
TT
1425 ccx->pst = pst;
1426 ccx->builder = nullptr;
891813be 1427 pst->context = ccx;
30d1f018
WP
1428
1429 return pst;
1430}
1431
1432/* Callback to add type TID to partial symbol table. */
1433
1434static int
1435ctf_psymtab_type_cb (ctf_id_t tid, void *arg)
1436{
1c7148dd 1437 struct ctf_context *ccp;
30d1f018
WP
1438 uint32_t kind;
1439 short section = -1;
1440
1c7148dd 1441 ccp = (struct ctf_context *) arg;
30d1f018
WP
1442
1443 domain_enum domain = UNDEF_DOMAIN;
1444 enum address_class aclass = LOC_UNDEF;
1445 kind = ctf_type_kind (ccp->fp, tid);
1446 switch (kind)
1447 {
dd99cf0c
WP
1448 case CTF_K_ENUM:
1449 ctf_psymtab_add_enums (ccp, tid);
1450 /* FALL THROUGH */
30d1f018
WP
1451 case CTF_K_STRUCT:
1452 case CTF_K_UNION:
30d1f018
WP
1453 domain = STRUCT_DOMAIN;
1454 aclass = LOC_TYPEDEF;
1455 break;
1456 case CTF_K_FUNCTION:
1457 case CTF_K_FORWARD:
1458 domain = VAR_DOMAIN;
1459 aclass = LOC_STATIC;
1460 section = SECT_OFF_TEXT (ccp->of);
1461 break;
1462 case CTF_K_CONST:
1463 domain = VAR_DOMAIN;
1464 aclass = LOC_STATIC;
1465 break;
1466 case CTF_K_TYPEDEF:
1467 case CTF_K_POINTER:
1468 case CTF_K_VOLATILE:
1469 case CTF_K_RESTRICT:
1470 domain = VAR_DOMAIN;
1471 aclass = LOC_TYPEDEF;
1472 break;
1473 case CTF_K_INTEGER:
1474 case CTF_K_FLOAT:
1475 domain = VAR_DOMAIN;
1476 aclass = LOC_TYPEDEF;
1477 break;
1478 case CTF_K_ARRAY:
1479 case CTF_K_UNKNOWN:
1480 return 0;
1481 }
1482
ea11a98d
WP
1483 const char *name = ctf_type_name_raw (ccp->fp, tid);
1484 if (name == nullptr || strlen (name) == 0)
dd99cf0c
WP
1485 return 0;
1486
ea11a98d 1487 ccp->pst->add_psymbol (name, false,
30d1f018
WP
1488 domain, aclass, section,
1489 psymbol_placement::GLOBAL,
7b249e47 1490 0, language_c, ccp->partial_symtabs, ccp->of);
30d1f018
WP
1491
1492 return 0;
1493}
1494
1495/* Callback to add variable NAME with ID to partial symbol table. */
1496
1497static int
1498ctf_psymtab_var_cb (const char *name, ctf_id_t id, void *arg)
1499{
1c7148dd 1500 struct ctf_context *ccp = (struct ctf_context *) arg;
30d1f018 1501
932539d7
TT
1502 ccp->pst->add_psymbol (name, true,
1503 VAR_DOMAIN, LOC_STATIC, -1,
1504 psymbol_placement::GLOBAL,
7b249e47 1505 0, language_c, ccp->partial_symtabs, ccp->of);
30d1f018
WP
1506 return 0;
1507}
1508
1509/* Setup partial_symtab's describing each source file for which
1510 debugging information is available. */
1511
1512static void
7b249e47
TT
1513scan_partial_symbols (ctf_dict_t *cfp, psymtab_storage *partial_symtabs,
1514 struct objfile *of)
30d1f018 1515{
30d1f018
WP
1516 bfd *abfd = of->obfd;
1517 const char *name = bfd_get_filename (abfd);
7b249e47 1518 ctf_psymtab *pst = create_partial_symtab (name, cfp, partial_symtabs, of);
30d1f018 1519
932539d7 1520 struct ctf_context *ccx = pst->context;
30d1f018 1521
932539d7 1522 if (ctf_type_iter (cfp, ctf_psymtab_type_cb, ccx) == CTF_ERR)
30d1f018
WP
1523 complaint (_("ctf_type_iter scan_partial_symbols failed - %s"),
1524 ctf_errmsg (ctf_errno (cfp)));
1525
932539d7 1526 if (ctf_variable_iter (cfp, ctf_psymtab_var_cb, ccx) == CTF_ERR)
30d1f018
WP
1527 complaint (_("ctf_variable_iter scan_partial_symbols failed - %s"),
1528 ctf_errmsg (ctf_errno (cfp)));
1529
1530 /* Scan CTF object and function sections which correspond to each
1531 STT_FUNC or STT_OBJECT entry in the symbol table,
1532 pick up what init_symtab has done. */
1533 for (unsigned long idx = 0; ; idx++)
1534 {
1535 ctf_id_t tid;
1536 if ((tid = ctf_lookup_by_symbol (cfp, idx)) == CTF_ERR)
1537 {
1538 if (ctf_errno (cfp) == EINVAL || ctf_errno (cfp) == ECTF_NOSYMTAB)
1539 break; // Done, reach end of the section.
1540 else
1541 continue;
1542 }
ea11a98d 1543 const char *tname = ctf_type_name_raw (cfp, tid);
30d1f018
WP
1544 uint32_t kind = ctf_type_kind (cfp, tid);
1545 address_class aclass;
1546 domain_enum tdomain;
1547 switch (kind)
1548 {
1549 case CTF_K_STRUCT:
1550 case CTF_K_UNION:
1551 case CTF_K_ENUM:
1552 tdomain = STRUCT_DOMAIN;
1553 break;
1554 default:
1555 tdomain = VAR_DOMAIN;
1556 break;
1557 }
1558
1559 if (kind == CTF_K_FUNCTION)
1560 aclass = LOC_STATIC;
1561 else if (kind == CTF_K_CONST)
1562 aclass = LOC_CONST;
1563 else
1564 aclass = LOC_TYPEDEF;
1565
ea11a98d 1566 pst->add_psymbol (tname, false,
932539d7
TT
1567 tdomain, aclass, -1,
1568 psymbol_placement::STATIC,
7b249e47 1569 0, language_c, partial_symtabs, of);
30d1f018
WP
1570 }
1571
ae7754b2 1572 pst->end ();
30d1f018
WP
1573}
1574
1575/* Read CTF debugging information from a BFD section. This is
1576 called from elfread.c. It does a quick pass through the
1577 .ctf section to set up the partial symbol table. */
1578
1579void
1580elfctf_build_psymtabs (struct objfile *of)
1581{
1582 bfd *abfd = of->obfd;
1583 int err;
1584
1585 ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
844be3f2 1586 if (arc == nullptr)
30d1f018
WP
1587 error (_("ctf_bfdopen failed on %s - %s"),
1588 bfd_get_filename (abfd), ctf_errmsg (err));
1589
ae41200b 1590 ctf_dict_t *fp = ctf_dict_open (arc, NULL, &err);
844be3f2 1591 if (fp == nullptr)
ae41200b 1592 error (_("ctf_dict_open failed on %s - %s"),
30d1f018 1593 bfd_get_filename (abfd), ctf_errmsg (err));
139633c3 1594 ctf_dict_key.emplace (of, fp);
30d1f018 1595
eb36a3eb
TT
1596 psymbol_functions *psf = new psymbol_functions ();
1597 psymtab_storage *partial_symtabs = psf->get_partial_symtabs ().get ();
1598 of->qf.emplace_front (psf);
7b249e47 1599 scan_partial_symbols (fp, partial_symtabs, of);
30d1f018 1600}
1776e3e5
NA
1601
1602#else
1603
1604void
1605elfctf_build_psymtabs (struct objfile *of)
1606{
1607 /* Nothing to do if CTF is disabled. */
1608}
1609
1610#endif /* ENABLE_LIBCTF */
This page took 0.26046 seconds and 4 git commands to generate.