1 /* XML target description support for GDB.
2 
3    Copyright (C) 2006-2021 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 #include <unordered_map>
30 #include <string>
31 
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 
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 
43 static struct target_desc *
tdesc_parse_xml(const char * document,xml_fetch_another fetcher)44 tdesc_parse_xml (const char *document, xml_fetch_another fetcher)
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 
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 
68 static std::unordered_map<std::string, target_desc_up> xml_cache;
69 
70 /* Callback data for target description parsing.  */
71 
72 struct tdesc_parsing_data
73 {
74   /* The target description we are building.  */
75   struct target_desc *tdesc;
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 
84   /* The struct or union we are currently parsing, or last parsed.  */
85   tdesc_type_with_fields *current_type;
86 
87   /* The byte size of the current struct/flags type, if specified.  Zero
88      if not specified.  Flags values must specify a size.  */
89   int current_type_size;
90 };
91 
92 /* Handle the end of an <architecture> element and its value.  */
93 
94 static void
tdesc_end_arch(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)95 tdesc_end_arch (struct gdb_xml_parser *parser,
96 		const struct gdb_xml_element *element,
97 		void *user_data, const char *body_text)
98 {
99   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
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 
109 /* Handle the end of an <osabi> element and its value.  */
110 
111 static void
tdesc_end_osabi(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)112 tdesc_end_osabi (struct gdb_xml_parser *parser,
113 		 const struct gdb_xml_element *element,
114 		 void *user_data, const char *body_text)
115 {
116   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
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 
127 /* Handle the end of a <compatible> element and its value.  */
128 
129 static void
tdesc_end_compatible(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)130 tdesc_end_compatible (struct gdb_xml_parser *parser,
131 		      const struct gdb_xml_element *element,
132 		      void *user_data, const char *body_text)
133 {
134   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
135   const struct bfd_arch_info *arch;
136 
137   arch = bfd_scan_arch (body_text);
138   tdesc_add_compatible (data->tdesc, arch);
139 }
140 
141 /* Handle the start of a <target> element.  */
142 
143 static void
tdesc_start_target(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)144 tdesc_start_target (struct gdb_xml_parser *parser,
145 		    const struct gdb_xml_element *element,
146 		    void *user_data, std::vector<gdb_xml_value> &attributes)
147 {
148   char *version
149     = (char *) xml_find_attribute (attributes, "version")->value.get ();
150 
151   if (strcmp (version, "1.0") != 0)
152     gdb_xml_error (parser,
153 		   _("Target description has unsupported version \"%s\""),
154 		   version);
155 }
156 
157 /* Handle the start of a <feature> element.  */
158 
159 static void
tdesc_start_feature(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)160 tdesc_start_feature (struct gdb_xml_parser *parser,
161 		     const struct gdb_xml_element *element,
162 		     void *user_data, std::vector<gdb_xml_value> &attributes)
163 {
164   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
165   char *name = (char *) xml_find_attribute (attributes, "name")->value.get ();
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 
173 static void
tdesc_start_reg(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)174 tdesc_start_reg (struct gdb_xml_parser *parser,
175 		 const struct gdb_xml_element *element,
176 		 void *user_data, std::vector<gdb_xml_value> &attributes)
177 {
178   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
179   int ix = 0;
180   char *name, *group;
181   const char *type;
182   int bitsize, regnum, save_restore;
183 
184   int length = attributes.size ();
185 
186   name = (char *) attributes[ix++].value.get ();
187   bitsize = * (ULONGEST *) attributes[ix++].value.get ();
188 
189   if (ix < length && strcmp (attributes[ix].name, "regnum") == 0)
190     regnum = * (ULONGEST *) attributes[ix++].value.get ();
191   else
192     regnum = data->next_regnum;
193 
194   if (ix < length && strcmp (attributes[ix].name, "type") == 0)
195     type = (char *) attributes[ix++].value.get ();
196   else
197     type = "int";
198 
199   if (ix < length && strcmp (attributes[ix].name, "group") == 0)
200     group = (char *) attributes[ix++].value.get ();
201   else
202     group = NULL;
203 
204   if (ix < length && strcmp (attributes[ix].name, "save-restore") == 0)
205     save_restore = * (ULONGEST *) attributes[ix++].value.get ();
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 
224 static void
tdesc_start_union(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)225 tdesc_start_union (struct gdb_xml_parser *parser,
226 		   const struct gdb_xml_element *element,
227 		   void *user_data, std::vector<gdb_xml_value> &attributes)
228 {
229   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
230   char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
231 
232   data->current_type = tdesc_create_union (data->current_feature, id);
233   data->current_type_size = 0;
234 }
235 
236 /* Handle the start of a <struct> element.  Initialize the type and
237    record it with the current feature.  */
238 
239 static void
tdesc_start_struct(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)240 tdesc_start_struct (struct gdb_xml_parser *parser,
241 		   const struct gdb_xml_element *element,
242 		   void *user_data, std::vector<gdb_xml_value> &attributes)
243 {
244   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
245   char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
246   struct gdb_xml_value *attr;
247 
248   tdesc_type_with_fields *type_with_fields
249     = tdesc_create_struct (data->current_feature, id);
250   data->current_type = type_with_fields;
251   data->current_type_size = 0;
252 
253   attr = xml_find_attribute (attributes, "size");
254   if (attr != NULL)
255     {
256       ULONGEST size = * (ULONGEST *) attr->value.get ();
257 
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 	}
264       tdesc_set_struct_size (type_with_fields, size);
265       data->current_type_size = size;
266     }
267 }
268 
269 static void
tdesc_start_flags(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)270 tdesc_start_flags (struct gdb_xml_parser *parser,
271 		   const struct gdb_xml_element *element,
272 		   void *user_data, std::vector<gdb_xml_value> &attributes)
273 {
274   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
275   char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
276   ULONGEST size = * (ULONGEST *)
277     xml_find_attribute (attributes, "size")->value.get ();
278 
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     }
285 
286   data->current_type = tdesc_create_flags (data->current_feature, id, size);
287   data->current_type_size = size;
288 }
289 
290 static void
tdesc_start_enum(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)291 tdesc_start_enum (struct gdb_xml_parser *parser,
292 		  const struct gdb_xml_element *element,
293 		  void *user_data, std::vector<gdb_xml_value> &attributes)
294 {
295   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
296   char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
297   int size = * (ULONGEST *)
298     xml_find_attribute (attributes, "size")->value.get ();
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     }
306 
307   data->current_type = tdesc_create_enum (data->current_feature, id, size);
308   data->current_type_size = 0;
309 }
310 
311 /* Handle the start of a <field> element.  Attach the field to the
312    current struct, union or flags.  */
313 
314 static void
tdesc_start_field(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)315 tdesc_start_field (struct gdb_xml_parser *parser,
316 		   const struct gdb_xml_element *element,
317 		   void *user_data, std::vector<gdb_xml_value> &attributes)
318 {
319   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
320   struct gdb_xml_value *attr;
321   struct tdesc_type *field_type;
322   char *field_name, *field_type_id;
323   int start, end;
324 
325   field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
326 
327   attr = xml_find_attribute (attributes, "type");
328   if (attr != NULL)
329     {
330       field_type_id = (char *) attr->value.get ();
331       field_type = tdesc_named_type (data->current_feature, field_type_id);
332     }
333   else
334     {
335       field_type_id = NULL;
336       field_type = NULL;
337     }
338 
339   attr = xml_find_attribute (attributes, "start");
340   if (attr != NULL)
341     {
342       ULONGEST ul_start = * (ULONGEST *) attr->value.get ();
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     }
352   else
353     start = -1;
354 
355   attr = xml_find_attribute (attributes, "end");
356   if (attr != NULL)
357     {
358       ULONGEST ul_end = * (ULONGEST *) attr->value.get ();
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     }
368   else
369     end = -1;
370 
371   if (start != -1)
372     {
373       tdesc_type_with_fields *t = data->current_type;
374 
375       /* Older versions of gdb can't handle elided end values.
376 	 Stick with that for now, to help ensure backward compatibility.
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 
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
387 	  && start != end)
388 	{
389 	  gdb_xml_error (parser,
390 			 _("Boolean fields must be one bit in size"));
391 	}
392 
393       if (end >= 64)
394 	gdb_xml_error (parser,
395 		       _("Bitfield \"%s\" goes past "
396 			 "64 bits (unsupported)"),
397 		       field_name);
398 
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)
406 	gdb_xml_error (parser, _("Bitfield \"%s\" does not fit in struct"),
407 		       field_name);
408 
409       if (field_type != NULL)
410 	tdesc_add_typed_bitfield (t, field_name, start, end, field_type);
411       else if (start == end)
412 	tdesc_add_flag (t, start, field_name);
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);
434     }
435   else
436     gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"),
437 		   field_name);
438 }
439 
440 /* Handle the start of an <evalue> element.  Attach the value to the
441    current enum.  */
442 
443 static void
tdesc_start_enum_value(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)444 tdesc_start_enum_value (struct gdb_xml_parser *parser,
445 			const struct gdb_xml_element *element,
446 			void *user_data, std::vector<gdb_xml_value> &attributes)
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 
454   field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
455 
456   attr = xml_find_attribute (attributes, "value");
457   ul_value = * (ULONGEST *) attr->value.get ();
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 
469 /* Handle the start of a <vector> element.  Initialize the type and
470    record it with the current feature.  */
471 
472 static void
tdesc_start_vector(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)473 tdesc_start_vector (struct gdb_xml_parser *parser,
474 		    const struct gdb_xml_element *element,
475 		    void *user_data, std::vector<gdb_xml_value> &attributes)
476 {
477   struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
478   struct tdesc_type *field_type;
479   char *id, *field_type_id;
480   ULONGEST count;
481 
482   id = (char *) attributes[0].value.get ();
483   field_type_id = (char *) attributes[1].value.get ();
484   count = * (ULONGEST *) attributes[2].value.get ();
485 
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 
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 
498   tdesc_create_vector (data->current_feature, id, field_type, count);
499 }
500 
501 /* The elements and attributes of an XML target description.  */
502 
503 static const struct gdb_xml_attribute field_attributes[] = {
504   { "name", GDB_XML_AF_NONE, NULL, NULL },
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 },
508   { NULL, GDB_XML_AF_NONE, NULL, NULL }
509 };
510 
511 static 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 
517 static const struct gdb_xml_element struct_union_children[] = {
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 
523 static 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 
529 static 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 
540 static const struct gdb_xml_attribute struct_union_attributes[] = {
541   { "id", GDB_XML_AF_NONE, NULL, NULL },
542   { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL},
543   { NULL, GDB_XML_AF_NONE, NULL, NULL }
544 };
545 
546 static 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},
549   { NULL, GDB_XML_AF_NONE, NULL, NULL }
550 };
551 
552 static 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 
558 static 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 
565 static 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 
570 static 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 },
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,
578     GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
579     tdesc_start_union, NULL },
580   { "flags", flags_attributes, struct_union_children,
581     GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
582     tdesc_start_flags, NULL },
583   { "enum", enum_attributes, enum_children,
584     GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
585     tdesc_start_enum, NULL },
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 
592 static 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 
597 static const struct gdb_xml_element target_children[] = {
598   { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
599     NULL, tdesc_end_arch },
600   { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL,
601     NULL, tdesc_end_osabi },
602   { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
603     NULL, tdesc_end_compatible },
604   { "feature", feature_attributes, feature_children,
605     GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
606     tdesc_start_feature, NULL },
607   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
608 };
609 
610 static const struct gdb_xml_element tdesc_elements[] = {
611   { "target", target_attributes, target_children, GDB_XML_EF_NONE,
612     tdesc_start_target, NULL },
613   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
614 };
615 
616 /* Parse DOCUMENT into a target description and return it.  */
617 
618 static struct target_desc *
tdesc_parse_xml(const char * document,xml_fetch_another fetcher)619 tdesc_parse_xml (const char *document, xml_fetch_another fetcher)
620 {
621   struct tdesc_parsing_data data;
622 
623   /* Expand all XInclude directives.  */
624   std::string expanded_text;
625 
626   if (!xml_process_xincludes (expanded_text,
627 			      _("target description"),
628 			      document, fetcher, 0))
629     {
630       warning (_("Could not load XML target description; ignoring"));
631       return NULL;
632     }
633 
634   /* Check for an exact match in the list of descriptions we have
635      previously parsed.  */
636   const auto it = xml_cache.find (expanded_text);
637   if (it != xml_cache.end ())
638     return it->second.get ();
639 
640   memset (&data, 0, sizeof (struct tdesc_parsing_data));
641   target_desc_up description (allocate_target_description ());
642   data.tdesc = description.get ();
643 
644   if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
645 			   tdesc_elements, expanded_text.c_str (), &data) == 0)
646     {
647       /* Parsed successfully.  */
648       xml_cache.emplace (std::move (expanded_text), std::move (description));
649       return data.tdesc;
650     }
651   else
652     {
653       warning (_("Could not load XML target description; ignoring"));
654       return NULL;
655     }
656 }
657 #endif /* HAVE_LIBEXPAT */
658 
659 
660 /* Read an XML target description from FILENAME.  Parse it, and return
661    the parsed description.  */
662 
663 const struct target_desc *
file_read_description_xml(const char * filename)664 file_read_description_xml (const char *filename)
665 {
666   gdb::optional<gdb::char_vector> tdesc_str
667     = xml_fetch_content_from_file (filename, NULL);
668   if (!tdesc_str)
669     {
670       warning (_("Could not open \"%s\""), filename);
671       return NULL;
672     }
673 
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);
681 }
682 
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 
690 static gdb::optional<gdb::char_vector>
fetch_available_features_from_target(const char * name,target_ops * ops)691 fetch_available_features_from_target (const char *name, target_ops *ops)
692 {
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 
700 
701 /* Read an XML target description using OPS.  Parse it, and return the
702    parsed description.  */
703 
704 const struct target_desc *
target_read_description_xml(struct target_ops * ops)705 target_read_description_xml (struct target_ops *ops)
706 {
707   gdb::optional<gdb::char_vector> tdesc_str
708     = fetch_available_features_from_target ("target.xml", ops);
709   if (!tdesc_str)
710     return NULL;
711 
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);
718 }
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 
724 gdb::optional<std::string>
target_fetch_description_xml(struct target_ops * ops)725 target_fetch_description_xml (struct target_ops *ops)
726 {
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 
737   return {};
738 #else
739   gdb::optional<gdb::char_vector>
740     tdesc_str = fetch_available_features_from_target ("target.xml", ops);
741   if (!tdesc_str)
742     return {};
743 
744   auto fetch_another = [ops] (const char *name)
745     {
746       return fetch_available_features_from_target (name, ops);
747     };
748   std::string output;
749   if (!xml_process_xincludes (output,
750 			      _("target description"),
751 			      tdesc_str->data (), fetch_another, 0))
752     {
753       warning (_("Could not load XML target description; ignoring"));
754       return {};
755     }
756   return output;
757 #endif
758 }
759 
760 /* See xml-tdesc.h.  */
761 
762 const struct target_desc *
string_read_description_xml(const char * xml)763 string_read_description_xml (const char *xml)
764 {
765   return tdesc_parse_xml (xml, [] (const char *href)
766     {
767       error (_("xincludes are unsupported with this method"));
768       return gdb::optional<gdb::char_vector> ();
769     });
770 }
771