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