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