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