1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003-2022 Free Software Foundation, Inc.
5 Contributed by Mark Kettenis.
7 This file is part of GDB.
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.
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.
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/>. */
23 #include "dwarf2/expr.h"
25 #include "dwarf2/leb.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
37 #include "complaints.h"
38 #include "dwarf2/frame.h"
39 #include "dwarf2/read.h"
40 #include "dwarf2/public.h"
42 #include "dwarf2/loc.h"
43 #include "dwarf2/frame-tailcall.h"
44 #include "gdbsupport/gdb_binary_search.h"
46 #include "gdbsupport/selftest.h"
47 #include "selftest-arch.h"
49 #include <unordered_map>
55 /* Call Frame Information (CFI). */
57 /* Common Information Entry (CIE). */
61 /* Computation Unit for this CIE. */
62 struct comp_unit *unit;
64 /* Offset into the .debug_frame section where this CIE was found.
65 Used to identify this CIE. */
68 /* Constant that is factored out of all advance location
70 ULONGEST code_alignment_factor;
72 /* Constants that is factored out of all offset instructions. */
73 LONGEST data_alignment_factor;
75 /* Return address column. */
76 ULONGEST return_address_register;
78 /* Instruction sequence to initialize a register set. */
79 const gdb_byte *initial_instructions;
82 /* Saved augmentation, in case it's needed later. */
85 /* Encoding of addresses. */
88 /* Target address size in bytes. */
91 /* Target pointer size in bytes. */
94 /* True if a 'z' augmentation existed. */
95 unsigned char saw_z_augmentation;
97 /* True if an 'S' augmentation existed. */
98 unsigned char signal_frame;
100 /* The version recorded in the CIE. */
101 unsigned char version;
103 /* The segment size. */
104 unsigned char segment_size;
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;
111 /* Frame Description Entry (FDE). */
115 /* CIE for this FDE. */
116 struct dwarf2_cie *cie;
118 /* First location associated with this FDE. */
119 CORE_ADDR initial_location;
121 /* Number of bytes of program instructions described by this FDE. */
122 CORE_ADDR address_range;
124 /* Instruction sequence. */
125 const gdb_byte *instructions;
128 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
130 unsigned char eh_frame_p;
133 typedef std::vector<dwarf2_fde *> dwarf2_fde_table;
135 /* A minimal decoding of DWARF2 compilation units. We only decode
136 what's needed to get to the call frame information. */
140 comp_unit (struct objfile *objf)
141 : abfd (objf->obfd.get ())
145 /* Keep the bfd convenient. */
148 /* Pointer to the .debug_frame section loaded into memory. */
149 const gdb_byte *dwarf_frame_buffer = nullptr;
151 /* Length of the loaded .debug_frame section. */
152 bfd_size_type dwarf_frame_size = 0;
154 /* Pointer to the .debug_frame section. */
155 asection *dwarf_frame_section = nullptr;
157 /* Base for DW_EH_PE_datarel encodings. */
160 /* Base for DW_EH_PE_textrel encodings. */
164 dwarf2_fde_table fde_table;
166 /* Hold data used by this module. */
167 auto_obstack obstack;
170 static struct dwarf2_fde *dwarf2_frame_find_fde
171 (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile);
173 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
176 static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
177 int ptr_len, const gdb_byte *buf,
178 unsigned int *bytes_read_ptr,
179 CORE_ADDR func_base);
182 /* See dwarf2/frame.h. */
183 bool dwarf2_frame_unwinders_enabled_p = true;
185 /* Store the length the expression for the CFA in the `cfa_reg' field,
186 which is unused in that case. */
187 #define cfa_exp_len cfa_reg
189 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie)
190 : pc (pc_), data_align (cie->data_alignment_factor),
191 code_align (cie->code_alignment_factor),
192 retaddr_column (cie->return_address_register)
196 /* Execute the required actions for both the DW_CFA_restore and
197 DW_CFA_restore_extended instructions. */
199 dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
200 struct dwarf2_frame_state *fs, int eh_frame_p)
204 reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
205 fs->regs.alloc_regs (reg + 1);
207 /* Check if this register was explicitly initialized in the
208 CIE initial instructions. If not, default the rule to
210 if (reg < fs->initial.reg.size ())
211 fs->regs.reg[reg] = fs->initial.reg[reg];
213 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
215 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
217 int regnum = dwarf_reg_to_regnum (gdbarch, reg);
220 incomplete CFI data; DW_CFA_restore unspecified\n\
221 register %s (#%d) at %s"),
222 gdbarch_register_name (gdbarch, regnum), regnum,
223 paddress (gdbarch, fs->pc));
228 execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
229 frame_info_ptr this_frame, CORE_ADDR initial,
230 int initial_in_stack_memory, dwarf2_per_objfile *per_objfile)
232 dwarf_expr_context ctx (per_objfile, addr_size);
233 scoped_value_mark free_values;
235 ctx.push_address (initial, initial_in_stack_memory);
236 value *result_val = ctx.evaluate (exp, len, true, nullptr, this_frame);
238 if (VALUE_LVAL (result_val) == lval_memory)
239 return value_address (result_val);
241 return value_as_address (result_val);
245 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
246 PC. Modify FS state accordingly. Return current INSN_PTR where the
247 execution has stopped, one can resume it on the next call. */
249 static const gdb_byte *
250 execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
251 const gdb_byte *insn_end, struct gdbarch *gdbarch,
252 CORE_ADDR pc, struct dwarf2_frame_state *fs,
253 CORE_ADDR text_offset)
255 int eh_frame_p = fde->eh_frame_p;
256 unsigned int bytes_read;
257 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
259 while (insn_ptr < insn_end && fs->pc <= pc)
261 gdb_byte insn = *insn_ptr++;
265 if ((insn & 0xc0) == DW_CFA_advance_loc)
266 fs->pc += (insn & 0x3f) * fs->code_align;
267 else if ((insn & 0xc0) == DW_CFA_offset)
270 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
271 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
272 offset = utmp * fs->data_align;
273 fs->regs.alloc_regs (reg + 1);
274 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
275 fs->regs.reg[reg].loc.offset = offset;
277 else if ((insn & 0xc0) == DW_CFA_restore)
280 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
287 fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
288 fde->cie->ptr_size, insn_ptr,
289 &bytes_read, fde->initial_location);
290 /* Apply the text offset for relocatable objects. */
291 fs->pc += text_offset;
292 insn_ptr += bytes_read;
295 case DW_CFA_advance_loc1:
296 utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
297 fs->pc += utmp * fs->code_align;
300 case DW_CFA_advance_loc2:
301 utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
302 fs->pc += utmp * fs->code_align;
305 case DW_CFA_advance_loc4:
306 utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
307 fs->pc += utmp * fs->code_align;
311 case DW_CFA_offset_extended:
312 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
313 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
314 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
315 offset = utmp * fs->data_align;
316 fs->regs.alloc_regs (reg + 1);
317 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
318 fs->regs.reg[reg].loc.offset = offset;
321 case DW_CFA_restore_extended:
322 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
323 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
326 case DW_CFA_undefined:
327 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
328 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
329 fs->regs.alloc_regs (reg + 1);
330 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
333 case DW_CFA_same_value:
334 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
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_SAME_VALUE;
340 case DW_CFA_register:
341 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
342 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
343 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
344 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
345 fs->regs.alloc_regs (reg + 1);
346 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
347 fs->regs.reg[reg].loc.reg = utmp;
350 case DW_CFA_remember_state:
352 struct dwarf2_frame_state_reg_info *new_rs;
354 new_rs = new dwarf2_frame_state_reg_info (fs->regs);
355 fs->regs.prev = new_rs;
359 case DW_CFA_restore_state:
361 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
366 bad CFI data; mismatched DW_CFA_restore_state at %s"),
367 paddress (gdbarch, fs->pc));
370 fs->regs = std::move (*old_rs);
375 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
376 fs->regs.cfa_reg = reg;
377 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
379 if (fs->armcc_cfa_offsets_sf)
380 utmp *= fs->data_align;
382 fs->regs.cfa_offset = utmp;
383 fs->regs.cfa_how = CFA_REG_OFFSET;
386 case DW_CFA_def_cfa_register:
387 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
388 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
390 fs->regs.cfa_how = CFA_REG_OFFSET;
393 case DW_CFA_def_cfa_offset:
394 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
396 if (fs->armcc_cfa_offsets_sf)
397 utmp *= fs->data_align;
399 fs->regs.cfa_offset = utmp;
400 /* cfa_how deliberately not set. */
406 case DW_CFA_def_cfa_expression:
407 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
408 fs->regs.cfa_exp_len = utmp;
409 fs->regs.cfa_exp = insn_ptr;
410 fs->regs.cfa_how = CFA_EXP;
411 insn_ptr += fs->regs.cfa_exp_len;
414 case DW_CFA_expression:
415 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
416 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
417 fs->regs.alloc_regs (reg + 1);
418 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
419 fs->regs.reg[reg].loc.exp.start = insn_ptr;
420 fs->regs.reg[reg].loc.exp.len = utmp;
421 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
425 case DW_CFA_offset_extended_sf:
426 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
427 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
428 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
429 offset *= fs->data_align;
430 fs->regs.alloc_regs (reg + 1);
431 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
432 fs->regs.reg[reg].loc.offset = offset;
435 case DW_CFA_val_offset:
436 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
437 fs->regs.alloc_regs (reg + 1);
438 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
439 offset = utmp * fs->data_align;
440 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
441 fs->regs.reg[reg].loc.offset = offset;
444 case DW_CFA_val_offset_sf:
445 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
446 fs->regs.alloc_regs (reg + 1);
447 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
448 offset *= fs->data_align;
449 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
450 fs->regs.reg[reg].loc.offset = offset;
453 case DW_CFA_val_expression:
454 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
455 fs->regs.alloc_regs (reg + 1);
456 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
457 fs->regs.reg[reg].loc.exp.start = insn_ptr;
458 fs->regs.reg[reg].loc.exp.len = utmp;
459 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
463 case DW_CFA_def_cfa_sf:
464 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
465 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
467 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
468 fs->regs.cfa_offset = offset * fs->data_align;
469 fs->regs.cfa_how = CFA_REG_OFFSET;
472 case DW_CFA_def_cfa_offset_sf:
473 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
474 fs->regs.cfa_offset = offset * fs->data_align;
475 /* cfa_how deliberately not set. */
478 case DW_CFA_GNU_args_size:
480 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
483 case DW_CFA_GNU_negative_offset_extended:
484 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
485 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
486 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
487 offset = utmp * fs->data_align;
488 fs->regs.alloc_regs (reg + 1);
489 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
490 fs->regs.reg[reg].loc.offset = -offset;
494 if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user)
496 /* Handle vendor-specific CFI for different architectures. */
497 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs))
498 error (_("Call Frame Instruction op %d in vendor extension "
499 "space is not handled on this architecture."),
503 internal_error (_("Unknown CFI encountered."));
508 if (fs->initial.reg.empty ())
510 /* Don't allow remember/restore between CIE and FDE programs. */
511 delete fs->regs.prev;
512 fs->regs.prev = NULL;
520 namespace selftests {
522 /* Unit test to function execute_cfa_program. */
525 execute_cfa_program_test (struct gdbarch *gdbarch)
527 struct dwarf2_fde fde;
528 struct dwarf2_cie cie;
530 memset (&fde, 0, sizeof fde);
531 memset (&cie, 0, sizeof cie);
533 cie.data_alignment_factor = -4;
534 cie.code_alignment_factor = 2;
537 dwarf2_frame_state fs (0, fde.cie);
541 DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
542 DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
543 DW_CFA_remember_state,
544 DW_CFA_restore_state,
547 const gdb_byte *insn_end = insns + sizeof (insns);
548 const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch,
551 SELF_CHECK (out == insn_end);
552 SELF_CHECK (fs.pc == 0);
554 /* The instructions above only use r1 and r2, but the register numbers
555 used are adjusted by dwarf2_frame_adjust_regnum. */
556 auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p);
557 auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p);
559 SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1));
561 SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET);
562 SELF_CHECK (fs.regs.reg[r2].loc.offset == -4);
564 for (auto i = 0; i < fs.regs.reg.size (); i++)
566 SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED);
568 SELF_CHECK (fs.regs.cfa_reg == 1);
569 SELF_CHECK (fs.regs.cfa_offset == 4);
570 SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET);
571 SELF_CHECK (fs.regs.cfa_exp == NULL);
572 SELF_CHECK (fs.regs.prev == NULL);
575 } // namespace selftests
576 #endif /* GDB_SELF_TEST */
580 /* Architecture-specific operations. */
582 static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch,
584 struct dwarf2_frame_state_reg *reg,
585 frame_info_ptr this_frame);
587 struct dwarf2_frame_ops
589 /* Pre-initialize the register state REG for register REGNUM. */
590 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
592 = dwarf2_frame_default_init_reg;
594 /* Check whether the THIS_FRAME is a signal trampoline. */
595 int (*signal_frame_p) (struct gdbarch *, frame_info_ptr) = nullptr;
597 /* Convert .eh_frame register number to DWARF register number, or
598 adjust .debug_frame register number. */
599 int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr;
602 /* Per-architecture data key. */
603 static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data;
605 /* Get or initialize the frame ops. */
606 static dwarf2_frame_ops *
607 get_frame_ops (struct gdbarch *gdbarch)
609 dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch);
610 if (result == nullptr)
611 result = dwarf2_frame_data.emplace (gdbarch);
615 /* Default architecture-specific register state initialization
619 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
620 struct dwarf2_frame_state_reg *reg,
621 frame_info_ptr this_frame)
623 /* If we have a register that acts as a program counter, mark it as
624 a destination for the return address. If we have a register that
625 serves as the stack pointer, arrange for it to be filled with the
626 call frame address (CFA). The other registers are marked as
629 We copy the return address to the program counter, since many
630 parts in GDB assume that it is possible to get the return address
631 by unwinding the program counter register. However, on ISA's
632 with a dedicated return address register, the CFI usually only
633 contains information to unwind that return address register.
635 The reason we're treating the stack pointer special here is
636 because in many cases GCC doesn't emit CFI for the stack pointer
637 and implicitly assumes that it is equal to the CFA. This makes
638 some sense since the DWARF specification (version 3, draft 8,
641 "Typically, the CFA is defined to be the value of the stack
642 pointer at the call site in the previous frame (which may be
643 different from its value on entry to the current frame)."
645 However, this isn't true for all platforms supported by GCC
646 (e.g. IBM S/390 and zSeries). Those architectures should provide
647 their own architecture-specific initialization function. */
649 if (regnum == gdbarch_pc_regnum (gdbarch))
650 reg->how = DWARF2_FRAME_REG_RA;
651 else if (regnum == gdbarch_sp_regnum (gdbarch))
652 reg->how = DWARF2_FRAME_REG_CFA;
655 /* Set the architecture-specific register state initialization
656 function for GDBARCH to INIT_REG. */
659 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
660 void (*init_reg) (struct gdbarch *, int,
661 struct dwarf2_frame_state_reg *,
664 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
666 ops->init_reg = init_reg;
669 /* Pre-initialize the register state REG for register REGNUM. */
672 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
673 struct dwarf2_frame_state_reg *reg,
674 frame_info_ptr this_frame)
676 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
678 ops->init_reg (gdbarch, regnum, reg, this_frame);
681 /* Set the architecture-specific signal trampoline recognition
682 function for GDBARCH to SIGNAL_FRAME_P. */
685 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
686 int (*signal_frame_p) (struct gdbarch *,
689 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
691 ops->signal_frame_p = signal_frame_p;
694 /* Query the architecture-specific signal frame recognizer for
698 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
699 frame_info_ptr this_frame)
701 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
703 if (ops->signal_frame_p == NULL)
705 return ops->signal_frame_p (gdbarch, this_frame);
708 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
712 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
713 int (*adjust_regnum) (struct gdbarch *,
716 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
718 ops->adjust_regnum = adjust_regnum;
721 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
725 dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
726 int regnum, int eh_frame_p)
728 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
730 if (ops->adjust_regnum == NULL)
732 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
736 dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
737 struct dwarf2_fde *fde)
739 struct compunit_symtab *cust;
741 cust = find_pc_compunit_symtab (fs->pc);
745 if (producer_is_realview (cust->producer ()))
747 if (fde->cie->version == 1)
748 fs->armcc_cfa_offsets_sf = 1;
750 if (fde->cie->version == 1)
751 fs->armcc_cfa_offsets_reversed = 1;
753 /* The reversed offset problem is present in some compilers
754 using DWARF3, but it was eventually fixed. Check the ARM
755 defined augmentations, which are in the format "armcc" followed
756 by a list of one-character options. The "+" option means
757 this problem is fixed (no quirk needed). If the armcc
758 augmentation is missing, the quirk is needed. */
759 if (fde->cie->version == 3
760 && (!startswith (fde->cie->augmentation, "armcc")
761 || strchr (fde->cie->augmentation + 5, '+') == NULL))
762 fs->armcc_cfa_offsets_reversed = 1;
769 /* See dwarf2/frame.h. */
772 dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
773 struct dwarf2_per_cu_data *data,
774 int *regnum_out, LONGEST *offset_out,
775 CORE_ADDR *text_offset_out,
776 const gdb_byte **cfa_start_out,
777 const gdb_byte **cfa_end_out)
779 struct dwarf2_fde *fde;
780 dwarf2_per_objfile *per_objfile;
783 /* Find the correct FDE. */
784 fde = dwarf2_frame_find_fde (&pc1, &per_objfile);
786 error (_("Could not compute CFA; needed to translate this expression"));
788 gdb_assert (per_objfile != nullptr);
790 dwarf2_frame_state fs (pc1, fde->cie);
792 /* Check for "quirks" - known bugs in producers. */
793 dwarf2_frame_find_quirks (&fs, fde);
795 /* First decode all the insns in the CIE. */
796 execute_cfa_program (fde, fde->cie->initial_instructions,
797 fde->cie->end, gdbarch, pc, &fs,
798 per_objfile->objfile->text_section_offset ());
800 /* Save the initialized register set. */
801 fs.initial = fs.regs;
803 /* Then decode the insns in the FDE up to our target PC. */
804 execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs,
805 per_objfile->objfile->text_section_offset ());
807 /* Calculate the CFA. */
808 switch (fs.regs.cfa_how)
812 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg);
814 *regnum_out = regnum;
815 if (fs.armcc_cfa_offsets_reversed)
816 *offset_out = -fs.regs.cfa_offset;
818 *offset_out = fs.regs.cfa_offset;
823 *text_offset_out = per_objfile->objfile->text_section_offset ();
824 *cfa_start_out = fs.regs.cfa_exp;
825 *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
829 internal_error (_("Unknown CFA rule."));
834 struct dwarf2_frame_cache
836 /* DWARF Call Frame Address. */
839 /* Set if the return address column was marked as unavailable
840 (required non-collected memory or registers to compute). */
841 int unavailable_retaddr;
843 /* Set if the return address column was marked as undefined. */
844 int undefined_retaddr;
846 /* Saved registers, indexed by GDB register number, not by DWARF
848 struct dwarf2_frame_state_reg *reg;
850 /* Return address register. */
851 struct dwarf2_frame_state_reg retaddr_reg;
853 /* Target address size in bytes. */
856 /* The dwarf2_per_objfile from which this frame description came. */
857 dwarf2_per_objfile *per_objfile;
859 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
860 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
861 involved. Non-bottom frames of a virtual tail call frames chain use
862 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
864 void *tailcall_cache;
867 static struct dwarf2_frame_cache *
868 dwarf2_frame_cache (frame_info_ptr this_frame, void **this_cache)
870 struct gdbarch *gdbarch = get_frame_arch (this_frame);
871 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
872 struct dwarf2_frame_cache *cache;
873 struct dwarf2_fde *fde;
875 const gdb_byte *instr;
878 return (struct dwarf2_frame_cache *) *this_cache;
880 /* Allocate a new cache. */
881 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
882 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
887 Note that if the next frame is never supposed to return (i.e. a call
888 to abort), the compiler might optimize away the instruction at
889 its return address. As a result the return address will
890 point at some random instruction, and the CFI for that
891 instruction is probably worthless to us. GCC's unwinder solves
892 this problem by substracting 1 from the return address to get an
893 address in the middle of a presumed call instruction (or the
894 instruction in the associated delay slot). This should only be
895 done for "normal" frames and not for resume-type frames (signal
896 handlers, sentinel frames, dummy frames). The function
897 get_frame_address_in_block does just this. It's not clear how
898 reliable the method is though; there is the potential for the
899 register state pre-call being different to that on return. */
900 CORE_ADDR pc1 = get_frame_address_in_block (this_frame);
902 /* Find the correct FDE. */
903 fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
904 gdb_assert (fde != NULL);
905 gdb_assert (cache->per_objfile != nullptr);
907 /* Allocate and initialize the frame state. */
908 struct dwarf2_frame_state fs (pc1, fde->cie);
910 cache->addr_size = fde->cie->addr_size;
912 /* Check for "quirks" - known bugs in producers. */
913 dwarf2_frame_find_quirks (&fs, fde);
915 /* First decode all the insns in the CIE. */
916 execute_cfa_program (fde, fde->cie->initial_instructions,
917 fde->cie->end, gdbarch,
918 get_frame_address_in_block (this_frame), &fs,
919 cache->per_objfile->objfile->text_section_offset ());
921 /* Save the initialized register set. */
922 fs.initial = fs.regs;
924 /* Fetching the entry pc for THIS_FRAME won't necessarily result
925 in an address that's within the range of FDE locations. This
926 is due to the possibility of the function occupying non-contiguous
928 LONGEST entry_cfa_sp_offset;
929 int entry_cfa_sp_offset_p = 0;
930 if (get_frame_func_if_available (this_frame, &entry_pc)
931 && fde->initial_location <= entry_pc
932 && entry_pc < fde->initial_location + fde->address_range)
934 /* Decode the insns in the FDE up to the entry PC. */
935 instr = execute_cfa_program
936 (fde, fde->instructions, fde->end, gdbarch, entry_pc, &fs,
937 cache->per_objfile->objfile->text_section_offset ());
939 if (fs.regs.cfa_how == CFA_REG_OFFSET
940 && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg)
941 == gdbarch_sp_regnum (gdbarch)))
943 entry_cfa_sp_offset = fs.regs.cfa_offset;
944 entry_cfa_sp_offset_p = 1;
948 instr = fde->instructions;
950 /* Then decode the insns in the FDE up to our target PC. */
951 execute_cfa_program (fde, instr, fde->end, gdbarch,
952 get_frame_address_in_block (this_frame), &fs,
953 cache->per_objfile->objfile->text_section_offset ());
957 /* Calculate the CFA. */
958 switch (fs.regs.cfa_how)
961 cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg);
962 if (fs.armcc_cfa_offsets_reversed)
963 cache->cfa -= fs.regs.cfa_offset;
965 cache->cfa += fs.regs.cfa_offset;
970 execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len,
971 cache->addr_size, this_frame, 0, 0,
976 internal_error (_("Unknown CFA rule."));
979 catch (const gdb_exception_error &ex)
981 if (ex.error == NOT_AVAILABLE_ERROR)
983 cache->unavailable_retaddr = 1;
990 /* Initialize the register state. */
994 for (regnum = 0; regnum < num_regs; regnum++)
995 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
998 /* Go through the DWARF2 CFI generated table and save its register
999 location information in the cache. Note that we don't skip the
1000 return address column; it's perfectly all right for it to
1001 correspond to a real register. */
1003 int column; /* CFI speak for "register number". */
1005 for (column = 0; column < fs.regs.reg.size (); column++)
1007 /* Use the GDB register number as the destination index. */
1008 int regnum = dwarf_reg_to_regnum (gdbarch, column);
1010 /* Protect against a target returning a bad register. */
1011 if (regnum < 0 || regnum >= num_regs)
1014 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1015 of all debug info registers. If it doesn't, complain (but
1016 not too loudly). It turns out that GCC assumes that an
1017 unspecified register implies "same value" when CFI (draft
1018 7) specifies nothing at all. Such a register could equally
1019 be interpreted as "undefined". Also note that this check
1020 isn't sufficient; it only checks that all registers in the
1021 range [0 .. max column] are specified, and won't detect
1022 problems when a debug info register falls outside of the
1023 table. We need a way of iterating through all the valid
1024 DWARF2 register numbers. */
1025 if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
1027 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
1029 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1030 gdbarch_register_name (gdbarch, regnum),
1031 paddress (gdbarch, fs.pc));
1034 cache->reg[regnum] = fs.regs.reg[column];
1038 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1039 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1043 for (regnum = 0; regnum < num_regs; regnum++)
1045 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1046 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
1048 const std::vector<struct dwarf2_frame_state_reg> ®s
1050 ULONGEST retaddr_column = fs.retaddr_column;
1052 /* It seems rather bizarre to specify an "empty" column as
1053 the return adress column. However, this is exactly
1054 what GCC does on some targets. It turns out that GCC
1055 assumes that the return address can be found in the
1056 register corresponding to the return address column.
1057 Incidentally, that's how we should treat a return
1058 address column specifying "same value" too. */
1059 if (fs.retaddr_column < fs.regs.reg.size ()
1060 && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED
1061 && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE)
1063 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1064 cache->reg[regnum] = regs[retaddr_column];
1066 cache->retaddr_reg = regs[retaddr_column];
1070 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1072 cache->reg[regnum].loc.reg = fs.retaddr_column;
1073 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1077 cache->retaddr_reg.loc.reg = fs.retaddr_column;
1078 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1085 if (fs.retaddr_column < fs.regs.reg.size ()
1086 && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1087 cache->undefined_retaddr = 1;
1089 dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
1090 (entry_cfa_sp_offset_p
1091 ? &entry_cfa_sp_offset : NULL));
1096 static enum unwind_stop_reason
1097 dwarf2_frame_unwind_stop_reason (frame_info_ptr this_frame,
1100 struct dwarf2_frame_cache *cache
1101 = dwarf2_frame_cache (this_frame, this_cache);
1103 if (cache->unavailable_retaddr)
1104 return UNWIND_UNAVAILABLE;
1106 if (cache->undefined_retaddr)
1107 return UNWIND_OUTERMOST;
1109 return UNWIND_NO_REASON;
1113 dwarf2_frame_this_id (frame_info_ptr this_frame, void **this_cache,
1114 struct frame_id *this_id)
1116 struct dwarf2_frame_cache *cache =
1117 dwarf2_frame_cache (this_frame, this_cache);
1119 if (cache->unavailable_retaddr)
1120 (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame));
1121 else if (cache->undefined_retaddr)
1124 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
1127 static struct value *
1128 dwarf2_frame_prev_register (frame_info_ptr this_frame, void **this_cache,
1131 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1132 struct dwarf2_frame_cache *cache =
1133 dwarf2_frame_cache (this_frame, this_cache);
1137 /* Non-bottom frames of a virtual tail call frames chain use
1138 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1139 them. If dwarf2_tailcall_prev_register_first does not have specific value
1140 unwind the register, tail call frames are assumed to have the register set
1141 of the top caller. */
1142 if (cache->tailcall_cache)
1146 val = dwarf2_tailcall_prev_register_first (this_frame,
1147 &cache->tailcall_cache,
1153 switch (cache->reg[regnum].how)
1155 case DWARF2_FRAME_REG_UNDEFINED:
1156 /* If CFI explicitly specified that the value isn't defined,
1157 mark it as optimized away; the value isn't available. */
1158 return frame_unwind_got_optimized (this_frame, regnum);
1160 case DWARF2_FRAME_REG_SAVED_OFFSET:
1161 addr = cache->cfa + cache->reg[regnum].loc.offset;
1162 return frame_unwind_got_memory (this_frame, regnum, addr);
1164 case DWARF2_FRAME_REG_SAVED_REG:
1165 realnum = dwarf_reg_to_regnum_or_error
1166 (gdbarch, cache->reg[regnum].loc.reg);
1167 return frame_unwind_got_register (this_frame, regnum, realnum);
1169 case DWARF2_FRAME_REG_SAVED_EXP:
1170 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1171 cache->reg[regnum].loc.exp.len,
1173 this_frame, cache->cfa, 1,
1174 cache->per_objfile);
1175 return frame_unwind_got_memory (this_frame, regnum, addr);
1177 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1178 addr = cache->cfa + cache->reg[regnum].loc.offset;
1179 return frame_unwind_got_constant (this_frame, regnum, addr);
1181 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1182 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1183 cache->reg[regnum].loc.exp.len,
1185 this_frame, cache->cfa, 1,
1186 cache->per_objfile);
1187 return frame_unwind_got_constant (this_frame, regnum, addr);
1189 case DWARF2_FRAME_REG_UNSPECIFIED:
1190 /* GCC, in its infinite wisdom decided to not provide unwind
1191 information for registers that are "same value". Since
1192 DWARF2 (3 draft 7) doesn't define such behavior, said
1193 registers are actually undefined (which is different to CFI
1194 "undefined"). Code above issues a complaint about this.
1195 Here just fudge the books, assume GCC, and that the value is
1196 more inner on the stack. */
1197 return frame_unwind_got_register (this_frame, regnum, regnum);
1199 case DWARF2_FRAME_REG_SAME_VALUE:
1200 return frame_unwind_got_register (this_frame, regnum, regnum);
1202 case DWARF2_FRAME_REG_CFA:
1203 return frame_unwind_got_address (this_frame, regnum, cache->cfa);
1205 case DWARF2_FRAME_REG_CFA_OFFSET:
1206 addr = cache->cfa + cache->reg[regnum].loc.offset;
1207 return frame_unwind_got_address (this_frame, regnum, addr);
1209 case DWARF2_FRAME_REG_RA_OFFSET:
1210 addr = cache->reg[regnum].loc.offset;
1211 regnum = dwarf_reg_to_regnum_or_error
1212 (gdbarch, cache->retaddr_reg.loc.reg);
1213 addr += get_frame_register_unsigned (this_frame, regnum);
1214 return frame_unwind_got_address (this_frame, regnum, addr);
1216 case DWARF2_FRAME_REG_FN:
1217 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1220 internal_error (_("Unknown register rule."));
1224 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1225 call frames chain. */
1228 dwarf2_frame_dealloc_cache (frame_info *self, void *this_cache)
1230 struct dwarf2_frame_cache *cache
1231 = dwarf2_frame_cache (frame_info_ptr (self), &this_cache);
1233 if (cache->tailcall_cache)
1234 dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
1238 dwarf2_frame_sniffer (const struct frame_unwind *self,
1239 frame_info_ptr this_frame, void **this_cache)
1241 if (!dwarf2_frame_unwinders_enabled_p)
1244 /* Grab an address that is guaranteed to reside somewhere within the
1245 function. get_frame_pc(), with a no-return next function, can
1246 end up returning something past the end of this function's body.
1247 If the frame we're sniffing for is a signal frame whose start
1248 address is placed on the stack by the OS, its FDE must
1249 extend one byte before its start address or we could potentially
1250 select the FDE of the previous function. */
1251 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1252 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
1257 /* On some targets, signal trampolines may have unwind information.
1258 We need to recognize them so that we set the frame type
1261 if (fde->cie->signal_frame
1262 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1264 return self->type == SIGTRAMP_FRAME;
1266 if (self->type != NORMAL_FRAME)
1272 static const struct frame_unwind dwarf2_frame_unwind =
1276 dwarf2_frame_unwind_stop_reason,
1277 dwarf2_frame_this_id,
1278 dwarf2_frame_prev_register,
1280 dwarf2_frame_sniffer,
1281 dwarf2_frame_dealloc_cache
1284 static const struct frame_unwind dwarf2_signal_frame_unwind =
1288 dwarf2_frame_unwind_stop_reason,
1289 dwarf2_frame_this_id,
1290 dwarf2_frame_prev_register,
1292 dwarf2_frame_sniffer,
1294 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1298 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1301 dwarf2_append_unwinders (struct gdbarch *gdbarch)
1303 frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1304 frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
1308 /* There is no explicitly defined relationship between the CFA and the
1309 location of frame's local variables and arguments/parameters.
1310 Therefore, frame base methods on this page should probably only be
1311 used as a last resort, just to avoid printing total garbage as a
1312 response to the "info frame" command. */
1315 dwarf2_frame_base_address (frame_info_ptr this_frame, void **this_cache)
1317 struct dwarf2_frame_cache *cache =
1318 dwarf2_frame_cache (this_frame, this_cache);
1323 static const struct frame_base dwarf2_frame_base =
1325 &dwarf2_frame_unwind,
1326 dwarf2_frame_base_address,
1327 dwarf2_frame_base_address,
1328 dwarf2_frame_base_address
1331 const struct frame_base *
1332 dwarf2_frame_base_sniffer (frame_info_ptr this_frame)
1334 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1336 if (dwarf2_frame_find_fde (&block_addr, NULL))
1337 return &dwarf2_frame_base;
1342 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1343 the DWARF unwinder. This is used to implement
1344 DW_OP_call_frame_cfa. */
1347 dwarf2_frame_cfa (frame_info_ptr this_frame)
1349 if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind)
1350 || frame_unwinder_is (this_frame, &record_btrace_frame_unwind))
1351 throw_error (NOT_AVAILABLE_ERROR,
1352 _("cfa not available for record btrace target"));
1354 while (get_frame_type (this_frame) == INLINE_FRAME)
1355 this_frame = get_prev_frame (this_frame);
1356 if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
1357 throw_error (NOT_AVAILABLE_ERROR,
1358 _("can't compute CFA for this frame: "
1359 "required registers or memory are unavailable"));
1361 if (get_frame_id (this_frame).stack_status != FID_STACK_VALID)
1362 throw_error (NOT_AVAILABLE_ERROR,
1363 _("can't compute CFA for this frame: "
1364 "frame base not available"));
1366 return get_frame_base (this_frame);
1369 /* We store the frame data on the BFD. This is only done if it is
1370 independent of the address space and so can be shared. */
1371 static const registry<bfd>::key<comp_unit> dwarf2_frame_bfd_data;
1373 /* If any BFD sections require relocations (note; really should be if
1374 any debug info requires relocations), then we store the frame data
1375 on the objfile instead, and do not share it. */
1376 static const registry<objfile>::key<comp_unit> dwarf2_frame_objfile_data;
1379 /* Pointer encoding helper functions. */
1381 /* GCC supports exception handling based on DWARF2 CFI. However, for
1382 technical reasons, it encodes addresses in its FDE's in a different
1383 way. Several "pointer encodings" are supported. The encoding
1384 that's used for a particular FDE is determined by the 'R'
1385 augmentation in the associated CIE. The argument of this
1386 augmentation is a single byte.
1388 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1389 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1390 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1391 address should be interpreted (absolute, relative to the current
1392 position in the FDE, ...). Bit 7, indicates that the address
1393 should be dereferenced. */
1396 encoding_for_size (unsigned int size)
1401 return DW_EH_PE_udata2;
1403 return DW_EH_PE_udata4;
1405 return DW_EH_PE_udata8;
1407 internal_error (_("Unsupported address size"));
1412 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1413 int ptr_len, const gdb_byte *buf,
1414 unsigned int *bytes_read_ptr,
1415 CORE_ADDR func_base)
1420 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1422 if (encoding & DW_EH_PE_indirect)
1423 internal_error (_("Unsupported encoding: DW_EH_PE_indirect"));
1425 *bytes_read_ptr = 0;
1427 switch (encoding & 0x70)
1429 case DW_EH_PE_absptr:
1432 case DW_EH_PE_pcrel:
1433 base = bfd_section_vma (unit->dwarf_frame_section);
1434 base += (buf - unit->dwarf_frame_buffer);
1436 case DW_EH_PE_datarel:
1439 case DW_EH_PE_textrel:
1442 case DW_EH_PE_funcrel:
1445 case DW_EH_PE_aligned:
1447 offset = buf - unit->dwarf_frame_buffer;
1448 if ((offset % ptr_len) != 0)
1450 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1451 buf += *bytes_read_ptr;
1455 internal_error (_("Invalid or unsupported encoding"));
1458 if ((encoding & 0x07) == 0x00)
1460 encoding |= encoding_for_size (ptr_len);
1461 if (bfd_get_sign_extend_vma (unit->abfd))
1462 encoding |= DW_EH_PE_signed;
1465 switch (encoding & 0x0f)
1467 case DW_EH_PE_uleb128:
1470 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1472 *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
1473 return base + value;
1475 case DW_EH_PE_udata2:
1476 *bytes_read_ptr += 2;
1477 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1478 case DW_EH_PE_udata4:
1479 *bytes_read_ptr += 4;
1480 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1481 case DW_EH_PE_udata8:
1482 *bytes_read_ptr += 8;
1483 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1484 case DW_EH_PE_sleb128:
1487 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1489 *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
1490 return base + value;
1492 case DW_EH_PE_sdata2:
1493 *bytes_read_ptr += 2;
1494 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1495 case DW_EH_PE_sdata4:
1496 *bytes_read_ptr += 4;
1497 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1498 case DW_EH_PE_sdata8:
1499 *bytes_read_ptr += 8;
1500 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1502 internal_error (_("Invalid or unsupported encoding"));
1507 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1508 static struct dwarf2_cie *
1509 find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer)
1511 auto iter = cie_table.find (cie_pointer);
1512 if (iter != cie_table.end ())
1513 return iter->second;
1518 bsearch_fde_cmp (const dwarf2_fde *fde, CORE_ADDR seek_pc)
1520 if (fde->initial_location + fde->address_range <= seek_pc)
1522 if (fde->initial_location <= seek_pc)
1527 /* Find an existing comp_unit for an objfile, if any. */
1530 find_comp_unit (struct objfile *objfile)
1532 bfd *abfd = objfile->obfd.get ();
1533 if (gdb_bfd_requires_relocations (abfd))
1534 return dwarf2_frame_objfile_data.get (objfile);
1536 return dwarf2_frame_bfd_data.get (abfd);
1539 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1543 set_comp_unit (struct objfile *objfile, struct comp_unit *unit)
1545 bfd *abfd = objfile->obfd.get ();
1546 if (gdb_bfd_requires_relocations (abfd))
1547 return dwarf2_frame_objfile_data.set (objfile, unit);
1549 return dwarf2_frame_bfd_data.set (abfd, unit);
1552 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1553 initial location associated with it into *PC. */
1555 static struct dwarf2_fde *
1556 dwarf2_frame_find_fde (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile)
1558 for (objfile *objfile : current_program_space->objfiles ())
1563 if (objfile->obfd == nullptr)
1566 comp_unit *unit = find_comp_unit (objfile);
1569 dwarf2_build_frame_info (objfile);
1570 unit = find_comp_unit (objfile);
1572 gdb_assert (unit != NULL);
1574 dwarf2_fde_table *fde_table = &unit->fde_table;
1575 if (fde_table->empty ())
1578 gdb_assert (!objfile->section_offsets.empty ());
1579 offset = objfile->text_section_offset ();
1581 gdb_assert (!fde_table->empty ());
1582 if (*pc < offset + (*fde_table)[0]->initial_location)
1585 seek_pc = *pc - offset;
1586 auto it = gdb::binary_search (fde_table->begin (), fde_table->end (),
1587 seek_pc, bsearch_fde_cmp);
1588 if (it != fde_table->end ())
1590 *pc = (*it)->initial_location + offset;
1591 if (out_per_objfile != nullptr)
1592 *out_per_objfile = get_dwarf2_per_objfile (objfile);
1600 /* Add FDE to FDE_TABLE. */
1602 add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
1604 if (fde->address_range == 0)
1605 /* Discard useless FDEs. */
1608 fde_table->push_back (fde);
1611 #define DW64_CIE_ID 0xffffffffffffffffULL
1613 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1618 EH_CIE_TYPE_ID = 1 << 0,
1619 EH_FDE_TYPE_ID = 1 << 1,
1620 EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
1623 static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch,
1624 struct comp_unit *unit,
1625 const gdb_byte *start,
1627 dwarf2_cie_table &cie_table,
1628 dwarf2_fde_table *fde_table,
1629 enum eh_frame_type entry_type);
1631 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1632 Return NULL if invalid input, otherwise the next byte to be processed. */
1634 static const gdb_byte *
1635 decode_frame_entry_1 (struct gdbarch *gdbarch,
1636 struct comp_unit *unit, const gdb_byte *start,
1638 dwarf2_cie_table &cie_table,
1639 dwarf2_fde_table *fde_table,
1640 enum eh_frame_type entry_type)
1642 const gdb_byte *buf, *end;
1644 unsigned int bytes_read;
1647 ULONGEST cie_pointer;
1652 length = read_initial_length (unit->abfd, buf, &bytes_read, false);
1654 end = buf + (size_t) length;
1659 /* Are we still within the section? */
1660 if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1663 /* Distinguish between 32 and 64-bit encoded frame info. */
1664 dwarf64_p = (bytes_read == 12);
1666 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1670 cie_id = DW64_CIE_ID;
1676 cie_pointer = read_8_bytes (unit->abfd, buf);
1681 cie_pointer = read_4_bytes (unit->abfd, buf);
1685 if (cie_pointer == cie_id)
1687 /* This is a CIE. */
1688 struct dwarf2_cie *cie;
1690 unsigned int cie_version;
1692 /* Check that a CIE was expected. */
1693 if ((entry_type & EH_CIE_TYPE_ID) == 0)
1694 error (_("Found a CIE when not expecting it."));
1696 /* Record the offset into the .debug_frame section of this CIE. */
1697 cie_pointer = start - unit->dwarf_frame_buffer;
1699 /* Check whether we've already read it. */
1700 if (find_cie (cie_table, cie_pointer))
1703 cie = XOBNEW (&unit->obstack, struct dwarf2_cie);
1704 cie->initial_instructions = NULL;
1705 cie->cie_pointer = cie_pointer;
1707 /* The encoding for FDE's in a normal .debug_frame section
1708 depends on the target address size. */
1709 cie->encoding = DW_EH_PE_absptr;
1711 /* We'll determine the final value later, but we need to
1712 initialize it conservatively. */
1713 cie->signal_frame = 0;
1715 /* Check version number. */
1716 cie_version = read_1_byte (unit->abfd, buf);
1717 if (cie_version != 1 && cie_version != 3 && cie_version != 4)
1719 cie->version = cie_version;
1722 /* Interpret the interesting bits of the augmentation. */
1723 cie->augmentation = augmentation = (char *) buf;
1724 buf += (strlen (augmentation) + 1);
1726 /* Ignore armcc augmentations. We only use them for quirks,
1727 and that doesn't happen until later. */
1728 if (startswith (augmentation, "armcc"))
1729 augmentation += strlen (augmentation);
1731 /* The GCC 2.x "eh" augmentation has a pointer immediately
1732 following the augmentation string, so it must be handled
1734 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1737 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1741 if (cie->version >= 4)
1743 /* FIXME: check that this is the same as from the CU header. */
1744 cie->addr_size = read_1_byte (unit->abfd, buf);
1746 cie->segment_size = read_1_byte (unit->abfd, buf);
1751 cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
1752 cie->segment_size = 0;
1754 /* Address values in .eh_frame sections are defined to have the
1755 target's pointer size. Watchout: This breaks frame info for
1756 targets with pointer size < address size, unless a .debug_frame
1757 section exists as well. */
1759 cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1761 cie->ptr_size = cie->addr_size;
1763 buf = gdb_read_uleb128 (buf, end, &uleb128);
1766 cie->code_alignment_factor = uleb128;
1768 buf = gdb_read_sleb128 (buf, end, &sleb128);
1771 cie->data_alignment_factor = sleb128;
1773 if (cie_version == 1)
1775 cie->return_address_register = read_1_byte (unit->abfd, buf);
1780 buf = gdb_read_uleb128 (buf, end, &uleb128);
1783 cie->return_address_register = uleb128;
1786 cie->return_address_register
1787 = dwarf2_frame_adjust_regnum (gdbarch,
1788 cie->return_address_register,
1791 cie->saw_z_augmentation = (*augmentation == 'z');
1792 if (cie->saw_z_augmentation)
1794 uint64_t uleb_length;
1796 buf = gdb_read_uleb128 (buf, end, &uleb_length);
1799 cie->initial_instructions = buf + uleb_length;
1803 while (*augmentation)
1805 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1806 if (*augmentation == 'L')
1813 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1814 else if (*augmentation == 'R')
1816 cie->encoding = *buf++;
1820 /* "P" indicates a personality routine in the CIE augmentation. */
1821 else if (*augmentation == 'P')
1823 /* Skip. Avoid indirection since we throw away the result. */
1824 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1825 read_encoded_value (unit, encoding, cie->ptr_size,
1826 buf, &bytes_read, 0);
1831 /* "S" indicates a signal frame, such that the return
1832 address must not be decremented to locate the call frame
1833 info for the previous frame; it might even be the first
1834 instruction of a function, so decrementing it would take
1835 us to a different function. */
1836 else if (*augmentation == 'S')
1838 cie->signal_frame = 1;
1842 /* Otherwise we have an unknown augmentation. Assume that either
1843 there is no augmentation data, or we saw a 'z' prefix. */
1846 if (cie->initial_instructions)
1847 buf = cie->initial_instructions;
1852 cie->initial_instructions = buf;
1856 cie_table[cie->cie_pointer] = cie;
1860 /* This is a FDE. */
1861 struct dwarf2_fde *fde;
1864 /* Check that an FDE was expected. */
1865 if ((entry_type & EH_FDE_TYPE_ID) == 0)
1866 error (_("Found an FDE when not expecting it."));
1868 /* In an .eh_frame section, the CIE pointer is the delta between the
1869 address within the FDE where the CIE pointer is stored and the
1870 address of the CIE. Convert it to an offset into the .eh_frame
1874 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1875 cie_pointer -= (dwarf64_p ? 8 : 4);
1878 /* In either case, validate the result is still within the section. */
1879 if (cie_pointer >= unit->dwarf_frame_size)
1882 fde = XOBNEW (&unit->obstack, struct dwarf2_fde);
1883 fde->cie = find_cie (cie_table, cie_pointer);
1884 if (fde->cie == NULL)
1886 decode_frame_entry (gdbarch, unit,
1887 unit->dwarf_frame_buffer + cie_pointer,
1888 eh_frame_p, cie_table, fde_table,
1890 fde->cie = find_cie (cie_table, cie_pointer);
1893 gdb_assert (fde->cie != NULL);
1895 addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
1896 buf, &bytes_read, 0);
1897 fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
1900 fde->address_range =
1901 read_encoded_value (unit, fde->cie->encoding & 0x0f,
1902 fde->cie->ptr_size, buf, &bytes_read, 0);
1903 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range);
1904 fde->address_range = addr - fde->initial_location;
1907 /* A 'z' augmentation in the CIE implies the presence of an
1908 augmentation field in the FDE as well. The only thing known
1909 to be in here at present is the LSDA entry for EH. So we
1910 can skip the whole thing. */
1911 if (fde->cie->saw_z_augmentation)
1913 uint64_t uleb_length;
1915 buf = gdb_read_uleb128 (buf, end, &uleb_length);
1923 fde->instructions = buf;
1926 fde->eh_frame_p = eh_frame_p;
1928 add_fde (fde_table, fde);
1934 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
1935 expect an FDE or a CIE. */
1937 static const gdb_byte *
1938 decode_frame_entry (struct gdbarch *gdbarch,
1939 struct comp_unit *unit, const gdb_byte *start,
1941 dwarf2_cie_table &cie_table,
1942 dwarf2_fde_table *fde_table,
1943 enum eh_frame_type entry_type)
1945 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1946 const gdb_byte *ret;
1947 ptrdiff_t start_offset;
1951 ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p,
1952 cie_table, fde_table, entry_type);
1956 /* We have corrupt input data of some form. */
1958 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1959 and mismatches wrt padding and alignment of debug sections. */
1960 /* Note that there is no requirement in the standard for any
1961 alignment at all in the frame unwind sections. Testing for
1962 alignment before trying to interpret data would be incorrect.
1964 However, GCC traditionally arranged for frame sections to be
1965 sized such that the FDE length and CIE fields happen to be
1966 aligned (in theory, for performance). This, unfortunately,
1967 was done with .align directives, which had the side effect of
1968 forcing the section to be aligned by the linker.
1970 This becomes a problem when you have some other producer that
1971 creates frame sections that are not as strictly aligned. That
1972 produces a hole in the frame info that gets filled by the
1975 The GCC behaviour is arguably a bug, but it's effectively now
1976 part of the ABI, so we're now stuck with it, at least at the
1977 object file level. A smart linker may decide, in the process
1978 of compressing duplicate CIE information, that it can rewrite
1979 the entire output section without this extra padding. */
1981 start_offset = start - unit->dwarf_frame_buffer;
1982 if (workaround < ALIGN4 && (start_offset & 3) != 0)
1984 start += 4 - (start_offset & 3);
1985 workaround = ALIGN4;
1988 if (workaround < ALIGN8 && (start_offset & 7) != 0)
1990 start += 8 - (start_offset & 7);
1991 workaround = ALIGN8;
1995 /* Nothing left to try. Arrange to return as if we've consumed
1996 the entire input section. Hopefully we'll get valid info from
1997 the other of .debug_frame/.eh_frame. */
1999 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2010 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2011 bfd_get_filename (unit->dwarf_frame_section->owner),
2012 bfd_section_name (unit->dwarf_frame_section));
2017 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2018 bfd_get_filename (unit->dwarf_frame_section->owner),
2019 bfd_section_name (unit->dwarf_frame_section));
2023 complaint (_("Corrupt data in %s:%s"),
2024 bfd_get_filename (unit->dwarf_frame_section->owner),
2025 bfd_section_name (unit->dwarf_frame_section));
2033 fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
2035 if (aa->initial_location == bb->initial_location)
2037 if (aa->address_range != bb->address_range
2038 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2039 /* Linker bug, e.g. gold/10400.
2040 Work around it by keeping stable sort order. */
2043 /* Put eh_frame entries after debug_frame ones. */
2044 return aa->eh_frame_p < bb->eh_frame_p;
2047 return aa->initial_location < bb->initial_location;
2051 dwarf2_build_frame_info (struct objfile *objfile)
2053 const gdb_byte *frame_ptr;
2054 dwarf2_cie_table cie_table;
2055 dwarf2_fde_table fde_table;
2057 struct gdbarch *gdbarch = objfile->arch ();
2059 /* Build a minimal decoding of the DWARF2 compilation unit. */
2060 std::unique_ptr<comp_unit> unit (new comp_unit (objfile));
2062 if (objfile->separate_debug_objfile_backlink == NULL)
2064 /* Do not read .eh_frame from separate file as they must be also
2065 present in the main file. */
2066 dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
2067 &unit->dwarf_frame_section,
2068 &unit->dwarf_frame_buffer,
2069 &unit->dwarf_frame_size);
2070 if (unit->dwarf_frame_size)
2072 asection *got, *txt;
2074 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2075 that is used for the i386/amd64 target, which currently is
2076 the only target in GCC that supports/uses the
2077 DW_EH_PE_datarel encoding. */
2078 got = bfd_get_section_by_name (unit->abfd, ".got");
2080 unit->dbase = got->vma;
2082 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2084 txt = bfd_get_section_by_name (unit->abfd, ".text");
2086 unit->tbase = txt->vma;
2090 frame_ptr = unit->dwarf_frame_buffer;
2091 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2092 frame_ptr = decode_frame_entry (gdbarch, unit.get (),
2094 cie_table, &fde_table,
2095 EH_CIE_OR_FDE_TYPE_ID);
2098 catch (const gdb_exception_error &e)
2100 warning (_("skipping .eh_frame info of %s: %s"),
2101 objfile_name (objfile), e.what ());
2104 /* The cie_table is discarded below. */
2111 dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
2112 &unit->dwarf_frame_section,
2113 &unit->dwarf_frame_buffer,
2114 &unit->dwarf_frame_size);
2115 if (unit->dwarf_frame_size)
2117 size_t num_old_fde_entries = fde_table.size ();
2121 frame_ptr = unit->dwarf_frame_buffer;
2122 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2123 frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0,
2124 cie_table, &fde_table,
2125 EH_CIE_OR_FDE_TYPE_ID);
2127 catch (const gdb_exception_error &e)
2129 warning (_("skipping .debug_frame info of %s: %s"),
2130 objfile_name (objfile), e.what ());
2132 fde_table.resize (num_old_fde_entries);
2136 struct dwarf2_fde *fde_prev = NULL;
2137 struct dwarf2_fde *first_non_zero_fde = NULL;
2139 /* Prepare FDE table for lookups. */
2140 std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than);
2142 /* Check for leftovers from --gc-sections. The GNU linker sets
2143 the relevant symbols to zero, but doesn't zero the FDE *end*
2144 ranges because there's no relocation there. It's (offset,
2145 length), not (start, end). On targets where address zero is
2146 just another valid address this can be a problem, since the
2147 FDEs appear to be non-empty in the output --- we could pick
2148 out the wrong FDE. To work around this, when overlaps are
2149 detected, we prefer FDEs that do not start at zero.
2151 Start by finding the first FDE with non-zero start. Below
2152 we'll discard all FDEs that start at zero and overlap this
2154 for (struct dwarf2_fde *fde : fde_table)
2156 if (fde->initial_location != 0)
2158 first_non_zero_fde = fde;
2163 /* Since we'll be doing bsearch, squeeze out identical (except
2164 for eh_frame_p) fde entries so bsearch result is predictable.
2165 Also discard leftovers from --gc-sections. */
2166 for (struct dwarf2_fde *fde : fde_table)
2168 if (fde->initial_location == 0
2169 && first_non_zero_fde != NULL
2170 && (first_non_zero_fde->initial_location
2171 < fde->initial_location + fde->address_range))
2174 if (fde_prev != NULL
2175 && fde_prev->initial_location == fde->initial_location)
2178 unit->fde_table.push_back (fde);
2181 unit->fde_table.shrink_to_fit ();
2183 set_comp_unit (objfile, unit.release ());
2186 /* Handle 'maintenance show dwarf unwinders'. */
2189 show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty,
2190 struct cmd_list_element *c,
2194 _("The DWARF stack unwinders are currently %s.\n"),
2198 void _initialize_dwarf2_frame ();
2200 _initialize_dwarf2_frame ()
2202 add_setshow_boolean_cmd ("unwinders", class_obscure,
2203 &dwarf2_frame_unwinders_enabled_p , _("\
2204 Set whether the DWARF stack frame unwinders are used."), _("\
2205 Show whether the DWARF stack frame unwinders are used."), _("\
2206 When enabled the DWARF stack frame unwinders can be used for architectures\n\
2207 that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\
2208 architecture that doesn't support them will have no effect."),
2210 show_dwarf_unwinders_enabled_p,
2212 &show_dwarf_cmdlist);
2215 selftests::register_test_foreach_arch ("execute_cfa_program",
2216 selftests::execute_cfa_program_test);