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