Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / arc-tdep.c
1 /* Target dependent code for ARC architecture, for GDB.
2
3 Copyright 2005-2021 Free Software Foundation, Inc.
4 Contributed by Synopsys Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* GDB header files. */
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "elf-bfd.h"
25 #include "disasm.h"
26 #include "dwarf2/frame.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
29 #include "gdbcore.h"
30 #include "reggroups.h"
31 #include "gdbcmd.h"
32 #include "objfiles.h"
33 #include "osabi.h"
34 #include "prologue-value.h"
35 #include "target-descriptions.h"
36 #include "trad-frame.h"
37
38 /* ARC header files. */
39 #include "opcode/arc.h"
40 #include "opcodes/arc-dis.h"
41 #include "arc-tdep.h"
42 #include "arch/arc.h"
43
44 /* Standard headers. */
45 #include <algorithm>
46 #include <sstream>
47
48 /* The frame unwind cache for ARC. */
49
50 struct arc_frame_cache
51 {
52 /* The stack pointer at the time this frame was created; i.e. the caller's
53 stack pointer when this function was called. It is used to identify this
54 frame. */
55 CORE_ADDR prev_sp;
56
57 /* Register that is a base for this frame - FP for normal frame, SP for
58 non-FP frames. */
59 int frame_base_reg;
60
61 /* Offset from the previous SP to the current frame base. If GCC uses
62 `SUB SP,SP,offset` to allocate space for local variables, then it will be
63 done after setting up a frame pointer, but it still will be considered
64 part of prologue, therefore SP will be lesser than FP at the end of the
65 prologue analysis. In this case that would be an offset from old SP to a
66 new FP. But in case of non-FP frames, frame base is an SP and thus that
67 would be an offset from old SP to new SP. What is important is that this
68 is an offset from old SP to a known register, so it can be used to find
69 old SP.
70
71 Using FP is preferable, when possible, because SP can change in function
72 body after prologue due to alloca, variadic arguments or other shenanigans.
73 If that is the case in the caller frame, then PREV_SP will point to SP at
74 the moment of function call, but it will be different from SP value at the
75 end of the caller prologue. As a result it will not be possible to
76 reconstruct caller's frame and go past it in the backtrace. Those things
77 are unlikely to happen to FP - FP value at the moment of function call (as
78 stored on stack in callee prologue) is also an FP value at the end of the
79 caller's prologue. */
80
81 LONGEST frame_base_offset;
82
83 /* Store addresses for registers saved in prologue. During prologue analysis
84 GDB stores offsets relatively to "old SP", then after old SP is evaluated,
85 offsets are replaced with absolute addresses. */
86 trad_frame_saved_reg *saved_regs;
87 };
88
89 /* Global debug flag. */
90
91 bool arc_debug;
92
93 /* List of "maintenance print arc" commands. */
94
95 static struct cmd_list_element *maintenance_print_arc_list = NULL;
96
97 /* A set of registers that we expect to find in a tdesc_feature. These
98 are used in ARC_TDESC_INIT when processing the target description. */
99
100 struct arc_register_feature
101 {
102 /* Information for a single register. */
103 struct register_info
104 {
105 /* The GDB register number for this register. */
106 int regnum;
107
108 /* List of names for this register. The first name in this list is the
109 preferred name, the name GDB will use when describing this register. */
110 std::vector<const char *> names;
111
112 /* When true, this register must be present in this feature set. */
113 bool required_p;
114 };
115
116 /* The name for this feature. This is the name used to find this feature
117 within the target description. */
118 const char *name;
119
120 /* List of all the registers that we expect to encounter in this register
121 set. */
122 std::vector<struct register_info> registers;
123 };
124
125 /* Obsolete feature names for backward compatibility. */
126 static const char *ARC_CORE_V1_OBSOLETE_FEATURE_NAME
127 = "org.gnu.gdb.arc.core.arcompact";
128 static const char *ARC_CORE_V2_OBSOLETE_FEATURE_NAME
129 = "org.gnu.gdb.arc.core.v2";
130 static const char *ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME
131 = "org.gnu.gdb.arc.core-reduced.v2";
132 static const char *ARC_AUX_OBSOLETE_FEATURE_NAME
133 = "org.gnu.gdb.arc.aux-minimal";
134 /* Modern feature names. */
135 static const char *ARC_CORE_FEATURE_NAME = "org.gnu.gdb.arc.core";
136 static const char *ARC_AUX_FEATURE_NAME = "org.gnu.gdb.arc.aux";
137
138 /* ARCv1 (ARC600, ARC601, ARC700) general core registers feature set.
139 See also arc_update_acc_reg_names() for "accl/acch" names. */
140
141 static struct arc_register_feature arc_v1_core_reg_feature =
142 {
143 ARC_CORE_FEATURE_NAME,
144 {
145 { ARC_R0_REGNUM + 0, { "r0" }, true },
146 { ARC_R0_REGNUM + 1, { "r1" }, true },
147 { ARC_R0_REGNUM + 2, { "r2" }, true },
148 { ARC_R0_REGNUM + 3, { "r3" }, true },
149 { ARC_R0_REGNUM + 4, { "r4" }, false },
150 { ARC_R0_REGNUM + 5, { "r5" }, false },
151 { ARC_R0_REGNUM + 6, { "r6" }, false },
152 { ARC_R0_REGNUM + 7, { "r7" }, false },
153 { ARC_R0_REGNUM + 8, { "r8" }, false },
154 { ARC_R0_REGNUM + 9, { "r9" }, false },
155 { ARC_R0_REGNUM + 10, { "r10" }, true },
156 { ARC_R0_REGNUM + 11, { "r11" }, true },
157 { ARC_R0_REGNUM + 12, { "r12" }, true },
158 { ARC_R0_REGNUM + 13, { "r13" }, true },
159 { ARC_R0_REGNUM + 14, { "r14" }, true },
160 { ARC_R0_REGNUM + 15, { "r15" }, true },
161 { ARC_R0_REGNUM + 16, { "r16" }, false },
162 { ARC_R0_REGNUM + 17, { "r17" }, false },
163 { ARC_R0_REGNUM + 18, { "r18" }, false },
164 { ARC_R0_REGNUM + 19, { "r19" }, false },
165 { ARC_R0_REGNUM + 20, { "r20" }, false },
166 { ARC_R0_REGNUM + 21, { "r21" }, false },
167 { ARC_R0_REGNUM + 22, { "r22" }, false },
168 { ARC_R0_REGNUM + 23, { "r23" }, false },
169 { ARC_R0_REGNUM + 24, { "r24" }, false },
170 { ARC_R0_REGNUM + 25, { "r25" }, false },
171 { ARC_R0_REGNUM + 26, { "gp" }, true },
172 { ARC_R0_REGNUM + 27, { "fp" }, true },
173 { ARC_R0_REGNUM + 28, { "sp" }, true },
174 { ARC_R0_REGNUM + 29, { "ilink1" }, false },
175 { ARC_R0_REGNUM + 30, { "ilink2" }, false },
176 { ARC_R0_REGNUM + 31, { "blink" }, true },
177 { ARC_R0_REGNUM + 32, { "r32" }, false },
178 { ARC_R0_REGNUM + 33, { "r33" }, false },
179 { ARC_R0_REGNUM + 34, { "r34" }, false },
180 { ARC_R0_REGNUM + 35, { "r35" }, false },
181 { ARC_R0_REGNUM + 36, { "r36" }, false },
182 { ARC_R0_REGNUM + 37, { "r37" }, false },
183 { ARC_R0_REGNUM + 38, { "r38" }, false },
184 { ARC_R0_REGNUM + 39, { "r39" }, false },
185 { ARC_R0_REGNUM + 40, { "r40" }, false },
186 { ARC_R0_REGNUM + 41, { "r41" }, false },
187 { ARC_R0_REGNUM + 42, { "r42" }, false },
188 { ARC_R0_REGNUM + 43, { "r43" }, false },
189 { ARC_R0_REGNUM + 44, { "r44" }, false },
190 { ARC_R0_REGNUM + 45, { "r45" }, false },
191 { ARC_R0_REGNUM + 46, { "r46" }, false },
192 { ARC_R0_REGNUM + 47, { "r47" }, false },
193 { ARC_R0_REGNUM + 48, { "r48" }, false },
194 { ARC_R0_REGNUM + 49, { "r49" }, false },
195 { ARC_R0_REGNUM + 50, { "r50" }, false },
196 { ARC_R0_REGNUM + 51, { "r51" }, false },
197 { ARC_R0_REGNUM + 52, { "r52" }, false },
198 { ARC_R0_REGNUM + 53, { "r53" }, false },
199 { ARC_R0_REGNUM + 54, { "r54" }, false },
200 { ARC_R0_REGNUM + 55, { "r55" }, false },
201 { ARC_R0_REGNUM + 56, { "r56" }, false },
202 { ARC_R0_REGNUM + 57, { "r57" }, false },
203 { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
204 { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
205 { ARC_R0_REGNUM + 60, { "lp_count" }, false },
206 { ARC_R0_REGNUM + 61, { "reserved" }, false },
207 { ARC_R0_REGNUM + 62, { "limm" }, false },
208 { ARC_R0_REGNUM + 63, { "pcl" }, true }
209 }
210 };
211
212 /* ARCv2 (ARCHS) general core registers feature set. See also
213 arc_update_acc_reg_names() for "accl/acch" names. */
214
215 static struct arc_register_feature arc_v2_core_reg_feature =
216 {
217 ARC_CORE_FEATURE_NAME,
218 {
219 { ARC_R0_REGNUM + 0, { "r0" }, true },
220 { ARC_R0_REGNUM + 1, { "r1" }, true },
221 { ARC_R0_REGNUM + 2, { "r2" }, true },
222 { ARC_R0_REGNUM + 3, { "r3" }, true },
223 { ARC_R0_REGNUM + 4, { "r4" }, false },
224 { ARC_R0_REGNUM + 5, { "r5" }, false },
225 { ARC_R0_REGNUM + 6, { "r6" }, false },
226 { ARC_R0_REGNUM + 7, { "r7" }, false },
227 { ARC_R0_REGNUM + 8, { "r8" }, false },
228 { ARC_R0_REGNUM + 9, { "r9" }, false },
229 { ARC_R0_REGNUM + 10, { "r10" }, true },
230 { ARC_R0_REGNUM + 11, { "r11" }, true },
231 { ARC_R0_REGNUM + 12, { "r12" }, true },
232 { ARC_R0_REGNUM + 13, { "r13" }, true },
233 { ARC_R0_REGNUM + 14, { "r14" }, true },
234 { ARC_R0_REGNUM + 15, { "r15" }, true },
235 { ARC_R0_REGNUM + 16, { "r16" }, false },
236 { ARC_R0_REGNUM + 17, { "r17" }, false },
237 { ARC_R0_REGNUM + 18, { "r18" }, false },
238 { ARC_R0_REGNUM + 19, { "r19" }, false },
239 { ARC_R0_REGNUM + 20, { "r20" }, false },
240 { ARC_R0_REGNUM + 21, { "r21" }, false },
241 { ARC_R0_REGNUM + 22, { "r22" }, false },
242 { ARC_R0_REGNUM + 23, { "r23" }, false },
243 { ARC_R0_REGNUM + 24, { "r24" }, false },
244 { ARC_R0_REGNUM + 25, { "r25" }, false },
245 { ARC_R0_REGNUM + 26, { "gp" }, true },
246 { ARC_R0_REGNUM + 27, { "fp" }, true },
247 { ARC_R0_REGNUM + 28, { "sp" }, true },
248 { ARC_R0_REGNUM + 29, { "ilink" }, false },
249 { ARC_R0_REGNUM + 30, { "r30" }, true },
250 { ARC_R0_REGNUM + 31, { "blink" }, true },
251 { ARC_R0_REGNUM + 32, { "r32" }, false },
252 { ARC_R0_REGNUM + 33, { "r33" }, false },
253 { ARC_R0_REGNUM + 34, { "r34" }, false },
254 { ARC_R0_REGNUM + 35, { "r35" }, false },
255 { ARC_R0_REGNUM + 36, { "r36" }, false },
256 { ARC_R0_REGNUM + 37, { "r37" }, false },
257 { ARC_R0_REGNUM + 38, { "r38" }, false },
258 { ARC_R0_REGNUM + 39, { "r39" }, false },
259 { ARC_R0_REGNUM + 40, { "r40" }, false },
260 { ARC_R0_REGNUM + 41, { "r41" }, false },
261 { ARC_R0_REGNUM + 42, { "r42" }, false },
262 { ARC_R0_REGNUM + 43, { "r43" }, false },
263 { ARC_R0_REGNUM + 44, { "r44" }, false },
264 { ARC_R0_REGNUM + 45, { "r45" }, false },
265 { ARC_R0_REGNUM + 46, { "r46" }, false },
266 { ARC_R0_REGNUM + 47, { "r47" }, false },
267 { ARC_R0_REGNUM + 48, { "r48" }, false },
268 { ARC_R0_REGNUM + 49, { "r49" }, false },
269 { ARC_R0_REGNUM + 50, { "r50" }, false },
270 { ARC_R0_REGNUM + 51, { "r51" }, false },
271 { ARC_R0_REGNUM + 52, { "r52" }, false },
272 { ARC_R0_REGNUM + 53, { "r53" }, false },
273 { ARC_R0_REGNUM + 54, { "r54" }, false },
274 { ARC_R0_REGNUM + 55, { "r55" }, false },
275 { ARC_R0_REGNUM + 56, { "r56" }, false },
276 { ARC_R0_REGNUM + 57, { "r57" }, false },
277 { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
278 { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
279 { ARC_R0_REGNUM + 60, { "lp_count" }, false },
280 { ARC_R0_REGNUM + 61, { "reserved" }, false },
281 { ARC_R0_REGNUM + 62, { "limm" }, false },
282 { ARC_R0_REGNUM + 63, { "pcl" }, true }
283 }
284 };
285
286 /* The common auxiliary registers feature set. The REGNUM field
287 must match the ARC_REGNUM enum in arc-tdep.h. */
288
289 static const struct arc_register_feature arc_common_aux_reg_feature =
290 {
291 ARC_AUX_FEATURE_NAME,
292 {
293 { ARC_FIRST_AUX_REGNUM + 0, { "pc" }, true },
294 { ARC_FIRST_AUX_REGNUM + 1, { "status32" }, true },
295 { ARC_FIRST_AUX_REGNUM + 2, { "lp_start" }, false },
296 { ARC_FIRST_AUX_REGNUM + 3, { "lp_end" }, false },
297 { ARC_FIRST_AUX_REGNUM + 4, { "bta" }, false }
298 }
299 };
300
301 static char *arc_disassembler_options = NULL;
302
303 /* Functions are sorted in the order as they are used in the
304 _initialize_arc_tdep (), which uses the same order as gdbarch.h. Static
305 functions are defined before the first invocation. */
306
307 /* Returns an unsigned value of OPERAND_NUM in instruction INSN.
308 For relative branch instructions returned value is an offset, not an actual
309 branch target. */
310
311 static ULONGEST
312 arc_insn_get_operand_value (const struct arc_instruction &insn,
313 unsigned int operand_num)
314 {
315 switch (insn.operands[operand_num].kind)
316 {
317 case ARC_OPERAND_KIND_LIMM:
318 gdb_assert (insn.limm_p);
319 return insn.limm_value;
320 case ARC_OPERAND_KIND_SHIMM:
321 return insn.operands[operand_num].value;
322 default:
323 /* Value in instruction is a register number. */
324 struct regcache *regcache = get_current_regcache ();
325 ULONGEST value;
326 regcache_cooked_read_unsigned (regcache,
327 insn.operands[operand_num].value,
328 &value);
329 return value;
330 }
331 }
332
333 /* Like arc_insn_get_operand_value, but returns a signed value. */
334
335 static LONGEST
336 arc_insn_get_operand_value_signed (const struct arc_instruction &insn,
337 unsigned int operand_num)
338 {
339 switch (insn.operands[operand_num].kind)
340 {
341 case ARC_OPERAND_KIND_LIMM:
342 gdb_assert (insn.limm_p);
343 /* Convert unsigned raw value to signed one. This assumes 2's
344 complement arithmetic, but so is the LONG_MIN value from generic
345 defs.h and that assumption is true for ARC. */
346 gdb_static_assert (sizeof (insn.limm_value) == sizeof (int));
347 return (((LONGEST) insn.limm_value) ^ INT_MIN) - INT_MIN;
348 case ARC_OPERAND_KIND_SHIMM:
349 /* Sign conversion has been done by binutils. */
350 return insn.operands[operand_num].value;
351 default:
352 /* Value in instruction is a register number. */
353 struct regcache *regcache = get_current_regcache ();
354 LONGEST value;
355 regcache_cooked_read_signed (regcache,
356 insn.operands[operand_num].value,
357 &value);
358 return value;
359 }
360 }
361
362 /* Get register with base address of memory operation. */
363
364 static int
365 arc_insn_get_memory_base_reg (const struct arc_instruction &insn)
366 {
367 /* POP_S and PUSH_S have SP as an implicit argument in a disassembler. */
368 if (insn.insn_class == PUSH || insn.insn_class == POP)
369 return ARC_SP_REGNUM;
370
371 gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
372
373 /* Other instructions all have at least two operands: operand 0 is data,
374 operand 1 is address. Operand 2 is offset from address. However, see
375 comment to arc_instruction.operands - in some cases, third operand may be
376 missing, namely if it is 0. */
377 gdb_assert (insn.operands_count >= 2);
378 return insn.operands[1].value;
379 }
380
381 /* Get offset of a memory operation INSN. */
382
383 static CORE_ADDR
384 arc_insn_get_memory_offset (const struct arc_instruction &insn)
385 {
386 /* POP_S and PUSH_S have offset as an implicit argument in a
387 disassembler. */
388 if (insn.insn_class == POP)
389 return 4;
390 else if (insn.insn_class == PUSH)
391 return -4;
392
393 gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
394
395 /* Other instructions all have at least two operands: operand 0 is data,
396 operand 1 is address. Operand 2 is offset from address. However, see
397 comment to arc_instruction.operands - in some cases, third operand may be
398 missing, namely if it is 0. */
399 if (insn.operands_count < 3)
400 return 0;
401
402 CORE_ADDR value = arc_insn_get_operand_value (insn, 2);
403 /* Handle scaling. */
404 if (insn.writeback_mode == ARC_WRITEBACK_AS)
405 {
406 /* Byte data size is not valid for AS. Halfword means shift by 1 bit.
407 Word and double word means shift by 2 bits. */
408 gdb_assert (insn.data_size_mode != ARC_SCALING_B);
409 if (insn.data_size_mode == ARC_SCALING_H)
410 value <<= 1;
411 else
412 value <<= 2;
413 }
414 return value;
415 }
416
417 CORE_ADDR
418 arc_insn_get_branch_target (const struct arc_instruction &insn)
419 {
420 gdb_assert (insn.is_control_flow);
421
422 /* BI [c]: PC = nextPC + (c << 2). */
423 if (insn.insn_class == BI)
424 {
425 ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
426 return arc_insn_get_linear_next_pc (insn) + (reg_value << 2);
427 }
428 /* BIH [c]: PC = nextPC + (c << 1). */
429 else if (insn.insn_class == BIH)
430 {
431 ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
432 return arc_insn_get_linear_next_pc (insn) + (reg_value << 1);
433 }
434 /* JLI and EI. */
435 /* JLI and EI depend on optional AUX registers. Not supported right now. */
436 else if (insn.insn_class == JLI)
437 {
438 fprintf_unfiltered (gdb_stderr,
439 "JLI_S instruction is not supported by the GDB.");
440 return 0;
441 }
442 else if (insn.insn_class == EI)
443 {
444 fprintf_unfiltered (gdb_stderr,
445 "EI_S instruction is not supported by the GDB.");
446 return 0;
447 }
448 /* LEAVE_S: PC = BLINK. */
449 else if (insn.insn_class == LEAVE)
450 {
451 struct regcache *regcache = get_current_regcache ();
452 ULONGEST value;
453 regcache_cooked_read_unsigned (regcache, ARC_BLINK_REGNUM, &value);
454 return value;
455 }
456 /* BBIT0/1, BRcc: PC = currentPC + operand. */
457 else if (insn.insn_class == BBIT0 || insn.insn_class == BBIT1
458 || insn.insn_class == BRCC)
459 {
460 /* Most instructions has branch target as their sole argument. However
461 conditional brcc/bbit has it as a third operand. */
462 CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 2);
463
464 /* Offset is relative to the 4-byte aligned address of the current
465 instruction, hence last two bits should be truncated. */
466 return pcrel_addr + align_down (insn.address, 4);
467 }
468 /* B, Bcc, BL, BLcc, LP, LPcc: PC = currentPC + operand. */
469 else if (insn.insn_class == BRANCH || insn.insn_class == LOOP)
470 {
471 CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 0);
472
473 /* Offset is relative to the 4-byte aligned address of the current
474 instruction, hence last two bits should be truncated. */
475 return pcrel_addr + align_down (insn.address, 4);
476 }
477 /* J, Jcc, JL, JLcc: PC = operand. */
478 else if (insn.insn_class == JUMP)
479 {
480 /* All jumps are single-operand. */
481 return arc_insn_get_operand_value (insn, 0);
482 }
483
484 /* This is some new and unknown instruction. */
485 gdb_assert_not_reached ("Unknown branch instruction.");
486 }
487
488 /* Dump INSN into gdb_stdlog. */
489
490 static void
491 arc_insn_dump (const struct arc_instruction &insn)
492 {
493 struct gdbarch *gdbarch = target_gdbarch ();
494
495 arc_print ("Dumping arc_instruction at %s\n",
496 paddress (gdbarch, insn.address));
497 arc_print ("\tlength = %u\n", insn.length);
498
499 if (!insn.valid)
500 {
501 arc_print ("\tThis is not a valid ARC instruction.\n");
502 return;
503 }
504
505 arc_print ("\tlength_with_limm = %u\n", insn.length + (insn.limm_p ? 4 : 0));
506 arc_print ("\tcc = 0x%x\n", insn.condition_code);
507 arc_print ("\tinsn_class = %u\n", insn.insn_class);
508 arc_print ("\tis_control_flow = %i\n", insn.is_control_flow);
509 arc_print ("\thas_delay_slot = %i\n", insn.has_delay_slot);
510
511 CORE_ADDR next_pc = arc_insn_get_linear_next_pc (insn);
512 arc_print ("\tlinear_next_pc = %s\n", paddress (gdbarch, next_pc));
513
514 if (insn.is_control_flow)
515 {
516 CORE_ADDR t = arc_insn_get_branch_target (insn);
517 arc_print ("\tbranch_target = %s\n", paddress (gdbarch, t));
518 }
519
520 arc_print ("\tlimm_p = %i\n", insn.limm_p);
521 if (insn.limm_p)
522 arc_print ("\tlimm_value = 0x%08x\n", insn.limm_value);
523
524 if (insn.insn_class == STORE || insn.insn_class == LOAD
525 || insn.insn_class == PUSH || insn.insn_class == POP)
526 {
527 arc_print ("\twriteback_mode = %u\n", insn.writeback_mode);
528 arc_print ("\tdata_size_mode = %u\n", insn.data_size_mode);
529 arc_print ("\tmemory_base_register = %s\n",
530 gdbarch_register_name (gdbarch,
531 arc_insn_get_memory_base_reg (insn)));
532 /* get_memory_offset returns an unsigned CORE_ADDR, but treat it as a
533 LONGEST for a nicer representation. */
534 arc_print ("\taddr_offset = %s\n",
535 plongest (arc_insn_get_memory_offset (insn)));
536 }
537
538 arc_print ("\toperands_count = %u\n", insn.operands_count);
539 for (unsigned int i = 0; i < insn.operands_count; ++i)
540 {
541 int is_reg = (insn.operands[i].kind == ARC_OPERAND_KIND_REG);
542
543 arc_print ("\toperand[%u] = {\n", i);
544 arc_print ("\t\tis_reg = %i\n", is_reg);
545 if (is_reg)
546 arc_print ("\t\tregister = %s\n",
547 gdbarch_register_name (gdbarch, insn.operands[i].value));
548 /* Don't know if this value is signed or not, so print both
549 representations. This tends to look quite ugly, especially for big
550 numbers. */
551 arc_print ("\t\tunsigned value = %s\n",
552 pulongest (arc_insn_get_operand_value (insn, i)));
553 arc_print ("\t\tsigned value = %s\n",
554 plongest (arc_insn_get_operand_value_signed (insn, i)));
555 arc_print ("\t}\n");
556 }
557 }
558
559 CORE_ADDR
560 arc_insn_get_linear_next_pc (const struct arc_instruction &insn)
561 {
562 /* In ARC long immediate is always 4 bytes. */
563 return (insn.address + insn.length + (insn.limm_p ? 4 : 0));
564 }
565
566 /* Implement the "write_pc" gdbarch method.
567
568 In ARC PC register is a normal register so in most cases setting PC value
569 is a straightforward process: debugger just writes PC value. However it
570 gets trickier in case when current instruction is an instruction in delay
571 slot. In this case CPU will execute instruction at current PC value, then
572 will set PC to the current value of BTA register; also current instruction
573 cannot be branch/jump and some of the other instruction types. Thus if
574 debugger would try to just change PC value in this case, this instruction
575 will get executed, but then core will "jump" to the original branch target.
576
577 Whether current instruction is a delay-slot instruction or not is indicated
578 by DE bit in STATUS32 register indicates if current instruction is a delay
579 slot instruction. This bit is writable by debug host, which allows debug
580 host to prevent core from jumping after the delay slot instruction. It
581 also works in another direction: setting this bit will make core to treat
582 any current instructions as a delay slot instruction and to set PC to the
583 current value of BTA register.
584
585 To workaround issues with changing PC register while in delay slot
586 instruction, debugger should check for the STATUS32.DE bit and reset it if
587 it is set. No other change is required in this function. Most common
588 case, where this function might be required is calling inferior functions
589 from debugger. Generic GDB logic handles this pretty well: current values
590 of registers are stored, value of PC is changed (that is the job of this
591 function), and after inferior function is executed, GDB restores all
592 registers, include BTA and STATUS32, which also means that core is returned
593 to its original state of being halted on delay slot instructions.
594
595 This method is useless for ARC 600, because it doesn't have externally
596 exposed BTA register. In the case of ARC 600 it is impossible to restore
597 core to its state in all occasions thus core should never be halted (from
598 the perspective of debugger host) in the delay slot. */
599
600 static void
601 arc_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
602 {
603 struct gdbarch *gdbarch = regcache->arch ();
604
605 arc_debug_printf ("Writing PC, new value=%s",
606 paddress (gdbarch, new_pc));
607
608 regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch),
609 new_pc);
610
611 ULONGEST status32;
612 regcache_cooked_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
613 &status32);
614
615 if ((status32 & ARC_STATUS32_DE_MASK) != 0)
616 {
617 arc_debug_printf ("Changing PC while in delay slot. Will "
618 "reset STATUS32.DE bit to zero. Value of STATUS32 "
619 "register is 0x%s",
620 phex (status32, ARC_REGISTER_SIZE));
621
622 /* Reset bit and write to the cache. */
623 status32 &= ~0x40;
624 regcache_cooked_write_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
625 status32);
626 }
627 }
628
629 /* Implement the "virtual_frame_pointer" gdbarch method.
630
631 According to ABI the FP (r27) is used to point to the middle of the current
632 stack frame, just below the saved FP and before local variables, register
633 spill area and outgoing args. However for optimization levels above O2 and
634 in any case in leaf functions, the frame pointer is usually not set at all.
635 The exception being when handling nested functions.
636
637 We use this function to return a "virtual" frame pointer, marking the start
638 of the current stack frame as a register-offset pair. If the FP is not
639 being used, then it should return SP, with an offset of the frame size.
640
641 The current implementation doesn't actually know the frame size, nor
642 whether the FP is actually being used, so for now we just return SP and an
643 offset of zero. This is no worse than other architectures, but is needed
644 to avoid assertion failures.
645
646 TODO: Can we determine the frame size to get a correct offset?
647
648 PC is a program counter where we need the virtual FP. REG_PTR is the base
649 register used for the virtual FP. OFFSET_PTR is the offset used for the
650 virtual FP. */
651
652 static void
653 arc_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
654 int *reg_ptr, LONGEST *offset_ptr)
655 {
656 *reg_ptr = gdbarch_sp_regnum (gdbarch);
657 *offset_ptr = 0;
658 }
659
660 /* Implement the "push_dummy_call" gdbarch method.
661
662 Stack Frame Layout
663
664 This shows the layout of the stack frame for the general case of a
665 function call; a given function might not have a variable number of
666 arguments or local variables, or might not save any registers, so it would
667 not have the corresponding frame areas. Additionally, a leaf function
668 (i.e. one which calls no other functions) does not need to save the
669 contents of the BLINK register (which holds its return address), and a
670 function might not have a frame pointer.
671
672 The stack grows downward, so SP points below FP in memory; SP always
673 points to the last used word on the stack, not the first one.
674
675 | | |
676 | arg word N | | caller's
677 | : | | frame
678 | arg word 10 | |
679 | arg word 9 | |
680 old SP ---> +-----------------------+ --+
681 | | |
682 | callee-saved | |
683 | registers | |
684 | including fp, blink | |
685 | | | callee's
686 new FP ---> +-----------------------+ | frame
687 | | |
688 | local | |
689 | variables | |
690 | | |
691 | register | |
692 | spill area | |
693 | | |
694 | outgoing args | |
695 | | |
696 new SP ---> +-----------------------+ --+
697 | |
698 | unused |
699 | |
700 |
701 |
702 V
703 downwards
704
705 The list of arguments to be passed to a function is considered to be a
706 sequence of _N_ words (as though all the parameters were stored in order in
707 memory with each parameter occupying an integral number of words). Words
708 1..8 are passed in registers 0..7; if the function has more than 8 words of
709 arguments then words 9..@em N are passed on the stack in the caller's frame.
710
711 If the function has a variable number of arguments, e.g. it has a form such
712 as `function (p1, p2, ...);' and _P_ words are required to hold the values
713 of the named parameters (which are passed in registers 0..@em P -1), then
714 the remaining 8 - _P_ words passed in registers _P_..7 are spilled into the
715 top of the frame so that the anonymous parameter words occupy a continuous
716 region.
717
718 Any arguments are already in target byte order. We just need to store
719 them!
720
721 BP_ADDR is the return address where breakpoint must be placed. NARGS is
722 the number of arguments to the function. ARGS is the arguments values (in
723 target byte order). SP is the Current value of SP register. STRUCT_RETURN
724 is TRUE if structures are returned by the function. STRUCT_ADDR is the
725 hidden address for returning a struct. Returns SP of a new frame. */
726
727 static CORE_ADDR
728 arc_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
729 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
730 struct value **args, CORE_ADDR sp,
731 function_call_return_method return_method,
732 CORE_ADDR struct_addr)
733 {
734 arc_debug_printf ("nargs = %d", nargs);
735
736 int arg_reg = ARC_FIRST_ARG_REGNUM;
737
738 /* Push the return address. */
739 regcache_cooked_write_unsigned (regcache, ARC_BLINK_REGNUM, bp_addr);
740
741 /* Are we returning a value using a structure return instead of a normal
742 value return? If so, struct_addr is the address of the reserved space for
743 the return structure to be written on the stack, and that address is
744 passed to that function as a hidden first argument. */
745 if (return_method == return_method_struct)
746 {
747 /* Pass the return address in the first argument register. */
748 regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
749
750 arc_debug_printf ("struct return address %s passed in R%d",
751 print_core_address (gdbarch, struct_addr), arg_reg);
752
753 arg_reg++;
754 }
755
756 if (nargs > 0)
757 {
758 unsigned int total_space = 0;
759
760 /* How much space do the arguments occupy in total? Must round each
761 argument's size up to an integral number of words. */
762 for (int i = 0; i < nargs; i++)
763 {
764 unsigned int len = TYPE_LENGTH (value_type (args[i]));
765 unsigned int space = align_up (len, 4);
766
767 total_space += space;
768
769 arc_debug_printf ("arg %d: %u bytes -> %u", i, len, space);
770 }
771
772 /* Allocate a buffer to hold a memory image of the arguments. */
773 gdb_byte *memory_image = XCNEWVEC (gdb_byte, total_space);
774
775 /* Now copy all of the arguments into the buffer, correctly aligned. */
776 gdb_byte *data = memory_image;
777 for (int i = 0; i < nargs; i++)
778 {
779 unsigned int len = TYPE_LENGTH (value_type (args[i]));
780 unsigned int space = align_up (len, 4);
781
782 memcpy (data, value_contents (args[i]), (size_t) len);
783 arc_debug_printf ("copying arg %d, val 0x%08x, len %d to mem",
784 i, *((int *) value_contents (args[i])), len);
785
786 data += space;
787 }
788
789 /* Now load as much as possible of the memory image into registers. */
790 data = memory_image;
791 while (arg_reg <= ARC_LAST_ARG_REGNUM)
792 {
793 arc_debug_printf ("passing 0x%02x%02x%02x%02x in register R%d",
794 data[0], data[1], data[2], data[3], arg_reg);
795
796 /* Note we don't use write_unsigned here, since that would convert
797 the byte order, but we are already in the correct byte order. */
798 regcache->cooked_write (arg_reg, data);
799
800 data += ARC_REGISTER_SIZE;
801 total_space -= ARC_REGISTER_SIZE;
802
803 /* All the data is now in registers. */
804 if (total_space == 0)
805 break;
806
807 arg_reg++;
808 }
809
810 /* If there is any data left, push it onto the stack (in a single write
811 operation). */
812 if (total_space > 0)
813 {
814 arc_debug_printf ("passing %d bytes on stack\n", total_space);
815
816 sp -= total_space;
817 write_memory (sp, data, (int) total_space);
818 }
819
820 xfree (memory_image);
821 }
822
823 /* Finally, update the SP register. */
824 regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
825
826 return sp;
827 }
828
829 /* Implement the "push_dummy_code" gdbarch method.
830
831 We don't actually push any code. We just identify where a breakpoint can
832 be inserted to which we are can return and the resume address where we
833 should be called.
834
835 ARC does not necessarily have an executable stack, so we can't put the
836 return breakpoint there. Instead we put it at the entry point of the
837 function. This means the SP is unchanged.
838
839 SP is a current stack pointer FUNADDR is an address of the function to be
840 called. ARGS is arguments to pass. NARGS is a number of args to pass.
841 VALUE_TYPE is a type of value returned. REAL_PC is a resume address when
842 the function is called. BP_ADDR is an address where breakpoint should be
843 set. Returns the updated stack pointer. */
844
845 static CORE_ADDR
846 arc_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
847 struct value **args, int nargs, struct type *value_type,
848 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
849 struct regcache *regcache)
850 {
851 *real_pc = funaddr;
852 *bp_addr = entry_point_address ();
853 return sp;
854 }
855
856 /* Implement the "cannot_fetch_register" gdbarch method. */
857
858 static int
859 arc_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
860 {
861 /* Assume that register is readable if it is unknown. LIMM and RESERVED are
862 not real registers, but specific register numbers. They are available as
863 regnums to align architectural register numbers with GDB internal regnums,
864 but they shouldn't appear in target descriptions generated by
865 GDB-servers. */
866 switch (regnum)
867 {
868 case ARC_RESERVED_REGNUM:
869 case ARC_LIMM_REGNUM:
870 return true;
871 default:
872 return false;
873 }
874 }
875
876 /* Implement the "cannot_store_register" gdbarch method. */
877
878 static int
879 arc_cannot_store_register (struct gdbarch *gdbarch, int regnum)
880 {
881 /* Assume that register is writable if it is unknown. See comment in
882 arc_cannot_fetch_register about LIMM and RESERVED. */
883 switch (regnum)
884 {
885 case ARC_RESERVED_REGNUM:
886 case ARC_LIMM_REGNUM:
887 case ARC_PCL_REGNUM:
888 return true;
889 default:
890 return false;
891 }
892 }
893
894 /* Get the return value of a function from the registers/memory used to
895 return it, according to the convention used by the ABI - 4-bytes values are
896 in the R0, while 8-byte values are in the R0-R1.
897
898 TODO: This implementation ignores the case of "complex double", where
899 according to ABI, value is returned in the R0-R3 registers.
900
901 TYPE is a returned value's type. VALBUF is a buffer for the returned
902 value. */
903
904 static void
905 arc_extract_return_value (struct gdbarch *gdbarch, struct type *type,
906 struct regcache *regcache, gdb_byte *valbuf)
907 {
908 unsigned int len = TYPE_LENGTH (type);
909
910 arc_debug_printf ("called");
911
912 if (len <= ARC_REGISTER_SIZE)
913 {
914 ULONGEST val;
915
916 /* Get the return value from one register. */
917 regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &val);
918 store_unsigned_integer (valbuf, (int) len,
919 gdbarch_byte_order (gdbarch), val);
920
921 arc_debug_printf ("returning 0x%s", phex (val, ARC_REGISTER_SIZE));
922 }
923 else if (len <= ARC_REGISTER_SIZE * 2)
924 {
925 ULONGEST low, high;
926
927 /* Get the return value from two registers. */
928 regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &low);
929 regcache_cooked_read_unsigned (regcache, ARC_R1_REGNUM, &high);
930
931 store_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
932 gdbarch_byte_order (gdbarch), low);
933 store_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
934 (int) len - ARC_REGISTER_SIZE,
935 gdbarch_byte_order (gdbarch), high);
936
937 arc_debug_printf ("returning 0x%s%s",
938 phex (high, ARC_REGISTER_SIZE),
939 phex (low, ARC_REGISTER_SIZE));
940 }
941 else
942 error (_("arc: extract_return_value: type length %u too large"), len);
943 }
944
945
946 /* Store the return value of a function into the registers/memory used to
947 return it, according to the convention used by the ABI.
948
949 TODO: This implementation ignores the case of "complex double", where
950 according to ABI, value is returned in the R0-R3 registers.
951
952 TYPE is a returned value's type. VALBUF is a buffer with the value to
953 return. */
954
955 static void
956 arc_store_return_value (struct gdbarch *gdbarch, struct type *type,
957 struct regcache *regcache, const gdb_byte *valbuf)
958 {
959 unsigned int len = TYPE_LENGTH (type);
960
961 arc_debug_printf ("called");
962
963 if (len <= ARC_REGISTER_SIZE)
964 {
965 ULONGEST val;
966
967 /* Put the return value into one register. */
968 val = extract_unsigned_integer (valbuf, (int) len,
969 gdbarch_byte_order (gdbarch));
970 regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, val);
971
972 arc_debug_printf ("storing 0x%s", phex (val, ARC_REGISTER_SIZE));
973 }
974 else if (len <= ARC_REGISTER_SIZE * 2)
975 {
976 ULONGEST low, high;
977
978 /* Put the return value into two registers. */
979 low = extract_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
980 gdbarch_byte_order (gdbarch));
981 high = extract_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
982 (int) len - ARC_REGISTER_SIZE,
983 gdbarch_byte_order (gdbarch));
984
985 regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, low);
986 regcache_cooked_write_unsigned (regcache, ARC_R1_REGNUM, high);
987
988 arc_debug_printf ("storing 0x%s%s",
989 phex (high, ARC_REGISTER_SIZE),
990 phex (low, ARC_REGISTER_SIZE));
991 }
992 else
993 error (_("arc_store_return_value: type length too large."));
994 }
995
996 /* Implement the "get_longjmp_target" gdbarch method. */
997
998 static int
999 arc_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1000 {
1001 arc_debug_printf ("called");
1002
1003 struct gdbarch *gdbarch = get_frame_arch (frame);
1004 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1005 int pc_offset = tdep->jb_pc * ARC_REGISTER_SIZE;
1006 gdb_byte buf[ARC_REGISTER_SIZE];
1007 CORE_ADDR jb_addr = get_frame_register_unsigned (frame, ARC_FIRST_ARG_REGNUM);
1008
1009 if (target_read_memory (jb_addr + pc_offset, buf, ARC_REGISTER_SIZE))
1010 return 0; /* Failed to read from memory. */
1011
1012 *pc = extract_unsigned_integer (buf, ARC_REGISTER_SIZE,
1013 gdbarch_byte_order (gdbarch));
1014 return 1;
1015 }
1016
1017 /* Implement the "return_value" gdbarch method. */
1018
1019 static enum return_value_convention
1020 arc_return_value (struct gdbarch *gdbarch, struct value *function,
1021 struct type *valtype, struct regcache *regcache,
1022 gdb_byte *readbuf, const gdb_byte *writebuf)
1023 {
1024 /* If the return type is a struct, or a union, or would occupy more than two
1025 registers, the ABI uses the "struct return convention": the calling
1026 function passes a hidden first parameter to the callee (in R0). That
1027 parameter is the address at which the value being returned should be
1028 stored. Otherwise, the result is returned in registers. */
1029 int is_struct_return = (valtype->code () == TYPE_CODE_STRUCT
1030 || valtype->code () == TYPE_CODE_UNION
1031 || TYPE_LENGTH (valtype) > 2 * ARC_REGISTER_SIZE);
1032
1033 arc_debug_printf ("readbuf = %s, writebuf = %s",
1034 host_address_to_string (readbuf),
1035 host_address_to_string (writebuf));
1036
1037 if (writebuf != NULL)
1038 {
1039 /* Case 1. GDB should not ask us to set a struct return value: it
1040 should know the struct return location and write the value there
1041 itself. */
1042 gdb_assert (!is_struct_return);
1043 arc_store_return_value (gdbarch, valtype, regcache, writebuf);
1044 }
1045 else if (readbuf != NULL)
1046 {
1047 /* Case 2. GDB should not ask us to get a struct return value: it
1048 should know the struct return location and read the value from there
1049 itself. */
1050 gdb_assert (!is_struct_return);
1051 arc_extract_return_value (gdbarch, valtype, regcache, readbuf);
1052 }
1053
1054 return (is_struct_return
1055 ? RETURN_VALUE_STRUCT_CONVENTION
1056 : RETURN_VALUE_REGISTER_CONVENTION);
1057 }
1058
1059 /* Return the base address of the frame. For ARC, the base address is the
1060 frame pointer. */
1061
1062 static CORE_ADDR
1063 arc_frame_base_address (struct frame_info *this_frame, void **prologue_cache)
1064 {
1065 return (CORE_ADDR) get_frame_register_unsigned (this_frame, ARC_FP_REGNUM);
1066 }
1067
1068 /* Helper function that returns valid pv_t for an instruction operand:
1069 either a register or a constant. */
1070
1071 static pv_t
1072 arc_pv_get_operand (pv_t *regs, const struct arc_instruction &insn, int operand)
1073 {
1074 if (insn.operands[operand].kind == ARC_OPERAND_KIND_REG)
1075 return regs[insn.operands[operand].value];
1076 else
1077 return pv_constant (arc_insn_get_operand_value (insn, operand));
1078 }
1079
1080 /* Determine whether the given disassembled instruction may be part of a
1081 function prologue. If it is, the information in the frame unwind cache will
1082 be updated. */
1083
1084 static bool
1085 arc_is_in_prologue (struct gdbarch *gdbarch, const struct arc_instruction &insn,
1086 pv_t *regs, struct pv_area *stack)
1087 {
1088 /* It might be that currently analyzed address doesn't contain an
1089 instruction, hence INSN is not valid. It likely means that address points
1090 to a data, non-initialized memory, or middle of a 32-bit instruction. In
1091 practice this may happen if GDB connects to a remote target that has
1092 non-zeroed memory. GDB would read PC value and would try to analyze
1093 prologue, but there is no guarantee that memory contents at the address
1094 specified in PC is address is a valid instruction. There is not much that
1095 that can be done about that. */
1096 if (!insn.valid)
1097 return false;
1098
1099 /* Branch/jump or a predicated instruction. */
1100 if (insn.is_control_flow || insn.condition_code != ARC_CC_AL)
1101 return false;
1102
1103 /* Store of some register. May or may not update base address register. */
1104 if (insn.insn_class == STORE || insn.insn_class == PUSH)
1105 {
1106 /* There is definitely at least one operand - register/value being
1107 stored. */
1108 gdb_assert (insn.operands_count > 0);
1109
1110 /* Store at some constant address. */
1111 if (insn.operands_count > 1
1112 && insn.operands[1].kind != ARC_OPERAND_KIND_REG)
1113 return false;
1114
1115 /* Writeback modes:
1116 Mode Address used Writeback value
1117 --------------------------------------------------
1118 No reg + offset no
1119 A/AW reg + offset reg + offset
1120 AB reg reg + offset
1121 AS reg + (offset << scaling) no
1122
1123 "PUSH reg" is an alias to "ST.AW reg, [SP, -4]" encoding. However
1124 16-bit PUSH_S is a distinct instruction encoding, where offset and
1125 base register are implied through opcode. */
1126
1127 /* Register with base memory address. */
1128 int base_reg = arc_insn_get_memory_base_reg (insn);
1129
1130 /* Address where to write. arc_insn_get_memory_offset returns scaled
1131 value for ARC_WRITEBACK_AS. */
1132 pv_t addr;
1133 if (insn.writeback_mode == ARC_WRITEBACK_AB)
1134 addr = regs[base_reg];
1135 else
1136 addr = pv_add_constant (regs[base_reg],
1137 arc_insn_get_memory_offset (insn));
1138
1139 if (stack->store_would_trash (addr))
1140 return false;
1141
1142 if (insn.data_size_mode != ARC_SCALING_D)
1143 {
1144 /* Find the value being stored. */
1145 pv_t store_value = arc_pv_get_operand (regs, insn, 0);
1146
1147 /* What is the size of a the stored value? */
1148 CORE_ADDR size;
1149 if (insn.data_size_mode == ARC_SCALING_B)
1150 size = 1;
1151 else if (insn.data_size_mode == ARC_SCALING_H)
1152 size = 2;
1153 else
1154 size = ARC_REGISTER_SIZE;
1155
1156 stack->store (addr, size, store_value);
1157 }
1158 else
1159 {
1160 if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
1161 {
1162 /* If this is a double store, than write N+1 register as well. */
1163 pv_t store_value1 = regs[insn.operands[0].value];
1164 pv_t store_value2 = regs[insn.operands[0].value + 1];
1165 stack->store (addr, ARC_REGISTER_SIZE, store_value1);
1166 stack->store (pv_add_constant (addr, ARC_REGISTER_SIZE),
1167 ARC_REGISTER_SIZE, store_value2);
1168 }
1169 else
1170 {
1171 pv_t store_value
1172 = pv_constant (arc_insn_get_operand_value (insn, 0));
1173 stack->store (addr, ARC_REGISTER_SIZE * 2, store_value);
1174 }
1175 }
1176
1177 /* Is base register updated? */
1178 if (insn.writeback_mode == ARC_WRITEBACK_A
1179 || insn.writeback_mode == ARC_WRITEBACK_AB)
1180 regs[base_reg] = pv_add_constant (regs[base_reg],
1181 arc_insn_get_memory_offset (insn));
1182
1183 return true;
1184 }
1185 else if (insn.insn_class == MOVE)
1186 {
1187 gdb_assert (insn.operands_count == 2);
1188
1189 /* Destination argument can be "0", so nothing will happen. */
1190 if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
1191 {
1192 int dst_regnum = insn.operands[0].value;
1193 regs[dst_regnum] = arc_pv_get_operand (regs, insn, 1);
1194 }
1195 return true;
1196 }
1197 else if (insn.insn_class == SUB)
1198 {
1199 gdb_assert (insn.operands_count == 3);
1200
1201 /* SUB 0,b,c. */
1202 if (insn.operands[0].kind != ARC_OPERAND_KIND_REG)
1203 return true;
1204
1205 int dst_regnum = insn.operands[0].value;
1206 regs[dst_regnum] = pv_subtract (arc_pv_get_operand (regs, insn, 1),
1207 arc_pv_get_operand (regs, insn, 2));
1208 return true;
1209 }
1210 else if (insn.insn_class == ENTER)
1211 {
1212 /* ENTER_S is a prologue-in-instruction - it saves all callee-saved
1213 registers according to given arguments thus greatly reducing code
1214 size. Which registers will be actually saved depends on arguments.
1215
1216 ENTER_S {R13-...,FP,BLINK} stores registers in following order:
1217
1218 new SP ->
1219 BLINK
1220 R13
1221 R14
1222 R15
1223 ...
1224 FP
1225 old SP ->
1226
1227 There are up to three arguments for this opcode, as presented by ARC
1228 disassembler:
1229 1) amount of general-purpose registers to be saved - this argument is
1230 always present even when it is 0;
1231 2) FP register number (27) if FP has to be stored, otherwise argument
1232 is not present;
1233 3) BLINK register number (31) if BLINK has to be stored, otherwise
1234 argument is not present. If both FP and BLINK are stored, then FP
1235 is present before BLINK in argument list. */
1236 gdb_assert (insn.operands_count > 0);
1237
1238 int regs_saved = arc_insn_get_operand_value (insn, 0);
1239
1240 bool is_fp_saved;
1241 if (insn.operands_count > 1)
1242 is_fp_saved = (insn.operands[1].value == ARC_FP_REGNUM);
1243 else
1244 is_fp_saved = false;
1245
1246 bool is_blink_saved;
1247 if (insn.operands_count > 1)
1248 is_blink_saved = (insn.operands[insn.operands_count - 1].value
1249 == ARC_BLINK_REGNUM);
1250 else
1251 is_blink_saved = false;
1252
1253 /* Amount of bytes to be allocated to store specified registers. */
1254 CORE_ADDR st_size = ((regs_saved + is_fp_saved + is_blink_saved)
1255 * ARC_REGISTER_SIZE);
1256 pv_t new_sp = pv_add_constant (regs[ARC_SP_REGNUM], -st_size);
1257
1258 /* Assume that if the last register (closest to new SP) can be written,
1259 then it is possible to write all of them. */
1260 if (stack->store_would_trash (new_sp))
1261 return false;
1262
1263 /* Current store address. */
1264 pv_t addr = regs[ARC_SP_REGNUM];
1265
1266 if (is_fp_saved)
1267 {
1268 addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
1269 stack->store (addr, ARC_REGISTER_SIZE, regs[ARC_FP_REGNUM]);
1270 }
1271
1272 /* Registers are stored in backward order: from GP (R26) to R13. */
1273 for (int i = ARC_R13_REGNUM + regs_saved - 1; i >= ARC_R13_REGNUM; i--)
1274 {
1275 addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
1276 stack->store (addr, ARC_REGISTER_SIZE, regs[i]);
1277 }
1278
1279 if (is_blink_saved)
1280 {
1281 addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
1282 stack->store (addr, ARC_REGISTER_SIZE,
1283 regs[ARC_BLINK_REGNUM]);
1284 }
1285
1286 gdb_assert (pv_is_identical (addr, new_sp));
1287
1288 regs[ARC_SP_REGNUM] = new_sp;
1289
1290 if (is_fp_saved)
1291 regs[ARC_FP_REGNUM] = regs[ARC_SP_REGNUM];
1292
1293 return true;
1294 }
1295
1296 /* Some other architectures, like nds32 or arm, try to continue as far as
1297 possible when building a prologue cache (as opposed to when skipping
1298 prologue), so that cache will be as full as possible. However current
1299 code for ARC doesn't recognize some instructions that may modify SP, like
1300 ADD, AND, OR, etc, hence there is no way to guarantee that SP wasn't
1301 clobbered by the skipped instruction. Potential existence of extension
1302 instruction, which may do anything they want makes this even more complex,
1303 so it is just better to halt on a first unrecognized instruction. */
1304
1305 return false;
1306 }
1307
1308 /* Copy of gdb_buffered_insn_length_fprintf from disasm.c. */
1309
1310 static int ATTRIBUTE_PRINTF (2, 3)
1311 arc_fprintf_disasm (void *stream, const char *format, ...)
1312 {
1313 return 0;
1314 }
1315
1316 struct disassemble_info
1317 arc_disassemble_info (struct gdbarch *gdbarch)
1318 {
1319 struct disassemble_info di;
1320 init_disassemble_info (&di, &null_stream, arc_fprintf_disasm);
1321 di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1322 di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1323 di.endian = gdbarch_byte_order (gdbarch);
1324 di.read_memory_func = [](bfd_vma memaddr, gdb_byte *myaddr,
1325 unsigned int len, struct disassemble_info *info)
1326 {
1327 return target_read_code (memaddr, myaddr, len);
1328 };
1329 return di;
1330 }
1331
1332 /* Analyze the prologue and update the corresponding frame cache for the frame
1333 unwinder for unwinding frames that doesn't have debug info. In such
1334 situation GDB attempts to parse instructions in the prologue to understand
1335 where each register is saved.
1336
1337 If CACHE is not NULL, then it will be filled with information about saved
1338 registers.
1339
1340 There are several variations of prologue which GDB may encounter. "Full"
1341 prologue looks like this:
1342
1343 sub sp,sp,<imm> ; Space for variadic arguments.
1344 push blink ; Store return address.
1345 push r13 ; Store callee saved registers (up to R26/GP).
1346 push r14
1347 push fp ; Store frame pointer.
1348 mov fp,sp ; Update frame pointer.
1349 sub sp,sp,<imm> ; Create space for local vars on the stack.
1350
1351 Depending on compiler options lots of things may change:
1352
1353 1) BLINK is not saved in leaf functions.
1354 2) Frame pointer is not saved and updated if -fomit-frame-pointer is used.
1355 3) 16-bit versions of those instructions may be used.
1356 4) Instead of a sequence of several push'es, compiler may instead prefer to
1357 do one subtract on stack pointer and then store registers using normal
1358 store, that doesn't update SP. Like this:
1359
1360
1361 sub sp,sp,8 ; Create space for callee-saved registers.
1362 st r13,[sp,4] ; Store callee saved registers (up to R26/GP).
1363 st r14,[sp,0]
1364
1365 5) ENTER_S instruction can encode most of prologue sequence in one
1366 instruction (except for those subtracts for variadic arguments and local
1367 variables).
1368 6) GCC may use "millicode" functions from libgcc to store callee-saved
1369 registers with minimal code-size requirements. This function currently
1370 doesn't support this.
1371
1372 ENTRYPOINT is a function entry point where prologue starts.
1373
1374 LIMIT_PC is a maximum possible end address of prologue (meaning address
1375 of first instruction after the prologue). It might also point to the middle
1376 of prologue if execution has been stopped by the breakpoint at this address
1377 - in this case debugger should analyze prologue only up to this address,
1378 because further instructions haven't been executed yet.
1379
1380 Returns address of the first instruction after the prologue. */
1381
1382 static CORE_ADDR
1383 arc_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR entrypoint,
1384 const CORE_ADDR limit_pc, struct arc_frame_cache *cache)
1385 {
1386 arc_debug_printf ("entrypoint=%s, limit_pc=%s",
1387 paddress (gdbarch, entrypoint),
1388 paddress (gdbarch, limit_pc));
1389
1390 /* Prologue values. Only core registers can be stored. */
1391 pv_t regs[ARC_LAST_CORE_REGNUM + 1];
1392 for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1393 regs[i] = pv_register (i, 0);
1394 pv_area stack (ARC_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1395
1396 CORE_ADDR current_prologue_end = entrypoint;
1397
1398 /* Look at each instruction in the prologue. */
1399 while (current_prologue_end < limit_pc)
1400 {
1401 struct arc_instruction insn;
1402 struct disassemble_info di = arc_disassemble_info (gdbarch);
1403 arc_insn_decode (current_prologue_end, &di, arc_delayed_print_insn,
1404 &insn);
1405
1406 if (arc_debug)
1407 arc_insn_dump (insn);
1408
1409 /* If this instruction is in the prologue, fields in the cache will be
1410 updated, and the saved registers mask may be updated. */
1411 if (!arc_is_in_prologue (gdbarch, insn, regs, &stack))
1412 {
1413 /* Found an instruction that is not in the prologue. */
1414 arc_debug_printf ("End of prologue reached at address %s",
1415 paddress (gdbarch, insn.address));
1416 break;
1417 }
1418
1419 current_prologue_end = arc_insn_get_linear_next_pc (insn);
1420 }
1421
1422 if (cache != NULL)
1423 {
1424 /* Figure out if it is a frame pointer or just a stack pointer. */
1425 if (pv_is_register (regs[ARC_FP_REGNUM], ARC_SP_REGNUM))
1426 {
1427 cache->frame_base_reg = ARC_FP_REGNUM;
1428 cache->frame_base_offset = -regs[ARC_FP_REGNUM].k;
1429 }
1430 else
1431 {
1432 cache->frame_base_reg = ARC_SP_REGNUM;
1433 cache->frame_base_offset = -regs[ARC_SP_REGNUM].k;
1434 }
1435
1436 /* Assign offset from old SP to all saved registers. */
1437 for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1438 {
1439 CORE_ADDR offset;
1440 if (stack.find_reg (gdbarch, i, &offset))
1441 cache->saved_regs[i].set_addr (offset);
1442 }
1443 }
1444
1445 return current_prologue_end;
1446 }
1447
1448 /* Estimated maximum prologue length in bytes. This should include:
1449 1) Store instruction for each callee-saved register (R25 - R13 + 1)
1450 2) Two instructions for FP
1451 3) One for BLINK
1452 4) Three substract instructions for SP (for variadic args, for
1453 callee saved regs and for local vars) and assuming that those SUB use
1454 long-immediate (hence double length).
1455 5) Stores of arguments registers are considered part of prologue too
1456 (R7 - R1 + 1).
1457 This is quite an extreme case, because even with -O0 GCC will collapse first
1458 two SUBs into one and long immediate values are quite unlikely to appear in
1459 this case, but still better to overshoot a bit - prologue analysis will
1460 anyway stop at the first instruction that doesn't fit prologue, so this
1461 limit will be rarely reached. */
1462
1463 const static int MAX_PROLOGUE_LENGTH
1464 = 4 * (ARC_R25_REGNUM - ARC_R13_REGNUM + 1 + 2 + 1 + 6
1465 + ARC_LAST_ARG_REGNUM - ARC_FIRST_ARG_REGNUM + 1);
1466
1467 /* Implement the "skip_prologue" gdbarch method.
1468
1469 Skip the prologue for the function at PC. This is done by checking from
1470 the line information read from the DWARF, if possible; otherwise, we scan
1471 the function prologue to find its end. */
1472
1473 static CORE_ADDR
1474 arc_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1475 {
1476 arc_debug_printf ("pc = %s", paddress (gdbarch, pc));
1477
1478 CORE_ADDR func_addr;
1479 const char *func_name;
1480
1481 /* See what the symbol table says. */
1482 if (find_pc_partial_function (pc, &func_name, &func_addr, NULL))
1483 {
1484 /* Found a function. */
1485 CORE_ADDR postprologue_pc
1486 = skip_prologue_using_sal (gdbarch, func_addr);
1487
1488 if (postprologue_pc != 0)
1489 return std::max (pc, postprologue_pc);
1490 }
1491
1492 /* No prologue info in symbol table, have to analyze prologue. */
1493
1494 /* Find an upper limit on the function prologue using the debug
1495 information. If there is no debug information about prologue end, then
1496 skip_prologue_using_sal will return 0. */
1497 CORE_ADDR limit_pc = skip_prologue_using_sal (gdbarch, pc);
1498
1499 /* If there is no debug information at all, it is required to give some
1500 semi-arbitrary hard limit on amount of bytes to scan during prologue
1501 analysis. */
1502 if (limit_pc == 0)
1503 limit_pc = pc + MAX_PROLOGUE_LENGTH;
1504
1505 /* Find the address of the first instruction after the prologue by scanning
1506 through it - no other information is needed, so pass NULL as a cache. */
1507 return arc_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1508 }
1509
1510 /* Implement the "print_insn" gdbarch method.
1511
1512 arc_get_disassembler () may return different functions depending on bfd
1513 type, so it is not possible to pass print_insn directly to
1514 set_gdbarch_print_insn (). Instead this wrapper function is used. It also
1515 may be used by other functions to get disassemble_info for address. It is
1516 important to note, that those print_insn from opcodes always print
1517 instruction to the stream specified in the INFO. If this is not desired,
1518 then either `print_insn` function in INFO should be set to some function
1519 that will not print, or `stream` should be different from standard
1520 gdb_stdlog. */
1521
1522 int
1523 arc_delayed_print_insn (bfd_vma addr, struct disassemble_info *info)
1524 {
1525 /* Standard BFD "machine number" field allows libopcodes disassembler to
1526 distinguish ARC 600, 700 and v2 cores, however v2 encompasses both ARC EM
1527 and HS, which have some difference between. There are two ways to specify
1528 what is the target core:
1529 1) via the disassemble_info->disassembler_options;
1530 2) otherwise libopcodes will use private (architecture-specific) ELF
1531 header.
1532
1533 Using disassembler_options is preferable, because it comes directly from
1534 GDBserver which scanned an actual ARC core identification info. However,
1535 not all GDBservers report core architecture, so as a fallback GDB still
1536 should support analysis of ELF header. The libopcodes disassembly code
1537 uses the section to find the BFD and the BFD to find the ELF header,
1538 therefore this function should set disassemble_info->section properly.
1539
1540 disassembler_options was already set by non-target specific code with
1541 proper options obtained via gdbarch_disassembler_options ().
1542
1543 This function might be called multiple times in a sequence, reusing same
1544 disassemble_info. */
1545 if ((info->disassembler_options == NULL) && (info->section == NULL))
1546 {
1547 struct obj_section *s = find_pc_section (addr);
1548 if (s != NULL)
1549 info->section = s->the_bfd_section;
1550 }
1551
1552 return default_print_insn (addr, info);
1553 }
1554
1555 /* Baremetal breakpoint instructions.
1556
1557 ARC supports both big- and little-endian. However, instructions for
1558 little-endian processors are encoded in the middle-endian: half-words are
1559 in big-endian, while bytes inside the half-words are in little-endian; data
1560 is represented in the "normal" little-endian. Big-endian processors treat
1561 data and code identically.
1562
1563 Assuming the number 0x01020304, it will be presented this way:
1564
1565 Address : N N+1 N+2 N+3
1566 little-endian : 0x04 0x03 0x02 0x01
1567 big-endian : 0x01 0x02 0x03 0x04
1568 ARC middle-endian : 0x02 0x01 0x04 0x03
1569 */
1570
1571 static const gdb_byte arc_brk_s_be[] = { 0x7f, 0xff };
1572 static const gdb_byte arc_brk_s_le[] = { 0xff, 0x7f };
1573 static const gdb_byte arc_brk_be[] = { 0x25, 0x6f, 0x00, 0x3f };
1574 static const gdb_byte arc_brk_le[] = { 0x6f, 0x25, 0x3f, 0x00 };
1575
1576 /* For ARC ELF, breakpoint uses the 16-bit BRK_S instruction, which is 0x7fff
1577 (little endian) or 0xff7f (big endian). We used to insert BRK_S even
1578 instead of 32-bit instructions, which works mostly ok, unless breakpoint is
1579 inserted into delay slot instruction. In this case if branch is taken
1580 BLINK value will be set to address of instruction after delay slot, however
1581 if we replaced 32-bit instruction in delay slot with 16-bit long BRK_S,
1582 then BLINK value will have an invalid value - it will point to the address
1583 after the BRK_S (which was there at the moment of branch execution) while
1584 it should point to the address after the 32-bit long instruction. To avoid
1585 such issues this function disassembles instruction at target location and
1586 evaluates it value.
1587
1588 ARC 600 supports only 16-bit BRK_S.
1589
1590 NB: Baremetal GDB uses BRK[_S], while user-space GDB uses TRAP_S. BRK[_S]
1591 is much better because it doesn't commit unlike TRAP_S, so it can be set in
1592 delay slots; however it cannot be used in user-mode, hence usage of TRAP_S
1593 in GDB for user-space. */
1594
1595 /* Implement the "breakpoint_kind_from_pc" gdbarch method. */
1596
1597 static int
1598 arc_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1599 {
1600 size_t length_with_limm = gdb_insn_length (gdbarch, *pcptr);
1601
1602 /* Replace 16-bit instruction with BRK_S, replace 32-bit instructions with
1603 BRK. LIMM is part of instruction length, so it can be either 4 or 8
1604 bytes for 32-bit instructions. */
1605 if ((length_with_limm == 4 || length_with_limm == 8)
1606 && !arc_mach_is_arc600 (gdbarch))
1607 return sizeof (arc_brk_le);
1608 else
1609 return sizeof (arc_brk_s_le);
1610 }
1611
1612 /* Implement the "sw_breakpoint_from_kind" gdbarch method. */
1613
1614 static const gdb_byte *
1615 arc_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1616 {
1617 gdb_assert (kind == 2 || kind == 4);
1618 *size = kind;
1619
1620 if (kind == sizeof (arc_brk_le))
1621 {
1622 return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1623 ? arc_brk_be
1624 : arc_brk_le);
1625 }
1626 else
1627 {
1628 return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1629 ? arc_brk_s_be
1630 : arc_brk_s_le);
1631 }
1632 }
1633
1634 /* Implement the "frame_align" gdbarch method. */
1635
1636 static CORE_ADDR
1637 arc_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1638 {
1639 return align_down (sp, 4);
1640 }
1641
1642 /* Dump the frame info. Used for internal debugging only. */
1643
1644 static void
1645 arc_print_frame_cache (struct gdbarch *gdbarch, const char *message,
1646 struct arc_frame_cache *cache, int addresses_known)
1647 {
1648 arc_debug_printf ("frame_info %s", message);
1649 arc_debug_printf ("prev_sp = %s", paddress (gdbarch, cache->prev_sp));
1650 arc_debug_printf ("frame_base_reg = %i", cache->frame_base_reg);
1651 arc_debug_printf ("frame_base_offset = %s",
1652 plongest (cache->frame_base_offset));
1653
1654 for (int i = 0; i <= ARC_BLINK_REGNUM; i++)
1655 {
1656 if (cache->saved_regs[i].is_addr ())
1657 arc_debug_printf ("saved register %s at %s %s",
1658 gdbarch_register_name (gdbarch, i),
1659 (addresses_known) ? "address" : "offset",
1660 paddress (gdbarch, cache->saved_regs[i].addr ()));
1661 }
1662 }
1663
1664 /* Frame unwinder for normal frames. */
1665
1666 static struct arc_frame_cache *
1667 arc_make_frame_cache (struct frame_info *this_frame)
1668 {
1669 arc_debug_printf ("called");
1670
1671 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1672
1673 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1674 CORE_ADDR entrypoint, prologue_end;
1675 if (find_pc_partial_function (block_addr, NULL, &entrypoint, &prologue_end))
1676 {
1677 struct symtab_and_line sal = find_pc_line (entrypoint, 0);
1678 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1679 if (sal.line == 0)
1680 /* No line info so use current PC. */
1681 prologue_end = prev_pc;
1682 else if (sal.end < prologue_end)
1683 /* The next line begins after the function end. */
1684 prologue_end = sal.end;
1685
1686 prologue_end = std::min (prologue_end, prev_pc);
1687 }
1688 else
1689 {
1690 /* If find_pc_partial_function returned nothing then there is no symbol
1691 information at all for this PC. Currently it is assumed in this case
1692 that current PC is entrypoint to function and try to construct the
1693 frame from that. This is, probably, suboptimal, for example ARM
1694 assumes in this case that program is inside the normal frame (with
1695 frame pointer). ARC, perhaps, should try to do the same. */
1696 entrypoint = get_frame_register_unsigned (this_frame,
1697 gdbarch_pc_regnum (gdbarch));
1698 prologue_end = entrypoint + MAX_PROLOGUE_LENGTH;
1699 }
1700
1701 /* Allocate new frame cache instance and space for saved register info.
1702 FRAME_OBSTACK_ZALLOC will initialize fields to zeroes. */
1703 struct arc_frame_cache *cache
1704 = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
1705 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1706
1707 arc_analyze_prologue (gdbarch, entrypoint, prologue_end, cache);
1708
1709 if (arc_debug)
1710 arc_print_frame_cache (gdbarch, "after prologue", cache, false);
1711
1712 CORE_ADDR unwound_fb = get_frame_register_unsigned (this_frame,
1713 cache->frame_base_reg);
1714 if (unwound_fb == 0)
1715 return cache;
1716 cache->prev_sp = unwound_fb + cache->frame_base_offset;
1717
1718 for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1719 {
1720 if (cache->saved_regs[i].is_addr ())
1721 cache->saved_regs[i].set_addr (cache->saved_regs[i].addr ()
1722 + cache->prev_sp);
1723 }
1724
1725 if (arc_debug)
1726 arc_print_frame_cache (gdbarch, "after previous SP found", cache, true);
1727
1728 return cache;
1729 }
1730
1731 /* Implement the "this_id" frame_unwind method. */
1732
1733 static void
1734 arc_frame_this_id (struct frame_info *this_frame, void **this_cache,
1735 struct frame_id *this_id)
1736 {
1737 arc_debug_printf ("called");
1738
1739 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1740
1741 if (*this_cache == NULL)
1742 *this_cache = arc_make_frame_cache (this_frame);
1743 struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
1744
1745 CORE_ADDR stack_addr = cache->prev_sp;
1746
1747 /* There are 4 possible situation which decide how frame_id->code_addr is
1748 evaluated:
1749
1750 1) Function is compiled with option -g. Then frame_id will be created
1751 in dwarf_* function and not in this function. NB: even if target
1752 binary is compiled with -g, some std functions like __start and _init
1753 are not, so they still will follow one of the following choices.
1754
1755 2) Function is compiled without -g and binary hasn't been stripped in
1756 any way. In this case GDB still has enough information to evaluate
1757 frame code_addr properly. This case is covered by call to
1758 get_frame_func ().
1759
1760 3) Binary has been striped with option -g (strip debug symbols). In
1761 this case there is still enough symbols for get_frame_func () to work
1762 properly, so this case is also covered by it.
1763
1764 4) Binary has been striped with option -s (strip all symbols). In this
1765 case GDB cannot get function start address properly, so we return current
1766 PC value instead.
1767 */
1768 CORE_ADDR code_addr = get_frame_func (this_frame);
1769 if (code_addr == 0)
1770 code_addr = get_frame_register_unsigned (this_frame,
1771 gdbarch_pc_regnum (gdbarch));
1772
1773 *this_id = frame_id_build (stack_addr, code_addr);
1774 }
1775
1776 /* Implement the "prev_register" frame_unwind method. */
1777
1778 static struct value *
1779 arc_frame_prev_register (struct frame_info *this_frame,
1780 void **this_cache, int regnum)
1781 {
1782 if (*this_cache == NULL)
1783 *this_cache = arc_make_frame_cache (this_frame);
1784 struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
1785
1786 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1787
1788 /* If we are asked to unwind the PC, then we need to return BLINK instead:
1789 the saved value of PC points into this frame's function's prologue, not
1790 the next frame's function's resume location. */
1791 if (regnum == gdbarch_pc_regnum (gdbarch))
1792 regnum = ARC_BLINK_REGNUM;
1793
1794 /* SP is a special case - we should return prev_sp, because
1795 trad_frame_get_prev_register will return _current_ SP value.
1796 Alternatively we could have stored cache->prev_sp in the cache->saved
1797 regs, but here we follow the lead of AArch64, ARM and Xtensa and will
1798 leave that logic in this function, instead of prologue analyzers. That I
1799 think is a bit more clear as `saved_regs` should contain saved regs, not
1800 computable.
1801
1802 Because value has been computed, "got_constant" should be used, so that
1803 returned value will be a "not_lval" - immutable. */
1804
1805 if (regnum == gdbarch_sp_regnum (gdbarch))
1806 return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
1807
1808 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
1809 }
1810
1811 /* Implement the "init_reg" dwarf2_frame method. */
1812
1813 static void
1814 arc_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1815 struct dwarf2_frame_state_reg *reg,
1816 struct frame_info *info)
1817 {
1818 if (regnum == gdbarch_pc_regnum (gdbarch))
1819 /* The return address column. */
1820 reg->how = DWARF2_FRAME_REG_RA;
1821 else if (regnum == gdbarch_sp_regnum (gdbarch))
1822 /* The call frame address. */
1823 reg->how = DWARF2_FRAME_REG_CFA;
1824 }
1825
1826 /* Signal trampoline frame unwinder. Allows frame unwinding to happen
1827 from within signal handlers. */
1828
1829 static struct arc_frame_cache *
1830 arc_make_sigtramp_frame_cache (struct frame_info *this_frame)
1831 {
1832 arc_debug_printf ("called");
1833
1834 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
1835
1836 /* Allocate new frame cache instance and space for saved register info. */
1837 struct arc_frame_cache *cache = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
1838 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1839
1840 /* Get the stack pointer and use it as the frame base. */
1841 cache->prev_sp = arc_frame_base_address (this_frame, NULL);
1842
1843 /* If the ARC-private target-dependent info doesn't have a table of
1844 offsets of saved register contents within an OS signal context
1845 structure, then there is nothing to analyze. */
1846 if (tdep->sc_reg_offset == NULL)
1847 return cache;
1848
1849 /* Find the address of the sigcontext structure. */
1850 CORE_ADDR addr = tdep->sigcontext_addr (this_frame);
1851
1852 /* For each register, if its contents have been saved within the
1853 sigcontext structure, determine the address of those contents. */
1854 gdb_assert (tdep->sc_num_regs <= (ARC_LAST_REGNUM + 1));
1855 for (int i = 0; i < tdep->sc_num_regs; i++)
1856 {
1857 if (tdep->sc_reg_offset[i] != ARC_OFFSET_NO_REGISTER)
1858 cache->saved_regs[i].set_addr (addr + tdep->sc_reg_offset[i]);
1859 }
1860
1861 return cache;
1862 }
1863
1864 /* Implement the "this_id" frame_unwind method for signal trampoline
1865 frames. */
1866
1867 static void
1868 arc_sigtramp_frame_this_id (struct frame_info *this_frame,
1869 void **this_cache, struct frame_id *this_id)
1870 {
1871 arc_debug_printf ("called");
1872
1873 if (*this_cache == NULL)
1874 *this_cache = arc_make_sigtramp_frame_cache (this_frame);
1875
1876 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1877 struct arc_frame_cache *cache = (struct arc_frame_cache *) *this_cache;
1878 CORE_ADDR stack_addr = cache->prev_sp;
1879 CORE_ADDR code_addr
1880 = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
1881 *this_id = frame_id_build (stack_addr, code_addr);
1882 }
1883
1884 /* Get a register from a signal handler frame. */
1885
1886 static struct value *
1887 arc_sigtramp_frame_prev_register (struct frame_info *this_frame,
1888 void **this_cache, int regnum)
1889 {
1890 arc_debug_printf ("regnum = %d", regnum);
1891
1892 /* Make sure we've initialized the cache. */
1893 if (*this_cache == NULL)
1894 *this_cache = arc_make_sigtramp_frame_cache (this_frame);
1895
1896 struct arc_frame_cache *cache = (struct arc_frame_cache *) *this_cache;
1897 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
1898 }
1899
1900 /* Frame sniffer for signal handler frame. Only recognize a frame if we
1901 have a sigcontext_addr handler in the target dependency. */
1902
1903 static int
1904 arc_sigtramp_frame_sniffer (const struct frame_unwind *self,
1905 struct frame_info *this_frame,
1906 void **this_cache)
1907 {
1908 struct gdbarch_tdep *tdep;
1909
1910 arc_debug_printf ("called");
1911
1912 tdep = gdbarch_tdep (get_frame_arch (this_frame));
1913
1914 /* If we have a sigcontext_addr handler, then just return 1 (same as the
1915 "default_frame_sniffer ()"). */
1916 return (tdep->sigcontext_addr != NULL && tdep->is_sigtramp != NULL
1917 && tdep->is_sigtramp (this_frame));
1918 }
1919
1920 /* Structure defining the ARC ordinary frame unwind functions. Since we are
1921 the fallback unwinder, we use the default frame sniffer, which always
1922 accepts the frame. */
1923
1924 static const struct frame_unwind arc_frame_unwind = {
1925 "arc prologue",
1926 NORMAL_FRAME,
1927 default_frame_unwind_stop_reason,
1928 arc_frame_this_id,
1929 arc_frame_prev_register,
1930 NULL,
1931 default_frame_sniffer,
1932 NULL,
1933 NULL
1934 };
1935
1936 /* Structure defining the ARC signal frame unwind functions. Custom
1937 sniffer is used, because this frame must be accepted only in the right
1938 context. */
1939
1940 static const struct frame_unwind arc_sigtramp_frame_unwind = {
1941 "arc sigtramp",
1942 SIGTRAMP_FRAME,
1943 default_frame_unwind_stop_reason,
1944 arc_sigtramp_frame_this_id,
1945 arc_sigtramp_frame_prev_register,
1946 NULL,
1947 arc_sigtramp_frame_sniffer,
1948 NULL,
1949 NULL
1950 };
1951
1952
1953 static const struct frame_base arc_normal_base = {
1954 &arc_frame_unwind,
1955 arc_frame_base_address,
1956 arc_frame_base_address,
1957 arc_frame_base_address
1958 };
1959
1960 /* Add all the expected register sets into GDBARCH. */
1961
1962 static void
1963 arc_add_reggroups (struct gdbarch *gdbarch)
1964 {
1965 reggroup_add (gdbarch, general_reggroup);
1966 reggroup_add (gdbarch, float_reggroup);
1967 reggroup_add (gdbarch, system_reggroup);
1968 reggroup_add (gdbarch, vector_reggroup);
1969 reggroup_add (gdbarch, all_reggroup);
1970 reggroup_add (gdbarch, save_reggroup);
1971 reggroup_add (gdbarch, restore_reggroup);
1972 }
1973
1974 static enum arc_isa
1975 mach_type_to_arc_isa (const unsigned long mach)
1976 {
1977 switch (mach)
1978 {
1979 case bfd_mach_arc_arc600:
1980 case bfd_mach_arc_arc601:
1981 case bfd_mach_arc_arc700:
1982 return ARC_ISA_ARCV1;
1983 case bfd_mach_arc_arcv2:
1984 return ARC_ISA_ARCV2;
1985 default:
1986 internal_error (__FILE__, __LINE__,
1987 _("unknown machine id %lu"), mach);
1988 }
1989 }
1990
1991 /* See arc-tdep.h. */
1992
1993 arc_arch_features
1994 arc_arch_features_create (const bfd *abfd, const unsigned long mach)
1995 {
1996 /* Use 4 as a fallback value. */
1997 int reg_size = 4;
1998
1999 /* Try to guess the features parameters by looking at the binary to be
2000 executed. If the user is providing a binary that does not match the
2001 target, then tough luck. This is the last effort to makes sense of
2002 what's going on. */
2003 if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2004 {
2005 unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
2006
2007 if (eclass == ELFCLASS32)
2008 reg_size = 4;
2009 else if (eclass == ELFCLASS64)
2010 reg_size = 8;
2011 else
2012 internal_error (__FILE__, __LINE__,
2013 _("unknown ELF header class %d"), eclass);
2014 }
2015
2016 /* MACH from a bfd_arch_info struct is used here. It should be a safe
2017 bet, as it looks like the struct is always initialized even when we
2018 don't pass any elf file to GDB at all (it uses default arch in that
2019 case). */
2020 arc_isa isa = mach_type_to_arc_isa (mach);
2021
2022 return arc_arch_features (reg_size, isa);
2023 }
2024
2025 /* Look for obsolete core feature names in TDESC. */
2026
2027 static const struct tdesc_feature *
2028 find_obsolete_core_names (const struct target_desc *tdesc)
2029 {
2030 const struct tdesc_feature *feat = nullptr;
2031
2032 feat = tdesc_find_feature (tdesc, ARC_CORE_V1_OBSOLETE_FEATURE_NAME);
2033
2034 if (feat == nullptr)
2035 feat = tdesc_find_feature (tdesc, ARC_CORE_V2_OBSOLETE_FEATURE_NAME);
2036
2037 if (feat == nullptr)
2038 feat = tdesc_find_feature
2039 (tdesc, ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME);
2040
2041 return feat;
2042 }
2043
2044 /* Look for obsolete aux feature names in TDESC. */
2045
2046 static const struct tdesc_feature *
2047 find_obsolete_aux_names (const struct target_desc *tdesc)
2048 {
2049 return tdesc_find_feature (tdesc, ARC_AUX_OBSOLETE_FEATURE_NAME);
2050 }
2051
2052 /* Based on the MACH value, determines which core register features set
2053 must be used. */
2054
2055 static arc_register_feature *
2056 determine_core_reg_feature_set (const unsigned long mach)
2057 {
2058 switch (mach_type_to_arc_isa (mach))
2059 {
2060 case ARC_ISA_ARCV1:
2061 return &arc_v1_core_reg_feature;
2062 case ARC_ISA_ARCV2:
2063 return &arc_v2_core_reg_feature;
2064 default:
2065 gdb_assert_not_reached
2066 ("Unknown machine type to determine the core feature set.");
2067 }
2068 }
2069
2070 /* At the moment, there is only 1 auxiliary register features set.
2071 This is a place holder for future extendability. */
2072
2073 static const arc_register_feature *
2074 determine_aux_reg_feature_set ()
2075 {
2076 return &arc_common_aux_reg_feature;
2077 }
2078
2079 /* Update accumulator register names (ACCH/ACCL) for r58 and r59 in the
2080 register sets. The endianness determines the assignment:
2081
2082 ,------.------.
2083 | acch | accl |
2084 ,----|------+------|
2085 | LE | r59 | r58 |
2086 | BE | r58 | r59 |
2087 `----^------^------' */
2088
2089 static void
2090 arc_update_acc_reg_names (const int byte_order)
2091 {
2092 const char *r58_alias
2093 = byte_order == BFD_ENDIAN_LITTLE ? "accl" : "acch";
2094 const char *r59_alias
2095 = byte_order == BFD_ENDIAN_LITTLE ? "acch" : "accl";
2096
2097 /* Subscript 1 must be OK because those registers have 2 names. */
2098 arc_v1_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
2099 arc_v1_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
2100 arc_v2_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
2101 arc_v2_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
2102 }
2103
2104 /* Go through all the registers in REG_SET and check if they exist
2105 in FEATURE. The TDESC_DATA is updated with the register number
2106 in REG_SET if it is found in the feature. If a required register
2107 is not found, this function returns false. */
2108
2109 static bool
2110 arc_check_tdesc_feature (struct tdesc_arch_data *tdesc_data,
2111 const struct tdesc_feature *feature,
2112 const struct arc_register_feature *reg_set)
2113 {
2114 for (const auto &reg : reg_set->registers)
2115 {
2116 bool found = false;
2117
2118 for (const char *name : reg.names)
2119 {
2120 found
2121 = tdesc_numbered_register (feature, tdesc_data, reg.regnum, name);
2122
2123 if (found)
2124 break;
2125 }
2126
2127 if (!found && reg.required_p)
2128 {
2129 std::ostringstream reg_names;
2130 for (std::size_t i = 0; i < reg.names.size(); ++i)
2131 {
2132 if (i == 0)
2133 reg_names << "'" << reg.names[0] << "'";
2134 else
2135 reg_names << " or '" << reg.names[0] << "'";
2136 }
2137 arc_print (_("Error: Cannot find required register(s) %s "
2138 "in feature '%s'.\n"), reg_names.str ().c_str (),
2139 feature->name.c_str ());
2140 return false;
2141 }
2142 }
2143
2144 return true;
2145 }
2146
2147 /* Check for the existance of "lp_start" and "lp_end" in target description.
2148 If both are present, assume there is hardware loop support in the target.
2149 This can be improved by looking into "lpc_size" field of "isa_config"
2150 auxiliary register. */
2151
2152 static bool
2153 arc_check_for_hw_loops (const struct target_desc *tdesc,
2154 struct tdesc_arch_data *data)
2155 {
2156 const auto feature_aux = tdesc_find_feature (tdesc, ARC_AUX_FEATURE_NAME);
2157 const auto aux_regset = determine_aux_reg_feature_set ();
2158
2159 if (feature_aux == nullptr)
2160 return false;
2161
2162 bool hw_loop_p = false;
2163 const auto lp_start_name =
2164 aux_regset->registers[ARC_LP_START_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
2165 const auto lp_end_name =
2166 aux_regset->registers[ARC_LP_END_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
2167
2168 hw_loop_p = tdesc_numbered_register (feature_aux, data,
2169 ARC_LP_START_REGNUM, lp_start_name);
2170 hw_loop_p &= tdesc_numbered_register (feature_aux, data,
2171 ARC_LP_END_REGNUM, lp_end_name);
2172
2173 return hw_loop_p;
2174 }
2175
2176 /* Initialize target description for the ARC.
2177
2178 Returns true if input TDESC was valid and in this case it will assign TDESC
2179 and TDESC_DATA output parameters. */
2180
2181 static bool
2182 arc_tdesc_init (struct gdbarch_info info, const struct target_desc **tdesc,
2183 tdesc_arch_data_up *tdesc_data)
2184 {
2185 const struct target_desc *tdesc_loc = info.target_desc;
2186 arc_debug_printf ("Target description initialization.");
2187
2188 /* If target doesn't provide a description, use the default ones. */
2189 if (!tdesc_has_registers (tdesc_loc))
2190 {
2191 arc_arch_features features
2192 = arc_arch_features_create (info.abfd,
2193 info.bfd_arch_info->mach);
2194 tdesc_loc = arc_lookup_target_description (features);
2195 }
2196 gdb_assert (tdesc_loc != nullptr);
2197
2198 arc_debug_printf ("Have got a target description");
2199
2200 const struct tdesc_feature *feature_core
2201 = tdesc_find_feature (tdesc_loc, ARC_CORE_FEATURE_NAME);
2202 const struct tdesc_feature *feature_aux
2203 = tdesc_find_feature (tdesc_loc, ARC_AUX_FEATURE_NAME);
2204
2205 /* Maybe there still is a chance to salvage the input. */
2206 if (feature_core == nullptr)
2207 feature_core = find_obsolete_core_names (tdesc_loc);
2208 if (feature_aux == nullptr)
2209 feature_aux = find_obsolete_aux_names (tdesc_loc);
2210
2211 if (feature_core == nullptr)
2212 {
2213 arc_print (_("Error: Cannot find required feature '%s' in supplied "
2214 "target description.\n"), ARC_CORE_FEATURE_NAME);
2215 return false;
2216 }
2217
2218 if (feature_aux == nullptr)
2219 {
2220 arc_print (_("Error: Cannot find required feature '%s' in supplied "
2221 "target description.\n"), ARC_AUX_FEATURE_NAME);
2222 return false;
2223 }
2224
2225 const arc_register_feature *arc_core_reg_feature
2226 = determine_core_reg_feature_set (info.bfd_arch_info->mach);
2227 const arc_register_feature *arc_aux_reg_feature
2228 = determine_aux_reg_feature_set ();
2229
2230 tdesc_arch_data_up tdesc_data_loc = tdesc_data_alloc ();
2231
2232 arc_update_acc_reg_names (info.byte_order);
2233
2234 bool valid_p = arc_check_tdesc_feature (tdesc_data_loc.get (),
2235 feature_core,
2236 arc_core_reg_feature);
2237
2238 valid_p &= arc_check_tdesc_feature (tdesc_data_loc.get (),
2239 feature_aux,
2240 arc_aux_reg_feature);
2241
2242 if (!valid_p)
2243 {
2244 arc_debug_printf ("Target description is not valid");
2245 return false;
2246 }
2247
2248 *tdesc = tdesc_loc;
2249 *tdesc_data = std::move (tdesc_data_loc);
2250
2251 return true;
2252 }
2253
2254 /* Implement the type_align gdbarch function. */
2255
2256 static ULONGEST
2257 arc_type_align (struct gdbarch *gdbarch, struct type *type)
2258 {
2259 switch (type->code ())
2260 {
2261 case TYPE_CODE_PTR:
2262 case TYPE_CODE_FUNC:
2263 case TYPE_CODE_FLAGS:
2264 case TYPE_CODE_INT:
2265 case TYPE_CODE_RANGE:
2266 case TYPE_CODE_FLT:
2267 case TYPE_CODE_ENUM:
2268 case TYPE_CODE_REF:
2269 case TYPE_CODE_RVALUE_REF:
2270 case TYPE_CODE_CHAR:
2271 case TYPE_CODE_BOOL:
2272 case TYPE_CODE_DECFLOAT:
2273 case TYPE_CODE_METHODPTR:
2274 case TYPE_CODE_MEMBERPTR:
2275 type = check_typedef (type);
2276 return std::min<ULONGEST> (4, TYPE_LENGTH (type));
2277 default:
2278 return 0;
2279 }
2280 }
2281
2282 /* Implement the "init" gdbarch method. */
2283
2284 static struct gdbarch *
2285 arc_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2286 {
2287 const struct target_desc *tdesc;
2288 tdesc_arch_data_up tdesc_data;
2289
2290 arc_debug_printf ("Architecture initialization.");
2291
2292 if (!arc_tdesc_init (info, &tdesc, &tdesc_data))
2293 return nullptr;
2294
2295 /* Allocate the ARC-private target-dependent information structure, and the
2296 GDB target-independent information structure. */
2297 gdb::unique_xmalloc_ptr<struct gdbarch_tdep> tdep
2298 (XCNEW (struct gdbarch_tdep));
2299 tdep->jb_pc = -1; /* No longjmp support by default. */
2300 tdep->has_hw_loops = arc_check_for_hw_loops (tdesc, tdesc_data.get ());
2301 struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep.release ());
2302
2303 /* Data types. */
2304 set_gdbarch_short_bit (gdbarch, 16);
2305 set_gdbarch_int_bit (gdbarch, 32);
2306 set_gdbarch_long_bit (gdbarch, 32);
2307 set_gdbarch_long_long_bit (gdbarch, 64);
2308 set_gdbarch_type_align (gdbarch, arc_type_align);
2309 set_gdbarch_float_bit (gdbarch, 32);
2310 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2311 set_gdbarch_double_bit (gdbarch, 64);
2312 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2313 set_gdbarch_ptr_bit (gdbarch, 32);
2314 set_gdbarch_addr_bit (gdbarch, 32);
2315 set_gdbarch_char_signed (gdbarch, 0);
2316
2317 set_gdbarch_write_pc (gdbarch, arc_write_pc);
2318
2319 set_gdbarch_virtual_frame_pointer (gdbarch, arc_virtual_frame_pointer);
2320
2321 /* tdesc_use_registers expects gdbarch_num_regs to return number of registers
2322 parsed by gdbarch_init, and then it will add all of the remaining
2323 registers and will increase number of registers. */
2324 set_gdbarch_num_regs (gdbarch, ARC_LAST_REGNUM + 1);
2325 set_gdbarch_num_pseudo_regs (gdbarch, 0);
2326 set_gdbarch_sp_regnum (gdbarch, ARC_SP_REGNUM);
2327 set_gdbarch_pc_regnum (gdbarch, ARC_PC_REGNUM);
2328 set_gdbarch_ps_regnum (gdbarch, ARC_STATUS32_REGNUM);
2329 set_gdbarch_fp0_regnum (gdbarch, -1); /* No FPU registers. */
2330
2331 set_gdbarch_push_dummy_call (gdbarch, arc_push_dummy_call);
2332 set_gdbarch_push_dummy_code (gdbarch, arc_push_dummy_code);
2333
2334 set_gdbarch_cannot_fetch_register (gdbarch, arc_cannot_fetch_register);
2335 set_gdbarch_cannot_store_register (gdbarch, arc_cannot_store_register);
2336
2337 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2338
2339 set_gdbarch_return_value (gdbarch, arc_return_value);
2340
2341 set_gdbarch_skip_prologue (gdbarch, arc_skip_prologue);
2342 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2343
2344 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arc_breakpoint_kind_from_pc);
2345 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arc_sw_breakpoint_from_kind);
2346
2347 /* On ARC 600 BRK_S instruction advances PC, unlike other ARC cores. */
2348 if (!arc_mach_is_arc600 (gdbarch))
2349 set_gdbarch_decr_pc_after_break (gdbarch, 0);
2350 else
2351 set_gdbarch_decr_pc_after_break (gdbarch, 2);
2352
2353 set_gdbarch_frame_align (gdbarch, arc_frame_align);
2354
2355 set_gdbarch_print_insn (gdbarch, arc_delayed_print_insn);
2356
2357 set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
2358
2359 /* "nonsteppable" watchpoint means that watchpoint triggers before
2360 instruction is committed, therefore it is required to remove watchpoint
2361 to step though instruction that triggers it. ARC watchpoints trigger
2362 only after instruction is committed, thus there is no need to remove
2363 them. In fact on ARC watchpoint for memory writes may trigger with more
2364 significant delay, like one or two instructions, depending on type of
2365 memory where write is performed (CCM or external) and next instruction
2366 after the memory write. */
2367 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 0);
2368
2369 /* This doesn't include possible long-immediate value. */
2370 set_gdbarch_max_insn_length (gdbarch, 4);
2371
2372 /* Add default register groups. */
2373 arc_add_reggroups (gdbarch);
2374
2375 /* Frame unwinders and sniffers. */
2376 dwarf2_frame_set_init_reg (gdbarch, arc_dwarf2_frame_init_reg);
2377 dwarf2_append_unwinders (gdbarch);
2378 frame_unwind_append_unwinder (gdbarch, &arc_sigtramp_frame_unwind);
2379 frame_unwind_append_unwinder (gdbarch, &arc_frame_unwind);
2380 frame_base_set_default (gdbarch, &arc_normal_base);
2381
2382 /* Setup stuff specific to a particular environment (baremetal or Linux).
2383 It can override functions set earlier. */
2384 gdbarch_init_osabi (info, gdbarch);
2385
2386 if (gdbarch_tdep (gdbarch)->jb_pc >= 0)
2387 set_gdbarch_get_longjmp_target (gdbarch, arc_get_longjmp_target);
2388
2389 /* Disassembler options. Enforce CPU if it was specified in XML target
2390 description, otherwise use default method of determining CPU (ELF private
2391 header). */
2392 if (info.target_desc != NULL)
2393 {
2394 const struct bfd_arch_info *tdesc_arch
2395 = tdesc_architecture (info.target_desc);
2396 if (tdesc_arch != NULL)
2397 {
2398 xfree (arc_disassembler_options);
2399 /* FIXME: It is not really good to change disassembler options
2400 behind the scene, because that might override options
2401 specified by the user. However as of now ARC doesn't support
2402 `set disassembler-options' hence this code is the only place
2403 where options are changed. It also changes options for all
2404 existing gdbarches, which also can be problematic, if
2405 arc_gdbarch_init will start reusing existing gdbarch
2406 instances. */
2407 /* Target description specifies a BFD architecture, which is
2408 different from ARC cpu, as accepted by disassembler (and most
2409 other ARC tools), because cpu values are much more fine grained -
2410 there can be multiple cpu values per single BFD architecture. As
2411 a result this code should translate architecture to some cpu
2412 value. Since there is no info on exact cpu configuration, it is
2413 best to use the most feature-rich CPU, so that disassembler will
2414 recognize all instructions available to the specified
2415 architecture. */
2416 switch (tdesc_arch->mach)
2417 {
2418 case bfd_mach_arc_arc601:
2419 arc_disassembler_options = xstrdup ("cpu=arc601");
2420 break;
2421 case bfd_mach_arc_arc600:
2422 arc_disassembler_options = xstrdup ("cpu=arc600");
2423 break;
2424 case bfd_mach_arc_arc700:
2425 arc_disassembler_options = xstrdup ("cpu=arc700");
2426 break;
2427 case bfd_mach_arc_arcv2:
2428 /* Machine arcv2 has three arches: ARCv2, EM and HS; where ARCv2
2429 is treated as EM. */
2430 if (arc_arch_is_hs (tdesc_arch))
2431 arc_disassembler_options = xstrdup ("cpu=hs38_linux");
2432 else
2433 arc_disassembler_options = xstrdup ("cpu=em4_fpuda");
2434 break;
2435 default:
2436 arc_disassembler_options = NULL;
2437 break;
2438 }
2439 }
2440 }
2441
2442 set_gdbarch_disassembler_options (gdbarch, &arc_disassembler_options);
2443 set_gdbarch_valid_disassembler_options (gdbarch,
2444 disassembler_options_arc ());
2445
2446 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
2447
2448 return gdbarch;
2449 }
2450
2451 /* Implement the "dump_tdep" gdbarch method. */
2452
2453 static void
2454 arc_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2455 {
2456 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2457
2458 fprintf_unfiltered (file, "arc_dump_tdep: jb_pc = %i\n", tdep->jb_pc);
2459
2460 fprintf_unfiltered (file, "arc_dump_tdep: is_sigtramp = <%s>\n",
2461 host_address_to_string (tdep->is_sigtramp));
2462 fprintf_unfiltered (file, "arc_dump_tdep: sigcontext_addr = <%s>\n",
2463 host_address_to_string (tdep->sigcontext_addr));
2464 fprintf_unfiltered (file, "arc_dump_tdep: sc_reg_offset = <%s>\n",
2465 host_address_to_string (tdep->sc_reg_offset));
2466 fprintf_unfiltered (file, "arc_dump_tdep: sc_num_regs = %d\n",
2467 tdep->sc_num_regs);
2468 }
2469
2470 /* This command accepts single argument - address of instruction to
2471 disassemble. */
2472
2473 static void
2474 dump_arc_instruction_command (const char *args, int from_tty)
2475 {
2476 struct value *val;
2477 if (args != NULL && strlen (args) > 0)
2478 val = evaluate_expression (parse_expression (args).get ());
2479 else
2480 val = access_value_history (0);
2481 record_latest_value (val);
2482
2483 CORE_ADDR address = value_as_address (val);
2484 struct arc_instruction insn;
2485 struct disassemble_info di = arc_disassemble_info (target_gdbarch ());
2486 arc_insn_decode (address, &di, arc_delayed_print_insn, &insn);
2487 arc_insn_dump (insn);
2488 }
2489
2490 void _initialize_arc_tdep ();
2491 void
2492 _initialize_arc_tdep ()
2493 {
2494 gdbarch_register (bfd_arch_arc, arc_gdbarch_init, arc_dump_tdep);
2495
2496 /* Register ARC-specific commands with gdb. */
2497
2498 /* Add root prefix command for "maintenance print arc" commands. */
2499 add_show_prefix_cmd ("arc", class_maintenance,
2500 _("ARC-specific maintenance commands for printing GDB "
2501 "internal state."),
2502 &maintenance_print_arc_list,
2503 0, &maintenanceprintlist);
2504
2505 add_cmd ("arc-instruction", class_maintenance,
2506 dump_arc_instruction_command,
2507 _("Dump arc_instruction structure for specified address."),
2508 &maintenance_print_arc_list);
2509
2510 /* Debug internals for ARC GDB. */
2511 add_setshow_boolean_cmd ("arc", class_maintenance,
2512 &arc_debug,
2513 _("Set ARC specific debugging."),
2514 _("Show ARC specific debugging."),
2515 _("When set, ARC specific debugging is enabled."),
2516 NULL, NULL, &setdebuglist, &showdebuglist);
2517 }
This page took 0.107978 seconds and 4 git commands to generate.