Add feature reference in .dat files
[deliverable/binutils-gdb.git] / gdb / xml-tdesc.c
CommitLineData
23181151
DJ
1/* XML target description support for GDB.
2
e2882c85 3 Copyright (C) 2006-2018 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 85 /* The struct or union we are currently parsing, or last parsed. */
d4a0e8b5 86 tdesc_type_with_fields *current_type;
f5dff777 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,
4d0fdd9b 147 void *user_data, std::vector<gdb_xml_value> &attributes)
1780a0ed 148{
4d0fdd9b
SM
149 char *version
150 = (char *) xml_find_attribute (attributes, "version")->value.get ();
1780a0ed
DJ
151
152 if (strcmp (version, "1.0") != 0)
153 gdb_xml_error (parser,
154 _("Target description has unsupported version \"%s\""),
155 version);
156}
157
123dc839
DJ
158/* Handle the start of a <feature> element. */
159
160static void
161tdesc_start_feature (struct gdb_xml_parser *parser,
162 const struct gdb_xml_element *element,
4d0fdd9b 163 void *user_data, std::vector<gdb_xml_value> &attributes)
123dc839 164{
19ba03f4 165 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
4d0fdd9b 166 char *name = (char *) xml_find_attribute (attributes, "name")->value.get ();
123dc839
DJ
167
168 data->current_feature = tdesc_create_feature (data->tdesc, name);
169}
170
171/* Handle the start of a <reg> element. Fill in the optional
172 attributes and attach it to the containing feature. */
173
174static void
175tdesc_start_reg (struct gdb_xml_parser *parser,
176 const struct gdb_xml_element *element,
4d0fdd9b 177 void *user_data, std::vector<gdb_xml_value> &attributes)
123dc839 178{
19ba03f4 179 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
4d0fdd9b 180 int ix = 0;
a121b7c1
PA
181 char *name, *group;
182 const char *type;
123dc839
DJ
183 int bitsize, regnum, save_restore;
184
4d0fdd9b 185 int length = attributes.size ();
123dc839 186
4d0fdd9b
SM
187 name = (char *) attributes[ix++].value.get ();
188 bitsize = * (ULONGEST *) attributes[ix++].value.get ();
123dc839 189
4d0fdd9b
SM
190 if (ix < length && strcmp (attributes[ix].name, "regnum") == 0)
191 regnum = * (ULONGEST *) attributes[ix++].value.get ();
123dc839
DJ
192 else
193 regnum = data->next_regnum;
194
4d0fdd9b
SM
195 if (ix < length && strcmp (attributes[ix].name, "type") == 0)
196 type = (char *) attributes[ix++].value.get ();
123dc839
DJ
197 else
198 type = "int";
199
4d0fdd9b
SM
200 if (ix < length && strcmp (attributes[ix].name, "group") == 0)
201 group = (char *) attributes[ix++].value.get ();
123dc839
DJ
202 else
203 group = NULL;
204
4d0fdd9b
SM
205 if (ix < length && strcmp (attributes[ix].name, "save-restore") == 0)
206 save_restore = * (ULONGEST *) attributes[ix++].value.get ();
123dc839
DJ
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,
4d0fdd9b 228 void *user_data, std::vector<gdb_xml_value> &attributes)
123dc839 229{
19ba03f4 230 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
4d0fdd9b 231 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
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,
4d0fdd9b 243 void *user_data, std::vector<gdb_xml_value> &attributes)
f5dff777 244{
19ba03f4 245 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
4d0fdd9b 246 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
3d2c1d41 247 struct gdb_xml_value *attr;
f5dff777 248
d4a0e8b5
SM
249 tdesc_type_with_fields *type_with_fields
250 = tdesc_create_struct (data->current_feature, id);
251 data->current_type = type_with_fields;
f5dff777 252 data->current_type_size = 0;
f5dff777 253
3d2c1d41
PA
254 attr = xml_find_attribute (attributes, "size");
255 if (attr != NULL)
f5dff777 256 {
4d0fdd9b 257 ULONGEST size = * (ULONGEST *) attr->value.get ();
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 }
d4a0e8b5 265 tdesc_set_struct_size (type_with_fields, size);
f5dff777
DJ
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,
4d0fdd9b 273 void *user_data, std::vector<gdb_xml_value> &attributes)
f5dff777 274{
19ba03f4 275 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
4d0fdd9b 276 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
54157a25 277 ULONGEST size = * (ULONGEST *)
4d0fdd9b 278 xml_find_attribute (attributes, "size")->value.get ();
f5dff777 279
54157a25
DE
280 if (size > MAX_FIELD_SIZE)
281 {
282 gdb_xml_error (parser,
283 _("Flags size %s is larger than maximum (%d)"),
284 pulongest (size), MAX_FIELD_SIZE);
285 }
f5dff777 286
d4a0e8b5 287 data->current_type = tdesc_create_flags (data->current_feature, id, size);
81516450
DE
288 data->current_type_size = size;
289}
290
291static void
292tdesc_start_enum (struct gdb_xml_parser *parser,
293 const struct gdb_xml_element *element,
4d0fdd9b 294 void *user_data, std::vector<gdb_xml_value> &attributes)
81516450 295{
bfeeb14b 296 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
4d0fdd9b 297 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
81516450 298 int size = * (ULONGEST *)
4d0fdd9b 299 xml_find_attribute (attributes, "size")->value.get ();
81516450
DE
300
301 if (size > MAX_FIELD_SIZE)
302 {
303 gdb_xml_error (parser,
304 _("Enum size %s is larger than maximum (%d)"),
305 pulongest (size), MAX_FIELD_SIZE);
306 }
81516450 307
d4a0e8b5 308 data->current_type = tdesc_create_enum (data->current_feature, id, size);
f5dff777 309 data->current_type_size = 0;
123dc839
DJ
310}
311
312/* Handle the start of a <field> element. Attach the field to the
81516450 313 current struct, union or flags. */
123dc839
DJ
314
315static void
316tdesc_start_field (struct gdb_xml_parser *parser,
317 const struct gdb_xml_element *element,
4d0fdd9b 318 void *user_data, std::vector<gdb_xml_value> &attributes)
123dc839 319{
19ba03f4 320 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
3d2c1d41 321 struct gdb_xml_value *attr;
ad068eab 322 struct tdesc_type *field_type;
123dc839 323 char *field_name, *field_type_id;
f5dff777 324 int start, end;
123dc839 325
4d0fdd9b 326 field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
123dc839 327
3d2c1d41
PA
328 attr = xml_find_attribute (attributes, "type");
329 if (attr != NULL)
81516450 330 {
4d0fdd9b 331 field_type_id = (char *) attr->value.get ();
81516450
DE
332 field_type = tdesc_named_type (data->current_feature, field_type_id);
333 }
f5dff777 334 else
81516450
DE
335 {
336 field_type_id = NULL;
337 field_type = NULL;
338 }
f5dff777 339
3d2c1d41
PA
340 attr = xml_find_attribute (attributes, "start");
341 if (attr != NULL)
54157a25 342 {
4d0fdd9b 343 ULONGEST ul_start = * (ULONGEST *) attr->value.get ();
54157a25
DE
344
345 if (ul_start > MAX_FIELD_BITSIZE)
346 {
347 gdb_xml_error (parser,
348 _("Field start %s is larger than maximum (%d)"),
349 pulongest (ul_start), MAX_FIELD_BITSIZE);
350 }
351 start = ul_start;
352 }
f5dff777
DJ
353 else
354 start = -1;
355
3d2c1d41
PA
356 attr = xml_find_attribute (attributes, "end");
357 if (attr != NULL)
54157a25 358 {
4d0fdd9b 359 ULONGEST ul_end = * (ULONGEST *) attr->value.get ();
54157a25
DE
360
361 if (ul_end > MAX_FIELD_BITSIZE)
362 {
363 gdb_xml_error (parser,
364 _("Field end %s is larger than maximum (%d)"),
365 pulongest (ul_end), MAX_FIELD_BITSIZE);
366 }
367 end = ul_end;
368 }
f5dff777
DJ
369 else
370 end = -1;
371
81516450 372 if (start != -1)
f5dff777 373 {
d4a0e8b5 374 tdesc_type_with_fields *t = data->current_type;
f5dff777 375
ee8da4b8
DE
376 /* Older versions of gdb can't handle elided end values.
377 Stick with that for now, to help ensure backward compatibility.
378 E.g., If a newer gdbserver is talking to an older gdb. */
379 if (end == -1)
380 gdb_xml_error (parser, _("Missing end value"));
381
81516450
DE
382 if (data->current_type_size == 0)
383 gdb_xml_error (parser,
384 _("Bitfields must live in explicitly sized types"));
385
386 if (field_type_id != NULL
387 && strcmp (field_type_id, "bool") == 0
ee8da4b8 388 && start != end)
f5dff777 389 {
81516450
DE
390 gdb_xml_error (parser,
391 _("Boolean fields must be one bit in size"));
392 }
f5dff777 393
81516450
DE
394 if (end >= 64)
395 gdb_xml_error (parser,
396 _("Bitfield \"%s\" goes past "
397 "64 bits (unsupported)"),
398 field_name);
f5dff777 399
ee8da4b8
DE
400 /* Assume that the bit numbering in XML is "lsb-zero". Most
401 architectures other than PowerPC use this ordering. In the
402 future, we can add an XML tag to indicate "msb-zero" numbering. */
403 if (start > end)
404 gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"),
405 field_name);
406 if (end >= data->current_type_size * TARGET_CHAR_BIT)
21047726
PA
407 gdb_xml_error (parser, _("Bitfield \"%s\" does not fit in struct"),
408 field_name);
f5dff777 409
ee8da4b8 410 if (field_type != NULL)
81516450 411 tdesc_add_typed_bitfield (t, field_name, start, end, field_type);
ee8da4b8
DE
412 else if (start == end)
413 tdesc_add_flag (t, start, field_name);
81516450
DE
414 else
415 tdesc_add_bitfield (t, field_name, start, end);
416 }
417 else if (start == -1 && end != -1)
418 gdb_xml_error (parser, _("End specified but not start"));
419 else if (field_type_id != NULL)
420 {
421 /* TDESC_TYPE_FLAGS values are explicitly sized, so the following test
422 catches adding non-bitfield types to flags as well. */
423 if (data->current_type_size != 0)
424 gdb_xml_error (parser,
425 _("Explicitly sized type cannot "
426 "contain non-bitfield \"%s\""),
427 field_name);
428
429 if (field_type == NULL)
430 gdb_xml_error (parser, _("Field \"%s\" references undefined "
431 "type \"%s\""),
432 field_name, field_type_id);
433
434 tdesc_add_field (data->current_type, field_name, field_type);
f5dff777
DJ
435 }
436 else
437 gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"),
438 field_name);
123dc839
DJ
439}
440
81516450
DE
441/* Handle the start of an <evalue> element. Attach the value to the
442 current enum. */
443
444static void
445tdesc_start_enum_value (struct gdb_xml_parser *parser,
446 const struct gdb_xml_element *element,
4d0fdd9b 447 void *user_data, std::vector<gdb_xml_value> &attributes)
81516450
DE
448{
449 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
450 struct gdb_xml_value *attr;
451 char *field_name;
452 ULONGEST ul_value;
453 int value;
454
4d0fdd9b 455 field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
81516450
DE
456
457 attr = xml_find_attribute (attributes, "value");
4d0fdd9b 458 ul_value = * (ULONGEST *) attr->value.get ();
81516450
DE
459 if (ul_value > INT_MAX)
460 {
461 gdb_xml_error (parser,
462 _("Enum value %s is larger than maximum (%d)"),
463 pulongest (ul_value), INT_MAX);
464 }
465 value = ul_value;
466
467 tdesc_add_enum_value (data->current_type, value, field_name);
468}
469
123dc839
DJ
470/* Handle the start of a <vector> element. Initialize the type and
471 record it with the current feature. */
472
473static void
474tdesc_start_vector (struct gdb_xml_parser *parser,
475 const struct gdb_xml_element *element,
4d0fdd9b 476 void *user_data, std::vector<gdb_xml_value> &attributes)
123dc839 477{
19ba03f4 478 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
ad068eab 479 struct tdesc_type *field_type;
123dc839 480 char *id, *field_type_id;
54157a25 481 ULONGEST count;
123dc839 482
4d0fdd9b
SM
483 id = (char *) attributes[0].value.get ();
484 field_type_id = (char *) attributes[1].value.get ();
485 count = * (ULONGEST *) attributes[2].value.get ();
123dc839 486
54157a25
DE
487 if (count > MAX_VECTOR_SIZE)
488 {
489 gdb_xml_error (parser,
490 _("Vector size %s is larger than maximum (%d)"),
491 pulongest (count), MAX_VECTOR_SIZE);
492 }
493
123dc839
DJ
494 field_type = tdesc_named_type (data->current_feature, field_type_id);
495 if (field_type == NULL)
496 gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""),
497 id, field_type_id);
498
ad068eab 499 tdesc_create_vector (data->current_feature, id, field_type, count);
123dc839
DJ
500}
501
23181151
DJ
502/* The elements and attributes of an XML target description. */
503
123dc839
DJ
504static const struct gdb_xml_attribute field_attributes[] = {
505 { "name", GDB_XML_AF_NONE, NULL, NULL },
f5dff777
DJ
506 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
507 { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
508 { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
123dc839
DJ
509 { NULL, GDB_XML_AF_NONE, NULL, NULL }
510};
511
81516450
DE
512static const struct gdb_xml_attribute enum_value_attributes[] = {
513 { "name", GDB_XML_AF_NONE, NULL, NULL },
514 { "value", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
515 { NULL, GDB_XML_AF_NONE, NULL, NULL }
516};
517
f5dff777 518static const struct gdb_xml_element struct_union_children[] = {
123dc839
DJ
519 { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE,
520 tdesc_start_field, NULL },
521 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
522};
523
81516450
DE
524static const struct gdb_xml_element enum_children[] = {
525 { "evalue", enum_value_attributes, NULL, GDB_XML_EF_REPEATABLE,
526 tdesc_start_enum_value, NULL },
527 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
528};
529
123dc839
DJ
530static const struct gdb_xml_attribute reg_attributes[] = {
531 { "name", GDB_XML_AF_NONE, NULL, NULL },
532 { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
533 { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
534 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
535 { "group", GDB_XML_AF_OPTIONAL, NULL, NULL },
536 { "save-restore", GDB_XML_AF_OPTIONAL,
537 gdb_xml_parse_attr_enum, gdb_xml_enums_boolean },
538 { NULL, GDB_XML_AF_NONE, NULL, NULL }
539};
540
f5dff777 541static const struct gdb_xml_attribute struct_union_attributes[] = {
123dc839 542 { "id", GDB_XML_AF_NONE, NULL, NULL },
f5dff777
DJ
543 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL},
544 { NULL, GDB_XML_AF_NONE, NULL, NULL }
545};
546
547static const struct gdb_xml_attribute flags_attributes[] = {
548 { "id", GDB_XML_AF_NONE, NULL, NULL },
549 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
123dc839
DJ
550 { NULL, GDB_XML_AF_NONE, NULL, NULL }
551};
552
81516450
DE
553static const struct gdb_xml_attribute enum_attributes[] = {
554 { "id", GDB_XML_AF_NONE, NULL, NULL },
555 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
556 { NULL, GDB_XML_AF_NONE, NULL, NULL }
557};
558
123dc839
DJ
559static const struct gdb_xml_attribute vector_attributes[] = {
560 { "id", GDB_XML_AF_NONE, NULL, NULL },
561 { "type", GDB_XML_AF_NONE, NULL, NULL },
562 { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
563 { NULL, GDB_XML_AF_NONE, NULL, NULL }
564};
565
566static const struct gdb_xml_attribute feature_attributes[] = {
567 { "name", GDB_XML_AF_NONE, NULL, NULL },
568 { NULL, GDB_XML_AF_NONE, NULL, NULL }
569};
570
571static const struct gdb_xml_element feature_children[] = {
572 { "reg", reg_attributes, NULL,
573 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
574 tdesc_start_reg, NULL },
f5dff777
DJ
575 { "struct", struct_union_attributes, struct_union_children,
576 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
577 tdesc_start_struct, NULL },
578 { "union", struct_union_attributes, struct_union_children,
123dc839 579 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
ad068eab 580 tdesc_start_union, NULL },
f5dff777
DJ
581 { "flags", flags_attributes, struct_union_children,
582 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
583 tdesc_start_flags, NULL },
81516450
DE
584 { "enum", enum_attributes, enum_children,
585 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
586 tdesc_start_enum, NULL },
123dc839
DJ
587 { "vector", vector_attributes, NULL,
588 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
589 tdesc_start_vector, NULL },
590 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
591};
592
1780a0ed
DJ
593static const struct gdb_xml_attribute target_attributes[] = {
594 { "version", GDB_XML_AF_NONE, NULL, NULL },
595 { NULL, GDB_XML_AF_NONE, NULL, NULL }
596};
597
123dc839 598static const struct gdb_xml_element target_children[] = {
23181151
DJ
599 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
600 NULL, tdesc_end_arch },
08d16641
PA
601 { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL,
602 NULL, tdesc_end_osabi },
e35359c5
UW
603 { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
604 NULL, tdesc_end_compatible },
123dc839
DJ
605 { "feature", feature_attributes, feature_children,
606 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
607 tdesc_start_feature, NULL },
23181151
DJ
608 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
609};
610
123dc839 611static const struct gdb_xml_element tdesc_elements[] = {
1780a0ed
DJ
612 { "target", target_attributes, target_children, GDB_XML_EF_NONE,
613 tdesc_start_target, NULL },
23181151
DJ
614 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
615};
616
617/* Parse DOCUMENT into a target description and return it. */
618
619static struct target_desc *
108546a0
DJ
620tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
621 void *fetcher_baton)
23181151 622{
23181151
DJ
623 struct tdesc_parsing_data data;
624
108546a0 625 /* Expand all XInclude directives. */
bd8a901f
PA
626 std::string expanded_text;
627
628 if (!xml_process_xincludes (expanded_text,
629 _("target description"),
630 document, fetcher, fetcher_baton, 0))
108546a0
DJ
631 {
632 warning (_("Could not load XML target description; ignoring"));
633 return NULL;
634 }
23181151 635
fc6e0168 636 /* Check for an exact match in the list of descriptions we have
bd8a901f
PA
637 previously parsed. */
638 const auto it = xml_cache.find (expanded_text);
639 if (it != xml_cache.end ())
640 return it->second;
23181151 641
108546a0 642 memset (&data, 0, sizeof (struct tdesc_parsing_data));
23181151 643 data.tdesc = allocate_target_description ();
bd8a901f
PA
644 struct cleanup *result_cleanup
645 = make_cleanup_free_target_description (data.tdesc);
23181151 646
efc0eabd 647 if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
bd8a901f 648 tdesc_elements, expanded_text.c_str (), &data) == 0)
23181151
DJ
649 {
650 /* Parsed successfully. */
bd8a901f 651 xml_cache.emplace (std::move (expanded_text), data.tdesc);
23181151 652 discard_cleanups (result_cleanup);
23181151
DJ
653 return data.tdesc;
654 }
655 else
656 {
657 warning (_("Could not load XML target description; ignoring"));
bd8a901f 658 do_cleanups (result_cleanup);
23181151
DJ
659 return NULL;
660 }
661}
23181151
DJ
662#endif /* HAVE_LIBEXPAT */
663\f
664
23181151
DJ
665/* Read an XML target description from FILENAME. Parse it, and return
666 the parsed description. */
667
668const struct target_desc *
669file_read_description_xml (const char *filename)
670{
9018be22 671 gdb::optional<gdb::char_vector> tdesc_str
b7b030ad 672 = xml_fetch_content_from_file (filename, NULL);
9018be22 673 if (!tdesc_str)
108546a0
DJ
674 {
675 warning (_("Could not open \"%s\""), filename);
676 return NULL;
677 }
23181151 678
9018be22 679 return tdesc_parse_xml (tdesc_str->data (), xml_fetch_content_from_file,
b7b030ad 680 (void *) ldirname (filename).c_str ());
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>
108546a0
DJ
691fetch_available_features_from_target (const char *name, void *baton_)
692{
19ba03f4 693 struct target_ops *ops = (struct target_ops *) baton_;
108546a0
DJ
694
695 /* Read this object as a string. This ensures that a NUL
696 terminator is added. */
697 return target_read_stralloc (ops,
698 TARGET_OBJECT_AVAILABLE_FEATURES,
699 name);
700}
701\f
702
23181151
DJ
703/* Read an XML target description using OPS. Parse it, and return the
704 parsed description. */
705
706const struct target_desc *
707target_read_description_xml (struct target_ops *ops)
708{
9018be22 709 gdb::optional<gdb::char_vector> tdesc_str
b7b030ad 710 = fetch_available_features_from_target ("target.xml", ops);
9018be22 711 if (!tdesc_str)
23181151
DJ
712 return NULL;
713
9018be22 714 return tdesc_parse_xml (tdesc_str->data (),
b7b030ad
TT
715 fetch_available_features_from_target,
716 ops);
23181151 717}
18d3cec5
MK
718
719/* Fetches an XML target description using OPS, processing
720 includes, but not parsing it. Used to dump whole tdesc
721 as a single XML file. */
722
bd8a901f 723gdb::optional<std::string>
18d3cec5
MK
724target_fetch_description_xml (struct target_ops *ops)
725{
d21b5f15
MK
726#if !defined(HAVE_LIBEXPAT)
727 static int have_warned;
728
729 if (!have_warned)
730 {
731 have_warned = 1;
732 warning (_("Can not fetch XML target description; XML support was "
733 "disabled at compile time"));
734 }
735
bd8a901f 736 return {};
d21b5f15 737#else
9018be22 738 gdb::optional<gdb::char_vector>
b7b030ad 739 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
9018be22 740 if (!tdesc_str)
bd8a901f 741 return {};
18d3cec5 742
bd8a901f
PA
743 std::string output;
744 if (!xml_process_xincludes (output,
745 _("target description"),
9018be22 746 tdesc_str->data (),
bd8a901f 747 fetch_available_features_from_target, ops, 0))
18d3cec5
MK
748 {
749 warning (_("Could not load XML target description; ignoring"));
bd8a901f 750 return {};
18d3cec5 751 }
bd8a901f 752 return output;
d21b5f15 753#endif
18d3cec5 754}
This page took 1.403181 seconds and 4 git commands to generate.