1 /* Target description support for GDB. 2 3 Copyright (C) 2006-2020 Free Software Foundation, Inc. 4 5 Contributed by CodeSourcery. 6 7 This file is part of GDB. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 22 #include "defs.h" 23 #include "arch-utils.h" 24 #include "gdbcmd.h" 25 #include "gdbtypes.h" 26 #include "reggroups.h" 27 #include "target.h" 28 #include "target-descriptions.h" 29 #include "xml-support.h" 30 #include "xml-tdesc.h" 31 #include "osabi.h" 32 33 #include "gdb_obstack.h" 34 #include "hashtab.h" 35 #include "inferior.h" 36 #include <algorithm> 37 #include "completer.h" 38 #include "readline/tilde.h" /* tilde_expand */ 39 40 /* Types. */ 41 42 struct property 43 { 44 property (const std::string &key_, const std::string &value_) 45 : key (key_), value (value_) 46 {} 47 48 std::string key; 49 std::string value; 50 }; 51 52 /* Convert a tdesc_type to a gdb type. */ 53 54 static type * 55 make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype) 56 { 57 class gdb_type_creator : public tdesc_element_visitor 58 { 59 public: 60 gdb_type_creator (struct gdbarch *gdbarch) 61 : m_gdbarch (gdbarch) 62 {} 63 64 type *get_type () 65 { 66 return m_type; 67 } 68 69 void visit (const tdesc_type_builtin *e) override 70 { 71 switch (e->kind) 72 { 73 /* Predefined types. */ 74 case TDESC_TYPE_BOOL: 75 m_type = builtin_type (m_gdbarch)->builtin_bool; 76 return; 77 case TDESC_TYPE_INT8: 78 m_type = builtin_type (m_gdbarch)->builtin_int8; 79 return; 80 case TDESC_TYPE_INT16: 81 m_type = builtin_type (m_gdbarch)->builtin_int16; 82 return; 83 case TDESC_TYPE_INT32: 84 m_type = builtin_type (m_gdbarch)->builtin_int32; 85 return; 86 case TDESC_TYPE_INT64: 87 m_type = builtin_type (m_gdbarch)->builtin_int64; 88 return; 89 case TDESC_TYPE_INT128: 90 m_type = builtin_type (m_gdbarch)->builtin_int128; 91 return; 92 case TDESC_TYPE_UINT8: 93 m_type = builtin_type (m_gdbarch)->builtin_uint8; 94 return; 95 case TDESC_TYPE_UINT16: 96 m_type = builtin_type (m_gdbarch)->builtin_uint16; 97 return; 98 case TDESC_TYPE_UINT32: 99 m_type = builtin_type (m_gdbarch)->builtin_uint32; 100 return; 101 case TDESC_TYPE_UINT64: 102 m_type = builtin_type (m_gdbarch)->builtin_uint64; 103 return; 104 case TDESC_TYPE_UINT128: 105 m_type = builtin_type (m_gdbarch)->builtin_uint128; 106 return; 107 case TDESC_TYPE_CODE_PTR: 108 m_type = builtin_type (m_gdbarch)->builtin_func_ptr; 109 return; 110 case TDESC_TYPE_DATA_PTR: 111 m_type = builtin_type (m_gdbarch)->builtin_data_ptr; 112 return; 113 } 114 115 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ()); 116 if (m_type != NULL) 117 return; 118 119 switch (e->kind) 120 { 121 case TDESC_TYPE_IEEE_HALF: 122 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_half", 123 floatformats_ieee_half); 124 return; 125 126 case TDESC_TYPE_IEEE_SINGLE: 127 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single", 128 floatformats_ieee_single); 129 return; 130 131 case TDESC_TYPE_IEEE_DOUBLE: 132 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double", 133 floatformats_ieee_double); 134 return; 135 case TDESC_TYPE_ARM_FPA_EXT: 136 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext", 137 floatformats_arm_ext); 138 return; 139 140 case TDESC_TYPE_I387_EXT: 141 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext", 142 floatformats_i387_ext); 143 return; 144 145 case TDESC_TYPE_BFLOAT16: 146 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_bfloat16", 147 floatformats_bfloat16); 148 return; 149 } 150 151 internal_error (__FILE__, __LINE__, 152 "Type \"%s\" has an unknown kind %d", 153 e->name.c_str (), e->kind); 154 } 155 156 void visit (const tdesc_type_vector *e) override 157 { 158 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ()); 159 if (m_type != NULL) 160 return; 161 162 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type); 163 m_type = init_vector_type (element_gdb_type, e->count); 164 m_type->set_name (xstrdup (e->name.c_str ())); 165 return; 166 } 167 168 void visit (const tdesc_type_with_fields *e) override 169 { 170 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ()); 171 if (m_type != NULL) 172 return; 173 174 switch (e->kind) 175 { 176 case TDESC_TYPE_STRUCT: 177 make_gdb_type_struct (e); 178 return; 179 case TDESC_TYPE_UNION: 180 make_gdb_type_union (e); 181 return; 182 case TDESC_TYPE_FLAGS: 183 make_gdb_type_flags (e); 184 return; 185 case TDESC_TYPE_ENUM: 186 make_gdb_type_enum (e); 187 return; 188 } 189 190 internal_error (__FILE__, __LINE__, 191 "Type \"%s\" has an unknown kind %d", 192 e->name.c_str (), e->kind); 193 } 194 195 private: 196 197 void make_gdb_type_struct (const tdesc_type_with_fields *e) 198 { 199 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT); 200 m_type->set_name (xstrdup (e->name.c_str ())); 201 202 for (const tdesc_type_field &f : e->fields) 203 { 204 if (f.start != -1 && f.end != -1) 205 { 206 /* Bitfield. */ 207 struct field *fld; 208 struct type *field_gdb_type; 209 int bitsize, total_size; 210 211 /* This invariant should be preserved while creating types. */ 212 gdb_assert (e->size != 0); 213 if (f.type != NULL) 214 field_gdb_type = make_gdb_type (m_gdbarch, f.type); 215 else if (e->size > 4) 216 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64; 217 else 218 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32; 219 220 fld = append_composite_type_field_raw 221 (m_type, xstrdup (f.name.c_str ()), field_gdb_type); 222 223 /* For little-endian, BITPOS counts from the LSB of 224 the structure and marks the LSB of the field. For 225 big-endian, BITPOS counts from the MSB of the 226 structure and marks the MSB of the field. Either 227 way, it is the number of bits to the "left" of the 228 field. To calculate this in big-endian, we need 229 the total size of the structure. */ 230 bitsize = f.end - f.start + 1; 231 total_size = e->size * TARGET_CHAR_BIT; 232 if (gdbarch_byte_order (m_gdbarch) == BFD_ENDIAN_BIG) 233 SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize); 234 else 235 SET_FIELD_BITPOS (fld[0], f.start); 236 FIELD_BITSIZE (fld[0]) = bitsize; 237 } 238 else 239 { 240 gdb_assert (f.start == -1 && f.end == -1); 241 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type); 242 append_composite_type_field (m_type, 243 xstrdup (f.name.c_str ()), 244 field_gdb_type); 245 } 246 } 247 248 if (e->size != 0) 249 TYPE_LENGTH (m_type) = e->size; 250 } 251 252 void make_gdb_type_union (const tdesc_type_with_fields *e) 253 { 254 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION); 255 m_type->set_name (xstrdup (e->name.c_str ())); 256 257 for (const tdesc_type_field &f : e->fields) 258 { 259 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type); 260 append_composite_type_field (m_type, xstrdup (f.name.c_str ()), 261 field_gdb_type); 262 263 /* If any of the children of a union are vectors, flag the 264 union as a vector also. This allows e.g. a union of two 265 vector types to show up automatically in "info vector". */ 266 if (TYPE_VECTOR (field_gdb_type)) 267 TYPE_VECTOR (m_type) = 1; 268 } 269 } 270 271 void make_gdb_type_flags (const tdesc_type_with_fields *e) 272 { 273 m_type = arch_flags_type (m_gdbarch, e->name.c_str (), 274 e->size * TARGET_CHAR_BIT); 275 276 for (const tdesc_type_field &f : e->fields) 277 { 278 int bitsize = f.end - f.start + 1; 279 280 gdb_assert (f.type != NULL); 281 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type); 282 append_flags_type_field (m_type, f.start, bitsize, 283 field_gdb_type, f.name.c_str ()); 284 } 285 } 286 287 void make_gdb_type_enum (const tdesc_type_with_fields *e) 288 { 289 m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT, 290 e->name.c_str ()); 291 292 TYPE_UNSIGNED (m_type) = 1; 293 for (const tdesc_type_field &f : e->fields) 294 { 295 struct field *fld 296 = append_composite_type_field_raw (m_type, 297 xstrdup (f.name.c_str ()), 298 NULL); 299 300 SET_FIELD_BITPOS (fld[0], f.start); 301 } 302 } 303 304 /* The gdbarch used. */ 305 struct gdbarch *m_gdbarch; 306 307 /* The type created. */ 308 type *m_type; 309 }; 310 311 gdb_type_creator gdb_type (gdbarch); 312 ttype->accept (gdb_type); 313 return gdb_type.get_type (); 314 } 315 316 /* Wrapper around bfd_arch_info_type. A class with this name is used in 317 the API that is shared between gdb and gdbserver code, but gdbserver 318 doesn't use compatibility information, so its version of this class is 319 empty. */ 320 321 class tdesc_compatible_info 322 { 323 public: 324 /* Constructor. */ 325 explicit tdesc_compatible_info (const bfd_arch_info_type *arch) 326 : m_arch (arch) 327 { /* Nothing. */ } 328 329 /* Access the contained pointer. */ 330 const bfd_arch_info_type *arch () const 331 { return m_arch; } 332 333 private: 334 /* Architecture information looked up from the <compatible> entity within 335 a target description. */ 336 const bfd_arch_info_type *m_arch; 337 }; 338 339 /* A target description. */ 340 341 struct target_desc : tdesc_element 342 { 343 target_desc () 344 {} 345 346 virtual ~target_desc () = default; 347 348 target_desc (const target_desc &) = delete; 349 void operator= (const target_desc &) = delete; 350 351 /* The architecture reported by the target, if any. */ 352 const struct bfd_arch_info *arch = NULL; 353 354 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN 355 otherwise. */ 356 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN; 357 358 /* The list of compatible architectures reported by the target. */ 359 std::vector<tdesc_compatible_info_up> compatible; 360 361 /* Any architecture-specific properties specified by the target. */ 362 std::vector<property> properties; 363 364 /* The features associated with this target. */ 365 std::vector<tdesc_feature_up> features; 366 367 /* Used to cache the generated xml version of the target description. */ 368 mutable char *xmltarget = nullptr; 369 370 void accept (tdesc_element_visitor &v) const override 371 { 372 v.visit_pre (this); 373 374 for (const tdesc_feature_up &feature : features) 375 feature->accept (v); 376 377 v.visit_post (this); 378 } 379 380 bool operator== (const target_desc &other) const 381 { 382 if (arch != other.arch) 383 return false; 384 385 if (osabi != other.osabi) 386 return false; 387 388 if (features.size () != other.features.size ()) 389 return false; 390 391 for (int ix = 0; ix < features.size (); ix++) 392 { 393 const tdesc_feature_up &feature1 = features[ix]; 394 const tdesc_feature_up &feature2 = other.features[ix]; 395 396 if (feature1 != feature2 && *feature1 != *feature2) 397 return false; 398 } 399 400 return true; 401 } 402 403 bool operator!= (const target_desc &other) const 404 { 405 return !(*this == other); 406 } 407 }; 408 409 /* Per-architecture data associated with a target description. The 410 target description may be shared by multiple architectures, but 411 this data is private to one gdbarch. */ 412 413 struct tdesc_arch_reg 414 { 415 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_) 416 : reg (reg_), type (type_) 417 {} 418 419 struct tdesc_reg *reg; 420 struct type *type; 421 }; 422 423 struct tdesc_arch_data 424 { 425 /* A list of register/type pairs, indexed by GDB's internal register number. 426 During initialization of the gdbarch this list is used to store 427 registers which the architecture assigns a fixed register number. 428 Registers which are NULL in this array, or off the end, are 429 treated as zero-sized and nameless (i.e. placeholders in the 430 numbering). */ 431 std::vector<tdesc_arch_reg> arch_regs; 432 433 /* Functions which report the register name, type, and reggroups for 434 pseudo-registers. */ 435 gdbarch_register_name_ftype *pseudo_register_name = NULL; 436 gdbarch_register_type_ftype *pseudo_register_type = NULL; 437 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL; 438 }; 439 440 /* Info about an inferior's target description. There's one of these 441 for each inferior. */ 442 443 struct target_desc_info 444 { 445 /* A flag indicating that a description has already been fetched 446 from the target, so it should not be queried again. */ 447 448 int fetched; 449 450 /* The description fetched from the target, or NULL if the target 451 did not supply any description. Only valid when 452 target_desc_fetched is set. Only the description initialization 453 code should access this; normally, the description should be 454 accessed through the gdbarch object. */ 455 456 const struct target_desc *tdesc; 457 458 /* The filename to read a target description from, as set by "set 459 tdesc filename ..." */ 460 461 char *filename; 462 }; 463 464 /* Get the inferior INF's target description info, allocating one on 465 the stop if necessary. */ 466 467 static struct target_desc_info * 468 get_tdesc_info (struct inferior *inf) 469 { 470 if (inf->tdesc_info == NULL) 471 inf->tdesc_info = XCNEW (struct target_desc_info); 472 return inf->tdesc_info; 473 } 474 475 /* A handle for architecture-specific data associated with the 476 target description (see struct tdesc_arch_data). */ 477 478 static struct gdbarch_data *tdesc_data; 479 480 /* See target-descriptions.h. */ 481 482 int 483 target_desc_info_from_user_p (struct target_desc_info *info) 484 { 485 return info != NULL && info->filename != NULL; 486 } 487 488 /* See target-descriptions.h. */ 489 490 void 491 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf) 492 { 493 struct target_desc_info *src = get_tdesc_info (srcinf); 494 struct target_desc_info *dest = get_tdesc_info (destinf); 495 496 dest->fetched = src->fetched; 497 dest->tdesc = src->tdesc; 498 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL; 499 } 500 501 /* See target-descriptions.h. */ 502 503 void 504 target_desc_info_free (struct target_desc_info *tdesc_info) 505 { 506 if (tdesc_info != NULL) 507 { 508 xfree (tdesc_info->filename); 509 xfree (tdesc_info); 510 } 511 } 512 513 /* Convenience helper macros. */ 514 515 #define target_desc_fetched \ 516 get_tdesc_info (current_inferior ())->fetched 517 #define current_target_desc \ 518 get_tdesc_info (current_inferior ())->tdesc 519 #define target_description_filename \ 520 get_tdesc_info (current_inferior ())->filename 521 522 /* The string manipulated by the "set tdesc filename ..." command. */ 523 524 static char *tdesc_filename_cmd_string; 525 526 /* Fetch the current target's description, and switch the current 527 architecture to one which incorporates that description. */ 528 529 void 530 target_find_description (void) 531 { 532 /* If we've already fetched a description from the target, don't do 533 it again. This allows a target to fetch the description early, 534 during its to_open or to_create_inferior, if it needs extra 535 information about the target to initialize. */ 536 if (target_desc_fetched) 537 return; 538 539 /* The current architecture should not have any target description 540 specified. It should have been cleared, e.g. when we 541 disconnected from the previous target. */ 542 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL); 543 544 /* First try to fetch an XML description from the user-specified 545 file. */ 546 current_target_desc = NULL; 547 if (target_description_filename != NULL 548 && *target_description_filename != '\0') 549 current_target_desc 550 = file_read_description_xml (target_description_filename); 551 552 /* Next try to read the description from the current target using 553 target objects. */ 554 if (current_target_desc == NULL) 555 current_target_desc = target_read_description_xml (current_top_target ()); 556 557 /* If that failed try a target-specific hook. */ 558 if (current_target_desc == NULL) 559 current_target_desc = target_read_description (current_top_target ()); 560 561 /* If a non-NULL description was returned, then update the current 562 architecture. */ 563 if (current_target_desc) 564 { 565 struct gdbarch_info info; 566 567 gdbarch_info_init (&info); 568 info.target_desc = current_target_desc; 569 if (!gdbarch_update_p (info)) 570 warning (_("Architecture rejected target-supplied description")); 571 else 572 { 573 struct tdesc_arch_data *data; 574 575 data = ((struct tdesc_arch_data *) 576 gdbarch_data (target_gdbarch (), tdesc_data)); 577 if (tdesc_has_registers (current_target_desc) 578 && data->arch_regs.empty ()) 579 warning (_("Target-supplied registers are not supported " 580 "by the current architecture")); 581 } 582 } 583 584 /* Now that we know this description is usable, record that we 585 fetched it. */ 586 target_desc_fetched = 1; 587 } 588 589 /* Discard any description fetched from the current target, and switch 590 the current architecture to one with no target description. */ 591 592 void 593 target_clear_description (void) 594 { 595 struct gdbarch_info info; 596 597 if (!target_desc_fetched) 598 return; 599 600 target_desc_fetched = 0; 601 current_target_desc = NULL; 602 603 gdbarch_info_init (&info); 604 if (!gdbarch_update_p (info)) 605 internal_error (__FILE__, __LINE__, 606 _("Could not remove target-supplied description")); 607 } 608 609 /* Return the global current target description. This should only be 610 used by gdbarch initialization code; most access should be through 611 an existing gdbarch. */ 612 613 const struct target_desc * 614 target_current_description (void) 615 { 616 if (target_desc_fetched) 617 return current_target_desc; 618 619 return NULL; 620 } 621 622 /* Return non-zero if this target description is compatible 623 with the given BFD architecture. */ 624 625 int 626 tdesc_compatible_p (const struct target_desc *target_desc, 627 const struct bfd_arch_info *arch) 628 { 629 for (const tdesc_compatible_info_up &compat : target_desc->compatible) 630 { 631 if (compat->arch () == arch 632 || arch->compatible (arch, compat->arch ()) 633 || compat->arch ()->compatible (compat->arch (), arch)) 634 return 1; 635 } 636 637 return 0; 638 } 639 640 641 /* Direct accessors for target descriptions. */ 642 643 /* Return the string value of a property named KEY, or NULL if the 644 property was not specified. */ 645 646 const char * 647 tdesc_property (const struct target_desc *target_desc, const char *key) 648 { 649 for (const property &prop : target_desc->properties) 650 if (prop.key == key) 651 return prop.value.c_str (); 652 653 return NULL; 654 } 655 656 /* Return the BFD architecture associated with this target 657 description, or NULL if no architecture was specified. */ 658 659 const struct bfd_arch_info * 660 tdesc_architecture (const struct target_desc *target_desc) 661 { 662 return target_desc->arch; 663 } 664 665 /* See gdbsupport/tdesc.h. */ 666 667 const char * 668 tdesc_architecture_name (const struct target_desc *target_desc) 669 { 670 if (target_desc->arch != NULL) 671 return target_desc->arch->printable_name; 672 return NULL; 673 } 674 675 /* See gdbsupport/tdesc.h. */ 676 677 const std::vector<tdesc_compatible_info_up> & 678 tdesc_compatible_info_list (const target_desc *target_desc) 679 { 680 return target_desc->compatible; 681 } 682 683 /* See gdbsupport/tdesc.h. */ 684 685 const char * 686 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible) 687 { 688 return compatible->arch ()->printable_name; 689 } 690 691 /* Return the OSABI associated with this target description, or 692 GDB_OSABI_UNKNOWN if no osabi was specified. */ 693 694 enum gdb_osabi 695 tdesc_osabi (const struct target_desc *target_desc) 696 { 697 return target_desc->osabi; 698 } 699 700 /* See gdbsupport/tdesc.h. */ 701 702 const char * 703 tdesc_osabi_name (const struct target_desc *target_desc) 704 { 705 enum gdb_osabi osabi = tdesc_osabi (target_desc); 706 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID) 707 return gdbarch_osabi_name (osabi); 708 return nullptr; 709 } 710 711 /* Return 1 if this target description includes any registers. */ 712 713 int 714 tdesc_has_registers (const struct target_desc *target_desc) 715 { 716 if (target_desc == NULL) 717 return 0; 718 719 for (const tdesc_feature_up &feature : target_desc->features) 720 if (!feature->registers.empty ()) 721 return 1; 722 723 return 0; 724 } 725 726 /* Return the feature with the given name, if present, or NULL if 727 the named feature is not found. */ 728 729 const struct tdesc_feature * 730 tdesc_find_feature (const struct target_desc *target_desc, 731 const char *name) 732 { 733 for (const tdesc_feature_up &feature : target_desc->features) 734 if (feature->name == name) 735 return feature.get (); 736 737 return NULL; 738 } 739 740 /* Return the name of FEATURE. */ 741 742 const char * 743 tdesc_feature_name (const struct tdesc_feature *feature) 744 { 745 return feature->name.c_str (); 746 } 747 748 /* Lookup type associated with ID. */ 749 750 struct type * 751 tdesc_find_type (struct gdbarch *gdbarch, const char *id) 752 { 753 tdesc_arch_data *data 754 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 755 756 for (const tdesc_arch_reg ® : data->arch_regs) 757 { 758 if (reg.reg 759 && reg.reg->tdesc_type 760 && reg.type 761 && reg.reg->tdesc_type->name == id) 762 return reg.type; 763 } 764 765 return NULL; 766 } 767 768 /* Support for registers from target descriptions. */ 769 770 /* Construct the per-gdbarch data. */ 771 772 static void * 773 tdesc_data_init (struct obstack *obstack) 774 { 775 return obstack_new<tdesc_arch_data> (obstack); 776 } 777 778 /* Similar, but for the temporary copy used during architecture 779 initialization. */ 780 781 struct tdesc_arch_data * 782 tdesc_data_alloc (void) 783 { 784 return new tdesc_arch_data (); 785 } 786 787 /* Free something allocated by tdesc_data_alloc, if it is not going 788 to be used (for instance if it was unsuitable for the 789 architecture). */ 790 791 void 792 tdesc_data_cleanup (void *data_untyped) 793 { 794 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped; 795 796 delete data; 797 } 798 799 /* Search FEATURE for a register named NAME. */ 800 801 static struct tdesc_reg * 802 tdesc_find_register_early (const struct tdesc_feature *feature, 803 const char *name) 804 { 805 for (const tdesc_reg_up ® : feature->registers) 806 if (strcasecmp (reg->name.c_str (), name) == 0) 807 return reg.get (); 808 809 return NULL; 810 } 811 812 /* Search FEATURE for a register named NAME. Assign REGNO to it. */ 813 814 int 815 tdesc_numbered_register (const struct tdesc_feature *feature, 816 struct tdesc_arch_data *data, 817 int regno, const char *name) 818 { 819 struct tdesc_reg *reg = tdesc_find_register_early (feature, name); 820 821 if (reg == NULL) 822 return 0; 823 824 /* Make sure the vector includes a REGNO'th element. */ 825 while (regno >= data->arch_regs.size ()) 826 data->arch_regs.emplace_back (nullptr, nullptr); 827 828 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL); 829 830 return 1; 831 } 832 833 /* Search FEATURE for a register named NAME, but do not assign a fixed 834 register number to it. */ 835 836 int 837 tdesc_unnumbered_register (const struct tdesc_feature *feature, 838 const char *name) 839 { 840 struct tdesc_reg *reg = tdesc_find_register_early (feature, name); 841 842 if (reg == NULL) 843 return 0; 844 845 return 1; 846 } 847 848 /* Search FEATURE for a register whose name is in NAMES and assign 849 REGNO to it. */ 850 851 int 852 tdesc_numbered_register_choices (const struct tdesc_feature *feature, 853 struct tdesc_arch_data *data, 854 int regno, const char *const names[]) 855 { 856 int i; 857 858 for (i = 0; names[i] != NULL; i++) 859 if (tdesc_numbered_register (feature, data, regno, names[i])) 860 return 1; 861 862 return 0; 863 } 864 865 /* Search FEATURE for a register named NAME, and return its size in 866 bits. The register must exist. */ 867 868 int 869 tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name) 870 { 871 struct tdesc_reg *reg = tdesc_find_register_early (feature, name); 872 873 gdb_assert (reg != NULL); 874 return reg->bitsize; 875 } 876 877 /* Look up a register by its GDB internal register number. */ 878 879 static struct tdesc_arch_reg * 880 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno) 881 { 882 struct tdesc_arch_data *data; 883 884 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 885 if (regno < data->arch_regs.size ()) 886 return &data->arch_regs[regno]; 887 else 888 return NULL; 889 } 890 891 static struct tdesc_reg * 892 tdesc_find_register (struct gdbarch *gdbarch, int regno) 893 { 894 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno); 895 896 return reg? reg->reg : NULL; 897 } 898 899 /* Return the name of register REGNO, from the target description or 900 from an architecture-provided pseudo_register_name method. */ 901 902 const char * 903 tdesc_register_name (struct gdbarch *gdbarch, int regno) 904 { 905 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno); 906 int num_regs = gdbarch_num_regs (gdbarch); 907 908 if (reg != NULL) 909 return reg->name.c_str (); 910 911 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch)) 912 { 913 struct tdesc_arch_data *data 914 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 915 916 gdb_assert (data->pseudo_register_name != NULL); 917 return data->pseudo_register_name (gdbarch, regno); 918 } 919 920 return ""; 921 } 922 923 struct type * 924 tdesc_register_type (struct gdbarch *gdbarch, int regno) 925 { 926 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno); 927 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL; 928 int num_regs = gdbarch_num_regs (gdbarch); 929 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch); 930 931 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs) 932 { 933 struct tdesc_arch_data *data 934 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 935 936 gdb_assert (data->pseudo_register_type != NULL); 937 return data->pseudo_register_type (gdbarch, regno); 938 } 939 940 if (reg == NULL) 941 /* Return "int0_t", since "void" has a misleading size of one. */ 942 return builtin_type (gdbarch)->builtin_int0; 943 944 if (arch_reg->type == NULL) 945 { 946 /* First check for a predefined or target defined type. */ 947 if (reg->tdesc_type) 948 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type); 949 950 /* Next try size-sensitive type shortcuts. */ 951 else if (reg->type == "float") 952 { 953 if (reg->bitsize == gdbarch_float_bit (gdbarch)) 954 arch_reg->type = builtin_type (gdbarch)->builtin_float; 955 else if (reg->bitsize == gdbarch_double_bit (gdbarch)) 956 arch_reg->type = builtin_type (gdbarch)->builtin_double; 957 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch)) 958 arch_reg->type = builtin_type (gdbarch)->builtin_long_double; 959 else 960 { 961 warning (_("Register \"%s\" has an unsupported size (%d bits)"), 962 reg->name.c_str (), reg->bitsize); 963 arch_reg->type = builtin_type (gdbarch)->builtin_double; 964 } 965 } 966 else if (reg->type == "int") 967 { 968 if (reg->bitsize == gdbarch_long_bit (gdbarch)) 969 arch_reg->type = builtin_type (gdbarch)->builtin_long; 970 else if (reg->bitsize == TARGET_CHAR_BIT) 971 arch_reg->type = builtin_type (gdbarch)->builtin_char; 972 else if (reg->bitsize == gdbarch_short_bit (gdbarch)) 973 arch_reg->type = builtin_type (gdbarch)->builtin_short; 974 else if (reg->bitsize == gdbarch_int_bit (gdbarch)) 975 arch_reg->type = builtin_type (gdbarch)->builtin_int; 976 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch)) 977 arch_reg->type = builtin_type (gdbarch)->builtin_long_long; 978 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch)) 979 /* A bit desperate by this point... */ 980 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr; 981 else 982 { 983 warning (_("Register \"%s\" has an unsupported size (%d bits)"), 984 reg->name.c_str (), reg->bitsize); 985 arch_reg->type = builtin_type (gdbarch)->builtin_long; 986 } 987 } 988 989 if (arch_reg->type == NULL) 990 internal_error (__FILE__, __LINE__, 991 "Register \"%s\" has an unknown type \"%s\"", 992 reg->name.c_str (), reg->type.c_str ()); 993 } 994 995 return arch_reg->type; 996 } 997 998 static int 999 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno) 1000 { 1001 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno); 1002 1003 if (reg != NULL) 1004 return reg->target_regnum; 1005 else 1006 return -1; 1007 } 1008 1009 /* Check whether REGNUM is a member of REGGROUP. Registers from the 1010 target description may be classified as general, float, vector or other 1011 register groups registered with reggroup_add(). Unlike a gdbarch 1012 register_reggroup_p method, this function will return -1 if it does not 1013 know; the caller should handle registers with no specified group. 1014 1015 The names of containing features are not used. This might be extended 1016 to display registers in some more useful groupings. 1017 1018 The save-restore flag is also implemented here. */ 1019 1020 int 1021 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno, 1022 struct reggroup *reggroup) 1023 { 1024 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno); 1025 1026 if (reg != NULL && !reg->group.empty () 1027 && (reg->group == reggroup_name (reggroup))) 1028 return 1; 1029 1030 if (reg != NULL 1031 && (reggroup == save_reggroup || reggroup == restore_reggroup)) 1032 return reg->save_restore; 1033 1034 return -1; 1035 } 1036 1037 /* Check whether REGNUM is a member of REGGROUP. Registers with no 1038 group specified go to the default reggroup function and are handled 1039 by type. */ 1040 1041 static int 1042 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno, 1043 struct reggroup *reggroup) 1044 { 1045 int num_regs = gdbarch_num_regs (gdbarch); 1046 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch); 1047 int ret; 1048 1049 if (regno >= num_regs && regno < num_regs + num_pseudo_regs) 1050 { 1051 struct tdesc_arch_data *data 1052 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 1053 1054 if (data->pseudo_register_reggroup_p != NULL) 1055 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup); 1056 /* Otherwise fall through to the default reggroup_p. */ 1057 } 1058 1059 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup); 1060 if (ret != -1) 1061 return ret; 1062 1063 return default_register_reggroup_p (gdbarch, regno, reggroup); 1064 } 1065 1066 /* Record architecture-specific functions to call for pseudo-register 1067 support. */ 1068 1069 void 1070 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch, 1071 gdbarch_register_name_ftype *pseudo_name) 1072 { 1073 struct tdesc_arch_data *data 1074 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 1075 1076 data->pseudo_register_name = pseudo_name; 1077 } 1078 1079 void 1080 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch, 1081 gdbarch_register_type_ftype *pseudo_type) 1082 { 1083 struct tdesc_arch_data *data 1084 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 1085 1086 data->pseudo_register_type = pseudo_type; 1087 } 1088 1089 void 1090 set_tdesc_pseudo_register_reggroup_p 1091 (struct gdbarch *gdbarch, 1092 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p) 1093 { 1094 struct tdesc_arch_data *data 1095 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 1096 1097 data->pseudo_register_reggroup_p = pseudo_reggroup_p; 1098 } 1099 1100 /* Update GDBARCH to use the target description for registers. */ 1101 1102 void 1103 tdesc_use_registers (struct gdbarch *gdbarch, 1104 const struct target_desc *target_desc, 1105 struct tdesc_arch_data *early_data, 1106 tdesc_unknown_register_ftype unk_reg_cb) 1107 { 1108 int num_regs = gdbarch_num_regs (gdbarch); 1109 struct tdesc_arch_data *data; 1110 htab_t reg_hash; 1111 1112 /* We can't use the description for registers if it doesn't describe 1113 any. This function should only be called after validating 1114 registers, so the caller should know that registers are 1115 included. */ 1116 gdb_assert (tdesc_has_registers (target_desc)); 1117 1118 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data); 1119 data->arch_regs = early_data->arch_regs; 1120 delete early_data; 1121 1122 /* Build up a set of all registers, so that we can assign register 1123 numbers where needed. The hash table expands as necessary, so 1124 the initial size is arbitrary. */ 1125 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 1126 for (const tdesc_feature_up &feature : target_desc->features) 1127 for (const tdesc_reg_up ® : feature->registers) 1128 { 1129 void **slot = htab_find_slot (reg_hash, reg.get (), INSERT); 1130 1131 *slot = reg.get (); 1132 /* Add reggroup if its new. */ 1133 if (!reg->group.empty ()) 1134 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL) 1135 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch, 1136 reg->group.c_str (), 1137 USER_REGGROUP)); 1138 } 1139 1140 /* Remove any registers which were assigned numbers by the 1141 architecture. */ 1142 for (const tdesc_arch_reg &arch_reg : data->arch_regs) 1143 if (arch_reg.reg != NULL) 1144 htab_remove_elt (reg_hash, arch_reg.reg); 1145 1146 /* Assign numbers to the remaining registers and add them to the 1147 list of registers. The new numbers are always above gdbarch_num_regs. 1148 Iterate over the features, not the hash table, so that the order 1149 matches that in the target description. */ 1150 1151 gdb_assert (data->arch_regs.size () <= num_regs); 1152 while (data->arch_regs.size () < num_regs) 1153 data->arch_regs.emplace_back (nullptr, nullptr); 1154 1155 /* First we give the target a chance to number previously unknown 1156 registers. This allows targets to record the numbers assigned based 1157 on which feature the register was from. */ 1158 if (unk_reg_cb != NULL) 1159 { 1160 for (const tdesc_feature_up &feature : target_desc->features) 1161 for (const tdesc_reg_up ® : feature->registers) 1162 if (htab_find (reg_hash, reg.get ()) != NULL) 1163 { 1164 int regno = unk_reg_cb (gdbarch, feature.get (), 1165 reg->name.c_str (), num_regs); 1166 gdb_assert (regno == -1 || regno >= num_regs); 1167 if (regno != -1) 1168 { 1169 while (regno >= data->arch_regs.size ()) 1170 data->arch_regs.emplace_back (nullptr, nullptr); 1171 data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL); 1172 num_regs = regno + 1; 1173 htab_remove_elt (reg_hash, reg.get ()); 1174 } 1175 } 1176 } 1177 1178 /* Ensure the array was sized correctly above. */ 1179 gdb_assert (data->arch_regs.size () == num_regs); 1180 1181 /* Now in a final pass we assign register numbers to any remaining 1182 unnumbered registers. */ 1183 for (const tdesc_feature_up &feature : target_desc->features) 1184 for (const tdesc_reg_up ® : feature->registers) 1185 if (htab_find (reg_hash, reg.get ()) != NULL) 1186 { 1187 data->arch_regs.emplace_back (reg.get (), nullptr); 1188 num_regs++; 1189 } 1190 1191 htab_delete (reg_hash); 1192 1193 /* Update the architecture. */ 1194 set_gdbarch_num_regs (gdbarch, num_regs); 1195 set_gdbarch_register_name (gdbarch, tdesc_register_name); 1196 set_gdbarch_register_type (gdbarch, tdesc_register_type); 1197 set_gdbarch_remote_register_number (gdbarch, 1198 tdesc_remote_register_number); 1199 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p); 1200 } 1201 1202 /* See gdbsupport/tdesc.h. */ 1203 1204 struct tdesc_feature * 1205 tdesc_create_feature (struct target_desc *tdesc, const char *name) 1206 { 1207 struct tdesc_feature *new_feature = new tdesc_feature (name); 1208 1209 tdesc->features.emplace_back (new_feature); 1210 1211 return new_feature; 1212 } 1213 1214 /* See gdbsupport/tdesc.h. */ 1215 1216 struct target_desc * 1217 allocate_target_description (void) 1218 { 1219 return new target_desc (); 1220 } 1221 1222 /* See gdbsupport/tdesc.h. */ 1223 1224 void 1225 target_desc_deleter::operator() (struct target_desc *target_desc) const 1226 { 1227 delete target_desc; 1228 } 1229 1230 void 1231 tdesc_add_compatible (struct target_desc *target_desc, 1232 const struct bfd_arch_info *compatible) 1233 { 1234 /* If this instance of GDB is compiled without BFD support for the 1235 compatible architecture, simply ignore it -- we would not be able 1236 to handle it anyway. */ 1237 if (compatible == NULL) 1238 return; 1239 1240 for (const tdesc_compatible_info_up &compat : target_desc->compatible) 1241 if (compat->arch () == compatible) 1242 internal_error (__FILE__, __LINE__, 1243 _("Attempted to add duplicate " 1244 "compatible architecture \"%s\""), 1245 compatible->printable_name); 1246 1247 target_desc->compatible.push_back 1248 (std::unique_ptr<tdesc_compatible_info> 1249 (new tdesc_compatible_info (compatible))); 1250 } 1251 1252 void 1253 set_tdesc_property (struct target_desc *target_desc, 1254 const char *key, const char *value) 1255 { 1256 gdb_assert (key != NULL && value != NULL); 1257 1258 if (tdesc_property (target_desc, key) != NULL) 1259 internal_error (__FILE__, __LINE__, 1260 _("Attempted to add duplicate property \"%s\""), key); 1261 1262 target_desc->properties.emplace_back (key, value); 1263 } 1264 1265 /* See gdbsupport/tdesc.h. */ 1266 1267 void 1268 set_tdesc_architecture (struct target_desc *target_desc, 1269 const char *name) 1270 { 1271 set_tdesc_architecture (target_desc, bfd_scan_arch (name)); 1272 } 1273 1274 void 1275 set_tdesc_architecture (struct target_desc *target_desc, 1276 const struct bfd_arch_info *arch) 1277 { 1278 target_desc->arch = arch; 1279 } 1280 1281 /* See gdbsupport/tdesc.h. */ 1282 1283 void 1284 set_tdesc_osabi (struct target_desc *target_desc, const char *name) 1285 { 1286 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name)); 1287 } 1288 1289 void 1290 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi) 1291 { 1292 target_desc->osabi = osabi; 1293 } 1294 1295 1296 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist; 1297 static struct cmd_list_element *tdesc_unset_cmdlist; 1298 1299 /* Helper functions for the CLI commands. */ 1300 1301 static void 1302 set_tdesc_filename_cmd (const char *args, int from_tty, 1303 struct cmd_list_element *c) 1304 { 1305 xfree (target_description_filename); 1306 target_description_filename = xstrdup (tdesc_filename_cmd_string); 1307 1308 target_clear_description (); 1309 target_find_description (); 1310 } 1311 1312 static void 1313 show_tdesc_filename_cmd (struct ui_file *file, int from_tty, 1314 struct cmd_list_element *c, 1315 const char *value) 1316 { 1317 value = target_description_filename; 1318 1319 if (value != NULL && *value != '\0') 1320 printf_filtered (_("The target description will be read from \"%s\".\n"), 1321 value); 1322 else 1323 printf_filtered (_("The target description will be " 1324 "read from the target.\n")); 1325 } 1326 1327 static void 1328 unset_tdesc_filename_cmd (const char *args, int from_tty) 1329 { 1330 xfree (target_description_filename); 1331 target_description_filename = NULL; 1332 target_clear_description (); 1333 target_find_description (); 1334 } 1335 1336 /* Print target description in C. */ 1337 1338 class print_c_tdesc : public tdesc_element_visitor 1339 { 1340 public: 1341 print_c_tdesc (std::string &filename_after_features) 1342 : m_filename_after_features (filename_after_features) 1343 { 1344 const char *inp; 1345 char *outp; 1346 const char *filename = lbasename (m_filename_after_features.c_str ()); 1347 1348 m_function = (char *) xmalloc (strlen (filename) + 1); 1349 for (inp = filename, outp = m_function; *inp != '\0'; inp++) 1350 if (*inp == '.') 1351 break; 1352 else if (*inp == '-') 1353 *outp++ = '_'; 1354 else if (*inp == ' ') 1355 *outp++ = '_'; 1356 else 1357 *outp++ = *inp; 1358 *outp = '\0'; 1359 1360 /* Standard boilerplate. */ 1361 printf_unfiltered ("/* THIS FILE IS GENERATED. " 1362 "-*- buffer-read-only: t -*- vi" 1363 ":set ro:\n"); 1364 } 1365 1366 ~print_c_tdesc () 1367 { 1368 xfree (m_function); 1369 } 1370 1371 void visit_pre (const target_desc *e) override 1372 { 1373 printf_unfiltered (" Original: %s */\n\n", 1374 lbasename (m_filename_after_features.c_str ())); 1375 1376 printf_unfiltered ("#include \"defs.h\"\n"); 1377 printf_unfiltered ("#include \"osabi.h\"\n"); 1378 printf_unfiltered ("#include \"target-descriptions.h\"\n"); 1379 printf_unfiltered ("\n"); 1380 1381 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function); 1382 printf_unfiltered ("static void\n"); 1383 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function); 1384 printf_unfiltered ("{\n"); 1385 printf_unfiltered 1386 (" struct target_desc *result = allocate_target_description ();\n"); 1387 1388 if (tdesc_architecture (e) != NULL) 1389 { 1390 printf_unfiltered 1391 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n", 1392 tdesc_architecture (e)->printable_name); 1393 printf_unfiltered ("\n"); 1394 } 1395 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN 1396 && tdesc_osabi (e) < GDB_OSABI_INVALID) 1397 { 1398 printf_unfiltered 1399 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n", 1400 gdbarch_osabi_name (tdesc_osabi (e))); 1401 printf_unfiltered ("\n"); 1402 } 1403 1404 for (const tdesc_compatible_info_up &compatible : e->compatible) 1405 printf_unfiltered 1406 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n", 1407 compatible->arch ()->printable_name); 1408 1409 if (!e->compatible.empty ()) 1410 printf_unfiltered ("\n"); 1411 1412 for (const property &prop : e->properties) 1413 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n", 1414 prop.key.c_str (), prop.value.c_str ()); 1415 1416 printf_unfiltered (" struct tdesc_feature *feature;\n"); 1417 } 1418 1419 void visit_pre (const tdesc_feature *e) override 1420 { 1421 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n", 1422 e->name.c_str ()); 1423 } 1424 1425 void visit_post (const tdesc_feature *e) override 1426 {} 1427 1428 void visit_post (const target_desc *e) override 1429 { 1430 printf_unfiltered ("\n tdesc_%s = result;\n", m_function); 1431 printf_unfiltered ("}\n"); 1432 } 1433 1434 void visit (const tdesc_type_builtin *type) override 1435 { 1436 error (_("C output is not supported type \"%s\"."), type->name.c_str ()); 1437 } 1438 1439 void visit (const tdesc_type_vector *type) override 1440 { 1441 if (!m_printed_element_type) 1442 { 1443 printf_unfiltered (" tdesc_type *element_type;\n"); 1444 m_printed_element_type = true; 1445 } 1446 1447 printf_unfiltered 1448 (" element_type = tdesc_named_type (feature, \"%s\");\n", 1449 type->element_type->name.c_str ()); 1450 printf_unfiltered 1451 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n", 1452 type->name.c_str (), type->count); 1453 1454 printf_unfiltered ("\n"); 1455 } 1456 1457 void visit (const tdesc_type_with_fields *type) override 1458 { 1459 if (!m_printed_type_with_fields) 1460 { 1461 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n"); 1462 m_printed_type_with_fields = true; 1463 } 1464 1465 switch (type->kind) 1466 { 1467 case TDESC_TYPE_STRUCT: 1468 case TDESC_TYPE_FLAGS: 1469 if (type->kind == TDESC_TYPE_STRUCT) 1470 { 1471 printf_unfiltered 1472 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n", 1473 type->name.c_str ()); 1474 if (type->size != 0) 1475 printf_unfiltered 1476 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size); 1477 } 1478 else 1479 { 1480 printf_unfiltered 1481 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n", 1482 type->name.c_str (), type->size); 1483 } 1484 for (const tdesc_type_field &f : type->fields) 1485 { 1486 const char *type_name; 1487 1488 gdb_assert (f.type != NULL); 1489 type_name = f.type->name.c_str (); 1490 1491 /* To minimize changes to generated files, don't emit type 1492 info for fields that have defaulted types. */ 1493 if (f.start != -1) 1494 { 1495 gdb_assert (f.end != -1); 1496 if (f.type->kind == TDESC_TYPE_BOOL) 1497 { 1498 gdb_assert (f.start == f.end); 1499 printf_unfiltered 1500 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n", 1501 f.start, f.name.c_str ()); 1502 } 1503 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32) 1504 || (type->size == 8 1505 && f.type->kind == TDESC_TYPE_UINT64)) 1506 { 1507 printf_unfiltered 1508 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n", 1509 f.name.c_str (), f.start, f.end); 1510 } 1511 else 1512 { 1513 printf_field_type_assignment 1514 ("tdesc_named_type (feature, \"%s\");\n", 1515 type_name); 1516 printf_unfiltered 1517 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\"," 1518 " %d, %d, field_type);\n", 1519 f.name.c_str (), f.start, f.end); 1520 } 1521 } 1522 else /* Not a bitfield. */ 1523 { 1524 gdb_assert (f.end == -1); 1525 gdb_assert (type->kind == TDESC_TYPE_STRUCT); 1526 printf_field_type_assignment 1527 ("tdesc_named_type (feature, \"%s\");\n", type_name); 1528 printf_unfiltered 1529 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n", 1530 f.name.c_str ()); 1531 } 1532 } 1533 break; 1534 case TDESC_TYPE_UNION: 1535 printf_unfiltered 1536 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n", 1537 type->name.c_str ()); 1538 for (const tdesc_type_field &f : type->fields) 1539 { 1540 printf_field_type_assignment 1541 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ()); 1542 printf_unfiltered 1543 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n", 1544 f.name.c_str ()); 1545 } 1546 break; 1547 case TDESC_TYPE_ENUM: 1548 printf_unfiltered 1549 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n", 1550 type->name.c_str (), type->size); 1551 for (const tdesc_type_field &f : type->fields) 1552 printf_unfiltered 1553 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n", 1554 f.start, f.name.c_str ()); 1555 break; 1556 default: 1557 error (_("C output is not supported type \"%s\"."), type->name.c_str ()); 1558 } 1559 1560 printf_unfiltered ("\n"); 1561 } 1562 1563 void visit (const tdesc_reg *reg) override 1564 { 1565 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ", 1566 reg->name.c_str (), reg->target_regnum, 1567 reg->save_restore); 1568 if (!reg->group.empty ()) 1569 printf_unfiltered ("\"%s\", ", reg->group.c_str ()); 1570 else 1571 printf_unfiltered ("NULL, "); 1572 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ()); 1573 } 1574 1575 protected: 1576 std::string m_filename_after_features; 1577 1578 private: 1579 1580 /* Print an assignment to the field_type variable. Print the declaration 1581 of field_type if that has not been done yet. */ 1582 ATTRIBUTE_PRINTF (2, 3) 1583 void printf_field_type_assignment (const char *fmt, ...) 1584 { 1585 if (!m_printed_field_type) 1586 { 1587 printf_unfiltered (" tdesc_type *field_type;\n"); 1588 m_printed_field_type = true; 1589 } 1590 1591 printf_unfiltered (" field_type = "); 1592 1593 va_list args; 1594 va_start (args, fmt); 1595 vprintf_unfiltered (fmt, args); 1596 va_end (args); 1597 } 1598 1599 char *m_function; 1600 1601 /* Did we print "struct tdesc_type *element_type;" yet? */ 1602 bool m_printed_element_type = false; 1603 1604 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */ 1605 bool m_printed_type_with_fields = false; 1606 1607 /* Did we print "struct tdesc_type *field_type;" yet? */ 1608 bool m_printed_field_type = false; 1609 }; 1610 1611 /* Print target description feature in C. */ 1612 1613 class print_c_feature : public print_c_tdesc 1614 { 1615 public: 1616 print_c_feature (std::string &file) 1617 : print_c_tdesc (file) 1618 { 1619 /* Trim ".tmp". */ 1620 auto const pos = m_filename_after_features.find_last_of ('.'); 1621 1622 m_filename_after_features = m_filename_after_features.substr (0, pos); 1623 } 1624 1625 void visit_pre (const target_desc *e) override 1626 { 1627 printf_unfiltered (" Original: %s */\n\n", 1628 lbasename (m_filename_after_features.c_str ())); 1629 1630 printf_unfiltered ("#include \"gdbsupport/tdesc.h\"\n"); 1631 printf_unfiltered ("\n"); 1632 } 1633 1634 void visit_post (const target_desc *e) override 1635 {} 1636 1637 void visit_pre (const tdesc_feature *e) override 1638 { 1639 std::string name (m_filename_after_features); 1640 1641 auto pos = name.find_first_of ('.'); 1642 1643 name = name.substr (0, pos); 1644 std::replace (name.begin (), name.end (), '/', '_'); 1645 std::replace (name.begin (), name.end (), '-', '_'); 1646 1647 printf_unfiltered ("static int\n"); 1648 printf_unfiltered ("create_feature_%s ", name.c_str ()); 1649 printf_unfiltered ("(struct target_desc *result, long regnum)\n"); 1650 1651 printf_unfiltered ("{\n"); 1652 printf_unfiltered (" struct tdesc_feature *feature;\n"); 1653 1654 printf_unfiltered 1655 ("\n feature = tdesc_create_feature (result, \"%s\");\n", 1656 e->name.c_str ()); 1657 } 1658 1659 void visit_post (const tdesc_feature *e) override 1660 { 1661 printf_unfiltered (" return regnum;\n"); 1662 printf_unfiltered ("}\n"); 1663 } 1664 1665 void visit (const tdesc_reg *reg) override 1666 { 1667 /* Most "reg" in XML target descriptions don't have "regnum" 1668 attribute, so the register number is allocated sequentially. 1669 In case that reg has "regnum" attribute, register number 1670 should be set by that explicitly. */ 1671 1672 if (reg->target_regnum < m_next_regnum) 1673 { 1674 /* The integrity check, it can catch some errors on register 1675 number collision, like this, 1676 1677 <reg name="x0" bitsize="32"/> 1678 <reg name="x1" bitsize="32"/> 1679 <reg name="x2" bitsize="32"/> 1680 <reg name="x3" bitsize="32"/> 1681 <reg name="ps" bitsize="32" regnum="3"/> 1682 1683 but it also has false negatives. The target description 1684 below is correct, 1685 1686 <reg name="x1" bitsize="32" regnum="1"/> 1687 <reg name="x3" bitsize="32" regnum="3"/> 1688 <reg name="x2" bitsize="32" regnum="2"/> 1689 <reg name="x4" bitsize="32" regnum="4"/> 1690 1691 but it is not a good practice, so still error on this, 1692 and also print the message so that it can be saved in the 1693 generated c file. */ 1694 1695 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ", 1696 reg->target_regnum); 1697 printf_unfiltered ("is not the largest number (%d).\n", 1698 m_next_regnum); 1699 error (_("\"regnum\" attribute %ld is not the largest number (%d)."), 1700 reg->target_regnum, m_next_regnum); 1701 } 1702 1703 if (reg->target_regnum > m_next_regnum) 1704 { 1705 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum); 1706 m_next_regnum = reg->target_regnum; 1707 } 1708 1709 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ", 1710 reg->name.c_str (), reg->save_restore); 1711 if (!reg->group.empty ()) 1712 printf_unfiltered ("\"%s\", ", reg->group.c_str ()); 1713 else 1714 printf_unfiltered ("NULL, "); 1715 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ()); 1716 1717 m_next_regnum++; 1718 } 1719 1720 private: 1721 /* The register number to use for the next register we see. */ 1722 int m_next_regnum = 0; 1723 }; 1724 1725 /* See gdbsupport/tdesc.h. */ 1726 1727 const char * 1728 tdesc_get_features_xml (const target_desc *tdesc) 1729 { 1730 if (tdesc->xmltarget == nullptr) 1731 { 1732 std::string buffer ("@"); 1733 print_xml_feature v (&buffer); 1734 tdesc->accept (v); 1735 tdesc->xmltarget = xstrdup (buffer.c_str ()); 1736 } 1737 return tdesc->xmltarget; 1738 } 1739 1740 static void 1741 maint_print_c_tdesc_cmd (const char *args, int from_tty) 1742 { 1743 const struct target_desc *tdesc; 1744 const char *filename; 1745 1746 if (args == NULL) 1747 { 1748 /* Use the global target-supplied description, not the current 1749 architecture's. This lets a GDB for one architecture generate C 1750 for another architecture's description, even though the gdbarch 1751 initialization code will reject the new description. */ 1752 tdesc = current_target_desc; 1753 filename = target_description_filename; 1754 } 1755 else 1756 { 1757 /* Use the target description from the XML file. */ 1758 filename = args; 1759 tdesc = file_read_description_xml (filename); 1760 } 1761 1762 if (tdesc == NULL) 1763 error (_("There is no target description to print.")); 1764 1765 if (filename == NULL) 1766 filename = "fetched from target"; 1767 1768 std::string filename_after_features (filename); 1769 auto loc = filename_after_features.rfind ("/features/"); 1770 1771 if (loc != std::string::npos) 1772 filename_after_features = filename_after_features.substr (loc + 10); 1773 1774 /* Print c files for target features instead of target descriptions, 1775 because c files got from target features are more flexible than the 1776 counterparts. */ 1777 if (startswith (filename_after_features.c_str (), "i386/32bit-") 1778 || startswith (filename_after_features.c_str (), "i386/64bit-") 1779 || startswith (filename_after_features.c_str (), "i386/x32-core.xml") 1780 || startswith (filename_after_features.c_str (), "riscv/") 1781 || startswith (filename_after_features.c_str (), "tic6x-") 1782 || startswith (filename_after_features.c_str (), "aarch64") 1783 || startswith (filename_after_features.c_str (), "arm/") 1784 || startswith (filename_after_features.c_str (), "arc/")) 1785 { 1786 print_c_feature v (filename_after_features); 1787 1788 tdesc->accept (v); 1789 } 1790 else 1791 { 1792 print_c_tdesc v (filename_after_features); 1793 1794 tdesc->accept (v); 1795 } 1796 } 1797 1798 /* Implement the maintenance print xml-tdesc command. */ 1799 1800 static void 1801 maint_print_xml_tdesc_cmd (const char *args, int from_tty) 1802 { 1803 const struct target_desc *tdesc; 1804 1805 if (args == NULL) 1806 { 1807 /* Use the global target-supplied description, not the current 1808 architecture's. This lets a GDB for one architecture generate XML 1809 for another architecture's description, even though the gdbarch 1810 initialization code will reject the new description. */ 1811 tdesc = current_target_desc; 1812 } 1813 else 1814 { 1815 /* Use the target description from the XML file. */ 1816 tdesc = file_read_description_xml (args); 1817 } 1818 1819 if (tdesc == NULL) 1820 error (_("There is no target description to print.")); 1821 1822 std::string buf; 1823 print_xml_feature v (&buf); 1824 tdesc->accept (v); 1825 puts_unfiltered (buf.c_str ()); 1826 } 1827 1828 namespace selftests { 1829 1830 /* A reference target description, used for testing (see record_xml_tdesc). */ 1831 1832 struct xml_test_tdesc 1833 { 1834 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc) 1835 : name (name), tdesc (std::move (tdesc)) 1836 {} 1837 1838 const char *name; 1839 std::unique_ptr<const target_desc> tdesc; 1840 }; 1841 1842 static std::vector<xml_test_tdesc> xml_tdesc; 1843 1844 #if GDB_SELF_TEST 1845 1846 /* See target-descriptions.h. */ 1847 1848 void 1849 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc) 1850 { 1851 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc)); 1852 } 1853 #endif 1854 1855 } 1856 1857 /* Test the conversion process of a target description to/from xml: Take a target 1858 description TDESC, convert to xml, back to a description, and confirm the new 1859 tdesc is identical to the original. */ 1860 static bool 1861 maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name) 1862 { 1863 const char *xml = tdesc_get_features_xml (tdesc); 1864 1865 if (xml == nullptr || *xml != '@') 1866 { 1867 printf_filtered (_("Could not convert description for %s to xml.\n"), 1868 name); 1869 return false; 1870 } 1871 1872 const target_desc *tdesc_trans = string_read_description_xml (xml + 1); 1873 1874 if (tdesc_trans == nullptr) 1875 { 1876 printf_filtered (_("Could not convert description for %s from xml.\n"), 1877 name); 1878 return false; 1879 } 1880 else if (*tdesc != *tdesc_trans) 1881 { 1882 printf_filtered (_("Converted description for %s does not match.\n"), 1883 name); 1884 return false; 1885 } 1886 return true; 1887 } 1888 1889 1890 /* Check that the target descriptions created dynamically by 1891 architecture-specific code equal the descriptions created from XML files 1892 found in the specified directory DIR. */ 1893 1894 static void 1895 maintenance_check_xml_descriptions (const char *dir, int from_tty) 1896 { 1897 if (dir == NULL) 1898 error (_("Missing dir name")); 1899 1900 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir)); 1901 std::string feature_dir (dir1.get ()); 1902 unsigned int failed = 0; 1903 1904 for (auto const &e : selftests::xml_tdesc) 1905 { 1906 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name); 1907 const target_desc *tdesc 1908 = file_read_description_xml (tdesc_xml.data ()); 1909 1910 if (tdesc == NULL || *tdesc != *e.tdesc) 1911 { 1912 printf_filtered ( _("Descriptions for %s do not match.\n"), e.name); 1913 failed++; 1914 } 1915 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name) 1916 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name)) 1917 failed++; 1918 } 1919 printf_filtered (_("Tested %lu XML files, %d failed\n"), 1920 (long) selftests::xml_tdesc.size (), failed); 1921 } 1922 1923 void _initialize_target_descriptions (); 1924 void 1925 _initialize_target_descriptions () 1926 { 1927 cmd_list_element *cmd; 1928 1929 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init); 1930 1931 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\ 1932 Set target description specific variables."), 1933 &tdesc_set_cmdlist, "set tdesc ", 1934 0 /* allow-unknown */, &setlist); 1935 add_show_prefix_cmd ("tdesc", class_maintenance, _("\ 1936 Show target description specific variables."), 1937 &tdesc_show_cmdlist, "show tdesc ", 1938 0 /* allow-unknown */, &showlist); 1939 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\ 1940 Unset target description specific variables."), 1941 &tdesc_unset_cmdlist, "unset tdesc ", 1942 0 /* allow-unknown */, &unsetlist); 1943 1944 add_setshow_filename_cmd ("filename", class_obscure, 1945 &tdesc_filename_cmd_string, 1946 _("\ 1947 Set the file to read for an XML target description."), _("\ 1948 Show the file to read for an XML target description."), _("\ 1949 When set, GDB will read the target description from a local\n\ 1950 file instead of querying the remote target."), 1951 set_tdesc_filename_cmd, 1952 show_tdesc_filename_cmd, 1953 &tdesc_set_cmdlist, &tdesc_show_cmdlist); 1954 1955 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\ 1956 Unset the file to read for an XML target description.\n\ 1957 When unset, GDB will read the description from the target."), 1958 &tdesc_unset_cmdlist); 1959 1960 cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\ 1961 Print the current target description as a C source file."), 1962 &maintenanceprintlist); 1963 set_cmd_completer (cmd, filename_completer); 1964 1965 cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\ 1966 Print the current target description as an XML file."), 1967 &maintenanceprintlist); 1968 set_cmd_completer (cmd, filename_completer); 1969 1970 cmd = add_cmd ("xml-descriptions", class_maintenance, 1971 maintenance_check_xml_descriptions, _("\ 1972 Check equality of GDB target descriptions and XML created descriptions.\n\ 1973 Check the target descriptions created in GDB equal the descriptions\n\ 1974 created from XML files in the directory.\n\ 1975 The parameter is the directory name."), 1976 &maintenancechecklist); 1977 set_cmd_completer (cmd, filename_completer); 1978 } 1979