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