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