Fix: A potential null_pointer_deference bug
[binutils-gdb.git] / gdbsupport / tdesc.cc
blob6ecb3232a867fbd4ff04361eb13e0d8cfc1472fa
1 /* Target description support for GDB.
3 Copyright (C) 2018-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
21 #include "gdbsupport/tdesc.h"
23 tdesc_reg::tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
24 int regnum, int save_restore_, const char *group_,
25 int bitsize_, const char *type_)
26 : name (name_), target_regnum (regnum),
27 save_restore (save_restore_),
28 group (group_ != NULL ? group_ : ""),
29 bitsize (bitsize_),
30 type (type_ != NULL ? type_ : "<unknown>")
32 /* If the register's type is target-defined, look it up now. We may not
33 have easy access to the containing feature when we want it later. */
34 tdesc_type = tdesc_named_type (feature, type.c_str ());
37 /* Predefined types. */
38 static tdesc_type_builtin tdesc_predefined_types[] =
40 { "bool", TDESC_TYPE_BOOL },
41 { "int8", TDESC_TYPE_INT8 },
42 { "int16", TDESC_TYPE_INT16 },
43 { "int32", TDESC_TYPE_INT32 },
44 { "int64", TDESC_TYPE_INT64 },
45 { "int128", TDESC_TYPE_INT128 },
46 { "uint8", TDESC_TYPE_UINT8 },
47 { "uint16", TDESC_TYPE_UINT16 },
48 { "uint32", TDESC_TYPE_UINT32 },
49 { "uint64", TDESC_TYPE_UINT64 },
50 { "uint128", TDESC_TYPE_UINT128 },
51 { "code_ptr", TDESC_TYPE_CODE_PTR },
52 { "data_ptr", TDESC_TYPE_DATA_PTR },
53 { "ieee_half", TDESC_TYPE_IEEE_HALF },
54 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
55 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
56 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
57 { "i387_ext", TDESC_TYPE_I387_EXT },
58 { "bfloat16", TDESC_TYPE_BFLOAT16 }
61 void tdesc_feature::accept (tdesc_element_visitor &v) const
63 v.visit_pre (this);
65 for (const tdesc_type_up &type : types)
66 type->accept (v);
68 for (const tdesc_reg_up &reg : registers)
69 reg->accept (v);
71 v.visit_post (this);
74 bool tdesc_feature::operator== (const tdesc_feature &other) const
76 if (name != other.name)
77 return false;
79 if (registers.size () != other.registers.size ())
80 return false;
82 for (int ix = 0; ix < registers.size (); ix++)
84 const tdesc_reg_up &reg1 = registers[ix];
85 const tdesc_reg_up &reg2 = other.registers[ix];
87 if (reg1 != reg2 && *reg1 != *reg2)
88 return false;
91 if (types.size () != other.types.size ())
92 return false;
94 for (int ix = 0; ix < types.size (); ix++)
96 const tdesc_type_up &type1 = types[ix];
97 const tdesc_type_up &type2 = other.types[ix];
99 if (type1 != type2 && *type1 != *type2)
100 return false;
103 return true;
106 /* Lookup a predefined type. */
108 static struct tdesc_type *
109 tdesc_predefined_type (enum tdesc_type_kind kind)
111 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
112 if (tdesc_predefined_types[ix].kind == kind)
113 return &tdesc_predefined_types[ix];
115 gdb_assert_not_reached ("bad predefined tdesc type");
118 /* See gdbsupport/tdesc.h. */
120 struct tdesc_type *
121 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
123 /* First try target-defined types. */
124 for (const tdesc_type_up &type : feature->types)
125 if (type->name == id)
126 return type.get ();
128 /* Next try the predefined types. */
129 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
130 if (tdesc_predefined_types[ix].name == id)
131 return &tdesc_predefined_types[ix];
133 return NULL;
136 /* See gdbsupport/tdesc.h. */
138 void
139 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
140 int regnum, int save_restore, const char *group,
141 int bitsize, const char *type)
143 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
144 group, bitsize, type);
146 feature->registers.emplace_back (reg);
149 /* See gdbsupport/tdesc.h. */
151 struct tdesc_type *
152 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
153 struct tdesc_type *field_type, int count)
155 tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
156 feature->types.emplace_back (type);
158 return type;
161 /* See gdbsupport/tdesc.h. */
163 tdesc_type_with_fields *
164 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
166 tdesc_type_with_fields *type
167 = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
168 feature->types.emplace_back (type);
170 return type;
173 /* See gdbsupport/tdesc.h. */
175 void
176 tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
178 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
179 gdb_assert (size > 0);
180 type->size = size;
183 /* See gdbsupport/tdesc.h. */
185 tdesc_type_with_fields *
186 tdesc_create_union (struct tdesc_feature *feature, const char *name)
188 tdesc_type_with_fields *type
189 = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
190 feature->types.emplace_back (type);
192 return type;
195 /* See gdbsupport/tdesc.h. */
197 tdesc_type_with_fields *
198 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
199 int size)
201 gdb_assert (size > 0);
203 tdesc_type_with_fields *type
204 = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
205 feature->types.emplace_back (type);
207 return type;
210 /* See gdbsupport/tdesc.h. */
212 tdesc_type_with_fields *
213 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
214 int size)
216 gdb_assert (size > 0);
218 tdesc_type_with_fields *type
219 = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
220 feature->types.emplace_back (type);
222 return type;
225 /* See gdbsupport/tdesc.h. */
227 void
228 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
229 struct tdesc_type *field_type)
231 gdb_assert (type->kind == TDESC_TYPE_UNION
232 || type->kind == TDESC_TYPE_STRUCT);
234 /* Initialize start and end so we know this is not a bit-field
235 when we print-c-tdesc. */
236 type->fields.emplace_back (field_name, field_type, -1, -1);
239 /* See gdbsupport/tdesc.h. */
241 void
242 tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
243 int start, int end, struct tdesc_type *field_type)
245 gdb_assert (type->kind == TDESC_TYPE_STRUCT
246 || type->kind == TDESC_TYPE_FLAGS);
247 gdb_assert (start >= 0 && end >= start);
249 type->fields.emplace_back (field_name, field_type, start, end);
252 /* See gdbsupport/tdesc.h. */
254 void
255 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
256 int start, int end)
258 struct tdesc_type *field_type;
260 gdb_assert (start >= 0 && end >= start);
262 if (type->size > 4)
263 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
264 else
265 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
267 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
270 /* See gdbsupport/tdesc.h. */
272 void
273 tdesc_add_flag (tdesc_type_with_fields *type, int start,
274 const char *flag_name)
276 gdb_assert (type->kind == TDESC_TYPE_FLAGS
277 || type->kind == TDESC_TYPE_STRUCT);
279 type->fields.emplace_back (flag_name,
280 tdesc_predefined_type (TDESC_TYPE_BOOL),
281 start, start);
284 /* See gdbsupport/tdesc.h. */
286 void
287 tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
288 const char *name)
290 gdb_assert (type->kind == TDESC_TYPE_ENUM);
291 type->fields.emplace_back (name,
292 tdesc_predefined_type (TDESC_TYPE_INT32),
293 value, -1);
296 void print_xml_feature::visit_pre (const tdesc_feature *e)
298 add_line ("<feature name=\"%s\">", e->name.c_str ());
299 indent (1);
302 void print_xml_feature::visit_post (const tdesc_feature *e)
304 indent (-1);
305 add_line ("</feature>");
308 void print_xml_feature::visit (const tdesc_type_builtin *t)
310 error (_("xml output is not supported for type \"%s\"."), t->name.c_str ());
313 void print_xml_feature::visit (const tdesc_type_vector *t)
315 add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
316 t->name.c_str (), t->element_type->name.c_str (), t->count);
319 void print_xml_feature::visit (const tdesc_type_with_fields *t)
321 const static char *types[] = { "struct", "union", "flags", "enum" };
323 gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM);
325 std::string tmp;
327 string_appendf (tmp,
328 "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT],
329 t->name.c_str ());
331 switch (t->kind)
333 case TDESC_TYPE_STRUCT:
334 case TDESC_TYPE_FLAGS:
335 if (t->size > 0)
336 string_appendf (tmp, " size=\"%d\"", t->size);
337 string_appendf (tmp, ">");
338 add_line (tmp);
340 for (const tdesc_type_field &f : t->fields)
342 tmp.clear ();
343 string_appendf (tmp, " <field name=\"%s\"", f.name.c_str ());
344 if (f.start != -1)
345 string_appendf (tmp, " start=\"%d\" end=\"%d\"", f.start,
346 f.end);
347 string_appendf (tmp, " type=\"%s\"/>",
348 f.type->name.c_str ());
349 add_line (tmp);
351 break;
353 case TDESC_TYPE_ENUM:
354 if (t->size > 0)
355 string_appendf (tmp, " size=\"%d\"", t->size);
356 string_appendf (tmp, ">");
357 add_line (tmp);
358 /* The 'start' of the field is reused as the enum value. The 'end'
359 of the field is always set to -1 for enum values. */
360 for (const tdesc_type_field &f : t->fields)
361 add_line (" <evalue name=\"%s\" value=\"%d\"/>",
362 f.name.c_str (), f.start);
363 break;
365 case TDESC_TYPE_UNION:
366 string_appendf (tmp, ">");
367 add_line (tmp);
368 for (const tdesc_type_field &f : t->fields)
369 add_line (" <field name=\"%s\" type=\"%s\"/>",
370 f.name.c_str (), f.type->name.c_str ());
371 break;
373 default:
374 error (_("xml output is not supported for type \"%s\"."),
375 t->name.c_str ());
378 add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]);
381 void print_xml_feature::visit (const tdesc_reg *r)
383 std::string tmp;
385 string_appendf (tmp,
386 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
387 r->name.c_str (), r->bitsize, r->type.c_str (),
388 r->target_regnum);
390 if (r->group.length () > 0)
391 string_appendf (tmp, " group=\"%s\"", r->group.c_str ());
393 if (r->save_restore == 0)
394 string_appendf (tmp, " save-restore=\"no\"");
396 string_appendf (tmp, "/>");
398 add_line (tmp);
401 void print_xml_feature::visit_pre (const target_desc *e)
403 #ifndef IN_PROCESS_AGENT
404 add_line ("<?xml version=\"1.0\"?>");
405 add_line ("<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
406 add_line ("<target>");
407 indent (1);
408 if (tdesc_architecture_name (e))
409 add_line ("<architecture>%s</architecture>",
410 tdesc_architecture_name (e));
412 const char *osabi = tdesc_osabi_name (e);
413 if (osabi != nullptr)
414 add_line ("<osabi>%s</osabi>", osabi);
416 const std::vector<tdesc_compatible_info_up> &compatible_list
417 = tdesc_compatible_info_list (e);
418 for (const auto &c : compatible_list)
419 add_line ("<compatible>%s</compatible>",
420 tdesc_compatible_info_arch_name (c));
421 #endif
424 void print_xml_feature::visit_post (const target_desc *e)
426 indent (-1);
427 add_line ("</target>");
430 /* See gdbsupport/tdesc.h. */
432 void
433 print_xml_feature::add_line (const std::string &str)
435 string_appendf (*m_buffer, "%*s", m_depth, "");
436 string_appendf (*m_buffer, "%s", str.c_str ());
437 string_appendf (*m_buffer, "\n");
440 /* See gdbsupport/tdesc.h. */
442 void
443 print_xml_feature::add_line (const char *fmt, ...)
445 std::string tmp;
447 va_list ap;
448 va_start (ap, fmt);
449 string_vappendf (tmp, fmt, ap);
450 va_end (ap);
451 add_line (tmp);