Document array indexing for Python gdb.Value
[binutils-gdb.git] / gdb / dwarf2 / frame.c
1 /* Frame unwinder for frames with DWARF Call Frame Information.
2
3 Copyright (C) 2003-2023 Free Software Foundation, Inc.
4
5 Contributed by Mark Kettenis.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "dwarf2/expr.h"
24 #include "dwarf2.h"
25 #include "dwarf2/leb.h"
26 #include "frame.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
29 #include "gdbcore.h"
30 #include "gdbtypes.h"
31 #include "symtab.h"
32 #include "objfiles.h"
33 #include "regcache.h"
34 #include "value.h"
35 #include "record.h"
36
37 #include "complaints.h"
38 #include "dwarf2/frame.h"
39 #include "dwarf2/read.h"
40 #include "dwarf2/public.h"
41 #include "ax.h"
42 #include "dwarf2/loc.h"
43 #include "dwarf2/frame-tailcall.h"
44 #include "gdbsupport/gdb_binary_search.h"
45 #if GDB_SELF_TEST
46 #include "gdbsupport/selftest.h"
47 #include "selftest-arch.h"
48 #endif
49 #include <unordered_map>
50
51 #include <algorithm>
52
53 struct comp_unit;
54
55 /* Call Frame Information (CFI). */
56
57 /* Common Information Entry (CIE). */
58
59 struct dwarf2_cie
60 {
61 /* Computation Unit for this CIE. */
62 struct comp_unit *unit;
63
64 /* Offset into the .debug_frame section where this CIE was found.
65 Used to identify this CIE. */
66 ULONGEST cie_pointer;
67
68 /* Constant that is factored out of all advance location
69 instructions. */
70 ULONGEST code_alignment_factor;
71
72 /* Constants that is factored out of all offset instructions. */
73 LONGEST data_alignment_factor;
74
75 /* Return address column. */
76 ULONGEST return_address_register;
77
78 /* Instruction sequence to initialize a register set. */
79 const gdb_byte *initial_instructions;
80 const gdb_byte *end;
81
82 /* Saved augmentation, in case it's needed later. */
83 const char *augmentation;
84
85 /* Encoding of addresses. */
86 gdb_byte encoding;
87
88 /* Target address size in bytes. */
89 int addr_size;
90
91 /* Target pointer size in bytes. */
92 int ptr_size;
93
94 /* True if a 'z' augmentation existed. */
95 unsigned char saw_z_augmentation;
96
97 /* True if an 'S' augmentation existed. */
98 unsigned char signal_frame;
99
100 /* The version recorded in the CIE. */
101 unsigned char version;
102
103 /* The segment size. */
104 unsigned char segment_size;
105 };
106
107 /* The CIE table is used to find CIEs during parsing, but then
108 discarded. It maps from the CIE's offset to the CIE. */
109 typedef std::unordered_map<ULONGEST, dwarf2_cie *> dwarf2_cie_table;
110
111 /* Frame Description Entry (FDE). */
112
113 struct dwarf2_fde
114 {
115 /* Return the final location in this FDE. */
116 unrelocated_addr end_addr () const
117 {
118 return (unrelocated_addr) ((ULONGEST) initial_location
119 + address_range);
120 }
121
122 /* CIE for this FDE. */
123 struct dwarf2_cie *cie;
124
125 /* First location associated with this FDE. */
126 unrelocated_addr initial_location;
127
128 /* Number of bytes of program instructions described by this FDE. */
129 ULONGEST address_range;
130
131 /* Instruction sequence. */
132 const gdb_byte *instructions;
133 const gdb_byte *end;
134
135 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
136 section. */
137 unsigned char eh_frame_p;
138 };
139
140 typedef std::vector<dwarf2_fde *> dwarf2_fde_table;
141
142 /* A minimal decoding of DWARF2 compilation units. We only decode
143 what's needed to get to the call frame information. */
144
145 struct comp_unit
146 {
147 comp_unit (struct objfile *objf)
148 : abfd (objf->obfd.get ())
149 {
150 }
151
152 /* Keep the bfd convenient. */
153 bfd *abfd;
154
155 /* Pointer to the .debug_frame section loaded into memory. */
156 const gdb_byte *dwarf_frame_buffer = nullptr;
157
158 /* Length of the loaded .debug_frame section. */
159 bfd_size_type dwarf_frame_size = 0;
160
161 /* Pointer to the .debug_frame section. */
162 asection *dwarf_frame_section = nullptr;
163
164 /* Base for DW_EH_PE_datarel encodings. */
165 bfd_vma dbase = 0;
166
167 /* Base for DW_EH_PE_textrel encodings. */
168 bfd_vma tbase = 0;
169
170 /* The FDE table. */
171 dwarf2_fde_table fde_table;
172
173 /* Hold data used by this module. */
174 auto_obstack obstack;
175 };
176
177 static struct dwarf2_fde *dwarf2_frame_find_fde
178 (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile);
179
180 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
181 int eh_frame_p);
182
183 static ULONGEST read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
184 int ptr_len, const gdb_byte *buf,
185 unsigned int *bytes_read_ptr,
186 unrelocated_addr func_base);
187 \f
188
189 /* See dwarf2/frame.h. */
190 bool dwarf2_frame_unwinders_enabled_p = true;
191
192 /* Store the length the expression for the CFA in the `cfa_reg' field,
193 which is unused in that case. */
194 #define cfa_exp_len cfa_reg
195
196 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie)
197 : pc (pc_), data_align (cie->data_alignment_factor),
198 code_align (cie->code_alignment_factor),
199 retaddr_column (cie->return_address_register)
200 {
201 }
202
203 /* Execute the required actions for both the DW_CFA_restore and
204 DW_CFA_restore_extended instructions. */
205 static void
206 dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
207 struct dwarf2_frame_state *fs, int eh_frame_p)
208 {
209 ULONGEST reg;
210
211 reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
212 fs->regs.alloc_regs (reg + 1);
213
214 /* Check if this register was explicitly initialized in the
215 CIE initial instructions. If not, default the rule to
216 UNSPECIFIED. */
217 if (reg < fs->initial.reg.size ())
218 fs->regs.reg[reg] = fs->initial.reg[reg];
219 else
220 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
221
222 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
223 {
224 int regnum = dwarf_reg_to_regnum (gdbarch, reg);
225
226 complaint (_("\
227 incomplete CFI data; DW_CFA_restore unspecified\n\
228 register %s (#%d) at %s"),
229 gdbarch_register_name (gdbarch, regnum), regnum,
230 paddress (gdbarch, fs->pc));
231 }
232 }
233
234 static CORE_ADDR
235 execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
236 frame_info_ptr this_frame, CORE_ADDR initial,
237 int initial_in_stack_memory, dwarf2_per_objfile *per_objfile)
238 {
239 dwarf_expr_context ctx (per_objfile, addr_size);
240 scoped_value_mark free_values;
241
242 ctx.push_address (initial, initial_in_stack_memory);
243 value *result_val = ctx.evaluate (exp, len, true, nullptr, this_frame);
244
245 if (result_val->lval () == lval_memory)
246 return result_val->address ();
247 else
248 return value_as_address (result_val);
249 }
250 \f
251
252 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
253 PC. Modify FS state accordingly. Return current INSN_PTR where the
254 execution has stopped, one can resume it on the next call. */
255
256 static const gdb_byte *
257 execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
258 const gdb_byte *insn_end, struct gdbarch *gdbarch,
259 CORE_ADDR pc, struct dwarf2_frame_state *fs,
260 CORE_ADDR text_offset)
261 {
262 int eh_frame_p = fde->eh_frame_p;
263 unsigned int bytes_read;
264 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
265
266 while (insn_ptr < insn_end && fs->pc <= pc)
267 {
268 gdb_byte insn = *insn_ptr++;
269 uint64_t utmp, reg;
270 int64_t offset;
271
272 if ((insn & 0xc0) == DW_CFA_advance_loc)
273 fs->pc += (insn & 0x3f) * fs->code_align;
274 else if ((insn & 0xc0) == DW_CFA_offset)
275 {
276 reg = insn & 0x3f;
277 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
278 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
279 offset = utmp * fs->data_align;
280 fs->regs.alloc_regs (reg + 1);
281 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
282 fs->regs.reg[reg].loc.offset = offset;
283 }
284 else if ((insn & 0xc0) == DW_CFA_restore)
285 {
286 reg = insn & 0x3f;
287 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
288 }
289 else
290 {
291 switch (insn)
292 {
293 case DW_CFA_set_loc:
294 fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
295 fde->cie->ptr_size, insn_ptr,
296 &bytes_read, fde->initial_location);
297 /* Apply the text offset for relocatable objects. */
298 fs->pc += text_offset;
299 insn_ptr += bytes_read;
300 break;
301
302 case DW_CFA_advance_loc1:
303 utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
304 fs->pc += utmp * fs->code_align;
305 insn_ptr++;
306 break;
307 case DW_CFA_advance_loc2:
308 utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
309 fs->pc += utmp * fs->code_align;
310 insn_ptr += 2;
311 break;
312 case DW_CFA_advance_loc4:
313 utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
314 fs->pc += utmp * fs->code_align;
315 insn_ptr += 4;
316 break;
317
318 case DW_CFA_offset_extended:
319 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
320 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
321 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
322 offset = utmp * fs->data_align;
323 fs->regs.alloc_regs (reg + 1);
324 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
325 fs->regs.reg[reg].loc.offset = offset;
326 break;
327
328 case DW_CFA_restore_extended:
329 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
330 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
331 break;
332
333 case DW_CFA_undefined:
334 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
335 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
336 fs->regs.alloc_regs (reg + 1);
337 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
338 break;
339
340 case DW_CFA_same_value:
341 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
342 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
343 fs->regs.alloc_regs (reg + 1);
344 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
345 break;
346
347 case DW_CFA_register:
348 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
349 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
350 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
351 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
352 fs->regs.alloc_regs (reg + 1);
353 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
354 fs->regs.reg[reg].loc.reg = utmp;
355 break;
356
357 case DW_CFA_remember_state:
358 {
359 struct dwarf2_frame_state_reg_info *new_rs;
360
361 new_rs = new dwarf2_frame_state_reg_info (fs->regs);
362 fs->regs.prev = new_rs;
363 }
364 break;
365
366 case DW_CFA_restore_state:
367 {
368 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
369
370 if (old_rs == NULL)
371 {
372 complaint (_("\
373 bad CFI data; mismatched DW_CFA_restore_state at %s"),
374 paddress (gdbarch, fs->pc));
375 }
376 else
377 fs->regs = std::move (*old_rs);
378 }
379 break;
380
381 case DW_CFA_def_cfa:
382 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
383 fs->regs.cfa_reg = reg;
384 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
385
386 if (fs->armcc_cfa_offsets_sf)
387 utmp *= fs->data_align;
388
389 fs->regs.cfa_offset = utmp;
390 fs->regs.cfa_how = CFA_REG_OFFSET;
391 break;
392
393 case DW_CFA_def_cfa_register:
394 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
395 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
396 eh_frame_p);
397 fs->regs.cfa_how = CFA_REG_OFFSET;
398 break;
399
400 case DW_CFA_def_cfa_offset:
401 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
402
403 if (fs->armcc_cfa_offsets_sf)
404 utmp *= fs->data_align;
405
406 fs->regs.cfa_offset = utmp;
407 /* cfa_how deliberately not set. */
408 break;
409
410 case DW_CFA_nop:
411 break;
412
413 case DW_CFA_def_cfa_expression:
414 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
415 fs->regs.cfa_exp_len = utmp;
416 fs->regs.cfa_exp = insn_ptr;
417 fs->regs.cfa_how = CFA_EXP;
418 insn_ptr += fs->regs.cfa_exp_len;
419 break;
420
421 case DW_CFA_expression:
422 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
423 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
424 fs->regs.alloc_regs (reg + 1);
425 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
426 fs->regs.reg[reg].loc.exp.start = insn_ptr;
427 fs->regs.reg[reg].loc.exp.len = utmp;
428 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
429 insn_ptr += utmp;
430 break;
431
432 case DW_CFA_offset_extended_sf:
433 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
434 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
435 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
436 offset *= fs->data_align;
437 fs->regs.alloc_regs (reg + 1);
438 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
439 fs->regs.reg[reg].loc.offset = offset;
440 break;
441
442 case DW_CFA_val_offset:
443 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
444 fs->regs.alloc_regs (reg + 1);
445 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
446 offset = utmp * fs->data_align;
447 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
448 fs->regs.reg[reg].loc.offset = offset;
449 break;
450
451 case DW_CFA_val_offset_sf:
452 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
453 fs->regs.alloc_regs (reg + 1);
454 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
455 offset *= fs->data_align;
456 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
457 fs->regs.reg[reg].loc.offset = offset;
458 break;
459
460 case DW_CFA_val_expression:
461 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
462 fs->regs.alloc_regs (reg + 1);
463 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
464 fs->regs.reg[reg].loc.exp.start = insn_ptr;
465 fs->regs.reg[reg].loc.exp.len = utmp;
466 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
467 insn_ptr += utmp;
468 break;
469
470 case DW_CFA_def_cfa_sf:
471 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
472 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
473 eh_frame_p);
474 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
475 fs->regs.cfa_offset = offset * fs->data_align;
476 fs->regs.cfa_how = CFA_REG_OFFSET;
477 break;
478
479 case DW_CFA_def_cfa_offset_sf:
480 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
481 fs->regs.cfa_offset = offset * fs->data_align;
482 /* cfa_how deliberately not set. */
483 break;
484
485 case DW_CFA_GNU_args_size:
486 /* Ignored. */
487 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
488 break;
489
490 case DW_CFA_GNU_negative_offset_extended:
491 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
492 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
493 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
494 offset = utmp * fs->data_align;
495 fs->regs.alloc_regs (reg + 1);
496 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
497 fs->regs.reg[reg].loc.offset = -offset;
498 break;
499
500 default:
501 if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user)
502 {
503 /* Handle vendor-specific CFI for different architectures. */
504 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs))
505 error (_("Call Frame Instruction op %d in vendor extension "
506 "space is not handled on this architecture."),
507 insn);
508 }
509 else
510 internal_error (_("Unknown CFI encountered."));
511 }
512 }
513 }
514
515 if (fs->initial.reg.empty ())
516 {
517 /* Don't allow remember/restore between CIE and FDE programs. */
518 delete fs->regs.prev;
519 fs->regs.prev = NULL;
520 }
521
522 return insn_ptr;
523 }
524
525 #if GDB_SELF_TEST
526
527 namespace selftests {
528
529 /* Unit test to function execute_cfa_program. */
530
531 static void
532 execute_cfa_program_test (struct gdbarch *gdbarch)
533 {
534 struct dwarf2_fde fde;
535 struct dwarf2_cie cie;
536
537 memset (&fde, 0, sizeof fde);
538 memset (&cie, 0, sizeof cie);
539
540 cie.data_alignment_factor = -4;
541 cie.code_alignment_factor = 2;
542 fde.cie = &cie;
543
544 dwarf2_frame_state fs (0, fde.cie);
545
546 gdb_byte insns[] =
547 {
548 DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
549 DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
550 DW_CFA_remember_state,
551 DW_CFA_restore_state,
552 };
553
554 const gdb_byte *insn_end = insns + sizeof (insns);
555 const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch,
556 0, &fs, 0);
557
558 SELF_CHECK (out == insn_end);
559 SELF_CHECK (fs.pc == 0);
560
561 /* The instructions above only use r1 and r2, but the register numbers
562 used are adjusted by dwarf2_frame_adjust_regnum. */
563 auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p);
564 auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p);
565
566 SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1));
567
568 SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET);
569 SELF_CHECK (fs.regs.reg[r2].loc.offset == -4);
570
571 for (auto i = 0; i < fs.regs.reg.size (); i++)
572 if (i != r2)
573 SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED);
574
575 SELF_CHECK (fs.regs.cfa_reg == 1);
576 SELF_CHECK (fs.regs.cfa_offset == 4);
577 SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET);
578 SELF_CHECK (fs.regs.cfa_exp == NULL);
579 SELF_CHECK (fs.regs.prev == NULL);
580 }
581
582 } // namespace selftests
583 #endif /* GDB_SELF_TEST */
584
585 \f
586
587 /* Architecture-specific operations. */
588
589 static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch,
590 int regnum,
591 struct dwarf2_frame_state_reg *reg,
592 frame_info_ptr this_frame);
593
594 struct dwarf2_frame_ops
595 {
596 /* Pre-initialize the register state REG for register REGNUM. */
597 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
598 frame_info_ptr)
599 = dwarf2_frame_default_init_reg;
600
601 /* Check whether the THIS_FRAME is a signal trampoline. */
602 int (*signal_frame_p) (struct gdbarch *, frame_info_ptr) = nullptr;
603
604 /* Convert .eh_frame register number to DWARF register number, or
605 adjust .debug_frame register number. */
606 int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr;
607 };
608
609 /* Per-architecture data key. */
610 static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data;
611
612 /* Get or initialize the frame ops. */
613 static dwarf2_frame_ops *
614 get_frame_ops (struct gdbarch *gdbarch)
615 {
616 dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch);
617 if (result == nullptr)
618 result = dwarf2_frame_data.emplace (gdbarch);
619 return result;
620 }
621
622 /* Default architecture-specific register state initialization
623 function. */
624
625 static void
626 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
627 struct dwarf2_frame_state_reg *reg,
628 frame_info_ptr this_frame)
629 {
630 /* If we have a register that acts as a program counter, mark it as
631 a destination for the return address. If we have a register that
632 serves as the stack pointer, arrange for it to be filled with the
633 call frame address (CFA). The other registers are marked as
634 unspecified.
635
636 We copy the return address to the program counter, since many
637 parts in GDB assume that it is possible to get the return address
638 by unwinding the program counter register. However, on ISA's
639 with a dedicated return address register, the CFI usually only
640 contains information to unwind that return address register.
641
642 The reason we're treating the stack pointer special here is
643 because in many cases GCC doesn't emit CFI for the stack pointer
644 and implicitly assumes that it is equal to the CFA. This makes
645 some sense since the DWARF specification (version 3, draft 8,
646 p. 102) says that:
647
648 "Typically, the CFA is defined to be the value of the stack
649 pointer at the call site in the previous frame (which may be
650 different from its value on entry to the current frame)."
651
652 However, this isn't true for all platforms supported by GCC
653 (e.g. IBM S/390 and zSeries). Those architectures should provide
654 their own architecture-specific initialization function. */
655
656 if (regnum == gdbarch_pc_regnum (gdbarch))
657 reg->how = DWARF2_FRAME_REG_RA;
658 else if (regnum == gdbarch_sp_regnum (gdbarch))
659 reg->how = DWARF2_FRAME_REG_CFA;
660 }
661
662 /* Set the architecture-specific register state initialization
663 function for GDBARCH to INIT_REG. */
664
665 void
666 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
667 void (*init_reg) (struct gdbarch *, int,
668 struct dwarf2_frame_state_reg *,
669 frame_info_ptr))
670 {
671 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
672
673 ops->init_reg = init_reg;
674 }
675
676 /* Pre-initialize the register state REG for register REGNUM. */
677
678 static void
679 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
680 struct dwarf2_frame_state_reg *reg,
681 frame_info_ptr this_frame)
682 {
683 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
684
685 ops->init_reg (gdbarch, regnum, reg, this_frame);
686 }
687
688 /* Set the architecture-specific signal trampoline recognition
689 function for GDBARCH to SIGNAL_FRAME_P. */
690
691 void
692 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
693 int (*signal_frame_p) (struct gdbarch *,
694 frame_info_ptr))
695 {
696 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
697
698 ops->signal_frame_p = signal_frame_p;
699 }
700
701 /* Query the architecture-specific signal frame recognizer for
702 THIS_FRAME. */
703
704 static int
705 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
706 frame_info_ptr this_frame)
707 {
708 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
709
710 if (ops->signal_frame_p == NULL)
711 return 0;
712 return ops->signal_frame_p (gdbarch, this_frame);
713 }
714
715 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
716 register numbers. */
717
718 void
719 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
720 int (*adjust_regnum) (struct gdbarch *,
721 int, int))
722 {
723 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
724
725 ops->adjust_regnum = adjust_regnum;
726 }
727
728 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
729 register. */
730
731 static int
732 dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
733 int regnum, int eh_frame_p)
734 {
735 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
736
737 if (ops->adjust_regnum == NULL)
738 return regnum;
739 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
740 }
741
742 static void
743 dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
744 struct dwarf2_fde *fde)
745 {
746 struct compunit_symtab *cust;
747
748 cust = find_pc_compunit_symtab (fs->pc);
749 if (cust == NULL)
750 return;
751
752 if (producer_is_realview (cust->producer ()))
753 {
754 if (fde->cie->version == 1)
755 fs->armcc_cfa_offsets_sf = 1;
756
757 if (fde->cie->version == 1)
758 fs->armcc_cfa_offsets_reversed = 1;
759
760 /* The reversed offset problem is present in some compilers
761 using DWARF3, but it was eventually fixed. Check the ARM
762 defined augmentations, which are in the format "armcc" followed
763 by a list of one-character options. The "+" option means
764 this problem is fixed (no quirk needed). If the armcc
765 augmentation is missing, the quirk is needed. */
766 if (fde->cie->version == 3
767 && (!startswith (fde->cie->augmentation, "armcc")
768 || strchr (fde->cie->augmentation + 5, '+') == NULL))
769 fs->armcc_cfa_offsets_reversed = 1;
770
771 return;
772 }
773 }
774 \f
775
776 /* See dwarf2/frame.h. */
777
778 int
779 dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
780 struct dwarf2_per_cu_data *data,
781 int *regnum_out, LONGEST *offset_out,
782 CORE_ADDR *text_offset_out,
783 const gdb_byte **cfa_start_out,
784 const gdb_byte **cfa_end_out)
785 {
786 struct dwarf2_fde *fde;
787 dwarf2_per_objfile *per_objfile;
788 CORE_ADDR pc1 = pc;
789
790 /* Find the correct FDE. */
791 fde = dwarf2_frame_find_fde (&pc1, &per_objfile);
792 if (fde == NULL)
793 error (_("Could not compute CFA; needed to translate this expression"));
794
795 gdb_assert (per_objfile != nullptr);
796
797 dwarf2_frame_state fs (pc1, fde->cie);
798
799 /* Check for "quirks" - known bugs in producers. */
800 dwarf2_frame_find_quirks (&fs, fde);
801
802 /* First decode all the insns in the CIE. */
803 execute_cfa_program (fde, fde->cie->initial_instructions,
804 fde->cie->end, gdbarch, pc, &fs,
805 per_objfile->objfile->text_section_offset ());
806
807 /* Save the initialized register set. */
808 fs.initial = fs.regs;
809
810 /* Then decode the insns in the FDE up to our target PC. */
811 execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs,
812 per_objfile->objfile->text_section_offset ());
813
814 /* Calculate the CFA. */
815 switch (fs.regs.cfa_how)
816 {
817 case CFA_REG_OFFSET:
818 {
819 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg);
820
821 *regnum_out = regnum;
822 if (fs.armcc_cfa_offsets_reversed)
823 *offset_out = -fs.regs.cfa_offset;
824 else
825 *offset_out = fs.regs.cfa_offset;
826 return 1;
827 }
828
829 case CFA_EXP:
830 *text_offset_out = per_objfile->objfile->text_section_offset ();
831 *cfa_start_out = fs.regs.cfa_exp;
832 *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
833 return 0;
834
835 default:
836 internal_error (_("Unknown CFA rule."));
837 }
838 }
839
840 \f
841 /* Custom function data object for architecture specific prev_register
842 implementation. Main purpose of this object is to allow caching of
843 expensive data lookups in the prev_register handling. */
844
845 struct dwarf2_frame_fn_data
846 {
847 /* The cookie to identify the custom function data by. */
848 fn_prev_register cookie;
849
850 /* The custom function data. */
851 void *data;
852
853 /* Pointer to the next custom function data object for this frame. */
854 struct dwarf2_frame_fn_data *next;
855 };
856
857 struct dwarf2_frame_cache
858 {
859 /* DWARF Call Frame Address. */
860 CORE_ADDR cfa;
861
862 /* Set if the return address column was marked as unavailable
863 (required non-collected memory or registers to compute). */
864 int unavailable_retaddr;
865
866 /* Set if the return address column was marked as undefined. */
867 int undefined_retaddr;
868
869 /* Saved registers, indexed by GDB register number, not by DWARF
870 register number. */
871 struct dwarf2_frame_state_reg *reg;
872
873 /* Return address register. */
874 struct dwarf2_frame_state_reg retaddr_reg;
875
876 /* Target address size in bytes. */
877 int addr_size;
878
879 /* The dwarf2_per_objfile from which this frame description came. */
880 dwarf2_per_objfile *per_objfile;
881
882 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
883 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
884 involved. Non-bottom frames of a virtual tail call frames chain use
885 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
886 them. */
887 void *tailcall_cache;
888
889 struct dwarf2_frame_fn_data *fn_data;
890 };
891
892 static struct dwarf2_frame_cache *
893 dwarf2_frame_cache (frame_info_ptr this_frame, void **this_cache)
894 {
895 struct gdbarch *gdbarch = get_frame_arch (this_frame);
896 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
897 struct dwarf2_frame_cache *cache;
898 struct dwarf2_fde *fde;
899 CORE_ADDR entry_pc;
900 const gdb_byte *instr;
901
902 if (*this_cache)
903 return (struct dwarf2_frame_cache *) *this_cache;
904
905 /* Allocate a new cache. */
906 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
907 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
908 *this_cache = cache;
909
910 /* Unwind the PC.
911
912 Note that if the next frame is never supposed to return (i.e. a call
913 to abort), the compiler might optimize away the instruction at
914 its return address. As a result the return address will
915 point at some random instruction, and the CFI for that
916 instruction is probably worthless to us. GCC's unwinder solves
917 this problem by substracting 1 from the return address to get an
918 address in the middle of a presumed call instruction (or the
919 instruction in the associated delay slot). This should only be
920 done for "normal" frames and not for resume-type frames (signal
921 handlers, sentinel frames, dummy frames). The function
922 get_frame_address_in_block does just this. It's not clear how
923 reliable the method is though; there is the potential for the
924 register state pre-call being different to that on return. */
925 CORE_ADDR pc1 = get_frame_address_in_block (this_frame);
926
927 /* Find the correct FDE. */
928 fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
929 gdb_assert (fde != NULL);
930 gdb_assert (cache->per_objfile != nullptr);
931
932 CORE_ADDR text_offset = cache->per_objfile->objfile->text_section_offset ();
933
934 /* Allocate and initialize the frame state. */
935 struct dwarf2_frame_state fs (pc1, fde->cie);
936
937 cache->addr_size = fde->cie->addr_size;
938
939 /* Check for "quirks" - known bugs in producers. */
940 dwarf2_frame_find_quirks (&fs, fde);
941
942 /* First decode all the insns in the CIE. */
943 execute_cfa_program (fde, fde->cie->initial_instructions,
944 fde->cie->end, gdbarch,
945 get_frame_address_in_block (this_frame), &fs,
946 text_offset);
947
948 /* Save the initialized register set. */
949 fs.initial = fs.regs;
950
951 /* Fetching the entry pc for THIS_FRAME won't necessarily result
952 in an address that's within the range of FDE locations. This
953 is due to the possibility of the function occupying non-contiguous
954 ranges. */
955 LONGEST entry_cfa_sp_offset;
956 int entry_cfa_sp_offset_p = 0;
957 if (get_frame_func_if_available (this_frame, &entry_pc)
958 && fde->initial_location <= (unrelocated_addr) (entry_pc - text_offset)
959 && (unrelocated_addr) (entry_pc - text_offset) < fde->end_addr ())
960 {
961 /* Decode the insns in the FDE up to the entry PC. */
962 instr = execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,
963 entry_pc, &fs, text_offset);
964
965 if (fs.regs.cfa_how == CFA_REG_OFFSET
966 && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg)
967 == gdbarch_sp_regnum (gdbarch)))
968 {
969 entry_cfa_sp_offset = fs.regs.cfa_offset;
970 entry_cfa_sp_offset_p = 1;
971 }
972 }
973 else
974 instr = fde->instructions;
975
976 /* Then decode the insns in the FDE up to our target PC. */
977 execute_cfa_program (fde, instr, fde->end, gdbarch,
978 get_frame_address_in_block (this_frame), &fs,
979 text_offset);
980
981 try
982 {
983 /* Calculate the CFA. */
984 switch (fs.regs.cfa_how)
985 {
986 case CFA_REG_OFFSET:
987 cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg);
988 if (fs.armcc_cfa_offsets_reversed)
989 cache->cfa -= fs.regs.cfa_offset;
990 else
991 cache->cfa += fs.regs.cfa_offset;
992 break;
993
994 case CFA_EXP:
995 cache->cfa =
996 execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len,
997 cache->addr_size, this_frame, 0, 0,
998 cache->per_objfile);
999 break;
1000
1001 default:
1002 internal_error (_("Unknown CFA rule."));
1003 }
1004 }
1005 catch (const gdb_exception_error &ex)
1006 {
1007 if (ex.error == NOT_AVAILABLE_ERROR)
1008 {
1009 cache->unavailable_retaddr = 1;
1010 return cache;
1011 }
1012
1013 throw;
1014 }
1015
1016 /* Initialize the register state. */
1017 {
1018 int regnum;
1019
1020 for (regnum = 0; regnum < num_regs; regnum++)
1021 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
1022 }
1023
1024 /* Go through the DWARF2 CFI generated table and save its register
1025 location information in the cache. Note that we don't skip the
1026 return address column; it's perfectly all right for it to
1027 correspond to a real register. */
1028 {
1029 int column; /* CFI speak for "register number". */
1030
1031 for (column = 0; column < fs.regs.reg.size (); column++)
1032 {
1033 /* Use the GDB register number as the destination index. */
1034 int regnum = dwarf_reg_to_regnum (gdbarch, column);
1035
1036 /* Protect against a target returning a bad register. */
1037 if (regnum < 0 || regnum >= num_regs)
1038 continue;
1039
1040 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1041 of all debug info registers. If it doesn't, complain (but
1042 not too loudly). It turns out that GCC assumes that an
1043 unspecified register implies "same value" when CFI (draft
1044 7) specifies nothing at all. Such a register could equally
1045 be interpreted as "undefined". Also note that this check
1046 isn't sufficient; it only checks that all registers in the
1047 range [0 .. max column] are specified, and won't detect
1048 problems when a debug info register falls outside of the
1049 table. We need a way of iterating through all the valid
1050 DWARF2 register numbers. */
1051 if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
1052 {
1053 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
1054 complaint (_("\
1055 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1056 gdbarch_register_name (gdbarch, regnum),
1057 paddress (gdbarch, fs.pc));
1058 }
1059 else
1060 cache->reg[regnum] = fs.regs.reg[column];
1061 }
1062 }
1063
1064 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1065 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1066 {
1067 int regnum;
1068
1069 for (regnum = 0; regnum < num_regs; regnum++)
1070 {
1071 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1072 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
1073 {
1074 const std::vector<struct dwarf2_frame_state_reg> &regs
1075 = fs.regs.reg;
1076 ULONGEST retaddr_column = fs.retaddr_column;
1077
1078 /* It seems rather bizarre to specify an "empty" column as
1079 the return adress column. However, this is exactly
1080 what GCC does on some targets. It turns out that GCC
1081 assumes that the return address can be found in the
1082 register corresponding to the return address column.
1083 Incidentally, that's how we should treat a return
1084 address column specifying "same value" too. */
1085 if (fs.retaddr_column < fs.regs.reg.size ()
1086 && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED
1087 && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE)
1088 {
1089 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1090 cache->reg[regnum] = regs[retaddr_column];
1091 else
1092 cache->retaddr_reg = regs[retaddr_column];
1093 }
1094 else
1095 {
1096 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1097 {
1098 cache->reg[regnum].loc.reg = fs.retaddr_column;
1099 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1100 }
1101 else
1102 {
1103 cache->retaddr_reg.loc.reg = fs.retaddr_column;
1104 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1105 }
1106 }
1107 }
1108 }
1109 }
1110
1111 if (fs.retaddr_column < fs.regs.reg.size ()
1112 && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1113 cache->undefined_retaddr = 1;
1114
1115 dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
1116 (entry_cfa_sp_offset_p
1117 ? &entry_cfa_sp_offset : NULL));
1118
1119 return cache;
1120 }
1121
1122 static enum unwind_stop_reason
1123 dwarf2_frame_unwind_stop_reason (frame_info_ptr this_frame,
1124 void **this_cache)
1125 {
1126 struct dwarf2_frame_cache *cache
1127 = dwarf2_frame_cache (this_frame, this_cache);
1128
1129 if (cache->unavailable_retaddr)
1130 return UNWIND_UNAVAILABLE;
1131
1132 if (cache->undefined_retaddr)
1133 return UNWIND_OUTERMOST;
1134
1135 return UNWIND_NO_REASON;
1136 }
1137
1138 static void
1139 dwarf2_frame_this_id (frame_info_ptr this_frame, void **this_cache,
1140 struct frame_id *this_id)
1141 {
1142 struct dwarf2_frame_cache *cache =
1143 dwarf2_frame_cache (this_frame, this_cache);
1144
1145 if (cache->unavailable_retaddr)
1146 (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame));
1147 else if (cache->undefined_retaddr)
1148 return;
1149 else
1150 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
1151 }
1152
1153 static struct value *
1154 dwarf2_frame_prev_register (frame_info_ptr this_frame, void **this_cache,
1155 int regnum)
1156 {
1157 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1158 struct dwarf2_frame_cache *cache =
1159 dwarf2_frame_cache (this_frame, this_cache);
1160 CORE_ADDR addr;
1161 int realnum;
1162
1163 /* Non-bottom frames of a virtual tail call frames chain use
1164 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1165 them. If dwarf2_tailcall_prev_register_first does not have specific value
1166 unwind the register, tail call frames are assumed to have the register set
1167 of the top caller. */
1168 if (cache->tailcall_cache)
1169 {
1170 struct value *val;
1171
1172 val = dwarf2_tailcall_prev_register_first (this_frame,
1173 &cache->tailcall_cache,
1174 regnum);
1175 if (val)
1176 return val;
1177 }
1178
1179 switch (cache->reg[regnum].how)
1180 {
1181 case DWARF2_FRAME_REG_UNDEFINED:
1182 /* If CFI explicitly specified that the value isn't defined,
1183 mark it as optimized away; the value isn't available. */
1184 return frame_unwind_got_optimized (this_frame, regnum);
1185
1186 case DWARF2_FRAME_REG_SAVED_OFFSET:
1187 addr = cache->cfa + cache->reg[regnum].loc.offset;
1188 return frame_unwind_got_memory (this_frame, regnum, addr);
1189
1190 case DWARF2_FRAME_REG_SAVED_REG:
1191 realnum = dwarf_reg_to_regnum_or_error
1192 (gdbarch, cache->reg[regnum].loc.reg);
1193 return frame_unwind_got_register (this_frame, regnum, realnum);
1194
1195 case DWARF2_FRAME_REG_SAVED_EXP:
1196 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1197 cache->reg[regnum].loc.exp.len,
1198 cache->addr_size,
1199 this_frame, cache->cfa, 1,
1200 cache->per_objfile);
1201 return frame_unwind_got_memory (this_frame, regnum, addr);
1202
1203 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1204 addr = cache->cfa + cache->reg[regnum].loc.offset;
1205 return frame_unwind_got_constant (this_frame, regnum, addr);
1206
1207 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1208 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1209 cache->reg[regnum].loc.exp.len,
1210 cache->addr_size,
1211 this_frame, cache->cfa, 1,
1212 cache->per_objfile);
1213 return frame_unwind_got_constant (this_frame, regnum, addr);
1214
1215 case DWARF2_FRAME_REG_UNSPECIFIED:
1216 /* GCC, in its infinite wisdom decided to not provide unwind
1217 information for registers that are "same value". Since
1218 DWARF2 (3 draft 7) doesn't define such behavior, said
1219 registers are actually undefined (which is different to CFI
1220 "undefined"). Code above issues a complaint about this.
1221 Here just fudge the books, assume GCC, and that the value is
1222 more inner on the stack. */
1223 return frame_unwind_got_register (this_frame, regnum, regnum);
1224
1225 case DWARF2_FRAME_REG_SAME_VALUE:
1226 return frame_unwind_got_register (this_frame, regnum, regnum);
1227
1228 case DWARF2_FRAME_REG_CFA:
1229 return frame_unwind_got_address (this_frame, regnum, cache->cfa);
1230
1231 case DWARF2_FRAME_REG_CFA_OFFSET:
1232 addr = cache->cfa + cache->reg[regnum].loc.offset;
1233 return frame_unwind_got_address (this_frame, regnum, addr);
1234
1235 case DWARF2_FRAME_REG_RA_OFFSET:
1236 addr = cache->reg[regnum].loc.offset;
1237 regnum = dwarf_reg_to_regnum_or_error
1238 (gdbarch, cache->retaddr_reg.loc.reg);
1239 addr += get_frame_register_unsigned (this_frame, regnum);
1240 return frame_unwind_got_address (this_frame, regnum, addr);
1241
1242 case DWARF2_FRAME_REG_FN:
1243 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1244
1245 default:
1246 internal_error (_("Unknown register rule."));
1247 }
1248 }
1249
1250 /* See frame.h. */
1251
1252 void *
1253 dwarf2_frame_get_fn_data (frame_info_ptr this_frame, void **this_cache,
1254 fn_prev_register cookie)
1255 {
1256 struct dwarf2_frame_fn_data *fn_data = nullptr;
1257 struct dwarf2_frame_cache *cache
1258 = dwarf2_frame_cache (this_frame, this_cache);
1259
1260 /* Find the object for the function. */
1261 for (fn_data = cache->fn_data; fn_data; fn_data = fn_data->next)
1262 if (fn_data->cookie == cookie)
1263 return fn_data->data;
1264
1265 return nullptr;
1266 }
1267
1268 /* See frame.h. */
1269
1270 void *
1271 dwarf2_frame_allocate_fn_data (frame_info_ptr this_frame, void **this_cache,
1272 fn_prev_register cookie, unsigned long size)
1273 {
1274 struct dwarf2_frame_fn_data *fn_data = nullptr;
1275 struct dwarf2_frame_cache *cache
1276 = dwarf2_frame_cache (this_frame, this_cache);
1277
1278 /* First try to find an existing object. */
1279 void *data = dwarf2_frame_get_fn_data (this_frame, this_cache, cookie);
1280 gdb_assert (data == nullptr);
1281
1282 /* No object found, lets create a new instance. */
1283 fn_data = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_fn_data);
1284 fn_data->cookie = cookie;
1285 fn_data->data = frame_obstack_zalloc (size);
1286 fn_data->next = cache->fn_data;
1287 cache->fn_data = fn_data;
1288
1289 return fn_data->data;
1290 }
1291
1292 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1293 call frames chain. */
1294
1295 static void
1296 dwarf2_frame_dealloc_cache (frame_info *self, void *this_cache)
1297 {
1298 struct dwarf2_frame_cache *cache
1299 = dwarf2_frame_cache (frame_info_ptr (self), &this_cache);
1300
1301 if (cache->tailcall_cache)
1302 dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
1303 }
1304
1305 static int
1306 dwarf2_frame_sniffer (const struct frame_unwind *self,
1307 frame_info_ptr this_frame, void **this_cache)
1308 {
1309 if (!dwarf2_frame_unwinders_enabled_p)
1310 return 0;
1311
1312 /* Grab an address that is guaranteed to reside somewhere within the
1313 function. get_frame_pc(), with a no-return next function, can
1314 end up returning something past the end of this function's body.
1315 If the frame we're sniffing for is a signal frame whose start
1316 address is placed on the stack by the OS, its FDE must
1317 extend one byte before its start address or we could potentially
1318 select the FDE of the previous function. */
1319 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1320 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
1321
1322 if (!fde)
1323 return 0;
1324
1325 /* On some targets, signal trampolines may have unwind information.
1326 We need to recognize them so that we set the frame type
1327 correctly. */
1328
1329 if (fde->cie->signal_frame
1330 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1331 this_frame))
1332 return self->type == SIGTRAMP_FRAME;
1333
1334 if (self->type != NORMAL_FRAME)
1335 return 0;
1336
1337 return 1;
1338 }
1339
1340 static const struct frame_unwind dwarf2_frame_unwind =
1341 {
1342 "dwarf2",
1343 NORMAL_FRAME,
1344 dwarf2_frame_unwind_stop_reason,
1345 dwarf2_frame_this_id,
1346 dwarf2_frame_prev_register,
1347 NULL,
1348 dwarf2_frame_sniffer,
1349 dwarf2_frame_dealloc_cache
1350 };
1351
1352 static const struct frame_unwind dwarf2_signal_frame_unwind =
1353 {
1354 "dwarf2 signal",
1355 SIGTRAMP_FRAME,
1356 dwarf2_frame_unwind_stop_reason,
1357 dwarf2_frame_this_id,
1358 dwarf2_frame_prev_register,
1359 NULL,
1360 dwarf2_frame_sniffer,
1361
1362 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1363 NULL
1364 };
1365
1366 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1367
1368 void
1369 dwarf2_append_unwinders (struct gdbarch *gdbarch)
1370 {
1371 frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1372 frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
1373 }
1374 \f
1375
1376 /* There is no explicitly defined relationship between the CFA and the
1377 location of frame's local variables and arguments/parameters.
1378 Therefore, frame base methods on this page should probably only be
1379 used as a last resort, just to avoid printing total garbage as a
1380 response to the "info frame" command. */
1381
1382 static CORE_ADDR
1383 dwarf2_frame_base_address (frame_info_ptr this_frame, void **this_cache)
1384 {
1385 struct dwarf2_frame_cache *cache =
1386 dwarf2_frame_cache (this_frame, this_cache);
1387
1388 return cache->cfa;
1389 }
1390
1391 static const struct frame_base dwarf2_frame_base =
1392 {
1393 &dwarf2_frame_unwind,
1394 dwarf2_frame_base_address,
1395 dwarf2_frame_base_address,
1396 dwarf2_frame_base_address
1397 };
1398
1399 const struct frame_base *
1400 dwarf2_frame_base_sniffer (frame_info_ptr this_frame)
1401 {
1402 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1403
1404 if (dwarf2_frame_find_fde (&block_addr, NULL))
1405 return &dwarf2_frame_base;
1406
1407 return NULL;
1408 }
1409
1410 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1411 the DWARF unwinder. This is used to implement
1412 DW_OP_call_frame_cfa. */
1413
1414 CORE_ADDR
1415 dwarf2_frame_cfa (frame_info_ptr this_frame)
1416 {
1417 if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind)
1418 || frame_unwinder_is (this_frame, &record_btrace_frame_unwind))
1419 throw_error (NOT_AVAILABLE_ERROR,
1420 _("cfa not available for record btrace target"));
1421
1422 while (get_frame_type (this_frame) == INLINE_FRAME)
1423 this_frame = get_prev_frame (this_frame);
1424 if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
1425 throw_error (NOT_AVAILABLE_ERROR,
1426 _("can't compute CFA for this frame: "
1427 "required registers or memory are unavailable"));
1428
1429 if (get_frame_id (this_frame).stack_status != FID_STACK_VALID)
1430 throw_error (NOT_AVAILABLE_ERROR,
1431 _("can't compute CFA for this frame: "
1432 "frame base not available"));
1433
1434 return get_frame_base (this_frame);
1435 }
1436 \f
1437 /* We store the frame data on the BFD. This is only done if it is
1438 independent of the address space and so can be shared. */
1439 static const registry<bfd>::key<comp_unit> dwarf2_frame_bfd_data;
1440
1441 /* If any BFD sections require relocations (note; really should be if
1442 any debug info requires relocations), then we store the frame data
1443 on the objfile instead, and do not share it. */
1444 static const registry<objfile>::key<comp_unit> dwarf2_frame_objfile_data;
1445 \f
1446
1447 /* Pointer encoding helper functions. */
1448
1449 /* GCC supports exception handling based on DWARF2 CFI. However, for
1450 technical reasons, it encodes addresses in its FDE's in a different
1451 way. Several "pointer encodings" are supported. The encoding
1452 that's used for a particular FDE is determined by the 'R'
1453 augmentation in the associated CIE. The argument of this
1454 augmentation is a single byte.
1455
1456 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1457 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1458 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1459 address should be interpreted (absolute, relative to the current
1460 position in the FDE, ...). Bit 7, indicates that the address
1461 should be dereferenced. */
1462
1463 static gdb_byte
1464 encoding_for_size (unsigned int size)
1465 {
1466 switch (size)
1467 {
1468 case 2:
1469 return DW_EH_PE_udata2;
1470 case 4:
1471 return DW_EH_PE_udata4;
1472 case 8:
1473 return DW_EH_PE_udata8;
1474 default:
1475 internal_error (_("Unsupported address size"));
1476 }
1477 }
1478
1479 static ULONGEST
1480 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1481 int ptr_len, const gdb_byte *buf,
1482 unsigned int *bytes_read_ptr,
1483 unrelocated_addr func_base)
1484 {
1485 ptrdiff_t offset;
1486 ULONGEST base;
1487
1488 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1489 FDE's. */
1490 if (encoding & DW_EH_PE_indirect)
1491 internal_error (_("Unsupported encoding: DW_EH_PE_indirect"));
1492
1493 *bytes_read_ptr = 0;
1494
1495 switch (encoding & 0x70)
1496 {
1497 case DW_EH_PE_absptr:
1498 base = 0;
1499 break;
1500 case DW_EH_PE_pcrel:
1501 base = bfd_section_vma (unit->dwarf_frame_section);
1502 base += (buf - unit->dwarf_frame_buffer);
1503 break;
1504 case DW_EH_PE_datarel:
1505 base = unit->dbase;
1506 break;
1507 case DW_EH_PE_textrel:
1508 base = unit->tbase;
1509 break;
1510 case DW_EH_PE_funcrel:
1511 base = (ULONGEST) func_base;
1512 break;
1513 case DW_EH_PE_aligned:
1514 base = 0;
1515 offset = buf - unit->dwarf_frame_buffer;
1516 if ((offset % ptr_len) != 0)
1517 {
1518 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1519 buf += *bytes_read_ptr;
1520 }
1521 break;
1522 default:
1523 internal_error (_("Invalid or unsupported encoding"));
1524 }
1525
1526 if ((encoding & 0x07) == 0x00)
1527 {
1528 encoding |= encoding_for_size (ptr_len);
1529 if (bfd_get_sign_extend_vma (unit->abfd))
1530 encoding |= DW_EH_PE_signed;
1531 }
1532
1533 switch (encoding & 0x0f)
1534 {
1535 case DW_EH_PE_uleb128:
1536 {
1537 uint64_t value;
1538 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1539
1540 *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
1541 return base + value;
1542 }
1543 case DW_EH_PE_udata2:
1544 *bytes_read_ptr += 2;
1545 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1546 case DW_EH_PE_udata4:
1547 *bytes_read_ptr += 4;
1548 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1549 case DW_EH_PE_udata8:
1550 *bytes_read_ptr += 8;
1551 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1552 case DW_EH_PE_sleb128:
1553 {
1554 int64_t value;
1555 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1556
1557 *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
1558 return base + value;
1559 }
1560 case DW_EH_PE_sdata2:
1561 *bytes_read_ptr += 2;
1562 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1563 case DW_EH_PE_sdata4:
1564 *bytes_read_ptr += 4;
1565 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1566 case DW_EH_PE_sdata8:
1567 *bytes_read_ptr += 8;
1568 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1569 default:
1570 internal_error (_("Invalid or unsupported encoding"));
1571 }
1572 }
1573 \f
1574
1575 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1576 static struct dwarf2_cie *
1577 find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer)
1578 {
1579 auto iter = cie_table.find (cie_pointer);
1580 if (iter != cie_table.end ())
1581 return iter->second;
1582 return NULL;
1583 }
1584
1585 static inline int
1586 bsearch_fde_cmp (const dwarf2_fde *fde, unrelocated_addr seek_pc)
1587 {
1588 if (fde->end_addr () <= seek_pc)
1589 return -1;
1590 if (fde->initial_location <= seek_pc)
1591 return 0;
1592 return 1;
1593 }
1594
1595 /* Find an existing comp_unit for an objfile, if any. */
1596
1597 static comp_unit *
1598 find_comp_unit (struct objfile *objfile)
1599 {
1600 bfd *abfd = objfile->obfd.get ();
1601 if (gdb_bfd_requires_relocations (abfd))
1602 return dwarf2_frame_objfile_data.get (objfile);
1603
1604 return dwarf2_frame_bfd_data.get (abfd);
1605 }
1606
1607 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1608 appropriate. */
1609
1610 static void
1611 set_comp_unit (struct objfile *objfile, struct comp_unit *unit)
1612 {
1613 bfd *abfd = objfile->obfd.get ();
1614 if (gdb_bfd_requires_relocations (abfd))
1615 return dwarf2_frame_objfile_data.set (objfile, unit);
1616
1617 return dwarf2_frame_bfd_data.set (abfd, unit);
1618 }
1619
1620 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1621 initial location associated with it into *PC. */
1622
1623 static struct dwarf2_fde *
1624 dwarf2_frame_find_fde (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile)
1625 {
1626 for (objfile *objfile : current_program_space->objfiles ())
1627 {
1628 CORE_ADDR offset;
1629
1630 if (objfile->obfd == nullptr)
1631 continue;
1632
1633 comp_unit *unit = find_comp_unit (objfile);
1634 if (unit == NULL)
1635 {
1636 dwarf2_build_frame_info (objfile);
1637 unit = find_comp_unit (objfile);
1638 }
1639 gdb_assert (unit != NULL);
1640
1641 dwarf2_fde_table *fde_table = &unit->fde_table;
1642 if (fde_table->empty ())
1643 continue;
1644
1645 gdb_assert (!objfile->section_offsets.empty ());
1646 offset = objfile->text_section_offset ();
1647
1648 gdb_assert (!fde_table->empty ());
1649 unrelocated_addr seek_pc = (unrelocated_addr) (*pc - offset);
1650 if (seek_pc < (*fde_table)[0]->initial_location)
1651 continue;
1652
1653 auto it = gdb::binary_search (fde_table->begin (), fde_table->end (),
1654 seek_pc, bsearch_fde_cmp);
1655 if (it != fde_table->end ())
1656 {
1657 *pc = (CORE_ADDR) (*it)->initial_location + offset;
1658 if (out_per_objfile != nullptr)
1659 *out_per_objfile = get_dwarf2_per_objfile (objfile);
1660
1661 return *it;
1662 }
1663 }
1664 return NULL;
1665 }
1666
1667 /* Add FDE to FDE_TABLE. */
1668 static void
1669 add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
1670 {
1671 if (fde->address_range == 0)
1672 /* Discard useless FDEs. */
1673 return;
1674
1675 fde_table->push_back (fde);
1676 }
1677
1678 #define DW64_CIE_ID 0xffffffffffffffffULL
1679
1680 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1681 or any of them. */
1682
1683 enum eh_frame_type
1684 {
1685 EH_CIE_TYPE_ID = 1 << 0,
1686 EH_FDE_TYPE_ID = 1 << 1,
1687 EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
1688 };
1689
1690 static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch,
1691 struct comp_unit *unit,
1692 const gdb_byte *start,
1693 int eh_frame_p,
1694 dwarf2_cie_table &cie_table,
1695 dwarf2_fde_table *fde_table,
1696 enum eh_frame_type entry_type);
1697
1698 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1699 Return NULL if invalid input, otherwise the next byte to be processed. */
1700
1701 static const gdb_byte *
1702 decode_frame_entry_1 (struct gdbarch *gdbarch,
1703 struct comp_unit *unit, const gdb_byte *start,
1704 int eh_frame_p,
1705 dwarf2_cie_table &cie_table,
1706 dwarf2_fde_table *fde_table,
1707 enum eh_frame_type entry_type)
1708 {
1709 const gdb_byte *buf, *end;
1710 ULONGEST length;
1711 unsigned int bytes_read;
1712 int dwarf64_p;
1713 ULONGEST cie_id;
1714 ULONGEST cie_pointer;
1715 int64_t sleb128;
1716 uint64_t uleb128;
1717
1718 buf = start;
1719 length = read_initial_length (unit->abfd, buf, &bytes_read, false);
1720 buf += bytes_read;
1721 end = buf + (size_t) length;
1722
1723 if (length == 0)
1724 return end;
1725
1726 /* Are we still within the section? */
1727 if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1728 return NULL;
1729
1730 /* Distinguish between 32 and 64-bit encoded frame info. */
1731 dwarf64_p = (bytes_read == 12);
1732
1733 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1734 if (eh_frame_p)
1735 cie_id = 0;
1736 else if (dwarf64_p)
1737 cie_id = DW64_CIE_ID;
1738 else
1739 cie_id = DW_CIE_ID;
1740
1741 if (dwarf64_p)
1742 {
1743 cie_pointer = read_8_bytes (unit->abfd, buf);
1744 buf += 8;
1745 }
1746 else
1747 {
1748 cie_pointer = read_4_bytes (unit->abfd, buf);
1749 buf += 4;
1750 }
1751
1752 if (cie_pointer == cie_id)
1753 {
1754 /* This is a CIE. */
1755 struct dwarf2_cie *cie;
1756 const char *augmentation;
1757 unsigned int cie_version;
1758
1759 /* Check that a CIE was expected. */
1760 if ((entry_type & EH_CIE_TYPE_ID) == 0)
1761 error (_("Found a CIE when not expecting it."));
1762
1763 /* Record the offset into the .debug_frame section of this CIE. */
1764 cie_pointer = start - unit->dwarf_frame_buffer;
1765
1766 /* Check whether we've already read it. */
1767 if (find_cie (cie_table, cie_pointer))
1768 return end;
1769
1770 cie = XOBNEW (&unit->obstack, struct dwarf2_cie);
1771 cie->initial_instructions = NULL;
1772 cie->cie_pointer = cie_pointer;
1773
1774 /* The encoding for FDE's in a normal .debug_frame section
1775 depends on the target address size. */
1776 cie->encoding = DW_EH_PE_absptr;
1777
1778 /* We'll determine the final value later, but we need to
1779 initialize it conservatively. */
1780 cie->signal_frame = 0;
1781
1782 /* Check version number. */
1783 cie_version = read_1_byte (unit->abfd, buf);
1784 if (cie_version != 1 && cie_version != 3 && cie_version != 4)
1785 return NULL;
1786 cie->version = cie_version;
1787 buf += 1;
1788
1789 /* Interpret the interesting bits of the augmentation. */
1790 cie->augmentation = augmentation = (const char *) buf;
1791 buf += (strlen (augmentation) + 1);
1792
1793 /* Ignore armcc augmentations. We only use them for quirks,
1794 and that doesn't happen until later. */
1795 if (startswith (augmentation, "armcc"))
1796 augmentation += strlen (augmentation);
1797
1798 /* The GCC 2.x "eh" augmentation has a pointer immediately
1799 following the augmentation string, so it must be handled
1800 first. */
1801 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1802 {
1803 /* Skip. */
1804 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1805 augmentation += 2;
1806 }
1807
1808 if (cie->version >= 4)
1809 {
1810 /* FIXME: check that this is the same as from the CU header. */
1811 cie->addr_size = read_1_byte (unit->abfd, buf);
1812 ++buf;
1813 cie->segment_size = read_1_byte (unit->abfd, buf);
1814 ++buf;
1815 }
1816 else
1817 {
1818 cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
1819 cie->segment_size = 0;
1820 }
1821 /* Address values in .eh_frame sections are defined to have the
1822 target's pointer size. Watchout: This breaks frame info for
1823 targets with pointer size < address size, unless a .debug_frame
1824 section exists as well. */
1825 if (eh_frame_p)
1826 cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1827 else
1828 cie->ptr_size = cie->addr_size;
1829
1830 buf = gdb_read_uleb128 (buf, end, &uleb128);
1831 if (buf == NULL)
1832 return NULL;
1833 cie->code_alignment_factor = uleb128;
1834
1835 buf = gdb_read_sleb128 (buf, end, &sleb128);
1836 if (buf == NULL)
1837 return NULL;
1838 cie->data_alignment_factor = sleb128;
1839
1840 if (cie_version == 1)
1841 {
1842 cie->return_address_register = read_1_byte (unit->abfd, buf);
1843 ++buf;
1844 }
1845 else
1846 {
1847 buf = gdb_read_uleb128 (buf, end, &uleb128);
1848 if (buf == NULL)
1849 return NULL;
1850 cie->return_address_register = uleb128;
1851 }
1852
1853 cie->return_address_register
1854 = dwarf2_frame_adjust_regnum (gdbarch,
1855 cie->return_address_register,
1856 eh_frame_p);
1857
1858 cie->saw_z_augmentation = (*augmentation == 'z');
1859 if (cie->saw_z_augmentation)
1860 {
1861 uint64_t uleb_length;
1862
1863 buf = gdb_read_uleb128 (buf, end, &uleb_length);
1864 if (buf == NULL)
1865 return NULL;
1866 cie->initial_instructions = buf + uleb_length;
1867 augmentation++;
1868 }
1869
1870 while (*augmentation)
1871 {
1872 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1873 if (*augmentation == 'L')
1874 {
1875 /* Skip. */
1876 buf++;
1877 augmentation++;
1878 }
1879
1880 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1881 else if (*augmentation == 'R')
1882 {
1883 cie->encoding = *buf++;
1884 augmentation++;
1885 }
1886
1887 /* "P" indicates a personality routine in the CIE augmentation. */
1888 else if (*augmentation == 'P')
1889 {
1890 /* Skip. Avoid indirection since we throw away the result. */
1891 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1892 read_encoded_value (unit, encoding, cie->ptr_size,
1893 buf, &bytes_read, (unrelocated_addr) 0);
1894 buf += bytes_read;
1895 augmentation++;
1896 }
1897
1898 /* "S" indicates a signal frame, such that the return
1899 address must not be decremented to locate the call frame
1900 info for the previous frame; it might even be the first
1901 instruction of a function, so decrementing it would take
1902 us to a different function. */
1903 else if (*augmentation == 'S')
1904 {
1905 cie->signal_frame = 1;
1906 augmentation++;
1907 }
1908
1909 /* Otherwise we have an unknown augmentation. Assume that either
1910 there is no augmentation data, or we saw a 'z' prefix. */
1911 else
1912 {
1913 if (cie->initial_instructions)
1914 buf = cie->initial_instructions;
1915 break;
1916 }
1917 }
1918
1919 cie->initial_instructions = buf;
1920 cie->end = end;
1921 cie->unit = unit;
1922
1923 cie_table[cie->cie_pointer] = cie;
1924 }
1925 else
1926 {
1927 /* This is a FDE. */
1928 struct dwarf2_fde *fde;
1929
1930 /* Check that an FDE was expected. */
1931 if ((entry_type & EH_FDE_TYPE_ID) == 0)
1932 error (_("Found an FDE when not expecting it."));
1933
1934 /* In an .eh_frame section, the CIE pointer is the delta between the
1935 address within the FDE where the CIE pointer is stored and the
1936 address of the CIE. Convert it to an offset into the .eh_frame
1937 section. */
1938 if (eh_frame_p)
1939 {
1940 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1941 cie_pointer -= (dwarf64_p ? 8 : 4);
1942 }
1943
1944 /* In either case, validate the result is still within the section. */
1945 if (cie_pointer >= unit->dwarf_frame_size)
1946 return NULL;
1947
1948 fde = XOBNEW (&unit->obstack, struct dwarf2_fde);
1949 fde->cie = find_cie (cie_table, cie_pointer);
1950 if (fde->cie == NULL)
1951 {
1952 decode_frame_entry (gdbarch, unit,
1953 unit->dwarf_frame_buffer + cie_pointer,
1954 eh_frame_p, cie_table, fde_table,
1955 EH_CIE_TYPE_ID);
1956 fde->cie = find_cie (cie_table, cie_pointer);
1957 }
1958
1959 gdb_assert (fde->cie != NULL);
1960
1961 ULONGEST init_addr
1962 = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
1963 buf, &bytes_read, (unrelocated_addr) 0);
1964 fde->initial_location
1965 = (unrelocated_addr) gdbarch_adjust_dwarf2_addr (gdbarch, init_addr);
1966 buf += bytes_read;
1967
1968 ULONGEST range
1969 = read_encoded_value (unit, fde->cie->encoding & 0x0f,
1970 fde->cie->ptr_size, buf, &bytes_read,
1971 (unrelocated_addr) 0);
1972 ULONGEST addr = gdbarch_adjust_dwarf2_addr (gdbarch, init_addr + range);
1973 fde->address_range = addr - (ULONGEST) fde->initial_location;
1974 buf += bytes_read;
1975
1976 /* A 'z' augmentation in the CIE implies the presence of an
1977 augmentation field in the FDE as well. The only thing known
1978 to be in here at present is the LSDA entry for EH. So we
1979 can skip the whole thing. */
1980 if (fde->cie->saw_z_augmentation)
1981 {
1982 uint64_t uleb_length;
1983
1984 buf = gdb_read_uleb128 (buf, end, &uleb_length);
1985 if (buf == NULL)
1986 return NULL;
1987 buf += uleb_length;
1988 if (buf > end)
1989 return NULL;
1990 }
1991
1992 fde->instructions = buf;
1993 fde->end = end;
1994
1995 fde->eh_frame_p = eh_frame_p;
1996
1997 add_fde (fde_table, fde);
1998 }
1999
2000 return end;
2001 }
2002
2003 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2004 expect an FDE or a CIE. */
2005
2006 static const gdb_byte *
2007 decode_frame_entry (struct gdbarch *gdbarch,
2008 struct comp_unit *unit, const gdb_byte *start,
2009 int eh_frame_p,
2010 dwarf2_cie_table &cie_table,
2011 dwarf2_fde_table *fde_table,
2012 enum eh_frame_type entry_type)
2013 {
2014 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
2015 const gdb_byte *ret;
2016 ptrdiff_t start_offset;
2017
2018 while (1)
2019 {
2020 ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p,
2021 cie_table, fde_table, entry_type);
2022 if (ret != NULL)
2023 break;
2024
2025 /* We have corrupt input data of some form. */
2026
2027 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2028 and mismatches wrt padding and alignment of debug sections. */
2029 /* Note that there is no requirement in the standard for any
2030 alignment at all in the frame unwind sections. Testing for
2031 alignment before trying to interpret data would be incorrect.
2032
2033 However, GCC traditionally arranged for frame sections to be
2034 sized such that the FDE length and CIE fields happen to be
2035 aligned (in theory, for performance). This, unfortunately,
2036 was done with .align directives, which had the side effect of
2037 forcing the section to be aligned by the linker.
2038
2039 This becomes a problem when you have some other producer that
2040 creates frame sections that are not as strictly aligned. That
2041 produces a hole in the frame info that gets filled by the
2042 linker with zeros.
2043
2044 The GCC behaviour is arguably a bug, but it's effectively now
2045 part of the ABI, so we're now stuck with it, at least at the
2046 object file level. A smart linker may decide, in the process
2047 of compressing duplicate CIE information, that it can rewrite
2048 the entire output section without this extra padding. */
2049
2050 start_offset = start - unit->dwarf_frame_buffer;
2051 if (workaround < ALIGN4 && (start_offset & 3) != 0)
2052 {
2053 start += 4 - (start_offset & 3);
2054 workaround = ALIGN4;
2055 continue;
2056 }
2057 if (workaround < ALIGN8 && (start_offset & 7) != 0)
2058 {
2059 start += 8 - (start_offset & 7);
2060 workaround = ALIGN8;
2061 continue;
2062 }
2063
2064 /* Nothing left to try. Arrange to return as if we've consumed
2065 the entire input section. Hopefully we'll get valid info from
2066 the other of .debug_frame/.eh_frame. */
2067 workaround = FAIL;
2068 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2069 break;
2070 }
2071
2072 switch (workaround)
2073 {
2074 case NONE:
2075 break;
2076
2077 case ALIGN4:
2078 complaint (_("\
2079 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2080 bfd_get_filename (unit->dwarf_frame_section->owner),
2081 bfd_section_name (unit->dwarf_frame_section));
2082 break;
2083
2084 case ALIGN8:
2085 complaint (_("\
2086 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2087 bfd_get_filename (unit->dwarf_frame_section->owner),
2088 bfd_section_name (unit->dwarf_frame_section));
2089 break;
2090
2091 default:
2092 complaint (_("Corrupt data in %s:%s"),
2093 bfd_get_filename (unit->dwarf_frame_section->owner),
2094 bfd_section_name (unit->dwarf_frame_section));
2095 break;
2096 }
2097
2098 return ret;
2099 }
2100 \f
2101 static bool
2102 fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
2103 {
2104 if (aa->initial_location == bb->initial_location)
2105 {
2106 if (aa->address_range != bb->address_range
2107 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2108 /* Linker bug, e.g. gold/10400.
2109 Work around it by keeping stable sort order. */
2110 return aa < bb;
2111 else
2112 /* Put eh_frame entries after debug_frame ones. */
2113 return aa->eh_frame_p < bb->eh_frame_p;
2114 }
2115
2116 return aa->initial_location < bb->initial_location;
2117 }
2118
2119 void
2120 dwarf2_build_frame_info (struct objfile *objfile)
2121 {
2122 const gdb_byte *frame_ptr;
2123 dwarf2_cie_table cie_table;
2124 dwarf2_fde_table fde_table;
2125
2126 struct gdbarch *gdbarch = objfile->arch ();
2127
2128 /* Build a minimal decoding of the DWARF2 compilation unit. */
2129 std::unique_ptr<comp_unit> unit (new comp_unit (objfile));
2130
2131 if (objfile->separate_debug_objfile_backlink == NULL)
2132 {
2133 /* Do not read .eh_frame from separate file as they must be also
2134 present in the main file. */
2135 dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
2136 &unit->dwarf_frame_section,
2137 &unit->dwarf_frame_buffer,
2138 &unit->dwarf_frame_size);
2139 if (unit->dwarf_frame_size)
2140 {
2141 asection *got, *txt;
2142
2143 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2144 that is used for the i386/amd64 target, which currently is
2145 the only target in GCC that supports/uses the
2146 DW_EH_PE_datarel encoding. */
2147 got = bfd_get_section_by_name (unit->abfd, ".got");
2148 if (got)
2149 unit->dbase = got->vma;
2150
2151 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2152 so far. */
2153 txt = bfd_get_section_by_name (unit->abfd, ".text");
2154 if (txt)
2155 unit->tbase = txt->vma;
2156
2157 try
2158 {
2159 frame_ptr = unit->dwarf_frame_buffer;
2160 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2161 frame_ptr = decode_frame_entry (gdbarch, unit.get (),
2162 frame_ptr, 1,
2163 cie_table, &fde_table,
2164 EH_CIE_OR_FDE_TYPE_ID);
2165 }
2166
2167 catch (const gdb_exception_error &e)
2168 {
2169 warning (_("skipping .eh_frame info of %s: %s"),
2170 objfile_name (objfile), e.what ());
2171
2172 fde_table.clear ();
2173 /* The cie_table is discarded below. */
2174 }
2175
2176 cie_table.clear ();
2177 }
2178 }
2179
2180 dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
2181 &unit->dwarf_frame_section,
2182 &unit->dwarf_frame_buffer,
2183 &unit->dwarf_frame_size);
2184 if (unit->dwarf_frame_size)
2185 {
2186 size_t num_old_fde_entries = fde_table.size ();
2187
2188 try
2189 {
2190 frame_ptr = unit->dwarf_frame_buffer;
2191 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2192 frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0,
2193 cie_table, &fde_table,
2194 EH_CIE_OR_FDE_TYPE_ID);
2195 }
2196 catch (const gdb_exception_error &e)
2197 {
2198 warning (_("skipping .debug_frame info of %s: %s"),
2199 objfile_name (objfile), e.what ());
2200
2201 fde_table.resize (num_old_fde_entries);
2202 }
2203 }
2204
2205 struct dwarf2_fde *fde_prev = NULL;
2206 struct dwarf2_fde *first_non_zero_fde = NULL;
2207
2208 /* Prepare FDE table for lookups. */
2209 std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than);
2210
2211 /* Check for leftovers from --gc-sections. The GNU linker sets
2212 the relevant symbols to zero, but doesn't zero the FDE *end*
2213 ranges because there's no relocation there. It's (offset,
2214 length), not (start, end). On targets where address zero is
2215 just another valid address this can be a problem, since the
2216 FDEs appear to be non-empty in the output --- we could pick
2217 out the wrong FDE. To work around this, when overlaps are
2218 detected, we prefer FDEs that do not start at zero.
2219
2220 Start by finding the first FDE with non-zero start. Below
2221 we'll discard all FDEs that start at zero and overlap this
2222 one. */
2223 for (struct dwarf2_fde *fde : fde_table)
2224 {
2225 if (fde->initial_location != (unrelocated_addr) 0)
2226 {
2227 first_non_zero_fde = fde;
2228 break;
2229 }
2230 }
2231
2232 /* Since we'll be doing bsearch, squeeze out identical (except
2233 for eh_frame_p) fde entries so bsearch result is predictable.
2234 Also discard leftovers from --gc-sections. */
2235 for (struct dwarf2_fde *fde : fde_table)
2236 {
2237 if (fde->initial_location == (unrelocated_addr) 0
2238 && first_non_zero_fde != NULL
2239 && first_non_zero_fde->initial_location < fde->end_addr ())
2240 continue;
2241
2242 if (fde_prev != NULL
2243 && fde_prev->initial_location == fde->initial_location)
2244 continue;
2245
2246 unit->fde_table.push_back (fde);
2247 fde_prev = fde;
2248 }
2249 unit->fde_table.shrink_to_fit ();
2250
2251 set_comp_unit (objfile, unit.release ());
2252 }
2253
2254 /* Handle 'maintenance show dwarf unwinders'. */
2255
2256 static void
2257 show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty,
2258 struct cmd_list_element *c,
2259 const char *value)
2260 {
2261 gdb_printf (file,
2262 _("The DWARF stack unwinders are currently %s.\n"),
2263 value);
2264 }
2265
2266 void _initialize_dwarf2_frame ();
2267 void
2268 _initialize_dwarf2_frame ()
2269 {
2270 add_setshow_boolean_cmd ("unwinders", class_obscure,
2271 &dwarf2_frame_unwinders_enabled_p , _("\
2272 Set whether the DWARF stack frame unwinders are used."), _("\
2273 Show whether the DWARF stack frame unwinders are used."), _("\
2274 When enabled the DWARF stack frame unwinders can be used for architectures\n\
2275 that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\
2276 architecture that doesn't support them will have no effect."),
2277 NULL,
2278 show_dwarf_unwinders_enabled_p,
2279 &set_dwarf_cmdlist,
2280 &show_dwarf_cmdlist);
2281
2282 #if GDB_SELF_TEST
2283 selftests::register_test_foreach_arch ("execute_cfa_program",
2284 selftests::execute_cfa_program_test);
2285 #endif
2286 }