* target-descriptions.c (tdesc_named_type): Move code_ptr and data_ptr
[deliverable/binutils-gdb.git] / gdb / xml-tdesc.c
1 /* XML target description support for GDB.
2
3 Copyright (C) 2006
4 Free Software Foundation, Inc.
5
6 Contributed by CodeSourcery.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
24
25 #include "defs.h"
26 #include "gdbtypes.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "xml-support.h"
30 #include "xml-tdesc.h"
31
32 #include "filenames.h"
33
34 #include "gdb_assert.h"
35
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 *
42 tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
43 void *fetcher_baton)
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
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
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;
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
91 /* The union we are currently parsing, or last parsed. */
92 struct type *current_union;
93 };
94
95 /* Handle the end of an <architecture> element and its value. */
96
97 static void
98 tdesc_end_arch (struct gdb_xml_parser *parser,
99 const struct gdb_xml_element *element,
100 void *user_data, const char *body_text)
101 {
102 struct tdesc_parsing_data *data = user_data;
103 const struct bfd_arch_info *arch;
104
105 arch = bfd_scan_arch (body_text);
106 if (arch == NULL)
107 gdb_xml_error (parser, _("Target description specified unknown "
108 "architecture \"%s\""), body_text);
109 set_tdesc_architecture (data->tdesc, arch);
110 }
111
112 /* Handle the start of a <feature> element. */
113
114 static void
115 tdesc_start_feature (struct gdb_xml_parser *parser,
116 const struct gdb_xml_element *element,
117 void *user_data, VEC(gdb_xml_value_s) *attributes)
118 {
119 struct tdesc_parsing_data *data = user_data;
120 char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
121
122 data->current_feature = tdesc_create_feature (data->tdesc, name);
123 }
124
125 /* Handle the start of a <reg> element. Fill in the optional
126 attributes and attach it to the containing feature. */
127
128 static void
129 tdesc_start_reg (struct gdb_xml_parser *parser,
130 const struct gdb_xml_element *element,
131 void *user_data, VEC(gdb_xml_value_s) *attributes)
132 {
133 struct tdesc_parsing_data *data = user_data;
134 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
135 int ix = 0, length;
136 char *name, *group, *type;
137 int bitsize, regnum, save_restore;
138
139 length = VEC_length (gdb_xml_value_s, attributes);
140
141 name = attrs[ix++].value;
142 bitsize = * (ULONGEST *) attrs[ix++].value;
143
144 if (ix < length && strcmp (attrs[ix].name, "regnum") == 0)
145 regnum = * (ULONGEST *) attrs[ix++].value;
146 else
147 regnum = data->next_regnum;
148
149 if (ix < length && strcmp (attrs[ix].name, "type") == 0)
150 type = attrs[ix++].value;
151 else
152 type = "int";
153
154 if (ix < length && strcmp (attrs[ix].name, "group") == 0)
155 group = attrs[ix++].value;
156 else
157 group = NULL;
158
159 if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0)
160 save_restore = * (ULONGEST *) attrs[ix++].value;
161 else
162 save_restore = 1;
163
164 if (strcmp (type, "int") != 0
165 && strcmp (type, "float") != 0
166 && strcmp (type, "code_ptr") != 0
167 && strcmp (type, "data_ptr") != 0
168 && tdesc_named_type (data->current_feature, type) == NULL)
169 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
170 name, type);
171
172 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
173 bitsize, type);
174
175 data->next_regnum = regnum + 1;
176 }
177
178 /* Handle the start of a <union> element. Initialize the type and
179 record it with the current feature. */
180
181 static void
182 tdesc_start_union (struct gdb_xml_parser *parser,
183 const struct gdb_xml_element *element,
184 void *user_data, VEC(gdb_xml_value_s) *attributes)
185 {
186 struct tdesc_parsing_data *data = user_data;
187 char *id = VEC_index (gdb_xml_value_s, attributes, 0)->value;
188 struct type *type;
189
190 type = init_composite_type (NULL, TYPE_CODE_UNION);
191 TYPE_NAME (type) = xstrdup (id);
192 tdesc_record_type (data->current_feature, type);
193 data->current_union = type;
194 }
195
196 /* Handle the end of a <union> element. */
197
198 static void
199 tdesc_end_union (struct gdb_xml_parser *parser,
200 const struct gdb_xml_element *element,
201 void *user_data, const char *body_text)
202 {
203 struct tdesc_parsing_data *data = user_data;
204 int i;
205
206 /* If any of the children of this union are vectors, flag the union
207 as a vector also. This allows e.g. a union of two vector types
208 to show up automatically in "info vector". */
209 for (i = 0; i < TYPE_NFIELDS (data->current_union); i++)
210 if (TYPE_VECTOR (TYPE_FIELD_TYPE (data->current_union, i)))
211 {
212 TYPE_FLAGS (data->current_union) |= TYPE_FLAG_VECTOR;
213 break;
214 }
215 }
216
217 /* Handle the start of a <field> element. Attach the field to the
218 current union. */
219
220 static void
221 tdesc_start_field (struct gdb_xml_parser *parser,
222 const struct gdb_xml_element *element,
223 void *user_data, VEC(gdb_xml_value_s) *attributes)
224 {
225 struct tdesc_parsing_data *data = user_data;
226 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
227 struct type *type, *field_type;
228 char *field_name, *field_type_id;
229
230 field_name = attrs[0].value;
231 field_type_id = attrs[1].value;
232
233 field_type = tdesc_named_type (data->current_feature, field_type_id);
234 if (field_type == NULL)
235 gdb_xml_error (parser, _("Union field \"%s\" references undefined "
236 "type \"%s\""),
237 field_name, field_type_id);
238
239 append_composite_type_field (data->current_union, xstrdup (field_name),
240 field_type);
241 }
242
243 /* Handle the start of a <vector> element. Initialize the type and
244 record it with the current feature. */
245
246 static void
247 tdesc_start_vector (struct gdb_xml_parser *parser,
248 const struct gdb_xml_element *element,
249 void *user_data, VEC(gdb_xml_value_s) *attributes)
250 {
251 struct tdesc_parsing_data *data = user_data;
252 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
253 struct type *type, *field_type, *range_type;
254 char *id, *field_type_id;
255 int count;
256
257 id = attrs[0].value;
258 field_type_id = attrs[1].value;
259 count = * (ULONGEST *) attrs[2].value;
260
261 field_type = tdesc_named_type (data->current_feature, field_type_id);
262 if (field_type == NULL)
263 gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""),
264 id, field_type_id);
265
266 /* A vector is just an array plus a special flag. */
267 range_type = create_range_type (NULL, builtin_type_int, 0, count - 1);
268 type = create_array_type (NULL, field_type, range_type);
269 TYPE_NAME (type) = xstrdup (id);
270
271 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
272
273 tdesc_record_type (data->current_feature, type);
274 }
275
276 /* The elements and attributes of an XML target description. */
277
278 static const struct gdb_xml_attribute field_attributes[] = {
279 { "name", GDB_XML_AF_NONE, NULL, NULL },
280 { "type", GDB_XML_AF_NONE, NULL, NULL },
281 { NULL, GDB_XML_AF_NONE, NULL, NULL }
282 };
283
284 static const struct gdb_xml_element union_children[] = {
285 { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE,
286 tdesc_start_field, NULL },
287 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
288 };
289
290 static const struct gdb_xml_attribute reg_attributes[] = {
291 { "name", GDB_XML_AF_NONE, NULL, NULL },
292 { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
293 { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
294 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
295 { "group", GDB_XML_AF_OPTIONAL, NULL, NULL },
296 { "save-restore", GDB_XML_AF_OPTIONAL,
297 gdb_xml_parse_attr_enum, gdb_xml_enums_boolean },
298 { NULL, GDB_XML_AF_NONE, NULL, NULL }
299 };
300
301 static const struct gdb_xml_attribute union_attributes[] = {
302 { "id", GDB_XML_AF_NONE, NULL, NULL },
303 { NULL, GDB_XML_AF_NONE, NULL, NULL }
304 };
305
306 static const struct gdb_xml_attribute vector_attributes[] = {
307 { "id", GDB_XML_AF_NONE, NULL, NULL },
308 { "type", GDB_XML_AF_NONE, NULL, NULL },
309 { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
310 { NULL, GDB_XML_AF_NONE, NULL, NULL }
311 };
312
313 static const struct gdb_xml_attribute feature_attributes[] = {
314 { "name", GDB_XML_AF_NONE, NULL, NULL },
315 { NULL, GDB_XML_AF_NONE, NULL, NULL }
316 };
317
318 static const struct gdb_xml_element feature_children[] = {
319 { "reg", reg_attributes, NULL,
320 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
321 tdesc_start_reg, NULL },
322 { "union", union_attributes, union_children,
323 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
324 tdesc_start_union, tdesc_end_union },
325 { "vector", vector_attributes, NULL,
326 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
327 tdesc_start_vector, NULL },
328 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
329 };
330
331 static const struct gdb_xml_element target_children[] = {
332 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
333 NULL, tdesc_end_arch },
334 { "feature", feature_attributes, feature_children,
335 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
336 tdesc_start_feature, NULL },
337 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
338 };
339
340 static const struct gdb_xml_element tdesc_elements[] = {
341 { "target", NULL, target_children, GDB_XML_EF_NONE,
342 NULL, NULL },
343 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
344 };
345
346 /* Parse DOCUMENT into a target description and return it. */
347
348 static struct target_desc *
349 tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
350 void *fetcher_baton)
351 {
352 struct cleanup *back_to, *result_cleanup;
353 struct gdb_xml_parser *parser;
354 struct tdesc_parsing_data data;
355 struct tdesc_xml_cache *cache;
356 char *expanded_text;
357 int ix;
358
359 /* Expand all XInclude directives. */
360 expanded_text = xml_process_xincludes (_("target description"),
361 document, fetcher, fetcher_baton, 0);
362 if (expanded_text == NULL)
363 {
364 warning (_("Could not load XML target description; ignoring"));
365 return NULL;
366 }
367
368 /* Check for an exact match in the list of descriptions we have
369 previously parsed. strcmp is a slightly inefficient way to
370 do this; an SHA-1 checksum would work as well. */
371 for (ix = 0; VEC_iterate (tdesc_xml_cache_s, xml_cache, ix, cache); ix++)
372 if (strcmp (cache->xml_document, expanded_text) == 0)
373 {
374 xfree (expanded_text);
375 return cache->tdesc;
376 }
377
378 back_to = make_cleanup (null_cleanup, NULL);
379 parser = gdb_xml_create_parser_and_cleanup (_("target description"),
380 tdesc_elements, &data);
381 gdb_xml_use_dtd (parser, "gdb-target.dtd");
382
383 memset (&data, 0, sizeof (struct tdesc_parsing_data));
384 data.tdesc = allocate_target_description ();
385 result_cleanup = make_cleanup_free_target_description (data.tdesc);
386 make_cleanup (xfree, expanded_text);
387
388 if (gdb_xml_parse (parser, expanded_text) == 0)
389 {
390 /* Parsed successfully. */
391 struct tdesc_xml_cache new_cache;
392
393 new_cache.xml_document = expanded_text;
394 new_cache.tdesc = data.tdesc;
395 VEC_safe_push (tdesc_xml_cache_s, xml_cache, &new_cache);
396 discard_cleanups (result_cleanup);
397 do_cleanups (back_to);
398 return data.tdesc;
399 }
400 else
401 {
402 warning (_("Could not load XML target description; ignoring"));
403 do_cleanups (back_to);
404 return NULL;
405 }
406 }
407 #endif /* HAVE_LIBEXPAT */
408 \f
409
410 /* Close FILE. */
411
412 static void
413 do_cleanup_fclose (void *file)
414 {
415 fclose (file);
416 }
417
418 /* Open FILENAME, read all its text into memory, close it, and return
419 the text. If something goes wrong, return NULL and warn. */
420
421 static char *
422 fetch_xml_from_file (const char *filename, void *baton)
423 {
424 const char *dirname = baton;
425 FILE *file;
426 struct cleanup *back_to;
427 char *text;
428 size_t len, offset;
429
430 if (dirname && *dirname)
431 {
432 char *fullname = concat (dirname, "/", filename, NULL);
433 if (fullname == NULL)
434 nomem (0);
435 file = fopen (fullname, FOPEN_RT);
436 xfree (fullname);
437 }
438 else
439 file = fopen (filename, FOPEN_RT);
440
441 if (file == NULL)
442 return NULL;
443
444 back_to = make_cleanup (do_cleanup_fclose, file);
445
446 /* Read in the whole file, one chunk at a time. */
447 len = 4096;
448 offset = 0;
449 text = xmalloc (len);
450 make_cleanup (free_current_contents, &text);
451 while (1)
452 {
453 size_t bytes_read;
454
455 /* Continue reading where the last read left off. Leave at least
456 one byte so that we can NUL-terminate the result. */
457 bytes_read = fread (text + offset, 1, len - offset - 1, file);
458 if (ferror (file))
459 {
460 warning (_("Read error from \"%s\""), filename);
461 do_cleanups (back_to);
462 return NULL;
463 }
464
465 offset += bytes_read;
466
467 if (feof (file))
468 break;
469
470 len = len * 2;
471 text = xrealloc (text, len);
472 }
473
474 fclose (file);
475 discard_cleanups (back_to);
476
477 text[offset] = '\0';
478 return text;
479 }
480
481 /* Read an XML target description from FILENAME. Parse it, and return
482 the parsed description. */
483
484 const struct target_desc *
485 file_read_description_xml (const char *filename)
486 {
487 struct target_desc *tdesc;
488 char *tdesc_str;
489 struct cleanup *back_to;
490 const char *base;
491 char *dirname;
492
493 tdesc_str = fetch_xml_from_file (filename, NULL);
494 if (tdesc_str == NULL)
495 {
496 warning (_("Could not open \"%s\""), filename);
497 return NULL;
498 }
499
500 back_to = make_cleanup (xfree, tdesc_str);
501
502 /* Simple, portable version of dirname that does not modify its
503 argument. */
504 base = lbasename (filename);
505 while (base > filename && IS_DIR_SEPARATOR (base[-1]))
506 --base;
507 if (base > filename)
508 {
509 dirname = xmalloc (base - filename + 2);
510 memcpy (dirname, filename, base - filename);
511
512 /* On DOS based file systems, convert "d:foo" to "d:.", so that
513 we create "d:./bar" later instead of the (different)
514 "d:/bar". */
515 if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
516 && !IS_DIR_SEPARATOR (filename[0]))
517 dirname[base++ - filename] = '.';
518
519 dirname[base - filename] = '\0';
520 make_cleanup (xfree, dirname);
521 }
522 else
523 dirname = NULL;
524
525 tdesc = tdesc_parse_xml (tdesc_str, fetch_xml_from_file, dirname);
526 do_cleanups (back_to);
527
528 return tdesc;
529 }
530
531 /* Read a string representation of available features from the target,
532 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
533 malloc allocated and NUL-terminated. NAME should be a non-NULL
534 string identifying the XML document we want; the top level document
535 is "target.xml". Other calls may be performed for the DTD or
536 for <xi:include>. */
537
538 static char *
539 fetch_available_features_from_target (const char *name, void *baton_)
540 {
541 struct target_ops *ops = baton_;
542
543 /* Read this object as a string. This ensures that a NUL
544 terminator is added. */
545 return target_read_stralloc (ops,
546 TARGET_OBJECT_AVAILABLE_FEATURES,
547 name);
548 }
549 \f
550
551 /* Read an XML target description using OPS. Parse it, and return the
552 parsed description. */
553
554 const struct target_desc *
555 target_read_description_xml (struct target_ops *ops)
556 {
557 struct target_desc *tdesc;
558 char *tdesc_str;
559 struct cleanup *back_to;
560
561 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
562 if (tdesc_str == NULL)
563 return NULL;
564
565 back_to = make_cleanup (xfree, tdesc_str);
566 tdesc = tdesc_parse_xml (tdesc_str,
567 fetch_available_features_from_target,
568 ops);
569 do_cleanups (back_to);
570
571 return tdesc;
572 }
This page took 0.059434 seconds and 5 git commands to generate.