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