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