Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / ft32-tdep.c
blobc5a48bebe5f23a68227fd8b47e3c7f6a204fe4af
1 /* Target-dependent code for FT32.
3 Copyright (C) 2009-2024 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 "frame.h"
21 #include "frame-unwind.h"
22 #include "frame-base.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "value.h"
28 #include "inferior.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "osabi.h"
32 #include "language.h"
33 #include "arch-utils.h"
34 #include "regcache.h"
35 #include "trad-frame.h"
36 #include "dis-asm.h"
37 #include "record.h"
39 #include "opcode/ft32.h"
41 #include "ft32-tdep.h"
42 #include "sim/sim-ft32.h"
43 #include <algorithm>
45 #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
47 /* Use an invalid address -1 as 'not available' marker. */
48 enum { REG_UNAVAIL = (CORE_ADDR) (-1) };
50 struct ft32_frame_cache
52 /* Base address of the frame */
53 CORE_ADDR base;
54 /* Function this frame belongs to */
55 CORE_ADDR pc;
56 /* Total size of this frame */
57 LONGEST framesize;
58 /* Saved registers in this frame */
59 CORE_ADDR saved_regs[FT32_NUM_REGS];
60 /* Saved SP in this frame */
61 CORE_ADDR saved_sp;
62 /* Has the new frame been LINKed. */
63 bfd_boolean established;
66 /* Implement the "frame_align" gdbarch method. */
68 static CORE_ADDR
69 ft32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
71 /* Align to the size of an instruction (so that they can safely be
72 pushed onto the stack. */
73 return sp & ~1;
77 constexpr gdb_byte ft32_break_insn[] = { 0x02, 0x00, 0x34, 0x00 };
79 typedef BP_MANIPULATION (ft32_break_insn) ft32_breakpoint;
81 /* FT32 register names. */
83 static const char *const ft32_register_names[] =
85 "fp", "sp",
86 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
87 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
88 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
89 "r24", "r25", "r26", "r27", "r28", "cc",
90 "pc"
93 /* Implement the "register_name" gdbarch method. */
95 static const char *
96 ft32_register_name (struct gdbarch *gdbarch, int reg_nr)
98 static_assert (ARRAY_SIZE (ft32_register_names) == FT32_NUM_REGS);
99 return ft32_register_names[reg_nr];
102 /* Implement the "register_type" gdbarch method. */
104 static struct type *
105 ft32_register_type (struct gdbarch *gdbarch, int reg_nr)
107 if (reg_nr == FT32_PC_REGNUM)
109 ft32_gdbarch_tdep *tdep = gdbarch_tdep<ft32_gdbarch_tdep> (gdbarch);
110 return tdep->pc_type;
112 else if (reg_nr == FT32_SP_REGNUM || reg_nr == FT32_FP_REGNUM)
113 return builtin_type (gdbarch)->builtin_data_ptr;
114 else
115 return builtin_type (gdbarch)->builtin_int32;
118 /* Write into appropriate registers a function return value
119 of type TYPE, given in virtual format. */
121 static void
122 ft32_store_return_value (struct type *type, struct regcache *regcache,
123 const gdb_byte *valbuf)
125 struct gdbarch *gdbarch = regcache->arch ();
126 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
127 CORE_ADDR regval;
128 int len = type->length ();
130 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
131 regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
132 regcache_cooked_write_unsigned (regcache, FT32_R0_REGNUM, regval);
133 if (len > 4)
135 regval = extract_unsigned_integer (valbuf + 4,
136 len - 4, byte_order);
137 regcache_cooked_write_unsigned (regcache, FT32_R1_REGNUM, regval);
141 /* Fetch a single 32-bit instruction from address a. If memory contains
142 a compressed instruction pair, return the expanded instruction. */
144 static ULONGEST
145 ft32_fetch_instruction (CORE_ADDR a, int *isize,
146 enum bfd_endian byte_order)
148 unsigned int sc[2];
149 ULONGEST inst;
151 CORE_ADDR a4 = a & ~3;
152 inst = read_code_unsigned_integer (a4, 4, byte_order);
153 *isize = ft32_decode_shortcode (a4, inst, sc) ? 2 : 4;
154 if (*isize == 2)
155 return sc[1 & (a >> 1)];
156 else
157 return inst;
160 /* Decode the instructions within the given address range. Decide
161 when we must have reached the end of the function prologue. If a
162 frame_info pointer is provided, fill in its saved_regs etc.
164 Returns the address of the first instruction after the prologue. */
166 static CORE_ADDR
167 ft32_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
168 struct ft32_frame_cache *cache,
169 struct gdbarch *gdbarch)
171 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
172 CORE_ADDR next_addr;
173 ULONGEST inst;
174 int isize = 0;
175 int regnum, pushreg;
176 struct bound_minimal_symbol msymbol;
177 const int first_saved_reg = 13; /* The first saved register. */
178 /* PROLOGS are addresses of the subroutine prologs, PROLOGS[n]
179 is the address of __prolog_$rN.
180 __prolog_$rN pushes registers from 13 through n inclusive.
181 So for example CALL __prolog_$r15 is equivalent to:
182 PUSH $r13
183 PUSH $r14
184 PUSH $r15
185 Note that PROLOGS[0] through PROLOGS[12] are unused. */
186 CORE_ADDR prologs[32];
188 cache->saved_regs[FT32_PC_REGNUM] = 0;
189 cache->framesize = 0;
191 for (regnum = first_saved_reg; regnum < 32; regnum++)
193 char prolog_symbol[32];
195 snprintf (prolog_symbol, sizeof (prolog_symbol), "__prolog_$r%02d",
196 regnum);
197 msymbol = lookup_minimal_symbol (prolog_symbol, NULL, NULL);
198 if (msymbol.minsym)
199 prologs[regnum] = msymbol.value_address ();
200 else
201 prologs[regnum] = 0;
204 if (start_addr >= end_addr)
205 return end_addr;
207 cache->established = 0;
208 for (next_addr = start_addr; next_addr < end_addr; next_addr += isize)
210 inst = ft32_fetch_instruction (next_addr, &isize, byte_order);
212 if (FT32_IS_PUSH (inst))
214 pushreg = FT32_PUSH_REG (inst);
215 cache->framesize += 4;
216 cache->saved_regs[FT32_R0_REGNUM + pushreg] = cache->framesize;
218 else if (FT32_IS_CALL (inst))
220 for (regnum = first_saved_reg; regnum < 32; regnum++)
222 if ((4 * (inst & 0x3ffff)) == prologs[regnum])
224 for (pushreg = first_saved_reg; pushreg <= regnum;
225 pushreg++)
227 cache->framesize += 4;
228 cache->saved_regs[FT32_R0_REGNUM + pushreg] =
229 cache->framesize;
233 break;
235 else
236 break;
238 for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++)
240 if (cache->saved_regs[regnum] != REG_UNAVAIL)
241 cache->saved_regs[regnum] =
242 cache->framesize - cache->saved_regs[regnum];
244 cache->saved_regs[FT32_PC_REGNUM] = cache->framesize;
246 /* It is a LINK? */
247 if (next_addr < end_addr)
249 inst = ft32_fetch_instruction (next_addr, &isize, byte_order);
250 if (FT32_IS_LINK (inst))
252 cache->established = 1;
253 for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++)
255 if (cache->saved_regs[regnum] != REG_UNAVAIL)
256 cache->saved_regs[regnum] += 4;
258 cache->saved_regs[FT32_PC_REGNUM] = cache->framesize + 4;
259 cache->saved_regs[FT32_FP_REGNUM] = 0;
260 cache->framesize += FT32_LINK_SIZE (inst);
261 next_addr += isize;
265 return next_addr;
268 /* Find the end of function prologue. */
270 static CORE_ADDR
271 ft32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
273 CORE_ADDR func_addr = 0, func_end = 0;
274 const char *func_name;
276 /* See if we can determine the end of the prologue via the symbol table.
277 If so, then return either PC, or the PC after the prologue, whichever
278 is greater. */
279 if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
281 CORE_ADDR post_prologue_pc
282 = skip_prologue_using_sal (gdbarch, func_addr);
283 if (post_prologue_pc != 0)
284 return std::max (pc, post_prologue_pc);
285 else
287 /* Can't determine prologue from the symbol table, need to examine
288 instructions. */
289 struct symtab_and_line sal;
290 struct symbol *sym;
291 struct ft32_frame_cache cache;
292 CORE_ADDR plg_end;
294 memset (&cache, 0, sizeof cache);
296 plg_end = ft32_analyze_prologue (func_addr,
297 func_end, &cache, gdbarch);
298 /* Found a function. */
299 sym = lookup_symbol (func_name, nullptr, SEARCH_FUNCTION_DOMAIN,
300 nullptr).symbol;
301 /* Don't use line number debug info for assembly source files. */
302 if ((sym != NULL) && sym->language () != language_asm)
304 sal = find_pc_line (func_addr, 0);
305 if (sal.end && sal.end < func_end)
307 /* Found a line number, use it as end of prologue. */
308 return sal.end;
311 /* No useable line symbol. Use result of prologue parsing method. */
312 return plg_end;
316 /* No function symbol -- just return the PC. */
317 return pc;
320 /* Implementation of `pointer_to_address' gdbarch method.
322 On FT32 address space zero is RAM, address space 1 is flash.
323 RAM appears at address RAM_BIAS, flash at address 0. */
325 static CORE_ADDR
326 ft32_pointer_to_address (struct gdbarch *gdbarch,
327 struct type *type, const gdb_byte *buf)
329 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
330 CORE_ADDR addr
331 = extract_unsigned_integer (buf, type->length (), byte_order);
333 if (TYPE_ADDRESS_CLASS_1 (type))
334 return addr;
335 else
336 return addr | RAM_BIAS;
339 /* Implementation of `address_class_type_flags' gdbarch method.
341 This method maps DW_AT_address_class attributes to a
342 type_instance_flag_value. */
344 static type_instance_flags
345 ft32_address_class_type_flags (int byte_size, int dwarf2_addr_class)
347 /* The value 1 of the DW_AT_address_class attribute corresponds to the
348 __flash__ qualifier, meaning pointer to data in FT32 program memory.
350 if (dwarf2_addr_class == 1)
351 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
352 return 0;
355 /* Implementation of `address_class_type_flags_to_name' gdbarch method.
357 Convert a type_instance_flag_value to an address space qualifier. */
359 static const char*
360 ft32_address_class_type_flags_to_name (struct gdbarch *gdbarch,
361 type_instance_flags type_flags)
363 if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
364 return "flash";
365 else
366 return NULL;
369 /* Implementation of `address_class_name_to_type_flags' gdbarch method.
371 Convert an address space qualifier to a type_instance_flag_value. */
373 static bool
374 ft32_address_class_name_to_type_flags (struct gdbarch *gdbarch,
375 const char* name,
376 type_instance_flags *type_flags_ptr)
378 if (strcmp (name, "flash") == 0)
380 *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
381 return true;
383 else
384 return false;
387 /* Given a return value in `regbuf' with a type `valtype',
388 extract and copy its value into `valbuf'. */
390 static void
391 ft32_extract_return_value (struct type *type, struct regcache *regcache,
392 gdb_byte *dst)
394 struct gdbarch *gdbarch = regcache->arch ();
395 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
396 bfd_byte *valbuf = dst;
397 int len = type->length ();
398 ULONGEST tmp;
400 /* By using store_unsigned_integer we avoid having to do
401 anything special for small big-endian values. */
402 regcache_cooked_read_unsigned (regcache, FT32_R0_REGNUM, &tmp);
403 store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp);
405 /* Ignore return values more than 8 bytes in size because the ft32
406 returns anything more than 8 bytes in the stack. */
407 if (len > 4)
409 regcache_cooked_read_unsigned (regcache, FT32_R1_REGNUM, &tmp);
410 store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
414 /* Implement the "return_value" gdbarch method. */
416 static enum return_value_convention
417 ft32_return_value (struct gdbarch *gdbarch, struct value *function,
418 struct type *valtype, struct regcache *regcache,
419 gdb_byte *readbuf, const gdb_byte *writebuf)
421 if (valtype->length () > 8)
422 return RETURN_VALUE_STRUCT_CONVENTION;
423 else
425 if (readbuf != NULL)
426 ft32_extract_return_value (valtype, regcache, readbuf);
427 if (writebuf != NULL)
428 ft32_store_return_value (valtype, regcache, writebuf);
429 return RETURN_VALUE_REGISTER_CONVENTION;
433 /* Allocate and initialize a ft32_frame_cache object. */
435 static struct ft32_frame_cache *
436 ft32_alloc_frame_cache (void)
438 struct ft32_frame_cache *cache;
439 int i;
441 cache = FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache);
443 for (i = 0; i < FT32_NUM_REGS; ++i)
444 cache->saved_regs[i] = REG_UNAVAIL;
446 return cache;
449 /* Populate a ft32_frame_cache object for this_frame. */
451 static struct ft32_frame_cache *
452 ft32_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
454 struct ft32_frame_cache *cache;
455 CORE_ADDR current_pc;
456 int i;
458 if (*this_cache)
459 return (struct ft32_frame_cache *) *this_cache;
461 cache = ft32_alloc_frame_cache ();
462 *this_cache = cache;
464 cache->base = get_frame_register_unsigned (this_frame, FT32_FP_REGNUM);
465 if (cache->base == 0)
466 return cache;
468 cache->pc = get_frame_func (this_frame);
469 current_pc = get_frame_pc (this_frame);
470 if (cache->pc)
472 struct gdbarch *gdbarch = get_frame_arch (this_frame);
474 ft32_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
475 if (!cache->established)
476 cache->base = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM);
479 cache->saved_sp = cache->base - 4;
481 for (i = 0; i < FT32_NUM_REGS; ++i)
482 if (cache->saved_regs[i] != REG_UNAVAIL)
483 cache->saved_regs[i] = cache->base + cache->saved_regs[i];
485 return cache;
488 /* Given a GDB frame, determine the address of the calling function's
489 frame. This will be used to create a new GDB frame struct. */
491 static void
492 ft32_frame_this_id (const frame_info_ptr &this_frame,
493 void **this_prologue_cache, struct frame_id *this_id)
495 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
496 this_prologue_cache);
498 /* This marks the outermost frame. */
499 if (cache->base == 0)
500 return;
502 *this_id = frame_id_build (cache->saved_sp, cache->pc);
505 /* Get the value of register regnum in the previous stack frame. */
507 static struct value *
508 ft32_frame_prev_register (const frame_info_ptr &this_frame,
509 void **this_prologue_cache, int regnum)
511 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
512 this_prologue_cache);
514 gdb_assert (regnum >= 0);
516 if (regnum == FT32_SP_REGNUM && cache->saved_sp)
517 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
519 if (regnum < FT32_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
520 return frame_unwind_got_memory (this_frame, regnum,
521 RAM_BIAS | cache->saved_regs[regnum]);
523 return frame_unwind_got_register (this_frame, regnum, regnum);
526 static const struct frame_unwind ft32_frame_unwind =
528 "ft32 prologue",
529 NORMAL_FRAME,
530 default_frame_unwind_stop_reason,
531 ft32_frame_this_id,
532 ft32_frame_prev_register,
533 NULL,
534 default_frame_sniffer
537 /* Return the base address of this_frame. */
539 static CORE_ADDR
540 ft32_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
542 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
543 this_cache);
545 return cache->base;
548 static const struct frame_base ft32_frame_base =
550 &ft32_frame_unwind,
551 ft32_frame_base_address,
552 ft32_frame_base_address,
553 ft32_frame_base_address
556 /* Allocate and initialize the ft32 gdbarch object. */
558 static struct gdbarch *
559 ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
561 struct type *void_type;
562 struct type *func_void_type;
564 /* If there is already a candidate, use it. */
565 arches = gdbarch_list_lookup_by_info (arches, &info);
566 if (arches != NULL)
567 return arches->gdbarch;
569 /* Allocate space for the new architecture. */
570 gdbarch *gdbarch
571 = gdbarch_alloc (&info, gdbarch_tdep_up (new ft32_gdbarch_tdep));
572 ft32_gdbarch_tdep *tdep = gdbarch_tdep<ft32_gdbarch_tdep> (gdbarch);
574 /* Create a type for PC. We can't use builtin types here, as they may not
575 be defined. */
576 type_allocator alloc (gdbarch);
577 void_type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
578 func_void_type = make_function_type (void_type, NULL);
579 tdep->pc_type = init_pointer_type (alloc, 4 * TARGET_CHAR_BIT, NULL,
580 func_void_type);
581 tdep->pc_type->set_instance_flags (tdep->pc_type->instance_flags ()
582 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1);
584 set_gdbarch_num_regs (gdbarch, FT32_NUM_REGS);
585 set_gdbarch_sp_regnum (gdbarch, FT32_SP_REGNUM);
586 set_gdbarch_pc_regnum (gdbarch, FT32_PC_REGNUM);
587 set_gdbarch_register_name (gdbarch, ft32_register_name);
588 set_gdbarch_register_type (gdbarch, ft32_register_type);
590 set_gdbarch_return_value (gdbarch, ft32_return_value);
592 set_gdbarch_pointer_to_address (gdbarch, ft32_pointer_to_address);
594 set_gdbarch_skip_prologue (gdbarch, ft32_skip_prologue);
595 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
596 set_gdbarch_breakpoint_kind_from_pc (gdbarch, ft32_breakpoint::kind_from_pc);
597 set_gdbarch_sw_breakpoint_from_kind (gdbarch, ft32_breakpoint::bp_from_kind);
598 set_gdbarch_frame_align (gdbarch, ft32_frame_align);
600 frame_base_set_default (gdbarch, &ft32_frame_base);
602 /* Hook in ABI-specific overrides, if they have been registered. */
603 gdbarch_init_osabi (info, gdbarch);
605 /* Hook in the default unwinders. */
606 frame_unwind_append_unwinder (gdbarch, &ft32_frame_unwind);
608 /* Support simple overlay manager. */
609 set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
611 set_gdbarch_address_class_type_flags (gdbarch, ft32_address_class_type_flags);
612 set_gdbarch_address_class_name_to_type_flags
613 (gdbarch, ft32_address_class_name_to_type_flags);
614 set_gdbarch_address_class_type_flags_to_name
615 (gdbarch, ft32_address_class_type_flags_to_name);
617 return gdbarch;
620 /* Register this machine's init routine. */
622 void _initialize_ft32_tdep ();
623 void
624 _initialize_ft32_tdep ()
626 gdbarch_register (bfd_arch_ft32, ft32_gdbarch_init);