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