1 /* XML target description support for GDB. 2 3 Copyright (C) 2006-2015 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 "target.h" 24 #include "target-descriptions.h" 25 #include "xml-support.h" 26 #include "xml-tdesc.h" 27 #include "osabi.h" 28 29 #include "filenames.h" 30 31 #if !defined(HAVE_LIBEXPAT) 32 33 /* Parse DOCUMENT into a target description. Or don't, since we don't have 34 an XML parser. */ 35 36 static struct target_desc * 37 tdesc_parse_xml (const char *document, xml_fetch_another fetcher, 38 void *fetcher_baton) 39 { 40 static int have_warned; 41 42 if (!have_warned) 43 { 44 have_warned = 1; 45 warning (_("Can not parse XML target description; XML support was " 46 "disabled at compile time")); 47 } 48 49 return NULL; 50 } 51 52 #else /* HAVE_LIBEXPAT */ 53 54 /* A record of every XML description we have parsed. We never discard 55 old descriptions, because we never discard gdbarches. As long as we 56 have a gdbarch referencing this description, we want to have a copy 57 of it here, so that if we parse the same XML document again we can 58 return the same "struct target_desc *"; if they are not singletons, 59 then we will create unnecessary duplicate gdbarches. See 60 gdbarch_list_lookup_by_info. */ 61 62 struct tdesc_xml_cache 63 { 64 const char *xml_document; 65 struct target_desc *tdesc; 66 }; 67 typedef struct tdesc_xml_cache tdesc_xml_cache_s; 68 DEF_VEC_O(tdesc_xml_cache_s); 69 70 static VEC(tdesc_xml_cache_s) *xml_cache; 71 72 /* Callback data for target description parsing. */ 73 74 struct tdesc_parsing_data 75 { 76 /* The target description we are building. */ 77 struct target_desc *tdesc; 78 79 /* The target feature we are currently parsing, or last parsed. */ 80 struct tdesc_feature *current_feature; 81 82 /* The register number to use for the next register we see, if 83 it does not have its own. This starts at zero. */ 84 int next_regnum; 85 86 /* The struct or union we are currently parsing, or last parsed. */ 87 struct tdesc_type *current_type; 88 89 /* The byte size of the current struct type, if specified. Zero 90 if not specified. */ 91 int current_type_size; 92 93 /* Whether the current type is a flags type. */ 94 int current_type_is_flags; 95 }; 96 97 /* Handle the end of an <architecture> element and its value. */ 98 99 static void 100 tdesc_end_arch (struct gdb_xml_parser *parser, 101 const struct gdb_xml_element *element, 102 void *user_data, const char *body_text) 103 { 104 struct tdesc_parsing_data *data = user_data; 105 const struct bfd_arch_info *arch; 106 107 arch = bfd_scan_arch (body_text); 108 if (arch == NULL) 109 gdb_xml_error (parser, _("Target description specified unknown " 110 "architecture \"%s\""), body_text); 111 set_tdesc_architecture (data->tdesc, arch); 112 } 113 114 /* Handle the end of an <osabi> element and its value. */ 115 116 static void 117 tdesc_end_osabi (struct gdb_xml_parser *parser, 118 const struct gdb_xml_element *element, 119 void *user_data, const char *body_text) 120 { 121 struct tdesc_parsing_data *data = user_data; 122 enum gdb_osabi osabi; 123 124 osabi = osabi_from_tdesc_string (body_text); 125 if (osabi == GDB_OSABI_UNKNOWN) 126 warning (_("Target description specified unknown osabi \"%s\""), 127 body_text); 128 else 129 set_tdesc_osabi (data->tdesc, osabi); 130 } 131 132 /* Handle the end of a <compatible> element and its value. */ 133 134 static void 135 tdesc_end_compatible (struct gdb_xml_parser *parser, 136 const struct gdb_xml_element *element, 137 void *user_data, const char *body_text) 138 { 139 struct tdesc_parsing_data *data = user_data; 140 const struct bfd_arch_info *arch; 141 142 arch = bfd_scan_arch (body_text); 143 tdesc_add_compatible (data->tdesc, arch); 144 } 145 146 /* Handle the start of a <target> element. */ 147 148 static void 149 tdesc_start_target (struct gdb_xml_parser *parser, 150 const struct gdb_xml_element *element, 151 void *user_data, VEC(gdb_xml_value_s) *attributes) 152 { 153 char *version = xml_find_attribute (attributes, "version")->value; 154 155 if (strcmp (version, "1.0") != 0) 156 gdb_xml_error (parser, 157 _("Target description has unsupported version \"%s\""), 158 version); 159 } 160 161 /* Handle the start of a <feature> element. */ 162 163 static void 164 tdesc_start_feature (struct gdb_xml_parser *parser, 165 const struct gdb_xml_element *element, 166 void *user_data, VEC(gdb_xml_value_s) *attributes) 167 { 168 struct tdesc_parsing_data *data = user_data; 169 char *name = xml_find_attribute (attributes, "name")->value; 170 171 data->current_feature = tdesc_create_feature (data->tdesc, name); 172 } 173 174 /* Handle the start of a <reg> element. Fill in the optional 175 attributes and attach it to the containing feature. */ 176 177 static void 178 tdesc_start_reg (struct gdb_xml_parser *parser, 179 const struct gdb_xml_element *element, 180 void *user_data, VEC(gdb_xml_value_s) *attributes) 181 { 182 struct tdesc_parsing_data *data = user_data; 183 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes); 184 int ix = 0, length; 185 char *name, *group, *type; 186 int bitsize, regnum, save_restore; 187 188 length = VEC_length (gdb_xml_value_s, attributes); 189 190 name = attrs[ix++].value; 191 bitsize = * (ULONGEST *) attrs[ix++].value; 192 193 if (ix < length && strcmp (attrs[ix].name, "regnum") == 0) 194 regnum = * (ULONGEST *) attrs[ix++].value; 195 else 196 regnum = data->next_regnum; 197 198 if (ix < length && strcmp (attrs[ix].name, "type") == 0) 199 type = attrs[ix++].value; 200 else 201 type = "int"; 202 203 if (ix < length && strcmp (attrs[ix].name, "group") == 0) 204 group = attrs[ix++].value; 205 else 206 group = NULL; 207 208 if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0) 209 save_restore = * (ULONGEST *) attrs[ix++].value; 210 else 211 save_restore = 1; 212 213 if (strcmp (type, "int") != 0 214 && strcmp (type, "float") != 0 215 && tdesc_named_type (data->current_feature, type) == NULL) 216 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""), 217 name, type); 218 219 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group, 220 bitsize, type); 221 222 data->next_regnum = regnum + 1; 223 } 224 225 /* Handle the start of a <union> element. Initialize the type and 226 record it with the current feature. */ 227 228 static void 229 tdesc_start_union (struct gdb_xml_parser *parser, 230 const struct gdb_xml_element *element, 231 void *user_data, VEC(gdb_xml_value_s) *attributes) 232 { 233 struct tdesc_parsing_data *data = user_data; 234 char *id = xml_find_attribute (attributes, "id")->value; 235 236 data->current_type = tdesc_create_union (data->current_feature, id); 237 data->current_type_size = 0; 238 data->current_type_is_flags = 0; 239 } 240 241 /* Handle the start of a <struct> element. Initialize the type and 242 record it with the current feature. */ 243 244 static void 245 tdesc_start_struct (struct gdb_xml_parser *parser, 246 const struct gdb_xml_element *element, 247 void *user_data, VEC(gdb_xml_value_s) *attributes) 248 { 249 struct tdesc_parsing_data *data = user_data; 250 char *id = xml_find_attribute (attributes, "id")->value; 251 struct tdesc_type *type; 252 struct gdb_xml_value *attr; 253 254 type = tdesc_create_struct (data->current_feature, id); 255 data->current_type = type; 256 data->current_type_size = 0; 257 data->current_type_is_flags = 0; 258 259 attr = xml_find_attribute (attributes, "size"); 260 if (attr != NULL) 261 { 262 int size = (int) * (ULONGEST *) attr->value; 263 264 tdesc_set_struct_size (type, size); 265 data->current_type_size = size; 266 } 267 } 268 269 static void 270 tdesc_start_flags (struct gdb_xml_parser *parser, 271 const struct gdb_xml_element *element, 272 void *user_data, VEC(gdb_xml_value_s) *attributes) 273 { 274 struct tdesc_parsing_data *data = user_data; 275 char *id = xml_find_attribute (attributes, "id")->value; 276 int length = (int) * (ULONGEST *) 277 xml_find_attribute (attributes, "size")->value; 278 struct tdesc_type *type; 279 280 type = tdesc_create_flags (data->current_feature, id, length); 281 282 data->current_type = type; 283 data->current_type_size = 0; 284 data->current_type_is_flags = 1; 285 } 286 287 /* Handle the start of a <field> element. Attach the field to the 288 current struct or union. */ 289 290 static void 291 tdesc_start_field (struct gdb_xml_parser *parser, 292 const struct gdb_xml_element *element, 293 void *user_data, VEC(gdb_xml_value_s) *attributes) 294 { 295 struct tdesc_parsing_data *data = user_data; 296 struct gdb_xml_value *attr; 297 struct tdesc_type *field_type; 298 char *field_name, *field_type_id; 299 int start, end; 300 301 field_name = xml_find_attribute (attributes, "name")->value; 302 303 attr = xml_find_attribute (attributes, "type"); 304 if (attr != NULL) 305 field_type_id = attr->value; 306 else 307 field_type_id = NULL; 308 309 attr = xml_find_attribute (attributes, "start"); 310 if (attr != NULL) 311 start = * (ULONGEST *) attr->value; 312 else 313 start = -1; 314 315 attr = xml_find_attribute (attributes, "end"); 316 if (attr != NULL) 317 end = * (ULONGEST *) attr->value; 318 else 319 end = -1; 320 321 if (field_type_id != NULL) 322 { 323 if (data->current_type_is_flags) 324 gdb_xml_error (parser, _("Cannot add typed field \"%s\" to flags"), 325 field_name); 326 if (data->current_type_size != 0) 327 gdb_xml_error (parser, 328 _("Explicitly sized type can not " 329 "contain non-bitfield \"%s\""), 330 field_name); 331 332 field_type = tdesc_named_type (data->current_feature, field_type_id); 333 if (field_type == NULL) 334 gdb_xml_error (parser, _("Field \"%s\" references undefined " 335 "type \"%s\""), 336 field_name, field_type_id); 337 338 tdesc_add_field (data->current_type, field_name, field_type); 339 } 340 else if (start != -1 && end != -1) 341 { 342 struct tdesc_type *t = data->current_type; 343 344 if (data->current_type_is_flags) 345 tdesc_add_flag (t, start, field_name); 346 else 347 { 348 if (data->current_type_size == 0) 349 gdb_xml_error (parser, 350 _("Implicitly sized type can " 351 "not contain bitfield \"%s\""), 352 field_name); 353 354 if (end >= 64) 355 gdb_xml_error (parser, 356 _("Bitfield \"%s\" goes past " 357 "64 bits (unsupported)"), 358 field_name); 359 360 /* Assume that the bit numbering in XML is "lsb-zero". Most 361 architectures other than PowerPC use this ordering. In 362 the future, we can add an XML tag to indicate "msb-zero" 363 numbering. */ 364 if (start > end) 365 gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"), 366 field_name); 367 368 if (end >= data->current_type_size * TARGET_CHAR_BIT) 369 gdb_xml_error (parser, 370 _("Bitfield \"%s\" does not fit in struct")); 371 372 tdesc_add_bitfield (t, field_name, start, end); 373 } 374 } 375 else 376 gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"), 377 field_name); 378 } 379 380 /* Handle the start of a <vector> element. Initialize the type and 381 record it with the current feature. */ 382 383 static void 384 tdesc_start_vector (struct gdb_xml_parser *parser, 385 const struct gdb_xml_element *element, 386 void *user_data, VEC(gdb_xml_value_s) *attributes) 387 { 388 struct tdesc_parsing_data *data = user_data; 389 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes); 390 struct tdesc_type *field_type; 391 char *id, *field_type_id; 392 int count; 393 394 id = attrs[0].value; 395 field_type_id = attrs[1].value; 396 count = * (ULONGEST *) attrs[2].value; 397 398 field_type = tdesc_named_type (data->current_feature, field_type_id); 399 if (field_type == NULL) 400 gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""), 401 id, field_type_id); 402 403 tdesc_create_vector (data->current_feature, id, field_type, count); 404 } 405 406 /* The elements and attributes of an XML target description. */ 407 408 static const struct gdb_xml_attribute field_attributes[] = { 409 { "name", GDB_XML_AF_NONE, NULL, NULL }, 410 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL }, 411 { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, 412 { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, 413 { NULL, GDB_XML_AF_NONE, NULL, NULL } 414 }; 415 416 static const struct gdb_xml_element struct_union_children[] = { 417 { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE, 418 tdesc_start_field, NULL }, 419 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 420 }; 421 422 static const struct gdb_xml_attribute reg_attributes[] = { 423 { "name", GDB_XML_AF_NONE, NULL, NULL }, 424 { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, 425 { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, 426 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL }, 427 { "group", GDB_XML_AF_OPTIONAL, NULL, NULL }, 428 { "save-restore", GDB_XML_AF_OPTIONAL, 429 gdb_xml_parse_attr_enum, gdb_xml_enums_boolean }, 430 { NULL, GDB_XML_AF_NONE, NULL, NULL } 431 }; 432 433 static const struct gdb_xml_attribute struct_union_attributes[] = { 434 { "id", GDB_XML_AF_NONE, NULL, NULL }, 435 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL}, 436 { NULL, GDB_XML_AF_NONE, NULL, NULL } 437 }; 438 439 static const struct gdb_xml_attribute flags_attributes[] = { 440 { "id", GDB_XML_AF_NONE, NULL, NULL }, 441 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL}, 442 { NULL, GDB_XML_AF_NONE, NULL, NULL } 443 }; 444 445 static const struct gdb_xml_attribute vector_attributes[] = { 446 { "id", GDB_XML_AF_NONE, NULL, NULL }, 447 { "type", GDB_XML_AF_NONE, NULL, NULL }, 448 { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, 449 { NULL, GDB_XML_AF_NONE, NULL, NULL } 450 }; 451 452 static const struct gdb_xml_attribute feature_attributes[] = { 453 { "name", GDB_XML_AF_NONE, NULL, NULL }, 454 { NULL, GDB_XML_AF_NONE, NULL, NULL } 455 }; 456 457 static const struct gdb_xml_element feature_children[] = { 458 { "reg", reg_attributes, NULL, 459 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 460 tdesc_start_reg, NULL }, 461 { "struct", struct_union_attributes, struct_union_children, 462 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 463 tdesc_start_struct, NULL }, 464 { "union", struct_union_attributes, struct_union_children, 465 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 466 tdesc_start_union, NULL }, 467 { "flags", flags_attributes, struct_union_children, 468 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 469 tdesc_start_flags, NULL }, 470 { "vector", vector_attributes, NULL, 471 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 472 tdesc_start_vector, NULL }, 473 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 474 }; 475 476 static const struct gdb_xml_attribute target_attributes[] = { 477 { "version", GDB_XML_AF_NONE, NULL, NULL }, 478 { NULL, GDB_XML_AF_NONE, NULL, NULL } 479 }; 480 481 static const struct gdb_xml_element target_children[] = { 482 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL, 483 NULL, tdesc_end_arch }, 484 { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL, 485 NULL, tdesc_end_osabi }, 486 { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 487 NULL, tdesc_end_compatible }, 488 { "feature", feature_attributes, feature_children, 489 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, 490 tdesc_start_feature, NULL }, 491 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 492 }; 493 494 static const struct gdb_xml_element tdesc_elements[] = { 495 { "target", target_attributes, target_children, GDB_XML_EF_NONE, 496 tdesc_start_target, NULL }, 497 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 498 }; 499 500 /* Parse DOCUMENT into a target description and return it. */ 501 502 static struct target_desc * 503 tdesc_parse_xml (const char *document, xml_fetch_another fetcher, 504 void *fetcher_baton) 505 { 506 struct cleanup *back_to, *result_cleanup; 507 struct tdesc_parsing_data data; 508 struct tdesc_xml_cache *cache; 509 char *expanded_text; 510 int ix; 511 512 /* Expand all XInclude directives. */ 513 expanded_text = xml_process_xincludes (_("target description"), 514 document, fetcher, fetcher_baton, 0); 515 if (expanded_text == NULL) 516 { 517 warning (_("Could not load XML target description; ignoring")); 518 return NULL; 519 } 520 521 /* Check for an exact match in the list of descriptions we have 522 previously parsed. strcmp is a slightly inefficient way to 523 do this; an SHA-1 checksum would work as well. */ 524 for (ix = 0; VEC_iterate (tdesc_xml_cache_s, xml_cache, ix, cache); ix++) 525 if (strcmp (cache->xml_document, expanded_text) == 0) 526 { 527 xfree (expanded_text); 528 return cache->tdesc; 529 } 530 531 back_to = make_cleanup (null_cleanup, NULL); 532 533 memset (&data, 0, sizeof (struct tdesc_parsing_data)); 534 data.tdesc = allocate_target_description (); 535 result_cleanup = make_cleanup_free_target_description (data.tdesc); 536 make_cleanup (xfree, expanded_text); 537 538 if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd", 539 tdesc_elements, expanded_text, &data) == 0) 540 { 541 /* Parsed successfully. */ 542 struct tdesc_xml_cache new_cache; 543 544 new_cache.xml_document = expanded_text; 545 new_cache.tdesc = data.tdesc; 546 VEC_safe_push (tdesc_xml_cache_s, xml_cache, &new_cache); 547 discard_cleanups (result_cleanup); 548 do_cleanups (back_to); 549 return data.tdesc; 550 } 551 else 552 { 553 warning (_("Could not load XML target description; ignoring")); 554 do_cleanups (back_to); 555 return NULL; 556 } 557 } 558 #endif /* HAVE_LIBEXPAT */ 559 560 561 /* Read an XML target description from FILENAME. Parse it, and return 562 the parsed description. */ 563 564 const struct target_desc * 565 file_read_description_xml (const char *filename) 566 { 567 struct target_desc *tdesc; 568 char *tdesc_str; 569 struct cleanup *back_to; 570 char *dirname; 571 572 tdesc_str = xml_fetch_content_from_file (filename, NULL); 573 if (tdesc_str == NULL) 574 { 575 warning (_("Could not open \"%s\""), filename); 576 return NULL; 577 } 578 579 back_to = make_cleanup (xfree, tdesc_str); 580 581 dirname = ldirname (filename); 582 if (dirname != NULL) 583 make_cleanup (xfree, dirname); 584 585 tdesc = tdesc_parse_xml (tdesc_str, xml_fetch_content_from_file, dirname); 586 do_cleanups (back_to); 587 588 return tdesc; 589 } 590 591 /* Read a string representation of available features from the target, 592 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is 593 malloc allocated and NUL-terminated. NAME should be a non-NULL 594 string identifying the XML document we want; the top level document 595 is "target.xml". Other calls may be performed for the DTD or 596 for <xi:include>. */ 597 598 static char * 599 fetch_available_features_from_target (const char *name, void *baton_) 600 { 601 struct target_ops *ops = baton_; 602 603 /* Read this object as a string. This ensures that a NUL 604 terminator is added. */ 605 return target_read_stralloc (ops, 606 TARGET_OBJECT_AVAILABLE_FEATURES, 607 name); 608 } 609 610 611 /* Read an XML target description using OPS. Parse it, and return the 612 parsed description. */ 613 614 const struct target_desc * 615 target_read_description_xml (struct target_ops *ops) 616 { 617 struct target_desc *tdesc; 618 char *tdesc_str; 619 struct cleanup *back_to; 620 621 tdesc_str = fetch_available_features_from_target ("target.xml", ops); 622 if (tdesc_str == NULL) 623 return NULL; 624 625 back_to = make_cleanup (xfree, tdesc_str); 626 tdesc = tdesc_parse_xml (tdesc_str, 627 fetch_available_features_from_target, 628 ops); 629 do_cleanups (back_to); 630 631 return tdesc; 632 } 633