xml-tdesc.c 24 KB

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