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