| 1 | /* XML target description support for GDB. |
| 2 | |
| 3 | Copyright (C) 2006-2016 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 | #include "filenames.h" |
| 29 | |
| 30 | /* Maximum sizes. |
| 31 | This is just to catch obviously wrong values. */ |
| 32 | #define MAX_FIELD_SIZE 65536 |
| 33 | #define MAX_FIELD_BITSIZE (MAX_FIELD_SIZE * TARGET_CHAR_BIT) |
| 34 | #define MAX_VECTOR_SIZE 65536 |
| 35 | |
| 36 | #if !defined(HAVE_LIBEXPAT) |
| 37 | |
| 38 | /* Parse DOCUMENT into a target description. Or don't, since we don't have |
| 39 | an XML parser. */ |
| 40 | |
| 41 | static struct target_desc * |
| 42 | tdesc_parse_xml (const char *document, xml_fetch_another fetcher, |
| 43 | void *fetcher_baton) |
| 44 | { |
| 45 | static int have_warned; |
| 46 | |
| 47 | if (!have_warned) |
| 48 | { |
| 49 | have_warned = 1; |
| 50 | warning (_("Can not parse XML target description; XML support was " |
| 51 | "disabled at compile time")); |
| 52 | } |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | #else /* HAVE_LIBEXPAT */ |
| 58 | |
| 59 | /* A record of every XML description we have parsed. We never discard |
| 60 | old descriptions, because we never discard gdbarches. As long as we |
| 61 | have a gdbarch referencing this description, we want to have a copy |
| 62 | of it here, so that if we parse the same XML document again we can |
| 63 | return the same "struct target_desc *"; if they are not singletons, |
| 64 | then we will create unnecessary duplicate gdbarches. See |
| 65 | gdbarch_list_lookup_by_info. */ |
| 66 | |
| 67 | struct tdesc_xml_cache |
| 68 | { |
| 69 | const char *xml_document; |
| 70 | struct target_desc *tdesc; |
| 71 | }; |
| 72 | typedef struct tdesc_xml_cache tdesc_xml_cache_s; |
| 73 | DEF_VEC_O(tdesc_xml_cache_s); |
| 74 | |
| 75 | static VEC(tdesc_xml_cache_s) *xml_cache; |
| 76 | |
| 77 | /* Callback data for target description parsing. */ |
| 78 | |
| 79 | struct tdesc_parsing_data |
| 80 | { |
| 81 | /* The target description we are building. */ |
| 82 | struct target_desc *tdesc; |
| 83 | |
| 84 | /* The target feature we are currently parsing, or last parsed. */ |
| 85 | struct tdesc_feature *current_feature; |
| 86 | |
| 87 | /* The register number to use for the next register we see, if |
| 88 | it does not have its own. This starts at zero. */ |
| 89 | int next_regnum; |
| 90 | |
| 91 | /* The struct or union we are currently parsing, or last parsed. */ |
| 92 | struct tdesc_type *current_type; |
| 93 | |
| 94 | /* The byte size of the current struct/flags type, if specified. Zero |
| 95 | if not specified. Flags values must specify a size. */ |
| 96 | int current_type_size; |
| 97 | }; |
| 98 | |
| 99 | /* Handle the end of an <architecture> element and its value. */ |
| 100 | |
| 101 | static void |
| 102 | tdesc_end_arch (struct gdb_xml_parser *parser, |
| 103 | const struct gdb_xml_element *element, |
| 104 | void *user_data, const char *body_text) |
| 105 | { |
| 106 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 107 | const struct bfd_arch_info *arch; |
| 108 | |
| 109 | arch = bfd_scan_arch (body_text); |
| 110 | if (arch == NULL) |
| 111 | gdb_xml_error (parser, _("Target description specified unknown " |
| 112 | "architecture \"%s\""), body_text); |
| 113 | set_tdesc_architecture (data->tdesc, arch); |
| 114 | } |
| 115 | |
| 116 | /* Handle the end of an <osabi> element and its value. */ |
| 117 | |
| 118 | static void |
| 119 | tdesc_end_osabi (struct gdb_xml_parser *parser, |
| 120 | const struct gdb_xml_element *element, |
| 121 | void *user_data, const char *body_text) |
| 122 | { |
| 123 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 124 | enum gdb_osabi osabi; |
| 125 | |
| 126 | osabi = osabi_from_tdesc_string (body_text); |
| 127 | if (osabi == GDB_OSABI_UNKNOWN) |
| 128 | warning (_("Target description specified unknown osabi \"%s\""), |
| 129 | body_text); |
| 130 | else |
| 131 | set_tdesc_osabi (data->tdesc, osabi); |
| 132 | } |
| 133 | |
| 134 | /* Handle the end of a <compatible> element and its value. */ |
| 135 | |
| 136 | static void |
| 137 | tdesc_end_compatible (struct gdb_xml_parser *parser, |
| 138 | const struct gdb_xml_element *element, |
| 139 | void *user_data, const char *body_text) |
| 140 | { |
| 141 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 142 | const struct bfd_arch_info *arch; |
| 143 | |
| 144 | arch = bfd_scan_arch (body_text); |
| 145 | tdesc_add_compatible (data->tdesc, arch); |
| 146 | } |
| 147 | |
| 148 | /* Handle the start of a <target> element. */ |
| 149 | |
| 150 | static void |
| 151 | tdesc_start_target (struct gdb_xml_parser *parser, |
| 152 | const struct gdb_xml_element *element, |
| 153 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 154 | { |
| 155 | char *version = (char *) xml_find_attribute (attributes, "version")->value; |
| 156 | |
| 157 | if (strcmp (version, "1.0") != 0) |
| 158 | gdb_xml_error (parser, |
| 159 | _("Target description has unsupported version \"%s\""), |
| 160 | version); |
| 161 | } |
| 162 | |
| 163 | /* Handle the start of a <feature> element. */ |
| 164 | |
| 165 | static void |
| 166 | tdesc_start_feature (struct gdb_xml_parser *parser, |
| 167 | const struct gdb_xml_element *element, |
| 168 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 169 | { |
| 170 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 171 | char *name = (char *) xml_find_attribute (attributes, "name")->value; |
| 172 | |
| 173 | data->current_feature = tdesc_create_feature (data->tdesc, name); |
| 174 | } |
| 175 | |
| 176 | /* Handle the start of a <reg> element. Fill in the optional |
| 177 | attributes and attach it to the containing feature. */ |
| 178 | |
| 179 | static void |
| 180 | tdesc_start_reg (struct gdb_xml_parser *parser, |
| 181 | const struct gdb_xml_element *element, |
| 182 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 183 | { |
| 184 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 185 | struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes); |
| 186 | int ix = 0, length; |
| 187 | char *name, *group, *type; |
| 188 | int bitsize, regnum, save_restore; |
| 189 | |
| 190 | length = VEC_length (gdb_xml_value_s, attributes); |
| 191 | |
| 192 | name = (char *) attrs[ix++].value; |
| 193 | bitsize = * (ULONGEST *) attrs[ix++].value; |
| 194 | |
| 195 | if (ix < length && strcmp (attrs[ix].name, "regnum") == 0) |
| 196 | regnum = * (ULONGEST *) attrs[ix++].value; |
| 197 | else |
| 198 | regnum = data->next_regnum; |
| 199 | |
| 200 | if (ix < length && strcmp (attrs[ix].name, "type") == 0) |
| 201 | type = (char *) attrs[ix++].value; |
| 202 | else |
| 203 | type = "int"; |
| 204 | |
| 205 | if (ix < length && strcmp (attrs[ix].name, "group") == 0) |
| 206 | group = (char *) attrs[ix++].value; |
| 207 | else |
| 208 | group = NULL; |
| 209 | |
| 210 | if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0) |
| 211 | save_restore = * (ULONGEST *) attrs[ix++].value; |
| 212 | else |
| 213 | save_restore = 1; |
| 214 | |
| 215 | if (strcmp (type, "int") != 0 |
| 216 | && strcmp (type, "float") != 0 |
| 217 | && tdesc_named_type (data->current_feature, type) == NULL) |
| 218 | gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""), |
| 219 | name, type); |
| 220 | |
| 221 | tdesc_create_reg (data->current_feature, name, regnum, save_restore, group, |
| 222 | bitsize, type); |
| 223 | |
| 224 | data->next_regnum = regnum + 1; |
| 225 | } |
| 226 | |
| 227 | /* Handle the start of a <union> element. Initialize the type and |
| 228 | record it with the current feature. */ |
| 229 | |
| 230 | static void |
| 231 | tdesc_start_union (struct gdb_xml_parser *parser, |
| 232 | const struct gdb_xml_element *element, |
| 233 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 234 | { |
| 235 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 236 | char *id = (char *) xml_find_attribute (attributes, "id")->value; |
| 237 | |
| 238 | data->current_type = tdesc_create_union (data->current_feature, id); |
| 239 | data->current_type_size = 0; |
| 240 | } |
| 241 | |
| 242 | /* Handle the start of a <struct> element. Initialize the type and |
| 243 | record it with the current feature. */ |
| 244 | |
| 245 | static void |
| 246 | tdesc_start_struct (struct gdb_xml_parser *parser, |
| 247 | const struct gdb_xml_element *element, |
| 248 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 249 | { |
| 250 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 251 | char *id = (char *) xml_find_attribute (attributes, "id")->value; |
| 252 | struct tdesc_type *type; |
| 253 | struct gdb_xml_value *attr; |
| 254 | |
| 255 | type = tdesc_create_struct (data->current_feature, id); |
| 256 | data->current_type = type; |
| 257 | data->current_type_size = 0; |
| 258 | |
| 259 | attr = xml_find_attribute (attributes, "size"); |
| 260 | if (attr != NULL) |
| 261 | { |
| 262 | ULONGEST size = * (ULONGEST *) attr->value; |
| 263 | |
| 264 | if (size > MAX_FIELD_SIZE) |
| 265 | { |
| 266 | gdb_xml_error (parser, |
| 267 | _("Struct size %s is larger than maximum (%d)"), |
| 268 | pulongest (size), MAX_FIELD_SIZE); |
| 269 | } |
| 270 | tdesc_set_struct_size (type, size); |
| 271 | data->current_type_size = size; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void |
| 276 | tdesc_start_flags (struct gdb_xml_parser *parser, |
| 277 | const struct gdb_xml_element *element, |
| 278 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 279 | { |
| 280 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 281 | char *id = (char *) xml_find_attribute (attributes, "id")->value; |
| 282 | ULONGEST size = * (ULONGEST *) |
| 283 | xml_find_attribute (attributes, "size")->value; |
| 284 | struct tdesc_type *type; |
| 285 | |
| 286 | if (size > MAX_FIELD_SIZE) |
| 287 | { |
| 288 | gdb_xml_error (parser, |
| 289 | _("Flags size %s is larger than maximum (%d)"), |
| 290 | pulongest (size), MAX_FIELD_SIZE); |
| 291 | } |
| 292 | type = tdesc_create_flags (data->current_feature, id, size); |
| 293 | |
| 294 | data->current_type = type; |
| 295 | data->current_type_size = size; |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | tdesc_start_enum (struct gdb_xml_parser *parser, |
| 300 | const struct gdb_xml_element *element, |
| 301 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 302 | { |
| 303 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 304 | char *id = (char *) xml_find_attribute (attributes, "id")->value; |
| 305 | int size = * (ULONGEST *) |
| 306 | xml_find_attribute (attributes, "size")->value; |
| 307 | struct tdesc_type *type; |
| 308 | |
| 309 | if (size > MAX_FIELD_SIZE) |
| 310 | { |
| 311 | gdb_xml_error (parser, |
| 312 | _("Enum size %s is larger than maximum (%d)"), |
| 313 | pulongest (size), MAX_FIELD_SIZE); |
| 314 | } |
| 315 | type = tdesc_create_enum (data->current_feature, id, size); |
| 316 | |
| 317 | data->current_type = type; |
| 318 | data->current_type_size = 0; |
| 319 | } |
| 320 | |
| 321 | /* Handle the start of a <field> element. Attach the field to the |
| 322 | current struct, union or flags. */ |
| 323 | |
| 324 | static void |
| 325 | tdesc_start_field (struct gdb_xml_parser *parser, |
| 326 | const struct gdb_xml_element *element, |
| 327 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 328 | { |
| 329 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 330 | struct gdb_xml_value *attr; |
| 331 | struct tdesc_type *field_type; |
| 332 | char *field_name, *field_type_id; |
| 333 | int start, end; |
| 334 | |
| 335 | field_name = (char *) xml_find_attribute (attributes, "name")->value; |
| 336 | |
| 337 | attr = xml_find_attribute (attributes, "type"); |
| 338 | if (attr != NULL) |
| 339 | { |
| 340 | field_type_id = (char *) attr->value; |
| 341 | field_type = tdesc_named_type (data->current_feature, field_type_id); |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | field_type_id = NULL; |
| 346 | field_type = NULL; |
| 347 | } |
| 348 | |
| 349 | attr = xml_find_attribute (attributes, "start"); |
| 350 | if (attr != NULL) |
| 351 | { |
| 352 | ULONGEST ul_start = * (ULONGEST *) attr->value; |
| 353 | |
| 354 | if (ul_start > MAX_FIELD_BITSIZE) |
| 355 | { |
| 356 | gdb_xml_error (parser, |
| 357 | _("Field start %s is larger than maximum (%d)"), |
| 358 | pulongest (ul_start), MAX_FIELD_BITSIZE); |
| 359 | } |
| 360 | start = ul_start; |
| 361 | } |
| 362 | else |
| 363 | start = -1; |
| 364 | |
| 365 | attr = xml_find_attribute (attributes, "end"); |
| 366 | if (attr != NULL) |
| 367 | { |
| 368 | ULONGEST ul_end = * (ULONGEST *) attr->value; |
| 369 | |
| 370 | if (ul_end > MAX_FIELD_BITSIZE) |
| 371 | { |
| 372 | gdb_xml_error (parser, |
| 373 | _("Field end %s is larger than maximum (%d)"), |
| 374 | pulongest (ul_end), MAX_FIELD_BITSIZE); |
| 375 | } |
| 376 | end = ul_end; |
| 377 | } |
| 378 | else |
| 379 | end = -1; |
| 380 | |
| 381 | if (start != -1) |
| 382 | { |
| 383 | struct tdesc_type *t = data->current_type; |
| 384 | |
| 385 | /* Older versions of gdb can't handle elided end values. |
| 386 | Stick with that for now, to help ensure backward compatibility. |
| 387 | E.g., If a newer gdbserver is talking to an older gdb. */ |
| 388 | if (end == -1) |
| 389 | gdb_xml_error (parser, _("Missing end value")); |
| 390 | |
| 391 | if (data->current_type_size == 0) |
| 392 | gdb_xml_error (parser, |
| 393 | _("Bitfields must live in explicitly sized types")); |
| 394 | |
| 395 | if (field_type_id != NULL |
| 396 | && strcmp (field_type_id, "bool") == 0 |
| 397 | && start != end) |
| 398 | { |
| 399 | gdb_xml_error (parser, |
| 400 | _("Boolean fields must be one bit in size")); |
| 401 | } |
| 402 | |
| 403 | if (end >= 64) |
| 404 | gdb_xml_error (parser, |
| 405 | _("Bitfield \"%s\" goes past " |
| 406 | "64 bits (unsupported)"), |
| 407 | field_name); |
| 408 | |
| 409 | /* Assume that the bit numbering in XML is "lsb-zero". Most |
| 410 | architectures other than PowerPC use this ordering. In the |
| 411 | future, we can add an XML tag to indicate "msb-zero" numbering. */ |
| 412 | if (start > end) |
| 413 | gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"), |
| 414 | field_name); |
| 415 | if (end >= data->current_type_size * TARGET_CHAR_BIT) |
| 416 | gdb_xml_error (parser, |
| 417 | _("Bitfield \"%s\" does not fit in struct")); |
| 418 | |
| 419 | if (field_type != NULL) |
| 420 | tdesc_add_typed_bitfield (t, field_name, start, end, field_type); |
| 421 | else if (start == end) |
| 422 | tdesc_add_flag (t, start, field_name); |
| 423 | else |
| 424 | tdesc_add_bitfield (t, field_name, start, end); |
| 425 | } |
| 426 | else if (start == -1 && end != -1) |
| 427 | gdb_xml_error (parser, _("End specified but not start")); |
| 428 | else if (field_type_id != NULL) |
| 429 | { |
| 430 | /* TDESC_TYPE_FLAGS values are explicitly sized, so the following test |
| 431 | catches adding non-bitfield types to flags as well. */ |
| 432 | if (data->current_type_size != 0) |
| 433 | gdb_xml_error (parser, |
| 434 | _("Explicitly sized type cannot " |
| 435 | "contain non-bitfield \"%s\""), |
| 436 | field_name); |
| 437 | |
| 438 | if (field_type == NULL) |
| 439 | gdb_xml_error (parser, _("Field \"%s\" references undefined " |
| 440 | "type \"%s\""), |
| 441 | field_name, field_type_id); |
| 442 | |
| 443 | tdesc_add_field (data->current_type, field_name, field_type); |
| 444 | } |
| 445 | else |
| 446 | gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"), |
| 447 | field_name); |
| 448 | } |
| 449 | |
| 450 | /* Handle the start of an <evalue> element. Attach the value to the |
| 451 | current enum. */ |
| 452 | |
| 453 | static void |
| 454 | tdesc_start_enum_value (struct gdb_xml_parser *parser, |
| 455 | const struct gdb_xml_element *element, |
| 456 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 457 | { |
| 458 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 459 | struct gdb_xml_value *attr; |
| 460 | char *field_name; |
| 461 | ULONGEST ul_value; |
| 462 | int value; |
| 463 | |
| 464 | field_name = (char *) xml_find_attribute (attributes, "name")->value; |
| 465 | |
| 466 | attr = xml_find_attribute (attributes, "value"); |
| 467 | ul_value = * (ULONGEST *) attr->value; |
| 468 | if (ul_value > INT_MAX) |
| 469 | { |
| 470 | gdb_xml_error (parser, |
| 471 | _("Enum value %s is larger than maximum (%d)"), |
| 472 | pulongest (ul_value), INT_MAX); |
| 473 | } |
| 474 | value = ul_value; |
| 475 | |
| 476 | tdesc_add_enum_value (data->current_type, value, field_name); |
| 477 | } |
| 478 | |
| 479 | /* Handle the start of a <vector> element. Initialize the type and |
| 480 | record it with the current feature. */ |
| 481 | |
| 482 | static void |
| 483 | tdesc_start_vector (struct gdb_xml_parser *parser, |
| 484 | const struct gdb_xml_element *element, |
| 485 | void *user_data, VEC(gdb_xml_value_s) *attributes) |
| 486 | { |
| 487 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
| 488 | struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes); |
| 489 | struct tdesc_type *field_type; |
| 490 | char *id, *field_type_id; |
| 491 | ULONGEST count; |
| 492 | |
| 493 | id = (char *) attrs[0].value; |
| 494 | field_type_id = (char *) attrs[1].value; |
| 495 | count = * (ULONGEST *) attrs[2].value; |
| 496 | |
| 497 | if (count > MAX_VECTOR_SIZE) |
| 498 | { |
| 499 | gdb_xml_error (parser, |
| 500 | _("Vector size %s is larger than maximum (%d)"), |
| 501 | pulongest (count), MAX_VECTOR_SIZE); |
| 502 | } |
| 503 | |
| 504 | field_type = tdesc_named_type (data->current_feature, field_type_id); |
| 505 | if (field_type == NULL) |
| 506 | gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""), |
| 507 | id, field_type_id); |
| 508 | |
| 509 | tdesc_create_vector (data->current_feature, id, field_type, count); |
| 510 | } |
| 511 | |
| 512 | /* The elements and attributes of an XML target description. */ |
| 513 | |
| 514 | static const struct gdb_xml_attribute field_attributes[] = { |
| 515 | { "name", GDB_XML_AF_NONE, NULL, NULL }, |
| 516 | { "type", GDB_XML_AF_OPTIONAL, NULL, NULL }, |
| 517 | { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, |
| 518 | { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, |
| 519 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 520 | }; |
| 521 | |
| 522 | static const struct gdb_xml_attribute enum_value_attributes[] = { |
| 523 | { "name", GDB_XML_AF_NONE, NULL, NULL }, |
| 524 | { "value", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, |
| 525 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 526 | }; |
| 527 | |
| 528 | static const struct gdb_xml_element struct_union_children[] = { |
| 529 | { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE, |
| 530 | tdesc_start_field, NULL }, |
| 531 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 532 | }; |
| 533 | |
| 534 | static const struct gdb_xml_element enum_children[] = { |
| 535 | { "evalue", enum_value_attributes, NULL, GDB_XML_EF_REPEATABLE, |
| 536 | tdesc_start_enum_value, NULL }, |
| 537 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 538 | }; |
| 539 | |
| 540 | static const struct gdb_xml_attribute reg_attributes[] = { |
| 541 | { "name", GDB_XML_AF_NONE, NULL, NULL }, |
| 542 | { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 543 | { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, |
| 544 | { "type", GDB_XML_AF_OPTIONAL, NULL, NULL }, |
| 545 | { "group", GDB_XML_AF_OPTIONAL, NULL, NULL }, |
| 546 | { "save-restore", GDB_XML_AF_OPTIONAL, |
| 547 | gdb_xml_parse_attr_enum, gdb_xml_enums_boolean }, |
| 548 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 549 | }; |
| 550 | |
| 551 | static const struct gdb_xml_attribute struct_union_attributes[] = { |
| 552 | { "id", GDB_XML_AF_NONE, NULL, NULL }, |
| 553 | { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL}, |
| 554 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 555 | }; |
| 556 | |
| 557 | static const struct gdb_xml_attribute flags_attributes[] = { |
| 558 | { "id", GDB_XML_AF_NONE, NULL, NULL }, |
| 559 | { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL}, |
| 560 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 561 | }; |
| 562 | |
| 563 | static const struct gdb_xml_attribute enum_attributes[] = { |
| 564 | { "id", GDB_XML_AF_NONE, NULL, NULL }, |
| 565 | { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL}, |
| 566 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 567 | }; |
| 568 | |
| 569 | static const struct gdb_xml_attribute vector_attributes[] = { |
| 570 | { "id", GDB_XML_AF_NONE, NULL, NULL }, |
| 571 | { "type", GDB_XML_AF_NONE, NULL, NULL }, |
| 572 | { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 573 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 574 | }; |
| 575 | |
| 576 | static const struct gdb_xml_attribute feature_attributes[] = { |
| 577 | { "name", GDB_XML_AF_NONE, NULL, NULL }, |
| 578 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 579 | }; |
| 580 | |
| 581 | static const struct gdb_xml_element feature_children[] = { |
| 582 | { "reg", reg_attributes, NULL, |
| 583 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 584 | tdesc_start_reg, NULL }, |
| 585 | { "struct", struct_union_attributes, struct_union_children, |
| 586 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 587 | tdesc_start_struct, NULL }, |
| 588 | { "union", struct_union_attributes, struct_union_children, |
| 589 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 590 | tdesc_start_union, NULL }, |
| 591 | { "flags", flags_attributes, struct_union_children, |
| 592 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 593 | tdesc_start_flags, NULL }, |
| 594 | { "enum", enum_attributes, enum_children, |
| 595 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 596 | tdesc_start_enum, NULL }, |
| 597 | { "vector", vector_attributes, NULL, |
| 598 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 599 | tdesc_start_vector, NULL }, |
| 600 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 601 | }; |
| 602 | |
| 603 | static const struct gdb_xml_attribute target_attributes[] = { |
| 604 | { "version", GDB_XML_AF_NONE, NULL, NULL }, |
| 605 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 606 | }; |
| 607 | |
| 608 | static const struct gdb_xml_element target_children[] = { |
| 609 | { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL, |
| 610 | NULL, tdesc_end_arch }, |
| 611 | { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL, |
| 612 | NULL, tdesc_end_osabi }, |
| 613 | { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 614 | NULL, tdesc_end_compatible }, |
| 615 | { "feature", feature_attributes, feature_children, |
| 616 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
| 617 | tdesc_start_feature, NULL }, |
| 618 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 619 | }; |
| 620 | |
| 621 | static const struct gdb_xml_element tdesc_elements[] = { |
| 622 | { "target", target_attributes, target_children, GDB_XML_EF_NONE, |
| 623 | tdesc_start_target, NULL }, |
| 624 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 625 | }; |
| 626 | |
| 627 | /* Parse DOCUMENT into a target description and return it. */ |
| 628 | |
| 629 | static struct target_desc * |
| 630 | tdesc_parse_xml (const char *document, xml_fetch_another fetcher, |
| 631 | void *fetcher_baton) |
| 632 | { |
| 633 | struct cleanup *back_to, *result_cleanup; |
| 634 | struct tdesc_parsing_data data; |
| 635 | struct tdesc_xml_cache *cache; |
| 636 | char *expanded_text; |
| 637 | int ix; |
| 638 | |
| 639 | /* Expand all XInclude directives. */ |
| 640 | expanded_text = xml_process_xincludes (_("target description"), |
| 641 | document, fetcher, fetcher_baton, 0); |
| 642 | if (expanded_text == NULL) |
| 643 | { |
| 644 | warning (_("Could not load XML target description; ignoring")); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | /* Check for an exact match in the list of descriptions we have |
| 649 | previously parsed. strcmp is a slightly inefficient way to |
| 650 | do this; an SHA-1 checksum would work as well. */ |
| 651 | for (ix = 0; VEC_iterate (tdesc_xml_cache_s, xml_cache, ix, cache); ix++) |
| 652 | if (strcmp (cache->xml_document, expanded_text) == 0) |
| 653 | { |
| 654 | xfree (expanded_text); |
| 655 | return cache->tdesc; |
| 656 | } |
| 657 | |
| 658 | back_to = make_cleanup (null_cleanup, NULL); |
| 659 | |
| 660 | memset (&data, 0, sizeof (struct tdesc_parsing_data)); |
| 661 | data.tdesc = allocate_target_description (); |
| 662 | result_cleanup = make_cleanup_free_target_description (data.tdesc); |
| 663 | make_cleanup (xfree, expanded_text); |
| 664 | |
| 665 | if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd", |
| 666 | tdesc_elements, expanded_text, &data) == 0) |
| 667 | { |
| 668 | /* Parsed successfully. */ |
| 669 | struct tdesc_xml_cache new_cache; |
| 670 | |
| 671 | new_cache.xml_document = expanded_text; |
| 672 | new_cache.tdesc = data.tdesc; |
| 673 | VEC_safe_push (tdesc_xml_cache_s, xml_cache, &new_cache); |
| 674 | discard_cleanups (result_cleanup); |
| 675 | do_cleanups (back_to); |
| 676 | return data.tdesc; |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | warning (_("Could not load XML target description; ignoring")); |
| 681 | do_cleanups (back_to); |
| 682 | return NULL; |
| 683 | } |
| 684 | } |
| 685 | #endif /* HAVE_LIBEXPAT */ |
| 686 | \f |
| 687 | |
| 688 | /* Read an XML target description from FILENAME. Parse it, and return |
| 689 | the parsed description. */ |
| 690 | |
| 691 | const struct target_desc * |
| 692 | file_read_description_xml (const char *filename) |
| 693 | { |
| 694 | struct target_desc *tdesc; |
| 695 | char *tdesc_str; |
| 696 | struct cleanup *back_to; |
| 697 | char *dirname; |
| 698 | |
| 699 | tdesc_str = xml_fetch_content_from_file (filename, NULL); |
| 700 | if (tdesc_str == NULL) |
| 701 | { |
| 702 | warning (_("Could not open \"%s\""), filename); |
| 703 | return NULL; |
| 704 | } |
| 705 | |
| 706 | back_to = make_cleanup (xfree, tdesc_str); |
| 707 | |
| 708 | dirname = ldirname (filename); |
| 709 | if (dirname != NULL) |
| 710 | make_cleanup (xfree, dirname); |
| 711 | |
| 712 | tdesc = tdesc_parse_xml (tdesc_str, xml_fetch_content_from_file, dirname); |
| 713 | do_cleanups (back_to); |
| 714 | |
| 715 | return tdesc; |
| 716 | } |
| 717 | |
| 718 | /* Read a string representation of available features from the target, |
| 719 | using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is |
| 720 | malloc allocated and NUL-terminated. NAME should be a non-NULL |
| 721 | string identifying the XML document we want; the top level document |
| 722 | is "target.xml". Other calls may be performed for the DTD or |
| 723 | for <xi:include>. */ |
| 724 | |
| 725 | static char * |
| 726 | fetch_available_features_from_target (const char *name, void *baton_) |
| 727 | { |
| 728 | struct target_ops *ops = (struct target_ops *) baton_; |
| 729 | |
| 730 | /* Read this object as a string. This ensures that a NUL |
| 731 | terminator is added. */ |
| 732 | return target_read_stralloc (ops, |
| 733 | TARGET_OBJECT_AVAILABLE_FEATURES, |
| 734 | name); |
| 735 | } |
| 736 | \f |
| 737 | |
| 738 | /* Read an XML target description using OPS. Parse it, and return the |
| 739 | parsed description. */ |
| 740 | |
| 741 | const struct target_desc * |
| 742 | target_read_description_xml (struct target_ops *ops) |
| 743 | { |
| 744 | struct target_desc *tdesc; |
| 745 | char *tdesc_str; |
| 746 | struct cleanup *back_to; |
| 747 | |
| 748 | tdesc_str = fetch_available_features_from_target ("target.xml", ops); |
| 749 | if (tdesc_str == NULL) |
| 750 | return NULL; |
| 751 | |
| 752 | back_to = make_cleanup (xfree, tdesc_str); |
| 753 | tdesc = tdesc_parse_xml (tdesc_str, |
| 754 | fetch_available_features_from_target, |
| 755 | ops); |
| 756 | do_cleanups (back_to); |
| 757 | |
| 758 | return tdesc; |
| 759 | } |
| 760 | |
| 761 | /* Fetches an XML target description using OPS, processing |
| 762 | includes, but not parsing it. Used to dump whole tdesc |
| 763 | as a single XML file. */ |
| 764 | |
| 765 | char * |
| 766 | target_fetch_description_xml (struct target_ops *ops) |
| 767 | { |
| 768 | #if !defined(HAVE_LIBEXPAT) |
| 769 | static int have_warned; |
| 770 | |
| 771 | if (!have_warned) |
| 772 | { |
| 773 | have_warned = 1; |
| 774 | warning (_("Can not fetch XML target description; XML support was " |
| 775 | "disabled at compile time")); |
| 776 | } |
| 777 | |
| 778 | return NULL; |
| 779 | #else |
| 780 | struct target_desc *tdesc; |
| 781 | char *tdesc_str; |
| 782 | char *expanded_text; |
| 783 | struct cleanup *back_to; |
| 784 | |
| 785 | tdesc_str = fetch_available_features_from_target ("target.xml", ops); |
| 786 | if (tdesc_str == NULL) |
| 787 | return NULL; |
| 788 | |
| 789 | back_to = make_cleanup (xfree, tdesc_str); |
| 790 | expanded_text = xml_process_xincludes (_("target description"), |
| 791 | tdesc_str, |
| 792 | fetch_available_features_from_target, ops, 0); |
| 793 | do_cleanups (back_to); |
| 794 | if (expanded_text == NULL) |
| 795 | { |
| 796 | warning (_("Could not load XML target description; ignoring")); |
| 797 | return NULL; |
| 798 | } |
| 799 | |
| 800 | return expanded_text; |
| 801 | #endif |
| 802 | } |