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