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